Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions services/npm/npm-latest-package-update.js
Original file line number Diff line number Diff line change
@@ -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),
})
}
}
16 changes: 16 additions & 0 deletions services/npm/npm-latest-package-update.spec.js
Original file line number Diff line number Diff line change
@@ -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')
*/