Skip to content

Commit

Permalink
Rewrite [npm] typedefs badge using new-style service (#1735)
Browse files Browse the repository at this point in the history
For #1358
  • Loading branch information
paulmelnikow authored Jun 16, 2018
1 parent f78e6f1 commit fa61247
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 53 deletions.
9 changes: 0 additions & 9 deletions lib/all-badge-examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -818,15 +818,6 @@ const allBadgeExamples = [
'node'
]
},
{
title: 'npm type definitions',
previewUri: '/npm/types/chalk.svg',
keywords: [
'node',
'typescript',
'flow'
]
},
{
title: 'PyPI',
previewUri: '/pypi/v/nine.svg',
Expand Down
44 changes: 0 additions & 44 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ const {
const {
defaultNpmRegistryUri,
makePackageDataUrl: makeNpmPackageDataUrl,
typeDefinitions: npmTypeDefinitions,
} = require('./lib/npm-badge-helpers');
const {
teamcityBadge
Expand Down Expand Up @@ -1918,49 +1917,6 @@ cache({
}
}));

// npm type definition integration.
camp.route(/^\/npm\/types\/(?:@([^/]+)\/)?([^/]+)\.(svg|png|gif|jpg|json)$/,
cache({
queryParams: ['registry_uri'],
handler: (queryParams, match, sendBadge, request) => {
// e.g. cycle, core, svg
const [, scope, packageName, format ] = match;
const apiUrl = makeNpmPackageDataUrl({
registryUrl: queryParams.registry_uri,
scope,
packageName,
});
const badgeData = getBadgeData('type definitions', queryParams);
request(apiUrl, { headers: { 'Accept': '*/*' } }, function(err, res, buffer) {
if (checkErrorResponse(badgeData, err, res, 'package not found')) {
sendBadge(format, badgeData);
return;
}
try {
const data = JSON.parse(buffer);
let packageData;
if (scope === undefined) {
packageData = data;
} else {
const latestVersion = data['dist-tags'].latest;
packageData = data.versions[latestVersion];
}
const typeDefinitions = npmTypeDefinitions(packageData);
if (typeDefinitions === 'none') {
badgeData.colorscheme = 'lightgray';
} else {
badgeData.colorscheme = 'blue';
}
badgeData.text[1] = typeDefinitions;
sendBadge(format, badgeData);
} catch(e) {
badgeData.text[1] = 'invalid';
sendBadge(format, badgeData);
}
});
}
}));

// Anaconda Cloud / conda package manager integration
camp.route(/^\/conda\/([dvp]n?)\/([^/]+)\/([^/]+)\.(svg|png|gif|jpg|json)$/,
cache(function(queryData, match, sendBadge, request) {
Expand Down
54 changes: 54 additions & 0 deletions services/npm/npm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict';

const { BaseJsonService } = require('../base');
const {
makePackageDataUrl,
typeDefinitions,
} = require('../../lib/npm-badge-helpers');

module.exports = class NPMTypeDefinitions extends BaseJsonService {
static get category() {
return 'version';
}

static get defaultBadgeData() {
return { label: 'type definitions' };
}

static get url() {
return {
base: 'npm/types',
format: '(?:@([^/]+)/)?([^/]+)',
capture: ['scope', 'packageName'],
queryParams: ['registry_uri'],
};
}

static get examples() {
return [{
title: 'npm type definitions',
previewUrl: 'chalk',
keywords: ['node', 'typescript', 'flow'],
}];
}

async handle({ scope, packageName }, { registry_uri: registryUrl }) {
const apiUrl = makePackageDataUrl({ registryUrl, scope, packageName });

const json = await this._requestJson(apiUrl, {}, 'package not found');

let packageData;
if (scope === undefined) {
packageData = json;
} else {
const latestVersion = json['dist-tags'].latest;
packageData = json.versions[latestVersion];
}

const message = typeDefinitions(packageData);
return {
message,
color: message === 'none' ? 'lightgray' : 'blue',
};
}
};

0 comments on commit fa61247

Please sign in to comment.