UNPKG

@t3ned/channel

Version:

Ergonomic, chaining-based Typescript framework for quick API development for Fastify

385 lines 11.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Delete = exports.Put = exports.Patch = exports.Post = exports.Get = exports.Route = void 0; const ChannelError_1 = require("../errors/ChannelError"); const constants_1 = require("../../constants"); const utils_1 = require("../../utils"); class Route { /** * @param options The route options */ constructor(options) { /** * The route path */ Object.defineProperty(this, "path", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** * The route method */ Object.defineProperty(this, "_method", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** * The supported versions for the route */ Object.defineProperty(this, "_versions", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** * The default http status for responses */ Object.defineProperty(this, "_httpStatus", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** * The schema for the request params */ Object.defineProperty(this, "_paramsSchema", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** * The schema for the request query */ Object.defineProperty(this, "_querySchema", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** * The schema for request body */ Object.defineProperty(this, "_bodySchema", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** * The onRequest hook for the route */ Object.defineProperty(this, "_onRequestHook", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** * The preParsing hook for the route */ Object.defineProperty(this, "_preParsingHook", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** * The preValidation hook for the route */ Object.defineProperty(this, "_preValidationHook", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** * The preHandler hook for the route */ Object.defineProperty(this, "_preHandlerHook", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** * The preSerialization hook for the route */ Object.defineProperty(this, "_preSerializationHook", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** * The onSend hook for the route */ Object.defineProperty(this, "_onSendHook", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** * The onResponse hook for the route */ Object.defineProperty(this, "_onResponseHook", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** * The onTimeout hook for the route */ Object.defineProperty(this, "_onTimeoutHook", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** * The onError hook for the route */ Object.defineProperty(this, "_onErrorHook", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** * The route handler */ Object.defineProperty(this, "_handler", { enumerable: true, configurable: true, writable: true, value: () => { throw new ChannelError_1.ChannelError("Handler not implemented"); } }); this.path = options.path; this._method = options.method; this._versions = options.versions ?? []; this._httpStatus = options.httpStatus ?? constants_1.HttpStatus.Ok; this._paramsSchema = options.paramsSchema; this._querySchema = options.querySchema; this._bodySchema = options.bodySchema; this._onRequestHook = options.onRequestHook; this._preParsingHook = options.preParsingHook; this._preValidationHook = options.preValidationHook; this._preHandlerHook = options.preHandlerHook; this._preSerializationHook = options.preSerializationHook; this._onSendHook = options.onSendHook; this._onResponseHook = options.onResponseHook; this._onTimeoutHook = options.onTimeoutHook; this._onErrorHook = options.onErrorHook; } /** * Add a supported version to the route * @param version The version * @param prefix The version prefix * * @returns The route */ version(version, prefix) { this._versions.push([prefix ?? null, version]); return this; } /** * Set the default http status for responses * @param httpStatus The http status * * @returns The route */ status(httpStatus) { this._httpStatus = httpStatus; return this; } /** * Set the params schema * @param schema The validation schema * * @returns The route */ params(schema) { return new Route({ path: this.path, method: this._method, versions: this._versions, httpStatus: this._httpStatus, paramsSchema: schema, querySchema: this._querySchema, bodySchema: this._bodySchema, }); } /** * Set the query schema * @param schema The validation schema * * @returns The route */ query(schema) { return new Route({ path: this.path, method: this._method, versions: this._versions, httpStatus: this._httpStatus, paramsSchema: this._paramsSchema, querySchema: schema, bodySchema: this._bodySchema, }); } /** * Set the body schema * @param schema The validation schema * * @returns The route */ body(schema) { return new Route({ path: this.path, method: this._method, versions: this._versions, httpStatus: this._httpStatus, paramsSchema: this._paramsSchema, querySchema: this._querySchema, bodySchema: schema, }); } /** * Set the handler for the route * @param handler The route handler * * @returns The route */ handler(handler) { this._handler = handler; return this; } /** * Define the onRequest hook for the route * @param handler The hook handler */ onRequest(handler) { this._onRequestHook = handler; return this; } /** * Define the preParsing hook for the route * @param handler The hook handler */ preParsing(handler) { this._preParsingHook = handler; return this; } /** * Define the preValidation hook for the route * @param handler The hook handler */ preValidation(handler) { this._preValidationHook = handler; return this; } /** * Define the preHandler hook for the route * @param handler The hook handler */ preHandler(handler) { this._preHandlerHook = handler; return this; } /** * Define the preSerialization hook for the route * @param handler The hook handler */ preSerialization(handler) { this._preSerializationHook = handler; return this; } /** * Define the onSend hook for the route * @param handler The hook handler */ onSend(handler) { this._onSendHook = handler; return this; } /** * Define the onResponse hook for the route * @param handler The hook handler */ onResponse(handler) { this._onResponseHook = handler; return this; } /** * Define the onTimeout hook for the route * @param handler The hook handler */ onTimeout(handler) { this._onTimeoutHook = handler; return this; } /** * Define the onError hook for the route * @param handler The hook handler */ onError(handler) { this._onErrorHook = handler; return this; } /** * Get the versioned routes */ toVersionedRoutes(application) { if (!this._versions.length) return [this.toJSON(application)]; return this._versions.map((version) => ({ ...this.toJSON(application), path: (0, utils_1.joinRoutePaths)(`${version[0] ?? application.routeDefaultVersionPrefix ?? ""}${version[1] ?? ""}`, this.path), })); } /** * * @returns */ toJSON(application) { return { path: (0, utils_1.joinRoutePaths)(application.routeDefaultVersion, this.path), method: this._method, httpStatus: this._httpStatus, paramsSchema: this._paramsSchema, querySchema: this._querySchema, bodySchema: this._bodySchema, handler: this._handler, onRequest: this._onRequestHook, preParsing: this._preParsingHook, preValidation: this._preValidationHook, preHandler: this._preHandlerHook, preSerialization: this._preSerializationHook, onSend: this._onSendHook, onResponse: this._onResponseHook, onTimeout: this._onTimeoutHook, onError: this._onErrorHook, }; } } exports.Route = Route; const Get = (path) => new Route({ path, method: "GET" }); exports.Get = Get; const Post = (path) => new Route({ path, method: "POST" }); exports.Post = Post; const Patch = (path) => new Route({ path, method: "PATCH" }); exports.Patch = Patch; const Put = (path) => new Route({ path, method: "PUT" }); exports.Put = Put; const Delete = (path) => new Route({ path, method: "DELETE" }); exports.Delete = Delete; //# sourceMappingURL=Route.js.map