Skip to content

Commit

Permalink
fix(node): allow passing requesterOptions (#1215)
Browse files Browse the repository at this point in the history
* fix(node): allow passing requestOptions

* lint

* change to requesterOptions

* add test

* too new syntax
  • Loading branch information
Samuel Bodin authored Oct 19, 2020
1 parent f6e9e56 commit 4348b38
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,31 @@ describe('error handling', (): void => {
expect(response.isTimedOut).toBe(false);
});
});

describe('requesterOptions', () => {
it('allows to pass requesterOptions', async () => {
const body = JSON.stringify({ foo: 'bar' });
const requesterTmp = createNodeHttpRequester({
requesterOptions: {
headers: {
'x-algolia-foo': 'bar',
},
},
});

nock('https://algolia-dns.net', {
reqheaders: {
...headers,
'x-algolia-foo': 'bar',
},
})
.post('/foo')
.query({ 'x-algolia-header': 'foo' })
.reply(200, body);

const response = await requesterTmp.send(requestStub);

expect(response.status).toBe(200);
expect(response.content).toBe(body);
});
});
8 changes: 7 additions & 1 deletion packages/requester-node-http/src/createNodeHttpRequester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type NodeHttpRequesterOptions = {
agent?: https.Agent | http.Agent;
httpAgent?: http.Agent;
httpsAgent?: https.Agent;
requesterOptions?: https.RequestOptions;
};

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

const options: https.RequestOptions = {
...requesterOptions,
agent: url.protocol === 'https:' ? httpsAgent : httpAgent,
hostname: url.hostname,
path,
method: request.method,
headers: request.headers,
headers: {
...(requesterOptions && requesterOptions.headers ? requesterOptions.headers : {}),
...request.headers,
},
...(url.port !== undefined ? { port: url.port || '' } : {}),
};

Expand Down

0 comments on commit 4348b38

Please sign in to comment.