-
-
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.
- Loading branch information
1 parent
5e40080
commit 09954e3
Showing
2 changed files
with
133 additions
and
0 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
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 }) | ||
} | ||
} |
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,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', | ||
}) |