2019-06-01 13:17:31 +00:00
|
|
|
import got from '../libs/got'
|
2019-11-18 15:52:07 +00:00
|
|
|
import { createBadgenHandler, PathArgs } from '../libs/create-badgen-handler'
|
2019-06-01 13:17:31 +00:00
|
|
|
|
2019-06-01 13:53:29 +00:00
|
|
|
const help = `
|
2021-01-08 03:27:34 +00:00
|
|
|
https://badgen.net/runkit/amio/cal-badge/Asia/Shanghai
|
|
|
|
──┬─ ────┬──── ──┬──────────
|
|
|
|
│ │ └─ path-args (optional)
|
|
|
|
│ notebook
|
|
|
|
│
|
|
|
|
owner
|
2019-06-01 13:17:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
## RunKit Endpoint
|
|
|
|
|
|
|
|
https://runkit.com/home#endpoint
|
|
|
|
|
|
|
|
If you are not familiar with RunKit endpoint, [this guide](https://runkit.com/docs/endpoint) would help.
|
|
|
|
|
|
|
|
## Use Badgen with RunKit Endpoint
|
|
|
|
|
|
|
|
1. Create a RunKit notebook (e.g. https://runkit.com/amio/cal-badge), which gives you an endpoint:
|
|
|
|
|
|
|
|
\`\`\`
|
2021-01-08 03:27:34 +00:00
|
|
|
https://runkit.io/amio/cal-badge/branches/master
|
2019-06-01 13:17:31 +00:00
|
|
|
\`\`\`
|
|
|
|
|
2019-06-01 13:57:47 +00:00
|
|
|
it returns a JSON like:
|
2019-06-01 13:17:31 +00:00
|
|
|
|
|
|
|
\`\`\`
|
|
|
|
{
|
|
|
|
"subject": "2019",
|
|
|
|
"status": "6-1",
|
|
|
|
"color": "blue"
|
|
|
|
}
|
|
|
|
\`\`\`
|
|
|
|
|
2021-01-08 03:27:34 +00:00
|
|
|
3. Construct badgen url using \`owner\` and \`notebook\` params:
|
2019-06-01 13:17:31 +00:00
|
|
|
|
|
|
|
\`\`\`
|
2021-01-08 03:27:34 +00:00
|
|
|
https://badgen.net/runkit/amio/cal-badge
|
2019-06-01 13:17:31 +00:00
|
|
|
\`\`\`
|
|
|
|
|
|
|
|
That's it:
|
|
|
|
|
2021-01-08 03:27:34 +00:00
|
|
|
![](/runkit/amio/cal-badge)
|
2019-06-01 13:17:31 +00:00
|
|
|
|
2019-06-01 16:05:35 +00:00
|
|
|
Furthermore, you can append arbitrary path args (e.g. \`/Asia/Shanghai\`) to the end of badgen url, Badgen will request RunKit endpoint with that. This badge:
|
2019-06-01 13:17:31 +00:00
|
|
|
|
|
|
|
\`\`\`
|
2021-01-08 03:27:34 +00:00
|
|
|
https://badgen.net/runkit/amio/cal-badge/Asia/Shanghai
|
2019-06-01 13:17:31 +00:00
|
|
|
\`\`\`
|
|
|
|
|
2021-01-08 03:27:34 +00:00
|
|
|
![](/runkit/amio/cal-badge/Asia/Shanghai)
|
|
|
|
|
2019-06-01 13:57:47 +00:00
|
|
|
represents data from:
|
2019-06-01 13:17:31 +00:00
|
|
|
|
|
|
|
\`\`\`
|
2021-01-08 03:27:34 +00:00
|
|
|
https://runkit.io/amio/cal-badge/branches/master/Asia/Shanghai
|
2019-06-01 13:17:31 +00:00
|
|
|
\`\`\`
|
|
|
|
`
|
|
|
|
|
2019-11-18 15:52:07 +00:00
|
|
|
export default createBadgenHandler({
|
2019-06-02 03:55:57 +00:00
|
|
|
title: 'With RunKit Endpoint',
|
2019-11-18 15:52:07 +00:00
|
|
|
help,
|
2019-06-01 13:17:31 +00:00
|
|
|
examples: {
|
2021-01-08 03:27:34 +00:00
|
|
|
'/runkit/vladimyr/metaweather/44418/state': 'metaweather (state)',
|
|
|
|
'/runkit/vladimyr/metaweather/44418/temperature': 'metaweather (temperature in °C)',
|
|
|
|
'/runkit/vladimyr/metaweather/44418/temperature/f': 'metaweather (temperature in °F)',
|
|
|
|
'/runkit/vladimyr/metaweather/44418/wind': 'metaweather (wind in km/h)',
|
|
|
|
'/runkit/vladimyr/metaweather/44418/wind/mph': 'metaweather (wind in mph)',
|
|
|
|
'/runkit/vladimyr/metaweather/44418/humidity': 'metaweather (humidity)',
|
2019-06-01 13:17:31 +00:00
|
|
|
},
|
2019-11-18 15:52:07 +00:00
|
|
|
handlers: {
|
2021-01-08 03:27:34 +00:00
|
|
|
'/runkit/:endpoint-id<.+-[a-z0-9]{12}>/:path*': handler, // legacy handler
|
|
|
|
'/runkit/:owner/:notebook/:path*': handler
|
2019-11-18 15:52:07 +00:00
|
|
|
}
|
|
|
|
})
|
2019-06-01 13:17:31 +00:00
|
|
|
|
2021-01-08 03:27:34 +00:00
|
|
|
async function handler ({ 'endpoint-id': id, owner, notebook, path = '' }: PathArgs) {
|
|
|
|
const endpoint = id
|
|
|
|
? `https://${id}.runkit.sh/${path}`
|
|
|
|
: `https://runkit.io/${owner}/${notebook}/branches/master/${path}`
|
2020-01-26 07:43:40 +00:00
|
|
|
return await got(endpoint).json<any>()
|
2019-06-01 13:17:31 +00:00
|
|
|
}
|