badgen.net/api/runkit.ts

85 wiersze
2.5 KiB
TypeScript
Czysty Zwykły widok Historia

2019-06-01 13:17:31 +00:00
import got from '../libs/got'
import { createBadgenHandler, PathArgs } from '../libs/create-badgen-handler'
2019-06-01 13:17:31 +00:00
const help = `
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:
\`\`\`
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"
}
\`\`\`
3. Construct badgen url using \`owner\` and \`notebook\` params:
2019-06-01 13:17:31 +00:00
\`\`\`
https://badgen.net/runkit/amio/cal-badge
2019-06-01 13:17:31 +00:00
\`\`\`
That's it:
![](/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
\`\`\`
https://badgen.net/runkit/amio/cal-badge/Asia/Shanghai
2019-06-01 13:17:31 +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
\`\`\`
https://runkit.io/amio/cal-badge/branches/master/Asia/Shanghai
2019-06-01 13:17:31 +00:00
\`\`\`
`
export default createBadgenHandler({
2019-06-02 03:55:57 +00:00
title: 'With RunKit Endpoint',
help,
2019-06-01 13:17:31 +00:00
examples: {
'/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
},
handlers: {
'/runkit/:endpoint-id<.+-[a-z0-9]{12}>/:path*': handler, // legacy handler
'/runkit/:owner/:notebook/:path*': handler
}
})
2019-06-01 13:17:31 +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}`
return await got(endpoint).json<any>()
2019-06-01 13:17:31 +00:00
}