Skip to content

Commit

Permalink
Added npm last update badge
Browse files Browse the repository at this point in the history
  • Loading branch information
MohanKumarAmbati committed Oct 26, 2024
1 parent 5e40080 commit 09954e3
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 0 deletions.
96 changes: 96 additions & 0 deletions services/npm/npm-last-update.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import Joi from 'joi'
import dayjs from 'dayjs'
import { optionalUrl } from '../validators.js'
import {
BaseJsonService,
InvalidParameter,
pathParam,
queryParam,
} from '../index.js'
import { formatDate } from '../text-formatters.js'
import { age as ageColor } from '../color-formatters.js'

const queryParamSchema = Joi.object({
registry_uri: optionalUrl,
version: Joi.string(),
}).required()

const updateResponseSchema = Joi.object({
time: Joi.object({
created: Joi.string().required(),
modified: Joi.string().required(),
})
.pattern(Joi.string(), Joi.any())
.required(),
}).required()

const defaultUrl = 'https://registry.npmjs.org'

export class NpmLastUpdate extends BaseJsonService {
static category = 'activity'

static route = {
base: 'npm/last-update',
pattern: ':packageName',
queryParamSchema,
}

static openApi = {
'/npm/last-update/{packageName}': {
get: {
summary: 'NPM Last Update',
parameters: [
pathParam({
name: 'packageName',
example: 'express',
}),
queryParam({
name: 'registry_uri',
example: 'https://registry.npmjs.com',
}),
queryParam({
name: 'version',
example: '5.0.0',
}),
],
},
},
}

static defaultBadgeData = { label: 'last updated' }

static render({ date }) {
return {
message: formatDate(date),
color: ageColor(date),
}
}

async handle({ packageName }, queryParams) {
let url

if (queryParams !== undefined && queryParams.registry_uri !== undefined) {
url = `${queryParams.registry_uri}/${packageName}`
} else {
url = `${defaultUrl}/${packageName}`
}

const { time } = await this._requestJson({
schema: updateResponseSchema,
url,
httpErrors: { 404: 'package not found' },
})

let date
if (queryParams !== undefined && queryParams.version !== undefined) {
if (time[queryParams.version] === undefined) {
throw new InvalidParameter({ prettyMessage: 'version not found' })
}
date = dayjs(time[queryParams.version])
} else {
date = dayjs(time.modified)
}

return this.constructor.render({ date })
}
}
37 changes: 37 additions & 0 deletions services/npm/npm-last-update.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { isFormattedDate } from '../test-validators.js'
import { createServiceTester } from '../tester.js'

export const t = await createServiceTester()

t.create('last updated date (valid package)').get('/express.json').expectBadge({
label: 'last updated',
message: isFormattedDate,
})

t.create('last updated date (invalid package)')
.get('/not-a-package.json')
.expectBadge({
label: 'last updated',
message: 'package not found',
})

t.create('last update from custom repository (valid scenario)')
.get('/express.json?registry_uri=https://registry.npmjs.com')
.expectBadge({
label: 'last updated',
message: isFormattedDate,
})

t.create('last updated date based on the version (valid scenario)')
.get('/express.json?version=5.0.0')
.expectBadge({
label: 'last updated',
message: isFormattedDate,
})

t.create('last updated date based on the version (invalid scenario)')
.get('/express.json?version=not-a-verion')
.expectBadge({
label: 'last updated',
message: 'version not found',
})

0 comments on commit 09954e3

Please sign in to comment.