-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
75 additions
and
3 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
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import * as _ from './core'; | ||
|
||
const json = async <T = unknown>(url: string) => (await _.json('GET', url)) as T; | ||
const json = async <T = unknown>(url: string, headers?: HeadersInit) => (await _.json('GET', url, headers)) as T; | ||
|
||
export { json }; |
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,26 @@ | ||
import * as v from 'valibot'; | ||
|
||
import { ftch, memo, url } from './@internal'; | ||
|
||
const PackageNameSchema = v.pipe(v.string(), v.regex(/^@[a-z0-9-~][a-z0-9-._~]*\/[a-z0-9-~][a-z0-9-._~]*$/), v.regex(/^(?!.*-$)[\s\S]*$/)); | ||
|
||
const getValidPackageNameParam = (param: Record<string, string>, key: string = 'name') => v.parse(PackageNameSchema, param[key]); | ||
|
||
type PackageItem = v.InferOutput<typeof PackageItemSchema>; | ||
|
||
const PackageItemSchema = v.looseObject({ | ||
scope: v.string(), | ||
name: v.string(), | ||
latest: v.string(), | ||
}); | ||
|
||
const loadPackageItem = memo<PackageItem>(); | ||
|
||
const registry = url('https://jsr.io'); | ||
|
||
const getPackageItem = (name: string): Promise<PackageItem> => loadPackageItem(name, async () => v.parse(PackageItemSchema, await ftch.get.json(registry`/${name}/meta.json`, { Accept: 'json' }))); | ||
|
||
export type { PackageItem }; | ||
export { PackageNameSchema }; | ||
export { getPackageItem }; | ||
export { getValidPackageNameParam }; |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export { default as bundlejs } from './bundlejs'; | ||
export { default as github } from './github'; | ||
export { default as npm } from './npm'; | ||
export { default as jsr } from './jsr'; | ||
export { default as ui } from './ui'; |
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,40 @@ | ||
import { Hono } from 'hono'; | ||
|
||
import { jsr } from '~/libs'; | ||
|
||
import { getValidColorQuery, getValidThemeQuery, core } from '~/ui'; | ||
|
||
import * as response from '~/utils/response'; | ||
|
||
export default new Hono() | ||
|
||
/** | ||
* api | ||
*/ | ||
.route( | ||
'/api', | ||
new Hono().get('/item/:name{.+$}', async (ctx) => { | ||
const { getPackageItem, getValidPackageNameParam } = await jsr(); | ||
|
||
return ctx.json(await getPackageItem(getValidPackageNameParam(ctx.req.param()))); | ||
}) | ||
) | ||
|
||
/** | ||
* version | ||
*/ | ||
.get('/v/:name{.+$}', async (ctx) => { | ||
const { getPackageItem, getValidPackageNameParam } = await jsr(); | ||
|
||
const param = ctx.req.param(); | ||
const query = ctx.req.query(); | ||
|
||
const n = getValidPackageNameParam(param); | ||
|
||
const c = getValidColorQuery(query); | ||
const t = getValidThemeQuery(query); | ||
|
||
const v = (await getPackageItem(n)).latest; | ||
|
||
return await response.svg(ctx, async () => core.Badge({ c, t, w: core.calcBadgeWidth(v), children: v })); | ||
}); |