@furystack/rest-service
Version:
Repository implementation for FuryStack
106 lines • 3.85 kB
TypeScript
import type { Injector } from '@furystack/inject';
import type { Method, RestApi } from '@furystack/rest';
import type { MatchFunction } from 'path-to-regexp';
import type { CorsOptions } from './models/cors-options.js';
import type { RequestAction } from './request-action-implementation.js';
import type { OnRequest } from './server-manager.js';
import './server-response-extensions.js';
export type RestApiImplementation<T extends RestApi> = {
[TMethod in keyof T]: {
[TUrl in keyof T[TMethod]]: T[TMethod][TUrl] extends {
result: unknown;
} ? RequestAction<T[TMethod][TUrl]> : never;
};
};
export interface ImplementApiOptions<T extends RestApi> {
/**
* The structure of the implemented API.
*/
api: RestApiImplementation<T>;
/**
* The Injector instance to use for dependency injection in the API actions.
*/
injector: Injector;
/**
* The host name for the API Server. If not provided, the default host (ServerManager.DEFAULT_HOST) will be used.
*/
hostName?: string;
/**
* The root path for the API. This will be prepended to all API paths.
*/
root: string;
/**
* The port on which the API server will listen.
*/
port: number;
/**
* CORS options to configure Cross-Origin Resource Sharing for the API.
*/
cors?: CorsOptions;
/**
* An optional function to deserialize query parameters from the URL.
* This function should take a query string (e.g., "?key=value") and return an object with the parsed parameters.
* If not provided, the default deserialization will be used.
*/
deserializeQueryParams?: (param: string) => any;
/**
* Adds an additional 'GET /schema' endpoint that returns the schema definitions of the API.
* Also adds a 'GET /swagger.json' endpoint that returns the API schema in OpenAPI 3.0 (Swagger) format.
*/
enableGetSchema?: boolean;
/**
* Optional name for the API, used in the generated schema.
* This can be useful for documentation or identification purposes.
*/
name?: string;
/**
* Optional description for the API, used in the generated schema.
* This can provide additional context or information about the API's purpose.
*/
description?: string;
/**
* Optional version for the API, used in the generated schema.
* This can help in versioning the API and tracking changes over time.
*/
version?: string;
}
export type NewCompiledApiEntry = {
method: Method;
fullPath: string;
matcher: MatchFunction<Partial<Record<string, string | string[]>>>;
action: RequestAction<any>;
};
export type NewCompiledApi = {
[K in Method]?: NewCompiledApiEntry[];
};
export type OnRequestOptions = OnRequest & {
compiledApi: NewCompiledApi;
hostName?: string;
port: number;
rootApiPath: string;
injector: Injector;
cors?: CorsOptions;
supportedMethods: string[];
deserializeQueryParams?: (param: string) => Record<string, unknown>;
};
export declare class ApiManager implements Disposable {
private readonly apis;
[Symbol.dispose](): void;
private getSuportedMethods;
private compileApi;
addApi<T extends RestApi>({ api, hostName, port, root, cors, injector, deserializeQueryParams, enableGetSchema, name, description, version, }: ImplementApiOptions<T>): Promise<{
shouldExec: (msg: OnRequest) => boolean;
onRequest: (msg: OnRequest) => Promise<void>;
}>;
shouldExecRequest(options: {
method?: Method;
url?: string;
rootApiPath: string;
supportedMethods: Method[];
}): boolean;
private getActionFromEndpoint;
private executeAction;
private onMessage;
private readonly serverManager;
}
//# sourceMappingURL=api-manager.d.ts.map