Simplify badgen-serve params

pull/282/head
amio 2019-05-14 09:15:16 +08:00
rodzic cddc56ff27
commit b35a002880
8 zmienionych plików z 23 dodań i 43 usunięć

Wyświetl plik

@ -1,6 +1,6 @@
import got from '../libs/got'
import { millify, stars, version, versionColor } from '../libs/utils'
import { badgenServe, BadgenServeHandlers, BadgenServeHandlerArgs } from '../libs/badgen-serve'
import { badgenServe } from '../libs/badgen-serve'
export const help = ``
@ -12,11 +12,12 @@ export const examples = [
'/amo/reviews/markdown-viewer-chrome',
]
const handlers = {
export const handlers = {
'/amo/:topic/:name': handler
}
async function handler ({ topic, name }: BadgenServeHandlerArgs) {
async function handler (args) {
const { topic, name } = args
const endpoint = `https://addons.mozilla.org/api/v3/addons/addon/${name}/`
const addon = await got(endpoint).then(res => res.body)
@ -60,4 +61,4 @@ async function handler ({ topic, name }: BadgenServeHandlerArgs) {
}
}
export default badgenServe(handlers, { examples })
export default badgenServe(handlers)

Wyświetl plik

@ -26,4 +26,4 @@ async function handler (args) {
}
}
export default badgenServe(handlers, { examples })
export default badgenServe(handlers)

Wyświetl plik

@ -1,5 +1,6 @@
import path from 'path'
import serve404 from '../libs/serve-404'
import serveHelp from '../libs/serve-help'
// Handles `/docs/:name`
export default async function (req, res) {
@ -11,27 +12,14 @@ export default async function (req, res) {
const { help, examples, handlers } = await import(handlerModulePath)
if (help || examples) {
return serveHelp(req, res, name, { help, examples, handlers })
} else {
return serve404(req, res)
}
} catch (error) {
if (error.code === 'MODULE_NOT_FOUND') {
return serve404(req, res)
} else {
if (error.code !== 'MODULE_NOT_FOUND') {
console.error(error)
return serve404(req, res)
throw error
}
}
}
serve404(req, res)
}
function serveHelp (req, res, id, { help, examples, handlers }) {
const Docs = help ? help : `# ${id}`
const Schemes = `## Schemes\n\n${Object.keys(handlers).join('\n')}`
const Examples = `## Examples\n\n${examples.join('\n')}`
const md = [Docs, Schemes, Examples].join('\n\n')
res.end(md)
}

Wyświetl plik

@ -24,4 +24,4 @@ async function handler (args) {
}
}
export default badgenServe(handlers, { examples })
export default badgenServe(handlers)

Wyświetl plik

@ -2,29 +2,19 @@ import url from 'url'
import PathParser from 'path-parser'
import serve404 from './serve-404.js'
import serveHelp from './serve-help.js'
import serveBadge from './serve-badge.js'
type BadgenParams = {
export type BadgenParams = {
subject: string
status: string
color: string
}
type BadgenServeOptions = {
help?: string
examples?: string[]
}
export type BadgenServeHandlerArgs = { [key: string]: string }
export type BadgenServeHandler = (args: BadgenServeHandlerArgs) => Promise<BadgenParams>
export type BadgenServeHandlers = { [key: string]: BadgenServeHandler }
export function badgenServe (
handlers: BadgenServeHandlers,
options: BadgenServeOptions
): Function {
const { help = '', examples = [] } = options
export function badgenServe (handlers: BadgenServeHandlers): Function {
return async function httpHandler (req, res) {
const { pathname = '/', query } = url.parse(req.url, true)
@ -33,11 +23,6 @@ export function badgenServe (
return res.end()
}
// serve help message
if (pathname.startsWith('/help')) {
return serveHelp(req, res, help, examples)
}
// Lookup handler
let matchedArgs
const matchedScheme = Object.keys(handlers).find(scheme => {

Wyświetl plik

@ -1,3 +0,0 @@
module.exports = async function (req, res, help, examples) {
res.end(help)
}

Wyświetl plik

@ -0,0 +1,8 @@
export default function serveHelp (req, res, id, { help, examples, handlers }) {
const Docs = help ? help : `# ${id}`
const Schemes = `## Schemes\n\n${Object.keys(handlers).join('\n')}`
const Examples = `## Examples\n\n${examples.join('\n')}`
const md = [Docs, Schemes, Examples].join('\n\n')
res.end(md)
}

Wyświetl plik

@ -12,7 +12,8 @@
"lib": ["esnext"]
},
"include": [
"./endpoints",
"./types"
"index.ts",
"endpoints",
"types"
]
}