@whatwg-node/router
Version:
Fetch API compliant HTTP Router
30 lines (27 loc) • 1.07 kB
JavaScript
import { createServerAdapter } from '@whatwg-node/server';
export { withCORS, withErrorHandling } from '@whatwg-node/server';
import { Router } from 'itty-router';
import { Request } from '@whatwg-node/fetch';
export { Response } from '@whatwg-node/fetch';
function createRouter(options) {
var _a;
let ittyRouter = Router({
base: options === null || options === void 0 ? void 0 : options.base,
});
ittyRouter.all('*', request => {
let parsedUrl;
Object.defineProperty(request, 'parsedUrl', {
get() {
if (!parsedUrl) {
parsedUrl = new URL(request.url);
}
return parsedUrl;
},
});
});
(_a = options === null || options === void 0 ? void 0 : options.plugins) === null || _a === void 0 ? void 0 : _a.forEach(plugin => {
ittyRouter = plugin(ittyRouter);
});
return createServerAdapter(ittyRouter, (options === null || options === void 0 ? void 0 : options.RequestCtor) || Request);
}
export { createRouter };