From d569b92b7a317fc0cd4bd3628b02a8ec080f4b31 Mon Sep 17 00:00:00 2001 From: Daniel Hirth Date: Mon, 9 Mar 2020 18:42:27 +0100 Subject: [PATCH] new types for package "overload-protection" (#42886) * types for overload-protection * prettier formatting * added parameter type for logging function Co-authored-by: Daniel Hirth --- types/overload-protection/index.d.ts | 51 +++++++++++++++++++ .../overload-protection-tests.ts | 40 +++++++++++++++ types/overload-protection/tsconfig.json | 24 +++++++++ types/overload-protection/tslint.json | 1 + 4 files changed, 116 insertions(+) create mode 100644 types/overload-protection/index.d.ts create mode 100644 types/overload-protection/overload-protection-tests.ts create mode 100644 types/overload-protection/tsconfig.json create mode 100644 types/overload-protection/tslint.json diff --git a/types/overload-protection/index.d.ts b/types/overload-protection/index.d.ts new file mode 100644 index 0000000000..b7c7534a44 --- /dev/null +++ b/types/overload-protection/index.d.ts @@ -0,0 +1,51 @@ +// Type definitions for overload-protection 1.2 +// Project: https://github.com/davidmarkclements/overload-protection +// Definitions by: Daniel Hirth +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare namespace protect { + type KoaFrameworkSelection = 'koa'; + type HttpFrameworkSelection = 'express' | 'http' | 'restify'; + + interface ProtectionConfig { + production?: boolean; + clientRetrySecs?: number; + sampleInterval?: number; + maxEventLoopDelay?: number; + maxHeapUsedBytes?: number; + maxRssBytes?: number; + errorPropagationMode?: boolean; + logging?: boolean | string | ((msg: string) => void); + logStatsOnReq?: false; + } + + interface ProtectionInstance { + overload: boolean; + eventLoopOverload: boolean; + heapUsedOverload: boolean; + rssOverload: boolean; + eventLoopDelay: number; + maxEventLoopDelay: number; + maxHeapUsedBytes: number; + maxRssBytes: number; + } + + interface KoaProtectionInstance extends ProtectionInstance { + (ctx: object, next: () => any): any; + } + + interface HttpProtectionInstance extends ProtectionInstance { + (req: object, res: object, next: () => any): any; + } +} + +declare function protect( + framework: protect.KoaFrameworkSelection, + config?: protect.ProtectionConfig, +): protect.KoaProtectionInstance; +declare function protect( + framework: protect.HttpFrameworkSelection, + config?: protect.ProtectionConfig, +): protect.HttpProtectionInstance; + +export = protect; diff --git a/types/overload-protection/overload-protection-tests.ts b/types/overload-protection/overload-protection-tests.ts new file mode 100644 index 0000000000..f85a82599a --- /dev/null +++ b/types/overload-protection/overload-protection-tests.ts @@ -0,0 +1,40 @@ +import op = require('overload-protection'); + +const config1: op.ProtectionConfig = { + production: true, + clientRetrySecs: 2, + sampleInterval: 1, + maxEventLoopDelay: 40, + maxHeapUsedBytes: 25, + maxRssBytes: 321, + errorPropagationMode: true, + logging: console.log, + logStatsOnReq: false, +}; + +const config2: op.ProtectionConfig = { + logging: false, +}; + +const config3: op.ProtectionConfig = { + logging: 'warn', +}; + +const instance = op('koa'); + +console.log(instance); +console.log(instance({ foo: 'bar' }, () => 'Hello')); +console.log(instance.overload); +console.log(instance.eventLoopOverload); +console.log(instance.heapUsedOverload); +console.log(instance.rssOverload); +console.log(instance.eventLoopDelay); +console.log(instance.maxEventLoopDelay); +console.log(instance.maxHeapUsedBytes); +console.log(instance.maxRssBytes); + +const instance2 = op('express', config1); +console.log(instance2({ foo: 'bar' }, { hello: 'world' }, () => 'World')); + +op('http', config2); +op('restify', config3); diff --git a/types/overload-protection/tsconfig.json b/types/overload-protection/tsconfig.json new file mode 100644 index 0000000000..a5ea66c7f5 --- /dev/null +++ b/types/overload-protection/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "overload-protection-tests.ts" + ] +} diff --git a/types/overload-protection/tslint.json b/types/overload-protection/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/overload-protection/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }