Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor [cdnjs] integration #1668

Merged
merged 2 commits into from
May 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions lib/all-badge-examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -739,14 +739,6 @@ const allBadgeExamples = [
name: 'Version'
},
examples: [
{
title: 'CDNJS',
previewUri: '/cdnjs/v/jquery.svg',
keywords: [
'cdn',
'cdnjs'
]
},
{
title: 'npm',
previewUri: '/npm/v/npm.svg',
Expand Down
2 changes: 1 addition & 1 deletion lib/error-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async function asJson({ buffer, res }) {
try {
return JSON.parse(buffer);
} catch (err) {
throw new InvalidResponse();
throw new InvalidResponse(undefined, err);
}
};

Expand Down
33 changes: 0 additions & 33 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -1609,39 +1609,6 @@ cache(function(data, match, sendBadge, request) {
});
}));

// CDNJS version integration
camp.route(/^\/cdnjs\/v\/(.*)\.(svg|png|gif|jpg|json)$/,
cache(function(data, match, sendBadge, request) {
const library = encodeURIComponent(match[1]); // eg, "express" or "@user/express"
const format = match[2];
const apiUrl = 'https://api.cdnjs.com/libraries/' + library + '?fields=version';
const badgeData = getBadgeData('cdnjs', data);
request(apiUrl, function(err, res, buffer) {
if (err != null) {
badgeData.text[1] = 'inaccessible';
sendBadge(format, badgeData);
return;
}
try {
const json = JSON.parse(buffer);
if (Object.keys(json).length === 0) {
/* Note the 'not found' response from cdnjs is:
status code = 200, body = {} */
badgeData.text[1] = 'not found';
sendBadge(format, badgeData);
return;
}
const version = json.version || 0;
badgeData.text[1] = versionText(version);
badgeData.colorscheme = versionColor(version);
sendBadge(format, badgeData);
} catch(e) {
badgeData.text[1] = 'invalid';
sendBadge(format, badgeData);
}
});
}));

// npm weekly download integration.
mapNpmDownloads({ camp, cache }, 'dw', 'last-week');

Expand Down
61 changes: 61 additions & 0 deletions services/cdnjs/cdnjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict';

const BaseService = require('../base');
const {
checkErrorResponse,
asJson,
} = require('../../lib/error-helper');
const { NotFound } = require('../errors');
const { addv: versionText } = require('../../lib/text-formatters');
const { version: versionColor} = require('../../lib/color-formatters');

module.exports = class Cdnjs extends BaseService {
async handle({library}) {
const apiUrl = 'https://api.cdnjs.com/libraries/' + library + '?fields=version';
const json = await this._sendAndCacheRequest(apiUrl, {
headers: { 'Accept': 'application/json' }
}).then(checkErrorResponse.asPromise())
.then(asJson);

if (Object.keys(json).length === 0) {
/* Note the 'not found' response from cdnjs is:
status code = 200, body = {} */
throw new NotFound();
}
const version = json.version || 0;

return {
message: versionText(version),
color: versionColor(version)
};
}

// Metadata
static get defaultBadgeData() {
return { label: 'cdnjs' };
}

static get category() {
return 'version';
}

static get url() {
return {
base: 'cdnjs/v',
format: '(.*)',
capture: ['library']
};
}

static get examples() {
return [
{
previewUrl: 'jquery',
keywords: [
'cdn',
'cdnjs'
]
}
];
}
};