badgen.net/pages/api/https.ts

65 wiersze
1.7 KiB
TypeScript

2023-07-26 12:17:48 +00:00
import got from '../../libs/got'
import { createBadgenHandler, PathArgs } from '../../libs/create-badgen-handler'
2019-06-02 03:47:11 +00:00
const help = `
## Use Badgen with HTTPS Endpoint
2020-04-26 13:29:13 +00:00
1. Create a https endpoint with [RunKit][runkit-href] / [Vercel][vercel-href]
2019-06-02 03:47:11 +00:00
or any platform, which returns a JSON in this format:
\`\`\`
{
subject: 'hello',
status: 'world',
color: 'blue'
}
\`\`\`
2. Assume the endpoint can be reached as:
\`\`\`
https://some-endpoint.example.com/what/ever/args
\`\`\`
then the corresponding badge url on Badgen is:
2019-06-02 03:47:11 +00:00
\`\`\`
/https/some-endpoint.example.com/what/ever/args
\`\`\`
Take this endpoint & badgen url for example:
- https://cal-badge-icd0onfvrxx6.runkit.sh
- https://badgen.net/https/cal-badge-icd0onfvrxx6.runkit.sh
Furthermore, you may append path args to it:
- https://cal-badge-icd0onfvrxx6.runkit.sh/Asia/Shanghai
- https://badgen.net/https/cal-badge-icd0onfvrxx6.runkit.sh/Asia/Shanghai
[runkit-href]: https://runkit.com/home#endpoint
2020-04-26 13:29:13 +00:00
[vercel-href]: https://vercel.com
2019-06-02 03:47:11 +00:00
<style>
li a { font-family: monospace; font-size: 0.9em }
</style>
`
export default createBadgenHandler({
2019-06-02 03:55:57 +00:00
title: 'With HTTPS Endpoint',
help,
2019-06-02 03:47:11 +00:00
examples: {
'/https/cal-badge-icd0onfvrxx6.runkit.sh': 'https endpoint',
'/https/cal-badge-icd0onfvrxx6.runkit.sh/Asia/Shanghai': 'https endpoint (with path args)',
2019-07-06 01:15:51 +00:00
'/https/cal-badge-icd0onfvrxx6.runkit.sh/America/Los_Angeles': 'https endpoint (with path args)',
2019-06-02 03:47:11 +00:00
},
handlers: {
'/https/:hostname/:pathname*': handler
}
})
2019-06-02 03:47:11 +00:00
async function handler ({ hostname, pathname }: PathArgs) {
const endpoint = `https://${hostname}/${pathname || ''}`
return await got(endpoint).json<any>()
2019-06-02 03:47:11 +00:00
}