Skip to content

feat(requestOptions): accept requesterOptions #1184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/requester-common/src/types/Request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ export type Request = {
* The data to be transfered to the server.
*/
readonly data: string | undefined;

/**
* options passed to the network request function
*/
readonly requesterOptions?: any;
};
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ describe('timeout handling', () => {
});
});

describe('error handling', (): void => {
describe('error handling', () => {
it('resolves dns not found', async () => {
const request = {
url: 'https://this-dont-exist.algolia.com',
Expand Down Expand Up @@ -251,3 +251,27 @@ describe('error handling', (): void => {
expect(response.isTimedOut).toBe(false);
});
});

describe('options', () => {
it('passes `requesterOptions` to http.request()', async () => {
const request = {
...requestStub,
requesterOptions: {
// override any parameter sent to http[s].request
// we can only test those which nock can handle, like method
method: 'GET',
},
};

const body = { response: 'foo' };

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

const response = await requester.send(request);

expect(response.content).toEqual(body);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export function createNodeHttpRequester({
method: request.method,
headers: request.headers,
...(url.port !== undefined ? { port: url.port || '' } : {}),
...(request.requesterOptions ? request.requesterOptions : {}),
};

const req = (url.protocol === 'https:' ? https : http).request(options, response => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,11 @@ describe('request options', () => {

expect(result.data).toBeUndefined();
});

it('passes requesterOptions through', () => {
const requesterOptions = { time: new Date() };
const result = createMappedRequestOptions({ requesterOptions });

expect(result.requesterOptions).toEqual(requesterOptions);
});
});
1 change: 1 addition & 0 deletions packages/transporter/src/concerns/retryableRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export function retryableRequest<TResponse>(
url: serializeUrl(host, request.path, queryParameters),
connectTimeout: getTimeout(timeoutsCount, transporter.timeouts.connect),
responseTimeout: getTimeout(timeoutsCount, requestOptions.timeout as number),
requesterOptions: requestOptions.requesterOptions,
};

/**
Expand Down
12 changes: 11 additions & 1 deletion packages/transporter/src/createMappedRequestOptions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { MappedRequestOptions, RequestOptions } from '.';

const SPECIAL_KEYS = [
'timeout',
'headers',
'queryParameters',
'data',
'cacheable',
'requesterOptions',
];

export function createMappedRequestOptions(
requestOptions?: RequestOptions,
timeout?: number
Expand All @@ -9,7 +18,7 @@ export function createMappedRequestOptions(
const data: Record<string, string> = options.data || {};

Object.keys(options).forEach(key => {
if (['timeout', 'headers', 'queryParameters', 'data', 'cacheable'].indexOf(key) === -1) {
if (SPECIAL_KEYS.indexOf(key) === -1) {
data[key] = options[key]; // eslint-disable-line functional/immutable-data
}
});
Expand All @@ -20,5 +29,6 @@ export function createMappedRequestOptions(
headers: options.headers || {},
queryParameters: options.queryParameters || {},
cacheable: options.cacheable,
...(options.requesterOptions ? { requesterOptions: options.requesterOptions } : {}),
};
}
5 changes: 5 additions & 0 deletions packages/transporter/src/types/MappedRequestOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ export type MappedRequestOptions = {
* The data to be transfered to the server.
*/
readonly data?: Record<string, string>;

/**
* options passed to the network request function
*/
readonly requesterOptions?: any;
};
13 changes: 9 additions & 4 deletions packages/transporter/src/types/RequestOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,35 @@ export type RequestOptions = {
readonly cacheable?: boolean;

/**
* Custom timeout for the request. Note that, in normal situacions
* Custom timeout for the request. Note that, in normal situations
* the given timeout will be applied. But the transporter layer may
* increase this timeout if there is need for it.
*/
readonly timeout?: number;

/**
* Custom headers for the request. This headers are
* going to be merged the transporter headers.
* going to be merged with the transporter headers.
*/
readonly headers?: Readonly<Record<string, string>>;

/**
* Custom query parameters for the request. This query parameters are
* going to be merged the transporter query parameters.
* going to be merged with the transporter query parameters.
*/
readonly queryParameters?: Record<string, any>;

/**
* Custom data for the request. This data are
* going to be merged the transporter data.
* going to be merged with the transporter data.
*/
readonly data?: Record<string, any>;

/**
* options passed to the network request function
*/
readonly requesterOptions?: any;

/**
* Additional request body values. It's only taken in
* consideration in `POST` and `PUT` requests.
Expand Down