new types for package "overload-protection" (#42886)

* types for overload-protection

* prettier formatting

* added parameter type for logging function

Co-authored-by: Daniel Hirth <daniel.hirth@wolterskluwer.com>
This commit is contained in:
Daniel Hirth
2020-03-09 18:42:27 +01:00
committed by GitHub
parent 22fe6436a5
commit d569b92b7a
4 changed files with 116 additions and 0 deletions

51
types/overload-protection/index.d.ts vendored Normal file
View File

@@ -0,0 +1,51 @@
// Type definitions for overload-protection 1.2
// Project: https://github.com/davidmarkclements/overload-protection
// Definitions by: Daniel Hirth <https://github.com/dornfeder>
// 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;

View File

@@ -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);

View File

@@ -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"
]
}

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }