Files
shields/core/base-service/base-yaml.js
dependabot-preview[bot] 3a31721631 Build(deps): bump js-yaml from 3.14.1 to 4.0.0; run [DynamicYaml] (#6026)
* Build(deps): bump js-yaml from 3.14.1 to 4.0.0

Bumps [js-yaml](https://github.com/nodeca/js-yaml) from 3.14.1 to 4.0.0.
- [Release notes](https://github.com/nodeca/js-yaml/releases)
- [Changelog](https://github.com/nodeca/js-yaml/blob/master/CHANGELOG.md)
- [Commits](https://github.com/nodeca/js-yaml/compare/3.14.1...4.0.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

* deps: apply js-yaml v4 changes

* deps: js-yaml v4 updates in gatsby config

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
Co-authored-by: Caleb Cartwright <caleb.cartwright@outlook.com>
Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
2021-01-10 15:23:32 +00:00

76 lines
2.2 KiB
JavaScript

/**
* @module
*/
'use strict'
const emojic = require('emojic')
const yaml = require('js-yaml')
const BaseService = require('./base')
const { InvalidResponse } = require('./errors')
const trace = require('./trace')
/**
* Services which query a YAML endpoint should extend BaseYamlService
*
* @abstract
*/
class BaseYamlService extends BaseService {
/**
* Request data from an upstream API serving YAML,
* 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)
* @param {object} [attrs.encoding='utf8'] Character encoding
* @returns {object} Parsed response
* @see https://github.com/request/request#requestoptions-callback
*/
async _requestYaml({
schema,
url,
options = {},
errorMessages = {},
encoding = 'utf8',
}) {
const logTrace = (...args) => trace.logTrace('fetch', ...args)
const mergedOptions = {
...{
headers: {
Accept:
'text/x-yaml, text/yaml, application/x-yaml, application/yaml, text/plain',
},
},
...options,
}
const { buffer } = await this._request({
url,
options: mergedOptions,
errorMessages,
})
let parsed
try {
parsed = yaml.load(buffer.toString(), encoding)
} catch (err) {
logTrace(emojic.dart, 'Response YAML (unparseable)', buffer)
throw new InvalidResponse({
prettyMessage: 'unparseable yaml response',
underlyingError: err,
})
}
logTrace(emojic.dart, 'Response YAML (before validation)', parsed, {
deep: true,
})
return this.constructor._validate(parsed, schema)
}
}
module.exports = BaseYamlService