From 8af7183c30d0e90bd9ab4bcc1f9b50e052d622d5 Mon Sep 17 00:00:00 2001 From: Marek Dorda Date: Thu, 22 Feb 2024 07:34:33 +0100 Subject: [PATCH] fix: typings for fastify `enableCors` method Fastify adapter uses typings from `@fastify/cors` package for `enableCors` method Fix nestjs/nest#13234 --- integration/cors/e2e/fastify.spec.ts | 10 ++++++---- .../interfaces/http/http-server.interface.ts | 8 ++------ .../interfaces/nest-application.interface.ts | 11 ----------- packages/core/adapters/http-adapter.ts | 9 +-------- packages/core/nest-application.ts | 10 ++-------- .../nest-express-application.interface.ts | 10 ++++++++-- .../platform-fastify/adapters/fastify-adapter.ts | 15 ++++++--------- .../nest-fastify-application.interface.ts | 14 +++++++++++--- 8 files changed, 36 insertions(+), 51 deletions(-) diff --git a/integration/cors/e2e/fastify.spec.ts b/integration/cors/e2e/fastify.spec.ts index df8caba4334..c24bfc6ff39 100644 --- a/integration/cors/e2e/fastify.spec.ts +++ b/integration/cors/e2e/fastify.spec.ts @@ -38,10 +38,12 @@ describe.skip('Fastify Cors', () => { ); let requestId = 0; - const configDelegation = function (req, cb) { - const config = configs[requestId]; - requestId++; - cb(null, config); + const configDelegation = { + delegator: function (req, cb) { + const config = configs[requestId]; + requestId++; + cb(null, config); + }, }; app.enableCors(configDelegation); diff --git a/packages/common/interfaces/http/http-server.interface.ts b/packages/common/interfaces/http/http-server.interface.ts index 28e8278b0fc..fcc0aaec9d6 100644 --- a/packages/common/interfaces/http/http-server.interface.ts +++ b/packages/common/interfaces/http/http-server.interface.ts @@ -1,10 +1,6 @@ import { RequestMethod } from '../../enums'; -import { - CorsOptions, - CorsOptionsDelegate, -} from '../../interfaces/external/cors-options.interface'; import { NestApplicationOptions } from '../../interfaces/nest-application-options.interface'; -import { VersioningOptions, VersionValue } from '../version-options.interface'; +import { VersionValue, VersioningOptions } from '../version-options.interface'; export type ErrorHandler = ( error: any, @@ -77,7 +73,7 @@ export interface HttpServer< getRequestUrl?(request: TRequest): string; getInstance(): ServerInstance; registerParserMiddleware(...args: any[]): any; - enableCors(options: CorsOptions | CorsOptionsDelegate): any; + enableCors(options: any): any; getHttpServer(): any; initHttpServer(options: NestApplicationOptions): void; close(): any; diff --git a/packages/common/interfaces/nest-application.interface.ts b/packages/common/interfaces/nest-application.interface.ts index eb2167e6416..532ab656a07 100644 --- a/packages/common/interfaces/nest-application.interface.ts +++ b/packages/common/interfaces/nest-application.interface.ts @@ -1,7 +1,3 @@ -import { - CorsOptions, - CorsOptionsDelegate, -} from './external/cors-options.interface'; import { CanActivate } from './features/can-activate.interface'; import { NestInterceptor } from './features/nest-interceptor.interface'; import { GlobalPrefixOptions } from './global-prefix-options.interface'; @@ -31,13 +27,6 @@ export interface INestApplication */ use(...args: any[]): this; - /** - * Enables CORS (Cross-Origin Resource Sharing) - * - * @returns {void} - */ - enableCors(options?: CorsOptions | CorsOptionsDelegate): void; - /** * Enables Versioning for the application. * By default, URI-based versioning is used. diff --git a/packages/core/adapters/http-adapter.ts b/packages/core/adapters/http-adapter.ts index 6e079fe55dd..f6dc3ea6d65 100644 --- a/packages/core/adapters/http-adapter.ts +++ b/packages/core/adapters/http-adapter.ts @@ -1,9 +1,5 @@ import { HttpServer, RequestMethod, VersioningOptions } from '@nestjs/common'; import { RequestHandler, VersionValue } from '@nestjs/common/interfaces'; -import { - CorsOptions, - CorsOptionsDelegate, -} from '@nestjs/common/interfaces/external/cors-options.interface'; import { NestApplicationOptions } from '@nestjs/common/interfaces/nest-application-options.interface'; /** @@ -123,10 +119,7 @@ export abstract class AbstractHttpAdapter< // TODO remove optional signature (v11) abstract appendHeader?(response: any, name: string, value: string); abstract registerParserMiddleware(prefix?: string, rawBody?: boolean); - abstract enableCors( - options: CorsOptions | CorsOptionsDelegate, - prefix?: string, - ); + abstract enableCors(options?: any, prefix?: string); abstract createMiddlewareFactory( requestMethod: RequestMethod, ): diff --git a/packages/core/nest-application.ts b/packages/core/nest-application.ts index ef123cde060..5fc2ea3b8b4 100644 --- a/packages/core/nest-application.ts +++ b/packages/core/nest-application.ts @@ -15,10 +15,6 @@ import { GlobalPrefixOptions, NestApplicationOptions, } from '@nestjs/common/interfaces'; -import { - CorsOptions, - CorsOptionsDelegate, -} from '@nestjs/common/interfaces/external/cors-options.interface'; import { Logger } from '@nestjs/common/services/logger.service'; import { loadPackage } from '@nestjs/common/utils/load-package.util'; import { @@ -129,9 +125,7 @@ export class NestApplication if (!passCustomOptions) { return this.enableCors(); } - return this.enableCors( - this.appOptions.cors as CorsOptions | CorsOptionsDelegate, - ); + return this.enableCors(this.appOptions.cors); } public createServer(): T { @@ -279,7 +273,7 @@ export class NestApplication return this; } - public enableCors(options?: CorsOptions | CorsOptionsDelegate): void { + public enableCors(options?: any): void { this.httpAdapter.enableCors(options); } diff --git a/packages/platform-express/interfaces/nest-express-application.interface.ts b/packages/platform-express/interfaces/nest-express-application.interface.ts index 3020c42ee47..ade7aecf00c 100644 --- a/packages/platform-express/interfaces/nest-express-application.interface.ts +++ b/packages/platform-express/interfaces/nest-express-application.interface.ts @@ -1,7 +1,11 @@ -import { INestApplication, HttpServer } from '@nestjs/common'; +import { HttpServer, INestApplication } from '@nestjs/common'; +import type { + CorsOptions, + CorsOptionsDelegate, +} from '@nestjs/common/interfaces/external/cors-options.interface'; +import type { Express } from 'express'; import type { Server as CoreHttpServer } from 'http'; import type { Server as CoreHttpsServer } from 'https'; -import type { Express } from 'express'; import { NestExpressBodyParserOptions } from './nest-express-body-parser-options.interface'; import { NestExpressBodyParserType } from './nest-express-body-parser.interface'; import { ServeStaticOptions } from './serve-static-options.interface'; @@ -86,6 +90,8 @@ export interface NestExpressApplication< */ useStaticAssets(path: string, options?: ServeStaticOptions): this; + enableCors(options?: CorsOptions | CorsOptionsDelegate): void; + /** * Register Express body parsers on the fly. Will respect * the application's `rawBody` option. diff --git a/packages/platform-fastify/adapters/fastify-adapter.ts b/packages/platform-fastify/adapters/fastify-adapter.ts index 54e2df387e6..1cb516838c7 100644 --- a/packages/platform-fastify/adapters/fastify-adapter.ts +++ b/packages/platform-fastify/adapters/fastify-adapter.ts @@ -1,3 +1,4 @@ +import { FastifyCorsOptions } from '@fastify/cors'; import { HttpStatus, Logger, @@ -9,10 +10,6 @@ import { VersioningType, } from '@nestjs/common'; import { VersionValue } from '@nestjs/common/interfaces'; -import { - CorsOptions, - CorsOptionsDelegate, -} from '@nestjs/common/interfaces/external/cors-options.interface'; import { loadPackage } from '@nestjs/common/utils/load-package.util'; import { isString, isUndefined } from '@nestjs/common/utils/shared.utils'; import { AbstractHttpAdapter } from '@nestjs/core/adapters/http-adapter'; @@ -47,15 +44,15 @@ import { import * as pathToRegexp from 'path-to-regexp'; // `querystring` is used internally in fastify for registering urlencoded body parser. import { parse as querystringParse } from 'querystring'; +import { + FASTIFY_ROUTE_CONFIG_METADATA, + FASTIFY_ROUTE_CONSTRAINTS_METADATA, +} from '../constants'; import { NestFastifyBodyParserOptions } from '../interfaces'; import { FastifyStaticOptions, FastifyViewOptions, } from '../interfaces/external'; -import { - FASTIFY_ROUTE_CONFIG_METADATA, - FASTIFY_ROUTE_CONSTRAINTS_METADATA, -} from '../constants'; type FastifyHttp2SecureOptions< Server extends http2.Http2SecureServer, @@ -492,7 +489,7 @@ export class FastifyAdapter< return this.getRequestOriginalUrl(request.raw || request); } - public enableCors(options: CorsOptions | CorsOptionsDelegate) { + public enableCors(options?: FastifyCorsOptions) { this.register( import('@fastify/cors') as Parameters[0], options, diff --git a/packages/platform-fastify/interfaces/nest-fastify-application.interface.ts b/packages/platform-fastify/interfaces/nest-fastify-application.interface.ts index c52d804a640..9847ab65015 100644 --- a/packages/platform-fastify/interfaces/nest-fastify-application.interface.ts +++ b/packages/platform-fastify/interfaces/nest-fastify-application.interface.ts @@ -1,4 +1,5 @@ -import { INestApplication, HttpServer } from '@nestjs/common'; +import { FastifyCorsOptions } from '@fastify/cors'; +import { HttpServer, INestApplication } from '@nestjs/common'; import { FastifyBodyParser, FastifyInstance, @@ -6,14 +7,14 @@ import { FastifyPluginCallback, FastifyPluginOptions, FastifyRegisterOptions, - FastifyRequest, FastifyReply, + FastifyRequest, RawServerBase, RawServerDefault, } from 'fastify'; import { - Chain as LightMyRequestChain, InjectOptions, + Chain as LightMyRequestChain, Response as LightMyRequestResponse, } from 'light-my-request'; import { FastifyStaticOptions, FastifyViewOptions } from './external'; @@ -74,6 +75,13 @@ export interface NestFastifyApplication< */ useStaticAssets(options: FastifyStaticOptions): this; + /** + * Enables CORS (Cross-Origin Resource Sharing) + * + * @returns {void} + */ + enableCors(options?: FastifyCorsOptions): void; + /** * Sets a view engine for templates (views), for example: `pug`, `handlebars`, or `ejs`. *