Skip to content

Commit f27719d

Browse files
committed
Add Typescript definition for node SDK
1 parent 1f1f8c5 commit f27719d

File tree

5 files changed

+78
-3
lines changed

5 files changed

+78
-3
lines changed

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.gitlab
22
release.sh
3+
*.test-d.ts

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ else
2525
endif
2626
.PHONY: test
2727

28+
test-types: npm-install ## Runs type definition tests
29+
ifdef GITLAB_CI
30+
$(ON_NODE_PRUNE) npx tsd
31+
else
32+
$(ON_NODE) npx tsd
33+
endif
34+
.PHONY: test-types
35+
2836
eslint: npm-install ## Runs Eslint to report code style issues
2937
ifdef GITLAB_CI
3038
$(ON_NODE_PRUNE) npx eslint src/ tests/

package.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,20 @@
77
},
88
"description": "Blackfire Continuous Profiler continuously collects and uploads profiling data to the Blackfire servers",
99
"main": "src/index.js",
10+
"types": "src/index.d.ts",
1011
"exports": {
1112
".": {
13+
"types": "./src/index.d.ts",
1214
"import": "./src/index.js",
1315
"require": "./src/index.js"
1416
}
1517
},
18+
"engines": {
19+
"node": ">=16.0.0"
20+
},
1621
"scripts": {
17-
"test": "jest --no-cache --runInBand --forceExit --detectOpenHandles --coverage"
22+
"test": "jest --no-cache --runInBand --forceExit --detectOpenHandles --coverage",
23+
"test:types": "tsd"
1824
},
1925
"author": "blackfire.io",
2026
"license": "MIT",
@@ -33,6 +39,7 @@
3339
"express-fileupload": "^1.4.0",
3440
"globals": "^15.11.0",
3541
"jest": "^29.7.0",
36-
"pprof-format": "^2.0.7"
42+
"pprof-format": "^2.0.7",
43+
"tsd": "^0.33.0"
3744
}
38-
}
45+
}

src/index.d.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export interface BlackfireConfiguration {
2+
/** Name of the application. Defaults to 'my-node-app'. */
3+
appName?: string;
4+
/** Socket to the Blackfire agent. Defaults to platform-specific socket. */
5+
agentSocket?: string;
6+
/** Blackfire Server ID (should be defined with serverToken). */
7+
serverId?: string;
8+
/** Blackfire Server Token (should be defined with serverId). */
9+
serverToken?: string;
10+
/** Labels to add to the profile. */
11+
labels?: Record<string, string>;
12+
/** Timeout in milliseconds for the upload request. Defaults to 10000. */
13+
uploadTimeoutMillis?: number;
14+
}
15+
16+
export function start(config: BlackfireConfiguration): boolean;
17+
export function stop(): boolean;
18+
19+
export const defaultConfig: Required<
20+
Pick<BlackfireConfiguration, 'appName' | 'agentSocket' | 'uploadTimeoutMillis' | 'labels'>
21+
> &
22+
Pick<BlackfireConfiguration, 'serverId' | 'serverToken'>;

src/index.test-d.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { expectType, expectError } from 'tsd';
2+
import { start, stop, defaultConfig, BlackfireConfiguration } from '.';
3+
4+
// start() accepts a valid configuration and returns boolean
5+
expectType<boolean>(start({ appName: 'my-app' }));
6+
7+
// start() accepts all configuration options
8+
expectType<boolean>(start({
9+
appName: 'my-app',
10+
agentSocket: 'tcp://127.0.0.1:8307',
11+
serverId: 'server-id',
12+
serverToken: 'server-token',
13+
labels: { env: 'production' },
14+
uploadTimeoutMillis: 5000,
15+
}));
16+
17+
// start() accepts empty config (all fields optional)
18+
expectType<boolean>(start({}));
19+
20+
// stop() returns boolean
21+
expectType<boolean>(stop());
22+
23+
// defaultConfig has required fields
24+
expectType<string>(defaultConfig.appName);
25+
expectType<string>(defaultConfig.agentSocket);
26+
expectType<number>(defaultConfig.uploadTimeoutMillis);
27+
28+
// defaultConfig.labels is always defined (not optional)
29+
expectType<Record<string, string>>(defaultConfig.labels);
30+
31+
// start() rejects invalid types
32+
expectError(start({ appName: 123 }));
33+
expectError(start({ uploadTimeoutMillis: 'not-a-number' }));
34+
expectError(start({ labels: 'not-an-object' }));
35+
36+
// start() rejects unknown properties
37+
expectError(start({ unknownProp: true }));

0 commit comments

Comments
 (0)