UNPKG

@akala/core

Version:
153 lines 5.67 kB
import { each } from '../each.js'; import { process, convertToErrorMiddleware, convertToMiddleware, isErrorMiddleware, isStandardMiddleware } from './shared.js'; /** * A composite middleware class with priority for synchronous operations. * @template T - The type of the arguments. * @template TSpecialNextParam - The type of the special next parameter. */ export class MiddlewareCompositeWithPriority { name; constructor(name) { this.name = name; } stack = []; /** * Adds middleware with a specified priority. * @param {number} priority - The priority of the middleware (the lowest first). * @param {...AnySyncMiddleware<T, TSpecialNextParam>} middlewares - The middlewares to add. * @returns {this} The instance of the middleware composite. */ useMiddleware(priority, ...middlewares) { this.stack.push(...middlewares.map(m => [priority, m])); this.stack.sort((a, b) => a[0] - b[0]); return this; } /** * Adds standard middleware with a specified priority. * @param {number} priority - The priority of the middleware (the lowest first). * @param {...Function} middlewares - The middlewares to add. * @returns {this} The instance of the middleware composite. */ use(priority, ...middlewares) { return this.useMiddleware(priority, ...middlewares.map(m => convertToMiddleware(m))); } /** * Adds error middleware with a specified priority. * @param {number} priority - The priority of the middleware (the lowest first). * @param {...Function} middlewares - The middlewares to add. * @returns {this} The instance of the middleware composite. */ useError(priority, ...middlewares) { return this.useMiddleware(priority, ...middlewares.map(m => convertToErrorMiddleware(m))); } /** * Processes the middleware stack. * @param {...T} req - The request parameters. * @returns {X} The result of the middleware processing. */ process(...req) { return process(this, ...req); } /** * Handles errors in the middleware stack. * @param {Error | OptionsResponse} error - The error to handle. * @param {...T} req - The request parameters. * @returns {MiddlewareResult<TSpecialNextParam>} The result of the error handling. */ handleError(error, ...req) { let failed = !!error; // this.stack.sort((a, b) => a[0] - b[0]); try { if (this.stack.length === 0) return error; each(this.stack, (middleware) => { try { if (failed && isErrorMiddleware(middleware[1])) { const err = middleware[1].handleError(error, ...req); if (err === 'break') throw err; if (typeof err != 'string' && typeof err != 'undefined') error = err; failed = true; } } catch (x) { throw { success: x }; } }); return error; } catch (err) { switch (typeof err) { case 'string': return; case 'undefined': return error; default: throw err.success; } } } /** * Handles the middleware stack. * @param {...T} req - The request parameters. * @returns {MiddlewareResult<TSpecialNextParam>} The result of the middleware handling. */ handle(...req) { let error = undefined; let failed = undefined; // this.stack.sort((a, b) => a[0] - b[0]); try { if (this.stack.length === 0) return error; each(this.stack, (middleware) => { if (failed && isErrorMiddleware(middleware[1])) { let err; try { err = middleware[1].handleError(error, ...req); } catch (e) { throw { success: e }; } if (err === 'break') throw err; if (typeof err != 'string' && typeof err != 'undefined') error = err; failed = true; } else if (!failed && isStandardMiddleware(middleware[1])) { let err; try { err = middleware[1].handle(...req); } catch (e) { throw { success: e }; } if (err === 'break') throw err; if (typeof err != 'string' && typeof err != 'undefined') { if (err['allow']) error['allow'].push(...err['allow']); else error = err; } failed = err instanceof Error; } else return; }); return error; } catch (err) { switch (typeof err) { case 'string': return; case 'undefined': return error; default: throw err.success; } } } } //# sourceMappingURL=composite-with-priority-sync.js.map