-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Copy pathbase-json.js
57 lines (52 loc) · 1.71 KB
/
base-json.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
/**
* @module
*/
'use strict'
const BaseService = require('./base')
const { parseJson } = require('./json')
/**
* Services which query a JSON endpoint should extend BaseJsonService
*
* @abstract
*/
class BaseJsonService extends BaseService {
/**
* Parse data from JSON endpoint
*
* @param {string} buffer JSON repsonse from upstream API
* @returns {object} Parsed response
*/
_parseJson(buffer) {
return parseJson(buffer)
}
/**
* Request data from an upstream API serving JSON,
* parse it and validate against a schema
*
* @param {object} attrs Refer to individual attrs
* @param {Joi} attrs.schema Joi schema to validate the response against
* @param {string} attrs.url URL to request
* @param {object} [attrs.options={}] Options to pass to request. See
* [documentation](/~https://github.com/request/request#requestoptions-callback)
* @param {object} [attrs.errorMessages={}] Key-value map of status codes
* and custom error messages e.g: `{ 404: 'package not found' }`.
* This can be used to extend or override the
* [default](/~https://github.com/badges/shields/blob/master/core/base-service/check-error-response.js#L5)
* @returns {object} Parsed response
* @see /~https://github.com/request/request#requestoptions-callback
*/
async _requestJson({ schema, url, options = {}, errorMessages = {} }) {
const mergedOptions = {
...{ headers: { Accept: 'application/json' } },
...options,
}
const { buffer } = await this._request({
url,
options: mergedOptions,
errorMessages,
})
const json = this._parseJson(buffer)
return this.constructor._validate(json, schema)
}
}
module.exports = BaseJsonService