Skip to content

Commit 42a415a

Browse files
authored
Merge pull request #7705 from BitGo/aloe/customReqId
2 parents 77e100a + b5ca774 commit 42a415a

File tree

3 files changed

+107
-1
lines changed

3 files changed

+107
-1
lines changed

modules/sdk-api/src/bitgoAPI.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,12 @@ export class BitGoAPI implements BitGoBase {
132132
protected _validate: boolean;
133133
public readonly cookiesPropagationEnabled: boolean;
134134
private _customProxyAgent?: Agent;
135+
private _requestIdPrefix?: string;
135136
private getAdditionalHeadersCb?: AdditionalHeadersCallback;
136137

137138
constructor(params: BitGoAPIOptions = {}) {
138139
this.getAdditionalHeadersCb = params.getAdditionalHeadersCb;
140+
this._requestIdPrefix = params.requestIdPrefix;
139141
this.cookiesPropagationEnabled = false;
140142
if (
141143
!common.validateParams(
@@ -395,7 +397,9 @@ export class BitGoAPI implements BitGoBase {
395397
}
396398

397399
if (!_.isUndefined(this._reqId)) {
398-
req.set('Request-ID', this._reqId.toString());
400+
const reqId = this._reqId.toString();
401+
const requestId = this._requestIdPrefix ? `${this._requestIdPrefix}${reqId}` : reqId;
402+
req.set('Request-ID', requestId);
399403

400404
// increment after setting the header so the sequence numbers start at 0
401405
this._reqId.inc();

modules/sdk-api/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export interface BitGoAPIOptions {
6464
validate?: boolean;
6565
cookiesPropagationEnabled?: boolean;
6666
getAdditionalHeadersCb?: AdditionalHeadersCallback;
67+
requestIdPrefix?: string;
6768
evm?: {
6869
[key: string]: {
6970
baseUrl?: string;

modules/sdk-api/test/unit/bitgoAPI.ts

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,107 @@ describe('Constructor', function () {
3838
bitgo.cookiesPropagationEnabled.should.equal(false);
3939
});
4040
});
41+
42+
describe('requestIdPrefix argument', function () {
43+
afterEach(function () {
44+
nock.cleanAll();
45+
});
46+
47+
it('should prepend requestIdPrefix to Request-ID header when set', async function () {
48+
const bitgo = new BitGoAPI({
49+
env: 'custom',
50+
customRootURI: 'https://app.example.local',
51+
requestIdPrefix: 'test-prefix-',
52+
});
53+
54+
const scope = nock('https://app.example.local')
55+
.get('/api/v1/ping')
56+
.matchHeader('Request-ID', /^test-prefix-/)
57+
.reply(200, { status: 'ok' });
58+
59+
await bitgo.ping({
60+
reqId: {
61+
toString: () => '12345',
62+
inc: () => {
63+
/* mock */
64+
},
65+
} as any,
66+
});
67+
68+
scope.isDone().should.be.true();
69+
});
70+
71+
it('should not modify Request-ID header when requestIdPrefix is not set', async function () {
72+
const bitgo = new BitGoAPI({
73+
env: 'custom',
74+
customRootURI: 'https://app.example.local',
75+
});
76+
77+
const scope = nock('https://app.example.local')
78+
.get('/api/v1/ping')
79+
.matchHeader('Request-ID', /^12345$/)
80+
.reply(200, { status: 'ok' });
81+
82+
await bitgo.ping({
83+
reqId: {
84+
toString: () => '12345',
85+
inc: () => {
86+
/* mock */
87+
},
88+
} as any,
89+
});
90+
91+
scope.isDone().should.be.true();
92+
});
93+
94+
it('should correctly format Request-ID with prefix and numeric sequence', async function () {
95+
const bitgo = new BitGoAPI({
96+
env: 'custom',
97+
customRootURI: 'https://app.example.local',
98+
requestIdPrefix: 'myapp-v1-',
99+
});
100+
101+
const scope = nock('https://app.example.local')
102+
.get('/api/v1/ping')
103+
.matchHeader('Request-ID', 'myapp-v1-trace-123')
104+
.reply(200, { status: 'ok' });
105+
106+
await bitgo.ping({
107+
reqId: {
108+
toString: () => 'trace-123',
109+
inc: () => {
110+
/* mock */
111+
},
112+
} as any,
113+
});
114+
115+
scope.isDone().should.be.true();
116+
});
117+
118+
it('should work with empty string prefix', async function () {
119+
const bitgo = new BitGoAPI({
120+
env: 'custom',
121+
customRootURI: 'https://app.example.local',
122+
requestIdPrefix: '',
123+
});
124+
125+
const scope = nock('https://app.example.local')
126+
.get('/api/v1/ping')
127+
.matchHeader('Request-ID', 'abc-123')
128+
.reply(200, { status: 'ok' });
129+
130+
await bitgo.ping({
131+
reqId: {
132+
toString: () => 'abc-123',
133+
inc: () => {
134+
/* mock */
135+
},
136+
} as any,
137+
});
138+
139+
scope.isDone().should.be.true();
140+
});
141+
});
41142
describe('http proxy agent', function () {
42143
it('http proxy agent shall be created when proxy(customProxyagent) is set', function () {
43144
const customProxyAgent = new ProxyAgent({

0 commit comments

Comments
 (0)