diff --git a/endpoints/pypi.ts b/endpoints/pypi.ts new file mode 100644 index 0000000..6b9113d --- /dev/null +++ b/endpoints/pypi.ts @@ -0,0 +1,43 @@ +import got from '../libs/got' +import { version, versionColor } from '../libs/utils' +import { + badgenServe, + BadgenServeMeta as Meta, + BadgenServeHandlers as Handlers, + BadgenServeHandlerArgs as Args +} from '../libs/badgen-serve' + +export const meta: Meta = { + title: 'Pypi', + examples: { + '/pypi/v/pip': 'version', + '/pypi/v/docutils': 'version', + '/pypi/license/pip': 'license', + } +} + +export const handlers: Handlers = { + '/pypi/:topic/:project': handler +} + +export default badgenServe(handlers) + +async function handler ({ topic, project }: Args) { + const endpoint = `https://pypi.org/pypi/${project}/json` + const { info } = await got(endpoint).then(res => res.body) + + switch (topic) { + case 'v': + return { + subject: 'pypi', + status: version(info.version), + color: versionColor(info.version) + } + case 'license': + return { + subject: 'license', + status: info.license || 'unknown', + color: 'blue' + } + } +} diff --git a/libs/examples.ts b/libs/examples.ts index 4998706..57e3945 100644 --- a/libs/examples.ts +++ b/libs/examples.ts @@ -17,6 +17,7 @@ const liveBadgeExampleList = [ 'homebrew', 'chrome-web-store', 'amo', + 'pypi', 'apm', // CI 'appveyor',