From 03648458341bfc40d2d1f17e76128fd4518c192e Mon Sep 17 00:00:00 2001 From: Taylor Lodge Date: Thu, 1 Nov 2018 08:54:50 +1300 Subject: [PATCH 1/3] Definitions & Tests for Raygun4Node --- types/raygun/index.d.ts | 116 +++++++++++++++++++++++++++++++++++ types/raygun/raygun-tests.ts | 59 ++++++++++++++++++ types/raygun/tsconfig.json | 23 +++++++ types/raygun/tslint.json | 1 + 4 files changed, 199 insertions(+) create mode 100644 types/raygun/index.d.ts create mode 100644 types/raygun/raygun-tests.ts create mode 100644 types/raygun/tsconfig.json create mode 100644 types/raygun/tslint.json diff --git a/types/raygun/index.d.ts b/types/raygun/index.d.ts new file mode 100644 index 0000000000..4be58a2b76 --- /dev/null +++ b/types/raygun/index.d.ts @@ -0,0 +1,116 @@ +// Type definitions for raygun 0.10 +// Project: https://github.com/MindscapeHQ/raygun4node +// Definitions by: Taylor Lodge +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 + +declare namespace raygun { + interface KeyValueObject { + [key: string]: string | number | boolean | KeyValueObject; + } + interface StackFrame { + lineNumber: number; + className: string; + fileName: string; + methodName: string; + columnNumber?: number; + } + interface RaygunErrorObject { + message: string; + className: string; + stackTrace: StackFrame[]; + innerError?: RaygunErrorObject; + } + interface RaygunRequest { + hostname?: string; + host?: string; + path?: string; + method?: string; + ip?: string; + queryString?: KeyValueObject; + headers?: KeyValueObject; + form?: KeyValueObject; + } + interface RaygunUser { + identifier: string; + email?: string; + fullName?: string; + firstName?: string; + uuid?: string; + } + interface RaygunPayload { + occurredOn: Date; + details: { + client: { + name: 'raygun-node'; + version: string; + }; + groupingKey?: string; + error: RaygunErrorObject; + environment: { + osVersion: string; + architecture: string; + totalPhysicalMemory: number; + availablePhysicalMemory: number; + utcOffset: number; + processorCount?: number; + cpu: { + model: string; + speed: number; + times: { + user: number; + nice: number; + sys: number; + idle: number; + irq: number; + }; + }; + }; + machineName: string; + userCustomData?: KeyValueObject; + tags: string[]; + request?: RaygunRequest; + user?: RaygunUser | { identifier: string } + version?: string; + }; + } + + interface RaygunOfflineStorageProvider { + init(options: any): RaygunOfflineStorageProvider; + save(item: TTransportItem, callback: (error?: Error) => void): void; + retrieve(callback: (error: Error, storageItems: ReadonlyArray) => void): void; + send(callback: (error: Error, sendItems: ReadonlyArray) => void): void; + } + + type OnBeforeSend = (payload: RaygunPayload, exception: Error, customData: KeyValueObject, request: RaygunRequest, tags: ReadonlyArray) => boolean | RaygunPayload; + interface RaygunOptions { + apiKey: string; + filters?: ReadonlyArray; + port?: number; + host?: string; + useSSL?: boolean; + onBeforeSend?: OnBeforeSend; + offlineStorage?: RaygunOfflineStorageProvider; + offlineStorageOptions?: any; + isOffline?: boolean; + groupingKey?: string; + tags?: ReadonlyArray; + userHumanStringForObject?: boolean; + reportColumnNumbers?: boolean; + innerErrorFieldName?: string; + } + + class Client { + init(options: RaygunOptions): Client; + setUser(user: RaygunUser): Client; + setVersion(version: string): Client; + onBeforeSend(callback: OnBeforeSend): Client; + groupingKey(groupingKey: string): Client; + offline(): Client; + online(): Client; + send(exception: Error | string, customData?: KeyValueObject, offlineStorageCallback?: (error?: Error) => void, request?: RaygunRequest, tags?: ReadonlyArray): RaygunPayload; + expressHandler(error: Error, request: RaygunRequest, res: any, next: any): void; + } +} + +export default raygun; diff --git a/types/raygun/raygun-tests.ts b/types/raygun/raygun-tests.ts new file mode 100644 index 0000000000..f2a92432c2 --- /dev/null +++ b/types/raygun/raygun-tests.ts @@ -0,0 +1,59 @@ +import raygun from 'raygun'; + +const client = new raygun.Client(); // $ExpectType Client +client.init({apiKey: '1'}); // $ExpectType Client + +client.init(); // $ExpectError +client.init({}); // $ExpectError +client.init({apiKey: 123}); // $ExpectError +// +// $ExpectType Client +client.setUser({ + identifier: '123' +}); +// $ExpectType Client +client.setUser({ + identifier: '123', + email: '123', + fullName: '123', + firstName: '123', + uuid: '123' +}); + +client.setUser(); // $ExpectError +client.setUser({}); // $ExpectError +client.setUser({identifier: 1}); // $ExpectError + +client.setVersion('123'); // $ExpectType Client + +client.setVersion(); // $ExpectError +client.setVersion({}); // $ExpectError + +client.onBeforeSend(payload => payload); // $ExpectType Client +// $ExpectType Client +client.onBeforeSend(payload => { + payload.details; + + return payload; +}); + +client.onBeforeSend(); // $ExpectError + +client.groupingKey('123'); // $ExpectType Client + +client.groupingKey(); // $ExpectError +client.groupingKey({}); // $ExpectError + +client.offline(); // $ExpectType Client + +client.online(); // $ExpectType Client + +client.send(new Error()); // $ExpectType RaygunPayload +client.send({foo: 'bar'}); // $ExpectType RaygunPayload +client.send('error message'); // $ExpectType RaygunPayload +client.send(new Error(), {foo: 'bar'}); // $ExpectType RaygunPayload +client.send(new Error(), {}, undefined, undefined, ['1', '2']); // $ExpectType RaygunPayload + +client.send(); // $ExpectError +client.send(null); // $ExpectError +client.send(undefined); // $ExpectError diff --git a/types/raygun/tsconfig.json b/types/raygun/tsconfig.json new file mode 100644 index 0000000000..f85de6fb88 --- /dev/null +++ b/types/raygun/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", + "raygun-tests.ts" + ] +} diff --git a/types/raygun/tslint.json b/types/raygun/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/raygun/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } From 7b3a099bf67018e27e896cabd573cf086769b325 Mon Sep 17 00:00:00 2001 From: Taylor Lodge Date: Thu, 1 Nov 2018 09:07:04 +1300 Subject: [PATCH 2/3] Fix issue with linting --- types/raygun/index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/types/raygun/index.d.ts b/types/raygun/index.d.ts index 4be58a2b76..9c4babede5 100644 --- a/types/raygun/index.d.ts +++ b/types/raygun/index.d.ts @@ -2,7 +2,7 @@ // Project: https://github.com/MindscapeHQ/raygun4node // Definitions by: Taylor Lodge // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.3 +// TypeScript Version: 2.4 declare namespace raygun { interface KeyValueObject { @@ -108,7 +108,7 @@ declare namespace raygun { groupingKey(groupingKey: string): Client; offline(): Client; online(): Client; - send(exception: Error | string, customData?: KeyValueObject, offlineStorageCallback?: (error?: Error) => void, request?: RaygunRequest, tags?: ReadonlyArray): RaygunPayload; + send(exception: Error | string | object, customData?: KeyValueObject, offlineStorageCallback?: (error?: Error) => void, request?: RaygunRequest, tags?: ReadonlyArray): RaygunPayload; expressHandler(error: Error, request: RaygunRequest, res: any, next: any): void; } } From 039e3a534d9efc351cb51aac531b3d9d305b0812 Mon Sep 17 00:00:00 2001 From: Taylor Lodge Date: Fri, 2 Nov 2018 09:22:08 +1300 Subject: [PATCH 3/3] Fix raygun typings based on feedback & run prettier --- types/raygun/index.d.ts | 70 +++++++++++++++++++++++++----------- types/raygun/raygun-tests.ts | 4 +-- 2 files changed, 52 insertions(+), 22 deletions(-) diff --git a/types/raygun/index.d.ts b/types/raygun/index.d.ts index 9c4babede5..9315161a33 100644 --- a/types/raygun/index.d.ts +++ b/types/raygun/index.d.ts @@ -5,7 +5,7 @@ // TypeScript Version: 2.4 declare namespace raygun { - interface KeyValueObject { + interface KeyValueObject { [key: string]: string | number | boolean | KeyValueObject; } interface StackFrame { @@ -42,7 +42,7 @@ declare namespace raygun { occurredOn: Date; details: { client: { - name: 'raygun-node'; + name: "raygun-node"; version: string; }; groupingKey?: string; @@ -70,19 +70,38 @@ declare namespace raygun { userCustomData?: KeyValueObject; tags: string[]; request?: RaygunRequest; - user?: RaygunUser | { identifier: string } + user?: RaygunUser | { identifier: string }; version?: string; }; } - interface RaygunOfflineStorageProvider { + interface RaygunOfflineStorageProvider< + TTransportItem = RaygunPayload, + TStorageItem = string + > { init(options: any): RaygunOfflineStorageProvider; save(item: TTransportItem, callback: (error?: Error) => void): void; - retrieve(callback: (error: Error, storageItems: ReadonlyArray) => void): void; - send(callback: (error: Error, sendItems: ReadonlyArray) => void): void; + retrieve( + callback: ( + error: Error, + storageItems: ReadonlyArray + ) => void + ): void; + send( + callback: ( + error: Error, + sendItems: ReadonlyArray + ) => void + ): void; } - type OnBeforeSend = (payload: RaygunPayload, exception: Error, customData: KeyValueObject, request: RaygunRequest, tags: ReadonlyArray) => boolean | RaygunPayload; + type OnBeforeSend = ( + payload: RaygunPayload, + exception: Error, + customData: KeyValueObject, + request: RaygunRequest, + tags: ReadonlyArray + ) => boolean | RaygunPayload; interface RaygunOptions { apiKey: string; filters?: ReadonlyArray; @@ -99,18 +118,29 @@ declare namespace raygun { reportColumnNumbers?: boolean; innerErrorFieldName?: string; } - - class Client { - init(options: RaygunOptions): Client; - setUser(user: RaygunUser): Client; - setVersion(version: string): Client; - onBeforeSend(callback: OnBeforeSend): Client; - groupingKey(groupingKey: string): Client; - offline(): Client; - online(): Client; - send(exception: Error | string | object, customData?: KeyValueObject, offlineStorageCallback?: (error?: Error) => void, request?: RaygunRequest, tags?: ReadonlyArray): RaygunPayload; - expressHandler(error: Error, request: RaygunRequest, res: any, next: any): void; - } } -export default raygun; +declare class Client { + init(options: raygun.RaygunOptions): Client; + setUser(user: raygun.RaygunUser): Client; + setVersion(version: string): Client; + onBeforeSend(callback: raygun.OnBeforeSend): Client; + groupingKey(groupingKey: string): Client; + offline(): Client; + online(): Client; + send( + exception: Error | string | object, + customData?: raygun.KeyValueObject, + offlineStorageCallback?: (error?: Error) => void, + request?: raygun.RaygunRequest, + tags?: ReadonlyArray + ): raygun.RaygunPayload; + expressHandler( + error: Error, + request: raygun.RaygunRequest, + res: any, + next: any + ): void; +} + +export = Client; diff --git a/types/raygun/raygun-tests.ts b/types/raygun/raygun-tests.ts index f2a92432c2..76328b01c5 100644 --- a/types/raygun/raygun-tests.ts +++ b/types/raygun/raygun-tests.ts @@ -1,6 +1,6 @@ -import raygun from 'raygun'; +import Client = require('raygun'); -const client = new raygun.Client(); // $ExpectType Client +const client = new Client(); // $ExpectType Client client.init({apiKey: '1'}); // $ExpectType Client client.init(); // $ExpectError