generated from shgysk8zer0/npm-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
42 lines (40 loc) · 1.31 KB
/
handler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { HTTPError } from './HTTPError.js';
/**
* Creates an HTTP request handler to handle various HTTP methods.
*
* @param {Object.<string, (req: Request, context: object) => Response | Promise<Response>} handlers HTTP method handlers (e.g., { get: fn }).
* @returns {(req: Request, context: object) => Promise<Response>}} HTTP request handler function.
* @throws {TypeError} If handlers is empty.
*/
export function createHandler(handlers) {
const methods = typeof handlers === 'object' ? Object.keys(handlers).map(method => method.toUpperCase()) : [];
if (methods.length === 0) {
throw new TypeError('Missing list of HTTP methods and handlers.');
} else {
/**
* @param {Request} request
* @param {object} context
* @returns {Promise<Response>}
* @throws {HTTPError}
*/
return async function(request, context) {
try {
if (handlers[request.method.toLowerCase()] instanceof Function) {
return await handlers[request.method.toLowerCase()](request, context);
} else {
return new Response(null, {
status: 405,
statusText: 'Method Not Allowed',
headers: { Allow: methods.join(', ') }
});
}
} catch(err) {
if (err instanceof HTTPError) {
throw err;
} else {
throw new HTTPError('An unknown error occured.', { cause: err });
}
}
};
}
}