-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split BaseService and BaseJsonService into separate modules (#1889)
There’s a lot of behavior here, and going to be even more, so I think it makes sense to split these up as I’ve done with the tests.
- Loading branch information
1 parent
ed81dcb
commit db4bffb
Showing
15 changed files
with
76 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
'use strict' | ||
|
||
// See available emoji at http://emoji.muan.co/ | ||
const emojic = require('emojic') | ||
const Joi = require('joi') | ||
const { checkErrorResponse, asJson } = require('../lib/error-helper') | ||
const BaseService = require('./base') | ||
const { InvalidResponse } = require('./errors') | ||
|
||
class BaseJsonService extends BaseService { | ||
static _validate(json, schema) { | ||
const { error, value } = Joi.validate(json, schema, { | ||
allowUnknown: true, | ||
stripUnknown: true, | ||
}) | ||
if (error) { | ||
this.logTrace( | ||
'error', | ||
emojic.womanShrugging, | ||
'Response did not match schema', | ||
error.message | ||
) | ||
throw new InvalidResponse({ | ||
prettyMessage: 'invalid json response', | ||
underlyingError: error, | ||
}) | ||
} else { | ||
this.logTrace('validate', emojic.bathtub, 'JSON after validation', value) | ||
return value | ||
} | ||
} | ||
|
||
async _requestJson({ schema, url, options = {}, notFoundMessage }) { | ||
const logTrace = (...args) => this.constructor.logTrace('fetch', ...args) | ||
if (!schema || !schema.isJoi) { | ||
throw Error('A Joi schema is required') | ||
} | ||
const mergedOptions = { | ||
...{ headers: { Accept: 'application/json' } }, | ||
...options, | ||
} | ||
logTrace(emojic.bowAndArrow, 'Request', url, '\n', mergedOptions) | ||
return this._sendAndCacheRequest(url, mergedOptions) | ||
.then(({ res, buffer }) => { | ||
logTrace(emojic.dart, 'Response status code', res.statusCode) | ||
return { res, buffer } | ||
}) | ||
.then( | ||
checkErrorResponse.asPromise( | ||
notFoundMessage ? { notFoundMessage: notFoundMessage } : undefined | ||
) | ||
) | ||
.then(asJson) | ||
.then(json => { | ||
logTrace(emojic.dart, 'Response JSON (before validation)', json) | ||
return json | ||
}) | ||
.then(json => this.constructor._validate(json, schema)) | ||
} | ||
} | ||
|
||
module.exports = BaseJsonService |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters