UNPKG

acady-api-builder

Version:
122 lines 4.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ApiBuilder = void 0; const route_matching_helper_1 = require("../helpers/route-matching-helper"); const api_headers_1 = require("./api-headers"); const express_converter_1 = require("../converters/express-converter"); const aws_gateway_converter_1 = require("../converters/aws-gateway-converter"); class ApiBuilder { constructor() { this.apiRoutes = []; } any(path, requestHandler) { this.request('ANY', path, requestHandler); } get(path, requestHandler) { this.request('GET', path, requestHandler); } post(path, requestHandler) { this.request('POST', path, requestHandler); } put(path, requestHandler) { this.request('PUT', path, requestHandler); } head(path, requestHandler) { this.request('HEAD', path, requestHandler); } delete(path, requestHandler) { this.request('DELETE', path, requestHandler); } options(path, requestHandler) { this.request('OPTIONS', path, requestHandler); } patch(path, requestHandler) { this.request('PATH', path, requestHandler); } request(method, path, requestHandler) { this.apiRoutes.push({ method, path, requestHandler, }); } async processAcadyRequest(acadyRequest) { let acadyResponse; try { let routes = this.getBestMatchingRoutes(acadyRequest); if (!routes.length) { acadyResponse = { headers: new api_headers_1.ApiHeaders([]), body: 'Error: Route not found', status: 404, }; } else { for (const route of routes) { const result = await route.requestHandler(acadyRequest); if (result) { acadyResponse = result; break; } } if (!acadyResponse) { acadyResponse = { headers: new api_headers_1.ApiHeaders([]), body: 'Error: No one routes returned response', status: 500, }; } } } catch (e) { console.log(e); acadyResponse = { headers: new api_headers_1.ApiHeaders([]), body: 'Error: ' + e.message, status: 500, }; } return acadyResponse; } async process(event, response, eventType) { let acadyResponse; try { let acadyRequest = this.convertRequest(event, eventType); acadyResponse = await this.processAcadyRequest(acadyRequest); } catch (e) { console.log(e); acadyResponse = { headers: new api_headers_1.ApiHeaders([]), body: 'Error: ' + e.message, status: 500, }; } this.convertResponse(acadyResponse, response, eventType); } convertRequest(event, eventType) { switch (eventType) { case express_converter_1.ExpressConverter.TYPE: return express_converter_1.ExpressConverter.convertRequest(event); case aws_gateway_converter_1.AwsGatewayConverter.TYPE: return aws_gateway_converter_1.AwsGatewayConverter.convertRequest(event); default: throw new Error('EventType ' + eventType + ' not known!'); } } convertResponse(acadyApiResponse, response, eventType) { switch (eventType) { case express_converter_1.ExpressConverter.TYPE: return express_converter_1.ExpressConverter.convertResponse(acadyApiResponse, response); case aws_gateway_converter_1.AwsGatewayConverter.TYPE: return aws_gateway_converter_1.AwsGatewayConverter.convertResponse(acadyApiResponse, response); default: throw new Error('EventType ' + eventType + ' not known!'); } } getBestMatchingRoutes(apiRequest) { return this.apiRoutes.filter((apiRoute) => route_matching_helper_1.RouteMatchingHelper.match(apiRoute, apiRequest)); } } exports.ApiBuilder = ApiBuilder; //# sourceMappingURL=api-builder.js.map