@types/fastify-cors add typings

This commit is contained in:
Jannik Keye
2018-09-23 09:56:29 +02:00
parent 5c937ec314
commit a9595d27ef
4 changed files with 181 additions and 0 deletions
+89
View File
@@ -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
};
+68
View File
@@ -0,0 +1,68 @@
// Type definitions for fastify-cors 0.1
// Project: https://github.com/fastify/fastify-cors#readme
// Definitions by: Jannik Keye <https://github.com/jannikkeye>
// 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;
+23
View File
@@ -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"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }