UNPKG

@webda/core

Version:

Expose API with Lambda

173 lines (172 loc) 4.34 kB
import { JSONSchema7 } from "json-schema"; import { OpenAPIV3 } from "openapi-types"; import { Core } from "./core.js"; import { CoreModel, CoreModelDefinition } from "./models/coremodel.js"; import { WebContext } from "./utils/context.js"; import { HttpMethodType } from "./utils/httpcontext.js"; type RecursivePartial<T> = { [P in keyof T]?: RecursivePartial<T[P]>; }; export interface OpenApiWebdaOperation extends RecursivePartial<OpenAPIV3.OperationObject> { schemas?: { input?: JSONSchema7 | string; output?: JSONSchema7 | string; }; } /** * Define overridable OpenAPI description */ export interface OpenAPIWebdaDefinition extends RecursivePartial<OpenAPIV3.PathItemObject> { /** * Do not output for this specific Route * * It can still be output with --include-hidden, as it is needed to declare * the route in API Gateway or any other internal documentation */ hidden?: boolean; /** * If defined will link to the model schema instead of a generic Object */ model?: string; /** * Tags defined for all methods */ tags?: string[]; post?: OpenApiWebdaOperation; put?: OpenApiWebdaOperation; patch?: OpenApiWebdaOperation; get?: OpenApiWebdaOperation; } /** * Route Information default information */ export interface RouteInfo { model?: CoreModelDefinition; /** * HTTP Method to expose */ methods: HttpMethodType[]; /** * Executor name */ executor: string; /** * Method name on the executor */ _method?: string | Function; /** * OpenAPI definition */ openapi?: OpenAPIWebdaDefinition; /** * URI Template parser */ _uriTemplateParse?: { fromUri: (uri: string, options?: { strict: boolean; }) => any; varNames: any; }; /** * Query parameters to extract */ _queryParams?: { name: string; required: boolean; }[]; /** * Catch all parameter */ _queryCatchAll?: string; /** * Hash */ hash?: string; /** * Intend to override existing */ override?: boolean; } /** * Manage Route resolution * @category CoreFeatures */ export declare class Router { protected routes: Map<string, RouteInfo[]>; protected initiated: boolean; protected pathMap: { url: string; config: RouteInfo; }[]; protected webda: Core; protected models: Map<string, string>; constructor(webda: Core); /** * Registration of a model * @param model * @param url */ registerModelUrl(model: string, url: string): void; /** * Return the route for model * @param model * @returns */ getModelUrl(model: string | CoreModel): string; /** * Include prefix to the url if not present * @param url * @returns */ getFinalUrl(url: string): string; /** * Return routes * @returns */ getRoutes(): Map<string, RouteInfo[]>; /** * Add a route dynamicaly * * @param {String} url of the route can contains dynamic part like {uuid} * @param {Object} info the type of executor */ addRoute(url: string, info: RouteInfo): void; /** * Remove a route dynamicly * * @param {String} url to remove */ removeRoute(url: string, info?: RouteInfo): void; /** * Reinit all routes * * It will readd the URITemplates if needed * Sort all routes again */ remapRoutes(): void; protected comparePath(a: any, b: any): number; /** * @hidden */ protected initURITemplates(config: Map<string, RouteInfo[]>): void; /** * Get all method for a specific url * @param config * @param method * @param url */ getRouteMethodsFromUrl(url: any): HttpMethodType[]; /** * Get the route from a method / url */ getRouteFromUrl(ctx: WebContext, method: HttpMethodType, url: string): any; protected getOpenAPISchema(schema: any): any; /** * Add all known routes to paths * * @param openapi to complete * @param skipHidden add hidden routes or not */ completeOpenAPI(openapi: OpenAPIV3.Document, skipHidden?: boolean): void; } export {};