backend-app-api: explicit request handler types for MiddlewareFactory
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -4,12 +4,10 @@
|
||||
|
||||
```ts
|
||||
/// <reference types="node" />
|
||||
/// <reference types="qs" />
|
||||
|
||||
import { BackendFeature } from '@backstage/backend-plugin-api';
|
||||
import { Config } from '@backstage/config';
|
||||
import { ConfigService } from '@backstage/backend-plugin-api';
|
||||
import cors from 'cors';
|
||||
import { CorsOptions } from 'cors';
|
||||
import { ErrorRequestHandler } from 'express';
|
||||
import { Express as Express_2 } from 'express';
|
||||
@@ -17,24 +15,18 @@ import { ExtensionPoint } from '@backstage/backend-plugin-api';
|
||||
import { HelmetOptions } from 'helmet';
|
||||
import * as http from 'http';
|
||||
import { HttpRouterService } from '@backstage/backend-plugin-api';
|
||||
import { IncomingMessage } from 'http';
|
||||
import { LifecycleService } from '@backstage/backend-plugin-api';
|
||||
import { LoggerService } from '@backstage/backend-plugin-api';
|
||||
import { ParamsDictionary } from 'express-serve-static-core';
|
||||
import { ParsedQs } from 'qs';
|
||||
import { PermissionsService } from '@backstage/backend-plugin-api';
|
||||
import { PluginCacheManager } from '@backstage/backend-common';
|
||||
import { PluginDatabaseManager } from '@backstage/backend-common';
|
||||
import { PluginEndpointDiscovery } from '@backstage/backend-common';
|
||||
import { Request as Request_2 } from 'express';
|
||||
import { RequestHandler } from 'express';
|
||||
import { RequestListener } from 'http';
|
||||
import { Response as Response_2 } from 'express';
|
||||
import { RootHttpRouterService } from '@backstage/backend-plugin-api';
|
||||
import { RootLifecycleService } from '@backstage/backend-plugin-api';
|
||||
import { RootLoggerService } from '@backstage/backend-plugin-api';
|
||||
import { SchedulerService } from '@backstage/backend-plugin-api';
|
||||
import { ServerResponse } from 'http';
|
||||
import { ServiceFactory } from '@backstage/backend-plugin-api';
|
||||
import { ServiceFactoryOrFunction } from '@backstage/backend-plugin-api';
|
||||
import { ServiceRef } from '@backstage/backend-plugin-api';
|
||||
@@ -146,35 +138,13 @@ export const loggerFactory: (
|
||||
|
||||
// @public
|
||||
export class MiddlewareFactory {
|
||||
compression(): RequestHandler<
|
||||
ParamsDictionary,
|
||||
any,
|
||||
any,
|
||||
ParsedQs,
|
||||
Record<string, any>
|
||||
>;
|
||||
cors(): (
|
||||
req: cors.CorsRequest,
|
||||
res: {
|
||||
statusCode?: number | undefined;
|
||||
setHeader(key: string, value: string): any;
|
||||
end(): any;
|
||||
},
|
||||
next: (err?: any) => any,
|
||||
) => void;
|
||||
compression(): RequestHandler;
|
||||
cors(): RequestHandler;
|
||||
static create(options: MiddlewareFactoryOptions): MiddlewareFactory;
|
||||
error(options?: MiddlewareFactoryErrorOptions): ErrorRequestHandler;
|
||||
helmet(): (
|
||||
req: IncomingMessage,
|
||||
res: ServerResponse<IncomingMessage>,
|
||||
next: (err?: unknown) => void,
|
||||
) => void;
|
||||
logging(): (
|
||||
req: IncomingMessage,
|
||||
res: ServerResponse<IncomingMessage>,
|
||||
callback: (err?: Error | undefined) => void,
|
||||
) => void;
|
||||
notFound(): (_req: Request_2, res: Response_2) => void;
|
||||
helmet(): RequestHandler;
|
||||
logging(): RequestHandler;
|
||||
notFound(): RequestHandler;
|
||||
}
|
||||
|
||||
// @public
|
||||
|
||||
@@ -15,7 +15,13 @@
|
||||
*/
|
||||
|
||||
import { ConfigService, LoggerService } from '@backstage/backend-plugin-api';
|
||||
import { Request, Response, ErrorRequestHandler, NextFunction } from 'express';
|
||||
import {
|
||||
Request,
|
||||
Response,
|
||||
ErrorRequestHandler,
|
||||
NextFunction,
|
||||
RequestHandler,
|
||||
} from 'express';
|
||||
import cors from 'cors';
|
||||
import helmet from 'helmet';
|
||||
import morgan from 'morgan';
|
||||
@@ -95,7 +101,7 @@ export class MiddlewareFactory {
|
||||
*
|
||||
* @returns An Express request handler
|
||||
*/
|
||||
notFound() {
|
||||
notFound(): RequestHandler {
|
||||
return (_req: Request, res: Response) => {
|
||||
res.status(404).end();
|
||||
};
|
||||
@@ -109,7 +115,7 @@ export class MiddlewareFactory {
|
||||
* The middleware will attempt to compress response bodies for all requests
|
||||
* that traverse through the middleware.
|
||||
*/
|
||||
compression() {
|
||||
compression(): RequestHandler {
|
||||
return compression();
|
||||
}
|
||||
|
||||
@@ -124,7 +130,7 @@ export class MiddlewareFactory {
|
||||
*
|
||||
* @returns An Express request handler
|
||||
*/
|
||||
logging() {
|
||||
logging(): RequestHandler {
|
||||
const logger = this.#logger.child({
|
||||
type: 'incomingRequest',
|
||||
});
|
||||
@@ -150,7 +156,7 @@ export class MiddlewareFactory {
|
||||
*
|
||||
* @returns An Express request handler
|
||||
*/
|
||||
helmet() {
|
||||
helmet(): RequestHandler {
|
||||
return helmet(readHelmetOptions(this.#config.getOptionalConfig('backend')));
|
||||
}
|
||||
|
||||
@@ -166,7 +172,7 @@ export class MiddlewareFactory {
|
||||
*
|
||||
* @returns An Express request handler
|
||||
*/
|
||||
cors() {
|
||||
cors(): RequestHandler {
|
||||
return cors(readCorsOptions(this.#config.getOptionalConfig('backend')));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user