Skip to content

Commit

Permalink
fix: typings for fastify enableCors method
Browse files Browse the repository at this point in the history
Fastify adapter uses typings from `@fastify/cors` package for `enableCors` method

Fix nestjs#13234
  • Loading branch information
Marek Dorda committed Feb 22, 2024
1 parent 3e61ec1 commit 8af7183
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 51 deletions.
10 changes: 6 additions & 4 deletions integration/cors/e2e/fastify.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
8 changes: 2 additions & 6 deletions packages/common/interfaces/http/http-server.interface.ts
Original file line number Diff line number Diff line change
@@ -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<TRequest = any, TResponse = any> = (
error: any,
Expand Down Expand Up @@ -77,7 +73,7 @@ export interface HttpServer<
getRequestUrl?(request: TRequest): string;
getInstance(): ServerInstance;
registerParserMiddleware(...args: any[]): any;
enableCors(options: CorsOptions | CorsOptionsDelegate<TRequest>): any;
enableCors(options: any): any;
getHttpServer(): any;
initHttpServer(options: NestApplicationOptions): void;
close(): any;
Expand Down
11 changes: 0 additions & 11 deletions packages/common/interfaces/nest-application.interface.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -31,13 +27,6 @@ export interface INestApplication<TServer = any>
*/
use(...args: any[]): this;

/**
* Enables CORS (Cross-Origin Resource Sharing)
*
* @returns {void}
*/
enableCors(options?: CorsOptions | CorsOptionsDelegate<any>): void;

/**
* Enables Versioning for the application.
* By default, URI-based versioning is used.
Expand Down
9 changes: 1 addition & 8 deletions packages/core/adapters/http-adapter.ts
Original file line number Diff line number Diff line change
@@ -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';

/**
Expand Down Expand Up @@ -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<TRequest>,
prefix?: string,
);
abstract enableCors(options?: any, prefix?: string);
abstract createMiddlewareFactory(
requestMethod: RequestMethod,
):
Expand Down
10 changes: 2 additions & 8 deletions packages/core/nest-application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -129,9 +125,7 @@ export class NestApplication
if (!passCustomOptions) {
return this.enableCors();
}
return this.enableCors(
this.appOptions.cors as CorsOptions | CorsOptionsDelegate<any>,
);
return this.enableCors(this.appOptions.cors);
}

public createServer<T = any>(): T {
Expand Down Expand Up @@ -279,7 +273,7 @@ export class NestApplication
return this;
}

public enableCors(options?: CorsOptions | CorsOptionsDelegate<any>): void {
public enableCors(options?: any): void {
this.httpAdapter.enableCors(options);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -86,6 +90,8 @@ export interface NestExpressApplication<
*/
useStaticAssets(path: string, options?: ServeStaticOptions): this;

enableCors(options?: CorsOptions | CorsOptionsDelegate<any>): void;

/**
* Register Express body parsers on the fly. Will respect
* the application's `rawBody` option.
Expand Down
15 changes: 6 additions & 9 deletions packages/platform-fastify/adapters/fastify-adapter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { FastifyCorsOptions } from '@fastify/cors';
import {
HttpStatus,
Logger,
Expand All @@ -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';
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -492,7 +489,7 @@ export class FastifyAdapter<
return this.getRequestOriginalUrl(request.raw || request);
}

public enableCors(options: CorsOptions | CorsOptionsDelegate<TRequest>) {
public enableCors(options?: FastifyCorsOptions) {
this.register(
import('@fastify/cors') as Parameters<TInstance['register']>[0],
options,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { INestApplication, HttpServer } from '@nestjs/common';
import { FastifyCorsOptions } from '@fastify/cors';
import { HttpServer, INestApplication } from '@nestjs/common';
import {
FastifyBodyParser,
FastifyInstance,
FastifyPluginAsync,
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';
Expand Down Expand Up @@ -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`.
*
Expand Down

0 comments on commit 8af7183

Please sign in to comment.