diff --git a/services/npm/npm-latest-package-update.js b/services/npm/npm-latest-package-update.js new file mode 100644 index 0000000000000..efb12d3228b09 --- /dev/null +++ b/services/npm/npm-latest-package-update.js @@ -0,0 +1,67 @@ +import Joi from 'joi' +import { BaseService, pathParams } from '../index.js' +import { formatRelativeDate } from '../text-formatters.js' + +const schema = Joi.object({ + time: Joi.object({ + modified: Joi.date().optional(), + created: Joi.date().required(), + }).required(), +}).required() + +const description = ` +Supply package name to display the latest package update time +` + +/* + * TODO: Add `version` parameter to get version-specific data + */ +export default class NpmLatestPackageUpdate extends BaseService { + static category = 'version' + + static route = { + base: '/npm/latest-update/', + pattern: ':name', + } + + static openApi = { + '/npm/latest-update/{name}': { + get: { + summary: 'NPM latest package update time', + description, + parameters: pathParams({ + name: 'name', + example: 'npm', + }), + }, + }, + } + + static defaultBadgeData = { + label: 'published', + color: 'blue', + } + + static render({ time }) { + return { + message: time, + label: this.defaultBadgeData.label, + color: this.defaultBadgeData.color, + } + } + + async fetch({ name }) { + return this._requestJson({ + schema, + url: `https://registry.npmjs.org/${name}`, + }) + } + + async handle({ name }) { + const data = await this.fetch({ name }) + + return this.constructor.render({ + time: formatRelativeDate(data.time), + }) + } +} diff --git a/services/npm/npm-latest-package-update.spec.js b/services/npm/npm-latest-package-update.spec.js new file mode 100644 index 0000000000000..f38cae4fea097 --- /dev/null +++ b/services/npm/npm-latest-package-update.spec.js @@ -0,0 +1,16 @@ +import { ServiceTester } from '../tester.js' + +export const t = new ServiceTester({ + id: 'npm-latest-package-update', + title: 'NPM Latest Package update Tests', +}) + +/* +>> TODO: Create tests + +t.create('NPM Latest Package update') + +t.create('NPM Latest Package update (negative)') + +t.create('NPM Latest Package update - Invalid') + */