Skip to content

Commit 5041784

Browse files
committed
- allow optional request options
1 parent bc6123b commit 5041784

File tree

7 files changed

+26
-0
lines changed

7 files changed

+26
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,18 @@ const getToken = async () => {
395395
OpenAPI.TOKEN = getToken;
396396
```
397397

398+
### Additional Http Request parameters
399+
Sometimes it might be useful to pass additional options to the HttpClient request. For example here we are setting the httpsAgent parameter on the request.
400+
401+
```typescript
402+
import { OpenAPI } from './generated';
403+
404+
OpenAPI.OTHER_OPTIONS = {
405+
// Anything required by the http client you are using (Axios, Fetch, ...)
406+
httpsAgent: new https.Agent({ rejectUnauthorized: false }),
407+
};
408+
```
409+
398410
### References
399411

400412
Local references to schema definitions (those beginning with `#/definitions/schemas/`)

src/templates/core/ApiRequestOptions.hbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ export type ApiRequestOptions = {
1111
readonly mediaType?: string;
1212
readonly responseHeader?: string;
1313
readonly errors?: Record<number, string>;
14+
readOnly otherOptions?: Record<string, any>;
1415
}

src/templates/core/OpenAPI.hbs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type Config = {
1515
PASSWORD?: string | Resolver<string>;
1616
HEADERS?: Headers | Resolver<Headers>;
1717
ENCODE_PATH?: (path: string) => string;
18+
OTHER_OPTIONS?: Record<string, string>;
1819
}
1920

2021
export const OpenAPI: Config = {
@@ -27,4 +28,5 @@ export const OpenAPI: Config = {
2728
PASSWORD: undefined,
2829
HEADERS: undefined,
2930
ENCODE_PATH: undefined,
31+
OTHER_OPTIONS: undefined,
3032
};

src/templates/core/axios/sendRequest.hbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ async function sendRequest(
1515
method: options.method,
1616
withCredentials: OpenAPI.WITH_CREDENTIALS,
1717
cancelToken: source.token,
18+
...options.otherOptions
1819
};
1920

2021
onCancel(() => source.cancel('The user aborted a request.'));

src/templates/core/fetch/sendRequest.hbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ async function sendRequest(
1313
body: body || formData,
1414
method: options.method,
1515
signal: controller.signal,
16+
...options.otherOptions
1617
};
1718

1819
if (OpenAPI.WITH_CREDENTIALS) {

src/templates/core/node/sendRequest.hbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ async function sendRequest(
1313
method: options.method,
1414
body: body || formData,
1515
signal: controller.signal,
16+
...options.otherOptions
1617
};
1718

1819
onCancel(() => controller.abort());

test/__snapshots__/index.spec.js.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export type ApiRequestOptions = {
3939
readonly mediaType?: string;
4040
readonly responseHeader?: string;
4141
readonly errors?: Record<number, string>;
42+
readOnly otherOptions?: Record<string, any>;
4243
}"
4344
`;
4445

@@ -191,6 +192,7 @@ type Config = {
191192
PASSWORD?: string | Resolver<string>;
192193
HEADERS?: Headers | Resolver<Headers>;
193194
ENCODE_PATH?: (path: string) => string;
195+
OTHER_OPTIONS?: Record<string, string>;
194196
}
195197

196198
export const OpenAPI: Config = {
@@ -203,6 +205,7 @@ export const OpenAPI: Config = {
203205
PASSWORD: undefined,
204206
HEADERS: undefined,
205207
ENCODE_PATH: undefined,
208+
OTHER_OPTIONS: undefined,
206209
};"
207210
`;
208211

@@ -382,6 +385,7 @@ async function sendRequest(
382385
body: body || formData,
383386
method: options.method,
384387
signal: controller.signal,
388+
...options.otherOptions
385389
};
386390

387391
if (OpenAPI.WITH_CREDENTIALS) {
@@ -2701,6 +2705,7 @@ export type ApiRequestOptions = {
27012705
readonly mediaType?: string;
27022706
readonly responseHeader?: string;
27032707
readonly errors?: Record<number, string>;
2708+
readOnly otherOptions?: Record<string, any>;
27042709
}"
27052710
`;
27062711

@@ -2853,6 +2858,7 @@ type Config = {
28532858
PASSWORD?: string | Resolver<string>;
28542859
HEADERS?: Headers | Resolver<Headers>;
28552860
ENCODE_PATH?: (path: string) => string;
2861+
OTHER_OPTIONS?: Record<string, string>;
28562862
}
28572863

28582864
export const OpenAPI: Config = {
@@ -2865,6 +2871,7 @@ export const OpenAPI: Config = {
28652871
PASSWORD: undefined,
28662872
HEADERS: undefined,
28672873
ENCODE_PATH: undefined,
2874+
OTHER_OPTIONS: undefined,
28682875
};"
28692876
`;
28702877

@@ -3044,6 +3051,7 @@ async function sendRequest(
30443051
body: body || formData,
30453052
method: options.method,
30463053
signal: controller.signal,
3054+
...options.otherOptions
30473055
};
30483056

30493057
if (OpenAPI.WITH_CREDENTIALS) {

0 commit comments

Comments
 (0)