diff --git a/types/fastify-cors/fastify-cors-tests.ts b/types/fastify-cors/fastify-cors-tests.ts new file mode 100644 index 0000000000..7425d61c5d --- /dev/null +++ b/types/fastify-cors/fastify-cors-tests.ts @@ -0,0 +1,89 @@ +import fastifyCors = require("fastify-cors"); + +fastifyCors(); + +const fastifyCorsOptions: fastifyCors.FastifyCorsOptions = { + origin: true, + allowedHeaders: "authorization,content-type", + methods: "GET,POST,PUT,PATCH,DELETE,OPTIONS", + credentials: true, + exposedHeaders: "authorization", + maxAge: 13000, + preflightContinue: false, + optionsSuccessStatus: 200, + preflight: false +}; + +const fastifyCorsOptionsArray: fastifyCors.FastifyCorsOptions = { + origin: true, + allowedHeaders: ["authorization", "content-type"], + methods: ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"], + credentials: true, + exposedHeaders: ["authorization"] + maxAge: 13000, + preflightContinue: false, + optionsSuccessStatus: 200, + preflight: false +}; + +const originString: fastifyCors.FastifyCorsOptions = { + origin: "*", + allowedHeaders: ["authorization", "content-type"], + methods: ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"], + credentials: true, + exposedHeaders: ["authorization"] + maxAge: 13000, + preflightContinue: false, + optionsSuccessStatus: 200, + preflight: false +}; + +const originRegexp: fastifyCors.FastifyCorsOptions = { + origin: /\*/, + allowedHeaders: ["authorization", "content-type"], + methods: ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"], + credentials: true, + exposedHeaders: ["authorization"] + maxAge: 13000, + preflightContinue: false, + optionsSuccessStatus: 200, + preflight: false +}; + +const originStringArray: fastifyCors.FastifyCorsOptions = { + origin: ["*", "something"], + allowedHeaders: ["authorization", "content-type"], + methods: ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"], + credentials: true, + exposedHeaders: ["authorization"] + maxAge: 13000, + preflightContinue: false, + optionsSuccessStatus: 200, + preflight: false +}; + +const originRegexpArray: fastifyCors.FastifyCorsOptions = { + origin: [/\*/, /something/], + allowedHeaders: ["authorization", "content-type"], + methods: ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"], + credentials: true, + exposedHeaders: ["authorization"] + maxAge: 13000, + preflightContinue: false, + optionsSuccessStatus: 200, + preflight: false +}; + +const originCallback: fastifyCors.FastifyCorsOptions = { + origin: (err: Error, allow: boolean) => { + throw err; + }, + allowedHeaders: ["authorization", "content-type"], + methods: ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"], + credentials: true, + exposedHeaders: ["authorization"] + maxAge: 13000, + preflightContinue: false, + optionsSuccessStatus: 200, + preflight: false +}; diff --git a/types/fastify-cors/index.d.ts b/types/fastify-cors/index.d.ts new file mode 100644 index 0000000000..907689a72c --- /dev/null +++ b/types/fastify-cors/index.d.ts @@ -0,0 +1,68 @@ +// Type definitions for fastify-cors 0.1 +// Project: https://github.com/fastify/fastify-cors#readme +// Definitions by: Jannik Keye +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +type originCallback = (err: Error, allow: boolean) => void; + +/** + * fastify-cors enables the use of CORS in a Fastify application. + */ +declare function fastifyCors(): void; + +declare namespace fastifyCors { + /** + * Options for configuring the fastify-cors plugin. + */ + interface FastifyCorsOptions { + /** + * Configures the Access-Control-Allow-Origin CORS header. + */ + origin?: string | boolean | RegExp | string[] | RegExp[] | originCallback; + /** + * 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; + } +} + +export = fastifyCors; diff --git a/types/fastify-cors/tsconfig.json b/types/fastify-cors/tsconfig.json new file mode 100644 index 0000000000..9e696b42e2 --- /dev/null +++ b/types/fastify-cors/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "fastify-cors-tests.ts" + ] +} diff --git a/types/fastify-cors/tslint.json b/types/fastify-cors/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/fastify-cors/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }