Files
shields/services/npm/npm-type-definitions.service.js
chris48s 9493d00483 migrate examples to openApi part 35; affects [npm] (#9866)
* migrate some services from examples to openApi

* clarify docs on scoped/unscoped packages
2024-01-06 16:41:42 +00:00

82 lines
2.0 KiB
JavaScript

import { pathParam, queryParam } from '../index.js'
import NpmBase, { packageNameDescription } from './npm-base.js'
// For this badge to correctly detect type definitions, either the relevant
// dependencies must be declared, or the `types` key must be set in
// package.json.
export default class NpmTypeDefinitions extends NpmBase {
static category = 'platform-support'
static route = this.buildRoute('npm/types', { withTag: false })
static openApi = {
'/npm/types/{packageName}': {
get: {
summary: 'NPM Type Definitions',
parameters: [
pathParam({
name: 'packageName',
example: 'chalk',
description: packageNameDescription,
}),
queryParam({
name: 'registry_uri',
example: 'https://registry.npmjs.com',
}),
],
},
},
}
static defaultBadgeData = {
label: 'types',
}
static render({ supportedLanguages }) {
if (supportedLanguages.length === 0) {
return { message: 'none', color: 'lightgray' }
} else {
return {
message: supportedLanguages.sort().join(' | '),
color: 'blue',
}
}
}
static transform({ devDependencies, types, typings, files }) {
const supportedLanguages = []
if (
types !== undefined ||
typings !== undefined ||
devDependencies.typescript !== undefined ||
files.includes('index.d.ts')
) {
supportedLanguages.push('TypeScript')
}
if (
devDependencies['flow-bin'] !== undefined ||
files.includes('index.js.flow')
) {
supportedLanguages.push('Flow')
}
return { supportedLanguages }
}
async handle(namedParams, queryParams) {
const { scope, packageName, registryUrl } = this.constructor.unpackParams(
namedParams,
queryParams,
)
const json = await this.fetchPackageData({
scope,
packageName,
registryUrl,
})
const props = this.constructor.transform(json)
return this.constructor.render(props)
}
}