-
-
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.
Rewrite [npm] typedefs badge using new-style service (#1735)
For #1358
- Loading branch information
1 parent
f78e6f1
commit fa61247
Showing
3 changed files
with
54 additions
and
53 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,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', | ||
}; | ||
} | ||
}; |