-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexternals.js
79 lines (67 loc) · 2.43 KB
/
externals.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const { STATUS_CODES } = require('http')
const got = require('got')
const { createError } = require('micro-errors')
const cacheStore = require('./cache-store')
const { mapKeysWith, stringifyParams } = require('./utils')
const { normalizeRestaurants, normalizeNullValues } = require('./helpers')
const { hotpepper: hotpepperParamsMap, gurunavi: gurunaviParamsMap } = require('./params-map')
const { hotpepper: hotpepperEntityMap, gurunavi: gurunaviEntityMap } = require('./entity-map')
const request = async (url, params) => {
return await got(`${url}${stringifyParams(params)}`, { json: true, cache: cacheStore, throwHttpErrors: false })
.catch(error => {
const codeAndTitle = error.name === 'ParseError'
? [500, STATUS_CODES[500]]
: [error.statusCode, error.statusMessage]
throw createError(...codeAndTitle, error, null, { name: error.name })
})
}
module.exports.hotpepper = async params => {
const res = await request('https://webservice.recruit.co.jp/hotpepper/gourmet/v1/', {
...mapKeysWith(params || {}, hotpepperParamsMap),
key: process.env.HOTPEPPER_API_KEY,
format: 'json',
})
const { body } = res
if (typeof body === 'string' && body.length === 0) { // TODO:
throw createError(404, STATUS_CODES[404], null, null, { _debug: { external: 'HOTPEPPER', message: '`response.body` is empty string' } })
}
const { results } = body
if (Array.isArray(results.error)) {
const [error] = results.error
const statusCode = { '3000': 400, '2000': 401, '1000': 500 }[error.code]
throw createError(
statusCode,
STATUS_CODES[statusCode],
null,
null,
{ detail: error.message }
)
}
return normalizeRestaurants(results.shop, hotpepperEntityMap)
}
module.exports.gurunavi = async params => {
const res = await request('https://api.gnavi.co.jp/RestSearchAPI/v3/', {
...mapKeysWith(params || {}, gurunaviParamsMap),
keyid: process.env.GURUNAVI_API_KEY,
})
const { statusCode, body } = res
if (
(statusCode >= 200 && statusCode < 300) ||
statusCode === 404
) {
const restaurants = body.rest || [] // if 404
return normalizeRestaurants(
normalizeNullValues(restaurants),
gurunaviEntityMap
)
}
const [error] = body.error
const code = { '601': 401, '602': 404, '603': 400, '604': 500 }[error.code] || error.code
throw createError(
code,
res.statusMessage,
null,
null,
{ detail: error.message }
)
}