From 1f103f9345ebd146a3a0d8e8549db432abe8b8c6 Mon Sep 17 00:00:00 2001 From: Superchupu <53496941+SuperchupuDev@users.noreply.github.com> Date: Thu, 22 Sep 2022 19:50:26 +0100 Subject: [PATCH 1/5] fix(types): use correct export statement --- index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 72b8514..ebdd885 100644 --- a/index.d.ts +++ b/index.d.ts @@ -75,4 +75,4 @@ export type FastifyCorsOptionsDelegate = FastifyCorsOptionsDelegateCallback | Fa export type FastifyPluginOptionsDelegate = (instance: FastifyInstance) => T; declare const fastifyCors: FastifyPluginCallback>; -export default fastifyCors; +export = fastifyCors; From 1685e8e3e4e1298ff0cc4f9792bcc52253de44eb Mon Sep 17 00:00:00 2001 From: Superchupu <53496941+SuperchupuDev@users.noreply.github.com> Date: Fri, 23 Sep 2022 13:49:06 +0000 Subject: [PATCH 2/5] properly fix types --- index.d.ts | 138 +++++++++++++++++++++++++++++------------------------ 1 file changed, 75 insertions(+), 63 deletions(-) diff --git a/index.d.ts b/index.d.ts index ebdd885..f4438bd 100644 --- a/index.d.ts +++ b/index.d.ts @@ -3,76 +3,88 @@ import { FastifyInstance, FastifyPluginCallback, FastifyRequest } from 'fastify'; type OriginCallback = (err: Error | null, allow: boolean) => void; -export type OriginFunction = (origin: string, callback: OriginCallback) => void; type OriginType = string | boolean | RegExp; type ValueOrArray = T | ArrayOfValueOrArray; interface ArrayOfValueOrArray extends Array> { } -export interface FastifyCorsOptions { - /** - * Configures the Access-Control-Allow-Origin CORS header. - */ - origin?: ValueOrArray | OriginFunction; - /** - * Configures the Access-Control-Allow-Credentials CORS header. - * Set to true to pass the header, otherwise it is omitted. - */ - credentials?: boolean; - /** - * Configures the Access-Control-Expose-Headers CORS header. - * Expects a comma-delimited string (ex: 'Content-Range,X-Content-Range') - * or an array (ex: ['Content-Range', 'X-Content-Range']). - * If not specified, no custom headers are exposed. - */ - exposedHeaders?: string | string[]; - /** - * Configures the Access-Control-Allow-Headers CORS header. - * Expects a comma-delimited string (ex: 'Content-Type,Authorization') - * or an array (ex: ['Content-Type', 'Authorization']). If not - * specified, defaults to reflecting the headers specified in the - * request's Access-Control-Request-Headers header. - */ - allowedHeaders?: string | string[]; - /** - * Configures the Access-Control-Allow-Methods CORS header. - * Expects a comma-delimited string (ex: 'GET,PUT,POST') or an array (ex: ['GET', 'PUT', 'POST']). - */ - methods?: string | string[]; - /** - * Configures the Access-Control-Max-Age CORS header. - * Set to an integer to pass the header, otherwise it is omitted. - */ - maxAge?: number; - /** - * Pass the CORS preflight response to the route handler (default: false). - */ - preflightContinue?: boolean; - /** - * Provides a status code to use for successful OPTIONS requests, - * since some legacy browsers (IE11, various SmartTVs) choke on 204. - */ - optionsSuccessStatus?: number; - /** - * Pass the CORS preflight response to the route handler (default: false). - */ - preflight?: boolean; - /** - * Enforces strict requirement of the CORS preflight request headers (Access-Control-Request-Method and Origin). - * Preflight requests without the required headers will result in 400 errors when set to `true` (default: `true`). - */ - strictPreflight?: boolean; - /** - * Hide options route from the documentation built using fastify-swagger (default: true). - */ - hideOptionsRoute?: boolean; +type FastifyCorsPlugin = FastifyPluginCallback< + NonNullable +>; + +declare namespace fastifyCors { + export type OriginFunction = (origin: string, callback: OriginCallback) => void; + + export interface FastifyCorsOptions { + /** + * Configures the Access-Control-Allow-Origin CORS header. + */ + origin?: ValueOrArray | fastifyCors.OriginFunction; + /** + * Configures the Access-Control-Allow-Credentials CORS header. + * Set to true to pass the header, otherwise it is omitted. + */ + credentials?: boolean; + /** + * Configures the Access-Control-Expose-Headers CORS header. + * Expects a comma-delimited string (ex: 'Content-Range,X-Content-Range') + * or an array (ex: ['Content-Range', 'X-Content-Range']). + * If not specified, no custom headers are exposed. + */ + exposedHeaders?: string | string[]; + /** + * Configures the Access-Control-Allow-Headers CORS header. + * Expects a comma-delimited string (ex: 'Content-Type,Authorization') + * or an array (ex: ['Content-Type', 'Authorization']). If not + * specified, defaults to reflecting the headers specified in the + * request's Access-Control-Request-Headers header. + */ + allowedHeaders?: string | string[]; + /** + * Configures the Access-Control-Allow-Methods CORS header. + * Expects a comma-delimited string (ex: 'GET,PUT,POST') or an array (ex: ['GET', 'PUT', 'POST']). + */ + methods?: string | string[]; + /** + * Configures the Access-Control-Max-Age CORS header. + * Set to an integer to pass the header, otherwise it is omitted. + */ + maxAge?: number; + /** + * Pass the CORS preflight response to the route handler (default: false). + */ + preflightContinue?: boolean; + /** + * Provides a status code to use for successful OPTIONS requests, + * since some legacy browsers (IE11, various SmartTVs) choke on 204. + */ + optionsSuccessStatus?: number; + /** + * Pass the CORS preflight response to the route handler (default: false). + */ + preflight?: boolean; + /** + * Enforces strict requirement of the CORS preflight request headers (Access-Control-Request-Method and Origin). + * Preflight requests without the required headers will result in 400 errors when set to `true` (default: `true`). + */ + strictPreflight?: boolean; + /** + * Hide options route from the documentation built using fastify-swagger (default: true). + */ + hideOptionsRoute?: boolean; + } + + export interface FastifyCorsOptionsDelegateCallback { (req: FastifyRequest, cb: (error: Error | null, corsOptions?: FastifyCorsOptions) => void): void } + export interface FastifyCorsOptionsDelegatePromise { (req: FastifyRequest): Promise } + export type FastifyCorsOptionsDelegate = FastifyCorsOptionsDelegateCallback | FastifyCorsOptionsDelegatePromise + export type FastifyPluginOptionsDelegate = (instance: FastifyInstance) => T; + + export { fastifyCors as default }; } -export interface FastifyCorsOptionsDelegateCallback { (req: FastifyRequest, cb: (error: Error | null, corsOptions?: FastifyCorsOptions) => void): void } -export interface FastifyCorsOptionsDelegatePromise { (req: FastifyRequest): Promise} -export type FastifyCorsOptionsDelegate = FastifyCorsOptionsDelegateCallback | FastifyCorsOptionsDelegatePromise -export type FastifyPluginOptionsDelegate = (instance: FastifyInstance) => T; +declare function fastifyCors( + ...params: Parameters +): ReturnType; -declare const fastifyCors: FastifyPluginCallback>; export = fastifyCors; From c43ce63c87552c93326fad0a230b09c8151e44f5 Mon Sep 17 00:00:00 2001 From: Uzlopak Date: Mon, 17 Oct 2022 14:30:38 +0200 Subject: [PATCH 3/5] fix --- index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index f4438bd..82a55fc 100644 --- a/index.d.ts +++ b/index.d.ts @@ -10,7 +10,7 @@ interface ArrayOfValueOrArray extends Array> { } type FastifyCorsPlugin = FastifyPluginCallback< - NonNullable + NonNullable | fastifyCors.FastifyCorsOptionsDelegate >; declare namespace fastifyCors { @@ -78,7 +78,7 @@ declare namespace fastifyCors { export interface FastifyCorsOptionsDelegateCallback { (req: FastifyRequest, cb: (error: Error | null, corsOptions?: FastifyCorsOptions) => void): void } export interface FastifyCorsOptionsDelegatePromise { (req: FastifyRequest): Promise } export type FastifyCorsOptionsDelegate = FastifyCorsOptionsDelegateCallback | FastifyCorsOptionsDelegatePromise - export type FastifyPluginOptionsDelegate = (instance: FastifyInstance) => T; + export type FastifyPluginOptionsDelegate = (instance: FastifyInstance) => T; export { fastifyCors as default }; } From 33956f27d56860b1b270511756945dabccf7ef54 Mon Sep 17 00:00:00 2001 From: Uzlopak Date: Fri, 21 Oct 2022 13:10:34 +0200 Subject: [PATCH 4/5] Update index.d.ts Co-authored-by: KaKa --- index.d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/index.d.ts b/index.d.ts index 82a55fc..dedbb77 100644 --- a/index.d.ts +++ b/index.d.ts @@ -80,6 +80,7 @@ declare namespace fastifyCors { export type FastifyCorsOptionsDelegate = FastifyCorsOptionsDelegateCallback | FastifyCorsOptionsDelegatePromise export type FastifyPluginOptionsDelegate = (instance: FastifyInstance) => T; +export const fastifyCors: FastifyCorsPlugin export { fastifyCors as default }; } From 4326b7b87d75beb131c8b0b14d51494b7d8845dc Mon Sep 17 00:00:00 2001 From: Uzlopak Date: Mon, 24 Oct 2022 11:13:06 +0200 Subject: [PATCH 5/5] Apply suggestions from code review --- index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index dedbb77..e0abaa0 100644 --- a/index.d.ts +++ b/index.d.ts @@ -80,7 +80,7 @@ declare namespace fastifyCors { export type FastifyCorsOptionsDelegate = FastifyCorsOptionsDelegateCallback | FastifyCorsOptionsDelegatePromise export type FastifyPluginOptionsDelegate = (instance: FastifyInstance) => T; -export const fastifyCors: FastifyCorsPlugin + export const fastifyCors: FastifyCorsPlugin export { fastifyCors as default }; }