@micahjonas/switchboard
Version:
Switchboard, a small fetch api router useful for cloudflare workers. Based on Trouter.
38 lines (34 loc) • 911 B
JavaScript
import Trouter from 'trouter'
export default class CFTrouter extends Trouter {
constructor(opts={}) {
super(opts)
}
async handleRequest(request) {
const url = new URL(request.url)
const { handlers, params } = this.find(request.method, url.pathname)
try {
for await (const handle of handlers) {
const res = await handle(request, params)
if (res && res instanceof Response) {
return res
}
}
return new Response(JSON.stringify(e, null, ' '), {
status: 400,
statusText: 'No response generated',
})
} catch (e) {
return new Response(JSON.stringify(e, null, ' '), {
status: 500,
statusText: 'Worker failed',
})
}
}
sendJsonResponse(json) {
return new Response(JSON.stringify(json), {
headers: {
'content-type': 'text/html;charset=UTF-8',
},
})
}
}