Skip to content
This repository was archived by the owner on Apr 3, 2024. It is now read-only.

Commit 003c662

Browse files
feat: add get() function (#709)
The `get()` function is used to access an agent if it has been started.
1 parent e2f52b1 commit 003c662

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,10 @@ function mergeConfigs<T>(options: T & {debug?: T}): T {
6969
delete result.debug;
7070
return Object.assign(result, options.debug);
7171
}
72+
73+
/* Used to access the agent if it has been started. Returns the agent
74+
* if the agent has been started. Otherwise, `undefined` is returned.
75+
*/
76+
export function get(): Debuglet | undefined {
77+
return debuglet;
78+
}

test/test-module.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@
1515
*/
1616

1717
import * as assert from 'assert';
18-
const m: NodeModule & {start: Function} = require('../..');
18+
const m: NodeModule & {
19+
start: Function;
20+
get: () => Debuglet | undefined;
21+
} = require('../..');
1922
import * as nock from 'nock';
2023
import * as nocks from './nocks';
24+
import {Debuglet} from '../src/agent/debuglet';
2125

2226
nock.disableNetConnect();
2327

@@ -39,4 +43,26 @@ describe('Debug module', () => {
3943
m.start();
4044
});
4145
});
46+
47+
it('should return the agent via the get() method', () => {
48+
const agent = m.get();
49+
assert(agent, 'Expected to get the started agent');
50+
assert.strictEqual(agent!.config.projectId, '0');
51+
});
52+
});
53+
54+
describe('Debug module without start() called', () => {
55+
it('get() should return `undefined`', () => {
56+
delete require.cache[require.resolve('../..')];
57+
const m: NodeModule & {
58+
start: Function;
59+
get: () => Debuglet | undefined;
60+
} = require('../..');
61+
const agent = m.get();
62+
assert.strictEqual(
63+
agent,
64+
undefined,
65+
'Expected `undefined` since the agent was not started'
66+
);
67+
});
4268
});

0 commit comments

Comments
 (0)