express-service-bootstrap
Version:
This is a convenience package for starting a express API with security, health checks, process exits etc.
98 lines (97 loc) • 5.69 kB
TypeScript
import { BootstrapConstructor } from "./bootstrap-constructor";
import { ApplicationBuilderMiddleware } from "./application-builder";
import { HelmetOptions } from "helmet";
import { IRouter } from "express";
import { CompressionOptions } from "compression";
export type ApplicationRouter = {
hostingPath: string;
router: IRouter;
};
/**
* A convenience class that provides a way to create middleware instances without the need to manually create them.
*/
export declare class Convenience {
private readonly customConstructor;
/**
* Creates a new instance of the convenience class.
* @param DIConstructor The dependency injection constructor to use to create instances of middleware.
*/
constructor(customConstructor?: BootstrapConstructor);
/**
* Creates a new instance of the body parser middleware for url encoding.
* @param urlEncodingOptions The options to use for the url encoding middleware.
* @param urlEncodingOptions.extended Whether to use the extended query string parsing. Default is true.
* @param urlEncodingOptions.limit The maximum size of the url encoded payload to parse. Can be a string (e.g., "1mb") or a number (in bytes). Default is '100kb'.
* @param urlEncodingOptions.parameterLimit The maximum number of parameters to parse in the url encoded payload. Default is 1000.
* @returns {ApplicationBuilderMiddleware} A new instance of the body parser middleware for url encoding.
*/
bodyParserURLEncodingMiddleware(urlEncodingOptions?: {
extended?: boolean;
limit?: string | number;
parameterLimit?: number;
}): ApplicationBuilderMiddleware;
/**
* Creates a new instance of the body parser middleware for JSON encoding.
* @param jsonOptions The options to use for the JSON encoding middleware.
* @param jsonOptions.limit The maximum size of the JSON payload to parse. Can be a string (e.g., "1mb") or a number (in bytes). Default is '1mb'.
* @param jsonOptions.strict If true, only objects and arrays will be parsed. Default is true.
* @param jsonOptions.type The media type(s) to parse. Can be a string, an array of strings, or a function that returns a boolean. Default is 'application/json'.
* @returns {ApplicationBuilderMiddleware} A new instance of the body parser middleware for JSON encoding.
*/
bodyParserJSONEncodingMiddleware(jsonOptions?: {
limit?: string | number;
strict?: boolean;
type?: string | string[] | ((req: any) => boolean);
}): ApplicationBuilderMiddleware;
/**
* Creates a new instance of the body parser middleware for raw encoding.
* @param helmetOptions The options to use for the raw encoding middleware.
* @returns {ApplicationBuilderMiddleware} A new instance of the helmet middleware.
*/
helmetMiddleware(helmetOptions?: Readonly<HelmetOptions>): (req: import("http").IncomingMessage, res: import("http").ServerResponse, next: (err?: unknown) => void) => void;
/**
* Creates a new instance of the swagger API documentation middleware.
* @param swaggerDocument The swagger document to use for the API documentation, typically a json object.
* @param hostPath The host path to use for the swagger API documentation.
* @returns {ApplicationRouter} A new instance of the swagger API documentation middleware.
*/
swaggerAPIDocs(swaggerDocument: any, hostPath?: string): ApplicationRouter;
/**
* Injects a specified object into the request under a given property name.
* @param requestPropertyName - The name of the property to add to the request object.
* @param object - The object to inject into the request.
* @returns {ApplicationBuilderMiddleware} A middleware function that injects the object into the request.
*/
injectInRequestMiddleware(requestPropertyName: string, object: any): ApplicationBuilderMiddleware;
/**
* Creates a new instance of the compression middleware.
* @param compressionOptions The options to use for the compression middleware.
* @returns {ApplicationBuilderMiddleware} A new instance of the compression middleware.
*/
compressionMiddleware(compressionOptions?: CompressionOptions): ApplicationBuilderMiddleware;
/**
* Creates a new instance of the static file serving middleware.
* @param staticPath The path to the static files to serve.
* @returns {ApplicationBuilderMiddleware} A new instance of the static file serving middleware.
*/
staticMiddleware(staticPath: string): ApplicationBuilderMiddleware;
/**
* Encodes a string payload into a ReadableStream.
* @param payload The string payload to encode.
* @returns An object containing the ReadableStream and the size of the encoded payload.
*/
encodeBodyStream(payload: string): {
stream: ReadableStream;
size: number;
};
/**
* Sends an HTTP request with GZIP compression if specified.
* @param httpVerb "GET", "POST", "PUT", "DELETE", etc.
* @param url The URL to send the request to.
* @param headers The headers to include in the request.
* @param bodyStream The body stream to send with the request, Default undefined.
* @param shouldCompress Whether to compress the request body using GZIP. Default is true when body is present.
* @returns A promise that resolves to the response of the HTTP request.
*/
compressibleRequestGZIP(httpVerb: string, url: URL, headers: Record<string, string>, bodyStream?: ReadableStream | undefined, shouldCompress?: boolean, context?: typeof globalThis): Promise<globalThis.Response>;
}