[jsDelivr] badges (#2970)
This commit is contained in:
39
services/jsdelivr/jsdelivr-base.js
Normal file
39
services/jsdelivr/jsdelivr-base.js
Normal file
@@ -0,0 +1,39 @@
|
||||
'use strict'
|
||||
|
||||
const Joi = require('joi')
|
||||
const { metric } = require('../../lib/text-formatters')
|
||||
const { BaseJsonService } = require('..')
|
||||
|
||||
const schema = Joi.object({
|
||||
total: Joi.number().required(),
|
||||
}).required()
|
||||
|
||||
const keywords = ['jsDelivr', 'hits']
|
||||
|
||||
const periodMap = {
|
||||
hd: 'day',
|
||||
hw: 'week',
|
||||
hm: 'month',
|
||||
hy: 'year',
|
||||
}
|
||||
|
||||
class BaseJsDelivrService extends BaseJsonService {
|
||||
static render({ period, hits }) {
|
||||
return {
|
||||
message: `${metric(hits)} hits/${periodMap[period]}`,
|
||||
}
|
||||
}
|
||||
|
||||
static get defaultBadgeData() {
|
||||
return {
|
||||
label: 'jsdelivr',
|
||||
color: 'orange',
|
||||
}
|
||||
}
|
||||
|
||||
static get category() {
|
||||
return 'other'
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { schema, keywords, periodMap, BaseJsDelivrService }
|
||||
81
services/jsdelivr/jsdelivr-hits-github.service.js
Normal file
81
services/jsdelivr/jsdelivr-hits-github.service.js
Normal file
@@ -0,0 +1,81 @@
|
||||
'use strict'
|
||||
|
||||
const {
|
||||
schema,
|
||||
keywords,
|
||||
periodMap,
|
||||
BaseJsDelivrService,
|
||||
} = require('./jsdelivr-base')
|
||||
|
||||
module.exports = class jsDelivrHitsGitHub extends BaseJsDelivrService {
|
||||
static get route() {
|
||||
return {
|
||||
base: 'jsdelivr/gh',
|
||||
pattern: ':period(hd|hw|hm|hy)/:user/:repo',
|
||||
}
|
||||
}
|
||||
|
||||
async handle({ period, user, repo }) {
|
||||
const { total } = await this.fetch({ period, user, repo })
|
||||
return this.constructor.render({ period, hits: total })
|
||||
}
|
||||
|
||||
async fetch({ period, user, repo }) {
|
||||
const url = `https://data.jsdelivr.com/v1/package/gh/${user}/${repo}/stats/date/${
|
||||
periodMap[period]
|
||||
}`
|
||||
|
||||
return this._requestJson({
|
||||
schema,
|
||||
url,
|
||||
headers: {
|
||||
'User-Agent': 'Shields.io',
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
static get examples() {
|
||||
return [
|
||||
{
|
||||
title: 'jsDelivr Hits (GitHub)',
|
||||
pattern: 'hd/:user/:repo',
|
||||
namedParams: {
|
||||
user: 'jquery',
|
||||
repo: 'jquery',
|
||||
},
|
||||
staticPreview: this.render({ period: 'hd', hits: 272042 }),
|
||||
keywords,
|
||||
},
|
||||
{
|
||||
title: 'jsDelivr Hits (GitHub)',
|
||||
pattern: 'hw/:user/:repo',
|
||||
namedParams: {
|
||||
user: 'jquery',
|
||||
repo: 'jquery',
|
||||
},
|
||||
staticPreview: this.render({ period: 'hw', hits: 2156336 }),
|
||||
keywords,
|
||||
},
|
||||
{
|
||||
title: 'jsDelivr Hits (GitHub)',
|
||||
pattern: 'hm/:user/:repo',
|
||||
namedParams: {
|
||||
user: 'jquery',
|
||||
repo: 'jquery',
|
||||
},
|
||||
staticPreview: this.render({ period: 'hm', hits: 9809876 }),
|
||||
keywords,
|
||||
},
|
||||
{
|
||||
title: 'jsDelivr Hits (GitHub)',
|
||||
pattern: 'hy/:user/:repo',
|
||||
namedParams: {
|
||||
user: 'jquery',
|
||||
repo: 'jquery',
|
||||
},
|
||||
staticPreview: this.render({ period: 'hy', hits: 95317723 }),
|
||||
keywords,
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
50
services/jsdelivr/jsdelivr-hits-github.tester.js
Normal file
50
services/jsdelivr/jsdelivr-hits-github.tester.js
Normal file
@@ -0,0 +1,50 @@
|
||||
'use strict'
|
||||
|
||||
const Joi = require('joi')
|
||||
const { withRegex } = require('../test-validators')
|
||||
|
||||
const t = (module.exports = require('../tester').createServiceTester())
|
||||
|
||||
t.create('(live) jquery/jquery hits/day')
|
||||
.get('/hd/jquery/jquery.json')
|
||||
.expectJSONTypes(
|
||||
Joi.object().keys({
|
||||
name: 'jsdelivr',
|
||||
value: withRegex(/^(\d)+([kMG])*( hits\/)+(day)$/),
|
||||
})
|
||||
)
|
||||
|
||||
t.create('(live) jquery/jquery hits/week')
|
||||
.get('/hw/jquery/jquery.json')
|
||||
.expectJSONTypes(
|
||||
Joi.object().keys({
|
||||
name: 'jsdelivr',
|
||||
value: withRegex(/^(\d)+([kMG])*( hits\/)+(week)$/),
|
||||
})
|
||||
)
|
||||
|
||||
t.create('(live) jquery/jquery hits/month')
|
||||
.get('/hm/jquery/jquery.json')
|
||||
.expectJSONTypes(
|
||||
Joi.object().keys({
|
||||
name: 'jsdelivr',
|
||||
value: withRegex(/^(\d)+([kMG])*( hits\/)+(month)$/),
|
||||
})
|
||||
)
|
||||
|
||||
t.create('(live) jquery/jquery hits/year')
|
||||
.get('/hy/jquery/jquery.json')
|
||||
.expectJSONTypes(
|
||||
Joi.object().keys({
|
||||
name: 'jsdelivr',
|
||||
value: withRegex(/^(\d)+([kMG])*( hits\/)+(year)$/),
|
||||
})
|
||||
)
|
||||
|
||||
t.create('(live) fake package')
|
||||
.get('/hd/somefakepackage/somefakepackage.json')
|
||||
.expectJSON({
|
||||
name: 'jsdelivr',
|
||||
// Will return 0 hits/day as the endpoint can't send 404s at present.
|
||||
value: '0 hits/day',
|
||||
})
|
||||
77
services/jsdelivr/jsdelivr-hits-npm.service.js
Normal file
77
services/jsdelivr/jsdelivr-hits-npm.service.js
Normal file
@@ -0,0 +1,77 @@
|
||||
'use strict'
|
||||
|
||||
const {
|
||||
schema,
|
||||
keywords,
|
||||
periodMap,
|
||||
BaseJsDelivrService,
|
||||
} = require('./jsdelivr-base')
|
||||
|
||||
module.exports = class jsDelivrHitsNPM extends BaseJsDelivrService {
|
||||
static get route() {
|
||||
return {
|
||||
base: 'jsdelivr/npm',
|
||||
pattern: ':period(hd|hw|hm|hy)/:pkg',
|
||||
}
|
||||
}
|
||||
|
||||
async handle({ period, pkg }) {
|
||||
const { total } = await this.fetch({ period, pkg })
|
||||
return this.constructor.render({ period, hits: total })
|
||||
}
|
||||
|
||||
async fetch({ period, pkg }) {
|
||||
const url = `https://data.jsdelivr.com/v1/package/npm/${pkg}/stats/date/${
|
||||
periodMap[period]
|
||||
}`
|
||||
|
||||
return this._requestJson({
|
||||
schema,
|
||||
url,
|
||||
headers: {
|
||||
'User-Agent': 'Shields.io',
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
static get examples() {
|
||||
return [
|
||||
{
|
||||
title: 'jsDelivr Hits (npm)',
|
||||
pattern: 'hd/:packageName',
|
||||
namedParams: {
|
||||
packageName: 'jquery',
|
||||
},
|
||||
staticPreview: this.render({ period: 'hd', hits: 31471644 }),
|
||||
keywords,
|
||||
},
|
||||
{
|
||||
title: 'jsDelivr Hits (npm)',
|
||||
pattern: 'hw/:packageName',
|
||||
namedParams: {
|
||||
packageName: 'jquery',
|
||||
},
|
||||
staticPreview: this.render({ period: 'hw', hits: 209922436 }),
|
||||
keywords,
|
||||
},
|
||||
{
|
||||
title: 'jsDelivr Hits (npm)',
|
||||
pattern: 'hm/:packageName',
|
||||
namedParams: {
|
||||
packageName: 'jquery',
|
||||
},
|
||||
staticPreview: this.render({ period: 'hm', hits: 920101789 }),
|
||||
keywords,
|
||||
},
|
||||
{
|
||||
title: 'jsDelivr Hits (npm)',
|
||||
pattern: 'hy/:packageName',
|
||||
namedParams: {
|
||||
packageName: 'jquery',
|
||||
},
|
||||
staticPreview: this.render({ period: 'hy', hits: 10576760414 }),
|
||||
keywords,
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
50
services/jsdelivr/jsdelivr-hits-npm.tester.js
Normal file
50
services/jsdelivr/jsdelivr-hits-npm.tester.js
Normal file
@@ -0,0 +1,50 @@
|
||||
'use strict'
|
||||
|
||||
const Joi = require('joi')
|
||||
const { withRegex } = require('../test-validators')
|
||||
|
||||
const t = (module.exports = require('../tester').createServiceTester())
|
||||
|
||||
t.create('(live) jquery hits/day')
|
||||
.get('/hd/jquery.json')
|
||||
.expectJSONTypes(
|
||||
Joi.object().keys({
|
||||
name: 'jsdelivr',
|
||||
value: withRegex(/^(\d)+([kMG])*( hits\/)+(day)$/),
|
||||
})
|
||||
)
|
||||
|
||||
t.create('(live) jquery hits/week')
|
||||
.get('/hw/jquery.json')
|
||||
.expectJSONTypes(
|
||||
Joi.object().keys({
|
||||
name: 'jsdelivr',
|
||||
value: withRegex(/^(\d)+([kMG])*( hits\/)+(week)$/),
|
||||
})
|
||||
)
|
||||
|
||||
t.create('(live) jquery hits/month')
|
||||
.get('/hm/jquery.json')
|
||||
.expectJSONTypes(
|
||||
Joi.object().keys({
|
||||
name: 'jsdelivr',
|
||||
value: withRegex(/^(\d)+([kMG])*( hits\/)+(month)$/),
|
||||
})
|
||||
)
|
||||
|
||||
t.create('(live) jquery hits/year')
|
||||
.get('/hy/jquery.json')
|
||||
.expectJSONTypes(
|
||||
Joi.object().keys({
|
||||
name: 'jsdelivr',
|
||||
value: withRegex(/^(\d)+([kMG])*( hits\/)+(year)$/),
|
||||
})
|
||||
)
|
||||
|
||||
t.create('(live) fake package')
|
||||
.get('/hd/somefakepackage.json')
|
||||
.expectJSON({
|
||||
name: 'jsdelivr',
|
||||
// Will return 0 hits/day as the endpoint can't send 404s at present.
|
||||
value: '0 hits/day',
|
||||
})
|
||||
Reference in New Issue
Block a user