-
-
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.
Rewrite the DynamicJson badge (#2399)
This starts the rewrite of the dynamic badges. I've pulled into BaseService an initial version of the query param validation from #2325. I've extended from BaseJsonService to avoid duplicating the deserialization logic, though it means there is a bit of duplicated code among the three dynamic services. The way to unravel this would be to move the logic from `_requestJson` and friends from the base classes into functions so DynamicJson can inherit from BaseDynamic. Would that be worth it? This introduces a regression of #1446 for this badge. Close #2345
- Loading branch information
1 parent
b2d5d3e
commit 6a737b7
Showing
12 changed files
with
180 additions
and
65 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
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,39 @@ | ||
'use strict' | ||
|
||
const Joi = require('joi') | ||
const { optionalUrl } = require('../validators') | ||
|
||
function createRoute(which) { | ||
return { | ||
base: `badge/dynamic/${which}`, | ||
pattern: '', | ||
queryParams: ['uri', 'url', 'query', 'prefix', 'suffix'], | ||
} | ||
} | ||
|
||
const queryParamSchema = Joi.object({ | ||
url: optionalUrl.required(), | ||
query: Joi.string().required(), | ||
prefix: Joi.alternatives().try(Joi.string(), Joi.number()), | ||
suffix: Joi.alternatives().try(Joi.string(), Joi.number()), | ||
}) | ||
.rename('uri', 'url', { ignoreUndefined: true, override: true }) | ||
.required() | ||
|
||
const errorMessages = { | ||
404: 'resource not found', | ||
} | ||
|
||
function renderDynamicBadge({ values, prefix = '', suffix = '' }) { | ||
return { | ||
message: `${prefix}${values.join(', ')}${suffix}`, | ||
color: 'brightgreen', | ||
} | ||
} | ||
|
||
module.exports = { | ||
createRoute, | ||
queryParamSchema, | ||
errorMessages, | ||
renderDynamicBadge, | ||
} |
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,51 @@ | ||
'use strict' | ||
|
||
const Joi = require('joi') | ||
const jp = require('jsonpath') | ||
const BaseJsonService = require('../base-json') | ||
const { InvalidResponse } = require('../errors') | ||
const { | ||
createRoute, | ||
queryParamSchema, | ||
errorMessages, | ||
renderDynamicBadge, | ||
} = require('./dynamic-helpers') | ||
|
||
module.exports = class DynamicJson extends BaseJsonService { | ||
static get category() { | ||
return 'dynamic' | ||
} | ||
|
||
static get route() { | ||
return createRoute('json') | ||
} | ||
|
||
static get defaultBadgeData() { | ||
return { | ||
label: 'custom badge', | ||
} | ||
} | ||
|
||
async handle(namedParams, queryParams) { | ||
const { | ||
url, | ||
query: pathExpression, | ||
prefix, | ||
suffix, | ||
} = this.constructor._validateQueryParams(queryParams, queryParamSchema) | ||
|
||
const data = await this._requestJson({ | ||
schema: Joi.any(), | ||
url, | ||
errorMessages, | ||
}) | ||
|
||
const values = jp.query(data, pathExpression) | ||
|
||
if (!values.length) { | ||
throw new InvalidResponse({ prettyMessage: 'no result' }) | ||
} | ||
|
||
return renderDynamicBadge({ values, prefix, suffix }) | ||
} | ||
} |
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
File renamed without changes.
File renamed without changes.
Oops, something went wrong.