diff --git a/next.config.js b/next.config.js index cd7a312..b4aed97 100644 --- a/next.config.js +++ b/next.config.js @@ -23,19 +23,25 @@ const nextConfig = { destination: `/api/badge/:path*`, }] - const badgeRedirects = liveBadgeRedirects.concat(staticBadgeRedirects) - - // return badgeRedirects - return [ - { source: '/static/:path*', destination: '/api/static' }, - { source: '/static', destination: '/api/static' }, - + const badgeRedirects = [ { source: '/badge/:path*', destination: '/api/static' }, { source: '/badge', destination: '/api/static' }, - - { source: '/xo/:path*', destination: '/api/xo' }, - { source: '/xo', destination: '/api/xo' }, ] + + const badgeApis = [ + '/static', + '/winget', + '/xo', + ] + + badgeApis.forEach(b => { + badgeRedirects.push({ source: `${b}/:path*`, destination: `/api${b}` }) // badges + badgeRedirects.push({ source: b, destination: `/api${b}` }) // doc pages + }) + + // const badgeRedirects = liveBadgeRedirects.concat(staticBadgeRedirects) + + return badgeRedirects }, } diff --git a/api/winget.ts b/pages/api/winget.ts similarity index 72% rename from api/winget.ts rename to pages/api/winget.ts index cf81a95..134567c 100644 --- a/api/winget.ts +++ b/pages/api/winget.ts @@ -1,9 +1,9 @@ -import got from '../libs/got' -import { restGithub } from '../libs/github' -import { parseDocument } from 'yaml' +import { restGithub } from '../../libs/github' import { basename, extname } from 'path' -import { version, versionColor } from '../libs/utils' -import { createBadgenHandler, PathArgs } from '../libs/create-badgen-handler' +import { version, versionColor } from '../../libs/utils' +import { createBadgenHandler } from '../../libs/create-badgen-handler-next' + +import type { PathArgs, BadgenResult } from '../../libs/create-badgen-handler-next' const WINGET_GITHUB_REPO = 'microsoft/winget-pkgs' @@ -98,16 +98,15 @@ class Version { export default createBadgenHandler({ title: 'winget', examples: { - '/winget/v/GitHub.cli': 'version', - '/winget/v/Balena.Etcher': 'version', - '/winget/license/Arduino.Arduino': 'license' + '/winget/v/aria2.aria2': 'version', + '/winget/v/Amazon.AWSCLI': 'version', }, handlers: { - '/winget/:topic/:appId': handler + '/winget/v/:appId': handler, } }) -async function handler ({ topic, appId }: PathArgs) { +async function handler ({ topic, appId }: PathArgs): BadgenResult { switch (topic) { case 'v': { const versions = await fetchVersions(appId) @@ -120,29 +119,25 @@ async function handler ({ topic, appId }: PathArgs) { } } case 'license': { - const yaml = await fetchManifest(appId) - const manifest = parseDocument(yaml) - const license = manifest.get('License') - return { subject: 'license', - status: license || 'unknown', + status: 'unknown', color: 'blue' } } + default: + return { + subject: 'winget', + status: 'unknown topic', + color: 'grey' + } } } -async function fetchManifest(appId: string) { - const versions = await fetchVersions(appId) - const version = last(versions) - const path = [...appId.split('.'), `${version}.yaml`].join('/') - return got(`https://github.com/${WINGET_GITHUB_REPO}/raw/master/manifests/${path}`).text() -} - async function fetchVersions(appId: string): Promise { - const path = appId.replace(/\./g, '/') - const files = await restGithub(`repos/${WINGET_GITHUB_REPO}/contents/manifests/${path}`) + const appPath = appId.replace(/\./g, '/') + const categoryPath = appPath[0].toLowerCase() + const files = await restGithub(`repos/${WINGET_GITHUB_REPO}/contents/manifests/${categoryPath}/${appPath}`) const versions = files.map(file => { const name = basename(file.name, extname(file.name)) return new Version(name)