Skip to content

Commit 4348b38

Browse files
author
Samuel Bodin
authored
fix(node): allow passing requesterOptions (#1215)
* fix(node): allow passing requestOptions * lint * change to requesterOptions * add test * too new syntax
1 parent f6e9e56 commit 4348b38

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

packages/requester-node-http/src/__tests__/unit/node-http-requester.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,31 @@ describe('error handling', (): void => {
251251
expect(response.isTimedOut).toBe(false);
252252
});
253253
});
254+
255+
describe('requesterOptions', () => {
256+
it('allows to pass requesterOptions', async () => {
257+
const body = JSON.stringify({ foo: 'bar' });
258+
const requesterTmp = createNodeHttpRequester({
259+
requesterOptions: {
260+
headers: {
261+
'x-algolia-foo': 'bar',
262+
},
263+
},
264+
});
265+
266+
nock('https://algolia-dns.net', {
267+
reqheaders: {
268+
...headers,
269+
'x-algolia-foo': 'bar',
270+
},
271+
})
272+
.post('/foo')
273+
.query({ 'x-algolia-header': 'foo' })
274+
.reply(200, body);
275+
276+
const response = await requesterTmp.send(requestStub);
277+
278+
expect(response.status).toBe(200);
279+
expect(response.content).toBe(body);
280+
});
281+
});

packages/requester-node-http/src/createNodeHttpRequester.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export type NodeHttpRequesterOptions = {
1010
agent?: https.Agent | http.Agent;
1111
httpAgent?: http.Agent;
1212
httpsAgent?: https.Agent;
13+
requesterOptions?: https.RequestOptions;
1314
};
1415

1516
const agentOptions = { keepAlive: true };
@@ -20,6 +21,7 @@ export function createNodeHttpRequester({
2021
agent: userGlobalAgent,
2122
httpAgent: userHttpAgent,
2223
httpsAgent: userHttpsAgent,
24+
requesterOptions = {},
2325
}: NodeHttpRequesterOptions = {}): Requester & Destroyable {
2426
const httpAgent = userHttpAgent || userGlobalAgent || defaultHttpAgent;
2527
const httpsAgent = userHttpsAgent || userGlobalAgent || defaultHttpsAgent;
@@ -32,11 +34,15 @@ export function createNodeHttpRequester({
3234
const path = url.query === null ? url.pathname : `${url.pathname}?${url.query}`;
3335

3436
const options: https.RequestOptions = {
37+
...requesterOptions,
3538
agent: url.protocol === 'https:' ? httpsAgent : httpAgent,
3639
hostname: url.hostname,
3740
path,
3841
method: request.method,
39-
headers: request.headers,
42+
headers: {
43+
...(requesterOptions && requesterOptions.headers ? requesterOptions.headers : {}),
44+
...request.headers,
45+
},
4046
...(url.port !== undefined ? { port: url.port || '' } : {}),
4147
};
4248

0 commit comments

Comments
 (0)