UNPKG

@quiltjs/quilt

Version:

Lightweight, type-safe handler and router abstraction for Node HTTP servers.

68 lines 1.94 kB
import { executeHandler } from './executeHandler.js'; export class Quilt { adapter; errorHandler; hooks; constructor(adapter) { this.adapter = adapter; } get(path, handler) { this.adapter.get(path, async (req, res) => { await this.handleRequest({ req, res }, handler); }); } post(path, handler) { this.adapter.post(path, async (req, res) => { await this.handleRequest({ req, res }, handler); }); } put(path, handler) { this.adapter.put(path, async (req, res) => { await this.handleRequest({ req, res }, handler); }); } patch(path, handler) { this.adapter.patch(path, async (req, res) => { await this.handleRequest({ req, res }, handler); }); } delete(path, handler) { this.adapter.delete(path, async (req, res) => { await this.handleRequest({ req, res }, handler); }); } options(path, handler) { this.adapter.options(path, async (req, res) => { await this.handleRequest({ req, res }, handler); }); } head(path, handler) { this.adapter.head(path, async (req, res) => { await this.handleRequest({ req, res }, handler); }); } setErrorHandler(errorHandler) { this.errorHandler = errorHandler; } setHooks(hooks) { this.hooks = hooks; } async handleRequest(ctx, handler) { if (!this.errorHandler) { await executeHandler(handler, ctx, this.hooks); return; } try { await executeHandler(handler, ctx, this.hooks); } catch (error) { if (error instanceof Error) { await this.errorHandler(error, ctx); } else { throw error; } } } } //# sourceMappingURL=Quilt.js.map