Skip to content

Commit

Permalink
Merge pull request #38 from ysa23/feat/typescript-declarations
Browse files Browse the repository at this point in the history
Feature: TypeScript declarations
  • Loading branch information
ysa23 committed Jan 31, 2023
2 parents 6a7b5c8 + c4009f9 commit 78a693e
Show file tree
Hide file tree
Showing 21 changed files with 218 additions and 12 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ jobs:
- run: npm install
- run: npm run lint
- run: npm test
- run: npm run tsc:build
1 change: 1 addition & 0 deletions .github/workflows/npmpublish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jobs:
- run: npm install
- run: npm run lint
- run: npm test
- run: npm run tsc:build

publish-npm:
needs: build
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@
"datadog",
"DogStatsD"
],
"version": "0.14.2",
"version": "0.15.0",
"repository": {
"type": "git",
"url": "https://github.com/ysa23/metrics-reporter"
},
"scripts": {
"test": "jest --runInBand --forceExit --detectOpenHandles",
"lint": "eslint src/index.js .",
"tsc:build": "tsc",
"tsc:watch": "tsc -w",
"example:graphite": "node ./examples/graphite.js",
"example:datadog": "node ./examples/datadog.js",
"docker:datadog:up": "cd ./docker && docker-compose -f docker-compose-datadog.yml up -d",
Expand All @@ -31,7 +33,8 @@
"eslint-plugin-import": "^2.20.1",
"eslint-plugin-jest": "^24.3.5",
"jest": "^26.6.3",
"jest-extended": "^0.11.5",
"jest-when": "^3.2.1",
"jest-extended": "^0.11.5"
"typescript": "^4.0.0"
}
}
4 changes: 4 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from "./metrics";
export * from "./reporters";
export * from "./types";

12 changes: 2 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
const { ConsoleReporter } = require('./reporters/console-reporter');
const { DataDogReporter } = require('./reporters/datadog-reporter');
const { GraphiteReporter } = require('./reporters/graphite-reporter');
const { InMemoryReporter } = require('./reporters/in-memory-reporter');
const { StringReporter } = require('./reporters/string-reporter');
const reporters = require('./reporters');
const { Metrics } = require('./metrics');

module.exports = {
Metrics,
ConsoleReporter,
DataDogReporter,
GraphiteReporter,
InMemoryReporter,
StringReporter,
...reporters,
};
15 changes: 15 additions & 0 deletions src/metrics.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {Space} from "./space";
import {Tags} from "./types/tags";
import {IReporter} from "./types/reporter";

declare interface MetricsOptions {
reporters: IReporter[];
tags: Tags;
errback?: ErrorCallback;
}

export declare class Metrics {
constructor(options: MetricsOptions);

space(key: string, tags?: Tags): Space;
}
20 changes: 20 additions & 0 deletions src/network/socket.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {ErrorCallback} from "../types/error-callback";

declare interface SocketOptions {
port: number;
host: string;
maxBufferSize?: number;
flushInterval?: number;
errback?: ErrorCallback;
}

declare interface SendProps {
message: any;
}

export declare class Socket {
constructor(options: SocketOptions);

send(props: SendProps) : void;
close() : void;
}
26 changes: 26 additions & 0 deletions src/network/statsd-socket.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {Tags} from "../types/tags";
import {ErrorCallback} from "../types/error-callback";

declare interface SendProps {
key: string;
value: number;
type: string;
tags?: Tags;
}

declare interface StatsdSocketOptions {
port?: number;
host: string;
batch?: boolean;
maxBufferSize?: number;
flushInterval?: number;
prefix?: string;
errback?: ErrorCallback;
}

export declare class StatsdSocket {
constructor(options: StatsdSocketOptions);

send(props: SendProps) : void;
close() : void;
}
10 changes: 10 additions & 0 deletions src/reporters/console-reporter.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {Tags} from "../types/tags";
import {IReporter} from "../types/reporter";

export declare class ConsoleReporter implements IReporter {
constructor();

report(key: string, value: number, tags?: Tags);
value(key: string, value: number, tags?: Tags);
increment(key: string, value?: number, tags?: Tags);
}
22 changes: 22 additions & 0 deletions src/reporters/datadog-reporter.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {IReporter} from "../types/reporter";
import {Tags} from "../types/tags";

declare interface DataDogReporterOptions {
host: string;
port?: number;
prefix?: string;
batch?: boolean;
maxBufferSize?: number;
flushInterval?: number;
errback?: ErrorCallback;
}

export declare class DataDogReporter implements IReporter {
constructor(options: DataDogReporterOptions);

report(key: string, value: number, tags?: Tags);
value(key: string, value: number, tags?: Tags);
increment(key: string, value?: number, tags?: Tags);

close();
}
20 changes: 20 additions & 0 deletions src/reporters/graphite-reporter.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {IReporter} from "../types/reporter";
import {Tags} from "../types/tags";

declare interface GraphiteReporterOptions {
host: string;
port?: number;
prefix?: string;
batch?: boolean;
maxBufferSize?: number;
flushInterval?: number;
errback?: ErrorCallback;
}

export declare class GraphiteReporter implements IReporter {
constructor(options: GraphiteReporterOptions);

report(key: string, value: number, tags?: Tags);
value(key: string, value: number, tags?: Tags);
increment(key: string, value?: number, tags?: Tags);
}
14 changes: 14 additions & 0 deletions src/reporters/in-memory-reporter.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {IReporter} from "../types/reporter";
import {Tags} from "../types/tags";

declare interface InMemoryReporterOptions {
buffer: any[];
}

export declare class InMemoryReporter implements IReporter {
constructor(options: InMemoryReporterOptions);

increment(key: string, value?: number, tags?: Tags);
report(key: string, value: number, tags?: Tags);
value(key: string, value: number, tags?: Tags);
}
5 changes: 5 additions & 0 deletions src/reporters/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from "./console-reporter";
export * from "./datadog-reporter";
export * from "./graphite-reporter";
export * from "./in-memory-reporter";
export * from "./string-reporter";
13 changes: 13 additions & 0 deletions src/reporters/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { ConsoleReporter } = require('./console-reporter');
const { DataDogReporter } = require('./datadog-reporter');
const { GraphiteReporter } = require('./graphite-reporter');
const { InMemoryReporter } = require('./in-memory-reporter');
const { StringReporter } = require('./string-reporter');

module.exports = {
ConsoleReporter,
DataDogReporter,
GraphiteReporter,
InMemoryReporter,
StringReporter,
};
14 changes: 14 additions & 0 deletions src/reporters/string-reporter.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {IReporter} from "../types/reporter";
import {Tags} from "../types/tags";

declare interface StringReporterOptions {
action: (metric: string) => void;
}

export declare class StringReporter implements IReporter {
constructor(options: StringReporterOptions);

increment(key: string, value?: number, tags?: Tags);
report(key: string, value: number, tags?: Tags);
value(key: string, value: number, tags?: Tags);
}
20 changes: 20 additions & 0 deletions src/space.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {Tags} from "./types/tags";
import {IReporter} from "./types/reporter";
import {ErrorCallback} from "./types/error-callback";

declare interface SpaceOptions {
key: string;
tags: Tags;
reporters: IReporter[];
errback?: ErrorCallback;
}

export declare class Space {
constructor(options: SpaceOptions);

value(val: number) : void;
increment(val?: number): void;
meter: <T>(func: () => T) => T;

space: (nextKey: string, nextTags?: Tags) => Space;
}
1 change: 1 addition & 0 deletions src/types/error-callback.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare type ErrorCallback = (err: Error) => void;
3 changes: 3 additions & 0 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./reporter";
export * from "./tags";
export * from './error-callback';
9 changes: 9 additions & 0 deletions src/types/reporter.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {Tags} from "./tags";

export declare interface IReporter {
report(key: string, value: number, tags?: Tags);

value(key: string, value: number, tags?: Tags);

increment(key: string, value?: number, tags?: Tags);
}
1 change: 1 addition & 0 deletions src/types/tags.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare type Tags = { [key: string]: string | number | boolean };
12 changes: 12 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es2021",
},
"include": [
"./src/**/*.d.ts"
],
"exclude": [
"node_modules",
]
}

0 comments on commit 78a693e

Please sign in to comment.