badgen.net/endpoints/docs.ts

26 wiersze
647 B
TypeScript
Czysty Zwykły widok Historia

2019-05-14 00:58:24 +00:00
import path from 'path'
import serve404 from '../libs/serve-404'
2019-05-14 01:15:16 +00:00
import serveHelp from '../libs/serve-help'
2019-05-14 00:58:24 +00:00
// Handles `/docs/:name`
export default async function (req, res) {
const [ , , name ] = req.url.split('/')
if (name) {
try {
const handlerModulePath = path.join(__dirname, name)
const { help, examples, handlers } = await import(handlerModulePath)
if (help || examples) {
return serveHelp(req, res, name, { help, examples, handlers })
}
} catch (error) {
2019-05-14 01:15:16 +00:00
if (error.code !== 'MODULE_NOT_FOUND') {
2019-05-14 00:58:24 +00:00
console.error(error)
2019-05-14 01:15:16 +00:00
throw error
2019-05-14 00:58:24 +00:00
}
}
}
serve404(req, res)
}