From 24f378d53ba3f9fac9870e07b0708ba8424f8dc3 Mon Sep 17 00:00:00 2001 From: Walter Rumsby Date: Tue, 23 Apr 2019 08:07:40 +1200 Subject: [PATCH] Add typings for node-insights (#34844) * Add typings for node-insights * Changes based on PR comments --- types/node-insights/index.d.ts | 87 ++++++++++++++++++++++ types/node-insights/node-insights-tests.ts | 25 +++++++ types/node-insights/tsconfig.json | 23 ++++++ types/node-insights/tslint.json | 1 + 4 files changed, 136 insertions(+) create mode 100644 types/node-insights/index.d.ts create mode 100644 types/node-insights/node-insights-tests.ts create mode 100644 types/node-insights/tsconfig.json create mode 100644 types/node-insights/tslint.json diff --git a/types/node-insights/index.d.ts b/types/node-insights/index.d.ts new file mode 100644 index 0000000000..4dfd02966e --- /dev/null +++ b/types/node-insights/index.d.ts @@ -0,0 +1,87 @@ +// Type definitions for node-insights 1.1 +// Project: https://github.com/flightstats/node-insights#readme +// Definitions by: Walter Rumsby +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +interface Config { + insertKey: string; + queryKey: string; + accountId: string; + timerInterval?: number; + maxPending?: number; + defaultEventType?: string; + enabled?: boolean; + gzip?: boolean; +} + +type CallbackFunc = (err: Error | null | undefined, responseBody: string) => void; + +declare class Insights { + static collectorHost: string; + static collectorBaseURL: string; + static queryBaseURL: string; + + enabled: boolean; + queryKey: string; + insertKey: string; + + /** + * Insights + */ + constructor(config: Config); + + /** + * Add insights data to the queue. + * It is sent when the queue reaches a max size or a period of time has elapsed + */ + add(data: object, eventType?: string): void; + + /** + * Build a nrql query string + * @example + * nrql = insights.nrql({ + * select: 'uniqueCount(session)', + * from: 'PageView', + * since: '1 week ago', + * until: '1 day ago', + * timeseries: '1 hour' + * }); + * insights.query(nrql); + */ + nrql(params: object): string; + + /** + * nrql where clause builder + */ + where(clause: string | string[] | object): string; + + /** + * Execute a nrql query + */ + query(query: string, callback: CallbackFunc): void; + + /** + * Start the timer that will send insights after some interval of time + * this is called implicitly when data is added via the add method + */ + start(): void; + + /** + * Stop the timer that will send insights after some interval of time + * this is called implicitly when the amount of data exceeds maxPending and the queue is sent + */ + stop(): void; + + /** + * Stop the timer after flushing. + */ + finish(): void; + + /** + * Send accumulated insights data to new relic (if enabled) + */ + send(done?: CallbackFunc): void; +} + +export = Insights; diff --git a/types/node-insights/node-insights-tests.ts b/types/node-insights/node-insights-tests.ts new file mode 100644 index 0000000000..dfb6b7bb07 --- /dev/null +++ b/types/node-insights/node-insights-tests.ts @@ -0,0 +1,25 @@ +import Insights = require('node-insights'); + +const INSERT_KEY = 'test-insert-key'; +const QUERY_KEY = 'test-query-key'; +const ACCOUNT_ID = 'test-account-id'; + +const insights = new Insights({ + insertKey: INSERT_KEY, + queryKey: QUERY_KEY, + accountId: ACCOUNT_ID +}); + +insights.add({ testAttribute: 1000 }, 'my-event-type'); + +const logResponse = (err: Error | null | undefined, response: string) => { + if (!err) { + // ... + } +}; + +insights.send((err, _body) => { + if (!err) { + insights.query('SELECT * FROM my-event-type', logResponse); + } +}); diff --git a/types/node-insights/tsconfig.json b/types/node-insights/tsconfig.json new file mode 100644 index 0000000000..a611b4f190 --- /dev/null +++ b/types/node-insights/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", + "node-insights-tests.ts" + ] +} diff --git a/types/node-insights/tslint.json b/types/node-insights/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/node-insights/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }