-
-
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.
Merge branch 'master' into refactor/convert-to-renderVersionBadge-3
- Loading branch information
Showing
11 changed files
with
904 additions
and
282 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import Joi from 'joi' | ||
import dayjs from 'dayjs' | ||
import { InvalidResponse, NotFound, pathParam, queryParam } from '../index.js' | ||
import { formatDate } from '../text-formatters.js' | ||
import { age as ageColor } from '../color-formatters.js' | ||
import NpmBase, { packageNameDescription } from './npm-base.js' | ||
|
||
const updateResponseSchema = Joi.object({ | ||
time: Joi.object({ | ||
created: Joi.string().required(), | ||
modified: Joi.string().required(), | ||
}) | ||
.pattern(Joi.string().required(), Joi.string().required()) | ||
.required(), | ||
'dist-tags': Joi.object() | ||
.pattern(Joi.string().required(), Joi.string().required()) | ||
.required(), | ||
}).required() | ||
|
||
export class NpmLastUpdate extends NpmBase { | ||
static category = 'activity' | ||
|
||
static route = this.buildRoute('npm/last-update', { withTag: true }) | ||
|
||
static openApi = { | ||
'/npm/last-update/{packageName}': { | ||
get: { | ||
summary: 'NPM Last Update', | ||
parameters: [ | ||
pathParam({ | ||
name: 'packageName', | ||
example: 'verdaccio', | ||
packageNameDescription, | ||
}), | ||
queryParam({ | ||
name: 'registry_uri', | ||
example: 'https://registry.npmjs.com', | ||
}), | ||
], | ||
}, | ||
}, | ||
'/npm/last-update/{packageName}/{tag}': { | ||
get: { | ||
summary: 'NPM Last Update (with dist tag)', | ||
parameters: [ | ||
pathParam({ | ||
name: 'packageName', | ||
example: 'verdaccio', | ||
packageNameDescription, | ||
}), | ||
pathParam({ | ||
name: 'tag', | ||
example: 'next-8', | ||
}), | ||
queryParam({ | ||
name: 'registry_uri', | ||
example: 'https://registry.npmjs.com', | ||
}), | ||
], | ||
}, | ||
}, | ||
} | ||
|
||
static defaultBadgeData = { label: 'last updated' } | ||
|
||
static render({ date }) { | ||
return { | ||
message: formatDate(date), | ||
color: ageColor(date), | ||
} | ||
} | ||
|
||
async handle(namedParams, queryParams) { | ||
const { scope, packageName, tag, registryUrl } = | ||
this.constructor.unpackParams(namedParams, queryParams) | ||
|
||
const packageData = await this.fetch({ | ||
registryUrl, | ||
scope, | ||
packageName, | ||
schema: updateResponseSchema, | ||
}) | ||
|
||
let date | ||
|
||
if (tag) { | ||
const tagVersion = packageData['dist-tags'][tag] | ||
|
||
if (!tagVersion) { | ||
throw new NotFound({ prettyMessage: 'tag not found' }) | ||
} | ||
|
||
date = dayjs(packageData.time[tagVersion]) | ||
} else { | ||
const timeKey = packageData.time.modified ? 'modified' : 'created' | ||
|
||
date = dayjs(packageData.time[timeKey]) | ||
} | ||
|
||
if (!date.isValid) { | ||
throw new InvalidResponse({ prettyMessage: 'invalid date' }) | ||
} | ||
|
||
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,53 @@ | ||
import { isFormattedDate } from '../test-validators.js' | ||
import { createServiceTester } from '../tester.js' | ||
|
||
export const t = await createServiceTester() | ||
|
||
t.create('last updated date (valid package)') | ||
.get('/verdaccio.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('/verdaccio.json?registry_uri=https://registry.npmjs.com') | ||
.expectBadge({ | ||
label: 'last updated', | ||
message: isFormattedDate, | ||
}) | ||
|
||
t.create('last update scoped package (valid scenario)') | ||
.get('/@npm/types.json') | ||
.expectBadge({ | ||
label: 'last updated', | ||
message: isFormattedDate, | ||
}) | ||
|
||
t.create('last update scoped package (invalid scenario)') | ||
.get('/@not-a-scoped-package/not-a-valid-package.json') | ||
.expectBadge({ | ||
label: 'last updated', | ||
message: 'package not found', | ||
}) | ||
|
||
t.create('last updated date with tag (valid scenario)') | ||
.get('/verdaccio/latest.json') | ||
.expectBadge({ | ||
label: 'last updated', | ||
message: isFormattedDate, | ||
}) | ||
|
||
t.create('last updated date (invalid tag)') | ||
.get('/verdaccio/not-a-valid-tag.json') | ||
.expectBadge({ | ||
label: 'last updated', | ||
message: 'tag not found', | ||
}) |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.