Skip to content

Commit

Permalink
feat: support npm rating
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 committed Jun 13, 2021
1 parent 956f2e0 commit 845da2f
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
70 changes: 70 additions & 0 deletions services/npm/npm-rating.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
'use strict'

const Joi = require('joi')
const { starRating } = require('../text-formatters')
const { BaseJsonService } = require('..')

// https://api-docs.npms.io/#api-Package-GetPackageInfo
const responseSchema = Joi.object({
score: Joi.object({
final: Joi.number().required(),
detail: Joi.object({
maintenance: Joi.number().required(),
popularity: Joi.number().required(),
quality: Joi.number().required(),
}),
}),
}).required()

module.exports = class NpmRating extends BaseJsonService {
static category = 'rating'

static route = {
base: 'npm',
pattern:
'rating/:type(maintenance|popularity|quality)?/:scope(@.+)?/:packageName',
}

static examples = [
{
title: 'npm rating',
namedParams: { packageName: 'egg' },
staticPreview: this.render({ score: 0.9711 }),
keywords: ['node'],
},
{
title: 'npm rating (maintenance)',
namedParams: { type: 'maintenance', packageName: 'command' },
staticPreview: this.render({ type: 'maintenance', score: 0.222 }),
keywords: ['node'],
},
]

static defaultBadgeData = {
label: 'Rating',
}

static render({ type, score }) {
return {
label: !type ? 'Rating' : type[0].toUpperCase() + type.substring(1),
message: starRating(score * 5),
color: score > 0.3 ? 'brightgreen' : 'red',
}
}

async handle({ type, scope, packageName }) {
const slug = scope ? `${scope}/${packageName}` : packageName
const url = `https://api.npms.io/v2/package/${encodeURIComponent(slug)}`

const json = await this._requestJson({
schema: responseSchema,
url,
errorMessages: { 404: 'package not found or too new' },
})

let score = json.score.final
if (type) score = json.score.detail[type]

return this.constructor.render({ type, score })
}
}
42 changes: 42 additions & 0 deletions services/npm/npm-rating.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict'

const { isStarRating } = require('../test-validators')
const t = (module.exports = require('../tester').createServiceTester())

t.create('score of vue').get('/rating/vue.json').expectBadge({
label: 'Rating',
message: isStarRating,
color: 'brightgreen',
})

t.create('score of @vue/cli').get('/rating/@vue/cli.json').expectBadge({
label: 'Rating',
message: isStarRating,
color: 'brightgreen',
})

t.create('maintenance of vue').get('/rating/maintenance/vue.json').expectBadge({
label: 'Maintenance',
message: isStarRating,
color: 'brightgreen',
})

t.create('popularity of vue').get('/rating/popularity/vue.json').expectBadge({
label: 'Popularity',
message: isStarRating,
color: 'brightgreen',
})

t.create('quality of vue').get('/rating/quality/vue.json').expectBadge({
label: 'Quality',
message: isStarRating,
color: 'brightgreen',
})

t.create('unknown package')
.get('/rating/npm-api-does-not-have-this-package.json')
.expectBadge({
label: 'Rating',
message: 'package not found or too new',
color: 'red',
})

0 comments on commit 845da2f

Please sign in to comment.