Skip to content

Commit b5f1e29

Browse files
authored
Merge pull request #457 from MiniGod/update-xo
style: update xo to latest
2 parents 8aa6527 + 1c82c86 commit b5f1e29

14 files changed

+710
-777
lines changed

endpoints/address/graphql_schema.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const {
44
GraphQLString,
55
} = require('graphql')
66

7-
const lookupAddresses = require('./index')
7+
const lookupAddresses = require('.')
88

99
const addressType = new GraphQLObjectType({
1010
name: 'Address',
@@ -44,6 +44,6 @@ module.exports = {
4444
},
4545
resolve: (_, args) => {
4646
const address = args.address.replace(' ', '+')
47-
return lookupAddresses(address).then(data => data, error => error)
47+
return lookupAddresses(address)
4848
},
4949
}

endpoints/address/index.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const lookupAddresses = address => new Promise((resolve, reject) => {
2828
})
2929
})
3030

31-
app.get('/address/:address?', (req, res) => {
31+
app.get('/address/:address?', async (req, res) => {
3232
const address = (
3333
req.query.address || req.params.address || ''
3434
).replace(' ', '+')
@@ -39,10 +39,12 @@ app.get('/address/:address?', (req, res) => {
3939
})
4040
}
4141

42-
lookupAddresses(address).then(
43-
results => res.cache().json({ results }),
44-
() => res.status(500).json({ error: 'www.postur.is refuses to respond or give back data' })
45-
)
42+
try {
43+
const results = await lookupAddresses(address)
44+
res.cache().json({ results })
45+
} catch (error) {
46+
res.status(500).json({ error: 'www.postur.is refuses to respond or give back data' })
47+
}
4648
})
4749

4850
module.exports = lookupAddresses

endpoints/bus/graphql_schema.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ module.exports = {
3232
args: {
3333
busses: { type: GraphQLString },
3434
},
35-
resolve: (_, args) => {
36-
return getBusRoutes(args).then(data => data.results, error => error)
35+
resolve: async (_, args) => {
36+
const data = await getBusRoutes(args)
37+
return data.results
3738
},
3839
}

endpoints/calendar/graphql_schema.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const {
66
} = require('graphql')
77
const { GraphQLDate } = require('../../graphql/types/GraphQLDate')
88

9-
const lookupHolidays = require('./index')
9+
const lookupHolidays = require('.')
1010

1111
const holiday = new GraphQLObjectType({
1212
name: 'Holiday',
@@ -36,9 +36,11 @@ module.exports = {
3636
month: { type: GraphQLString },
3737
day: { type: GraphQLString },
3838
},
39-
resolve: (_, { year, month, day }) => {
40-
return lookupHolidays(year, month, day)
41-
.then(data => data)
42-
.catch(({ error }) => { throw new Error(error) })
39+
resolve: async (_, { year, month, day }) => {
40+
try {
41+
return await lookupHolidays(year, month, day)
42+
} catch (error) {
43+
throw new Error(error)
44+
}
4345
},
4446
}

endpoints/calendar/index.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ const lookupHolidays = (yearStr, monthStr, dayStr) => new Promise((resolve, reje
5454
}
5555
})
5656

57+
const lookupAndRespondHolidays = async (res, ...args) => {
58+
try {
59+
const results = await lookupHolidays(...args)
60+
res.json({ results })
61+
} catch (error) {
62+
res.status(400).json(error)
63+
}
64+
}
65+
5766
app.get('/calendar/', (req, res) => {
5867
return res.json({
5968
description: 'Returns if a given date range has or is a holiday',
@@ -68,26 +77,17 @@ app.get('/calendar/', (req, res) => {
6877

6978
app.get('/calendar/:year', (req, res) => {
7079
const { year } = req.params
71-
72-
lookupHolidays(year)
73-
.then(holidays => res.json({ results: holidays }))
74-
.catch(error => res.status(400).json(error))
80+
lookupAndRespondHolidays(res, year)
7581
})
7682

7783
app.get('/calendar/:year/:month', (req, res) => {
7884
const { year, month } = req.params
79-
80-
lookupHolidays(year, month)
81-
.then(holidays => res.json({ results: holidays }))
82-
.catch(error => res.status(400).json(error))
85+
lookupAndRespondHolidays(res, year, month)
8386
})
8487

8588
app.get('/calendar/:year/:month/:day', (req, res) => {
8689
const { year, month, day } = req.params
87-
88-
lookupHolidays(year, month, day)
89-
.then(holiday => res.json({ results: holiday }))
90-
.catch(error => res.status(400).json(error))
90+
lookupAndRespondHolidays(res, year, month, day)
9191
})
9292

9393
module.exports = lookupHolidays

endpoints/calendar/tests/integration_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const request = require('request')
22
const expect = require('expect')
33
const helpers = require('../../../lib/test_helpers')
4-
const { normalizeParams } = require('../')
4+
const { normalizeParams } = require('..')
55

66
describe('calendar/:year', () => {
77
it('should return an array of objects containing correct fields', (done) => {

endpoints/car/graphql_schema.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const {
33
GraphQLString,
44
} = require('graphql')
55

6-
const lookupCar = require('./index')
6+
const lookupCar = require('.')
77

88
const car = new GraphQLObjectType({
99
name: 'Car',
@@ -64,7 +64,11 @@ module.exports = {
6464
description: 'The numer on the cars plate',
6565
},
6666
},
67-
resolve: (_, { carPlate }) => lookupCar(carPlate)
68-
.then(data => data)
69-
.catch((error) => { throw new Error(error) }),
67+
resolve: async (_, { carPlate }) => {
68+
try {
69+
return await lookupCar(carPlate)
70+
} catch (error) {
71+
throw new Error(error)
72+
}
73+
}
7074
}

endpoints/car/index.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,19 @@ const lookupCar = plate => new Promise((resolve, reject) => {
4545
})
4646
})
4747

48-
app.get('/car', (req, res) => {
48+
app.get('/car', async (req, res) => {
4949
const carPlate = req.query.number || req.query.carPlate || ''
5050

5151
if (!carPlate) {
5252
return res.status(431).json({ error: 'Please provide a valid carPlate to lookup' })
5353
}
5454

55-
lookupCar(carPlate)
56-
.then(car => res.cache().json({ results: [car] }))
57-
.catch(error => res.status(500).json({ error }))
55+
try {
56+
const car = await lookupCar(carPlate)
57+
res.cache().json({ results: [car] })
58+
} catch (error) {
59+
res.status(500).json({ error })
60+
}
5861
})
5962

6063
module.exports = lookupCar

endpoints/earthquake/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function parseJavaScriptVariable(body) {
5151
})
5252

5353
// Convert the variable to JavaScript Object Notation and fix seperators in values.
54-
const resString = jsonString.replace(/(:\'[-0-9][0-9]*)(,)([0-9]*)/g, '$1.$3')
54+
const resString = jsonString.replace(/(:\'[-0-9]\d*)(,)(\d*)/g, '$1.$3')
5555

5656
// Create a Regular expression and change date representation.
5757
// eslint-disable-next-line max-len

endpoints/ship/index.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,19 @@ const lookupShip = searchStr => new Promise((resolve, reject) => {
109109
})
110110
})
111111

112-
app.get('/ship', (req, res) => {
112+
app.get('/ship', async (req, res) => {
113113
const search = req.query.search || ''
114114

115115
if (!search) {
116116
return res.status(431).json({ error: 'Please provide a valid search string to lookup' })
117117
}
118118

119-
lookupShip(search)
120-
.then(ships => res.cache().json({ results: ships }))
121-
.catch(error => res.status(500).json({ error }))
119+
try {
120+
const ships = await lookupShip(search)
121+
res.cache().json({ results: ships })
122+
} catch (error) {
123+
res.status(500).json({ error })
124+
}
122125
})
123126

124127
module.exports = lookupShip

lib/cache.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ module.exports = function () {
2222
if (error) {
2323
debug('Error in caching layer:', error)
2424
return next()
25-
} else if (reply) {
25+
}
26+
27+
if (reply) {
2628
debug('cache HIT for %s', key)
2729
res.type('json')
2830
res.send(reply)

make-docs.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
const fs = require('fs')
22
const globby = require('globby')
3-
const marked = require('marked')
3+
const marked = require('marked');
4+
5+
(async () => {
6+
const paths = await globby(['./docs/*.md', './endpoints/**/*.md', '!node_modules/**'])
47

5-
globby(['./docs/*.md', './endpoints/**/*.md', '!node_modules/**']).then((paths) => {
68
let content = ''
79
paths.forEach((path) => {
810
content += fs.readFileSync(path, 'utf8')
@@ -13,6 +15,6 @@ globby(['./docs/*.md', './endpoints/**/*.md', '!node_modules/**']).then((paths)
1315
fs.mkdirSync('./docs/dist/')
1416
}
1517
fs.writeFileSync('./docs/dist/index.html', html, 'utf8')
16-
}).catch((error) => {
18+
})().catch((error) => {
1719
console.error(error)
1820
})

0 commit comments

Comments
 (0)