Skip to content

Commit ddd81aa

Browse files
authored
feat: add support for accepting axios options in sendRequest (#299)
This adds logic to extract the Axios options from the request parameters and pass them to the final request. It also exposes a type for generated SDKs to utilize, so they don't need to install Axios just for that type. Signed-off-by: Dustin Popp <[email protected]>
1 parent 8b601a3 commit ddd81aa

File tree

5 files changed

+60
-1
lines changed

5 files changed

+60
-1
lines changed

etc/ibm-cloud-sdk-core.api.md

+3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66

77
/// <reference types="node" />
88

9+
import { GenericAbortSignal as AbortSignal_2 } from 'axios';
910
import { AxiosInstance } from 'axios';
1011
import type { CookieJar } from 'tough-cookie';
1112
import { Debugger } from 'debug';
1213
import { OutgoingHttpHeaders } from 'http';
1314
import { Stream } from 'stream';
1415

16+
export { AbortSignal_2 as AbortSignal }
17+
1518
// @public
1619
export function atLeastOne(...args: any): boolean;
1720

index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* @module ibm-cloud-sdk-core
1919
*/
2020

21+
export * from './lib/axios-types';
2122
export { BaseService, UserOptions } from './lib/base-service';
2223
export * from './auth';
2324
export * from './lib/helper';

lib/axios-types.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* (C) Copyright IBM Corp. 2025.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
export { GenericAbortSignal as AbortSignal } from 'axios';

lib/request-wrapper.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ export class RequestWrapper {
245245
*/
246246
public async sendRequest(parameters): Promise<any> {
247247
const options = extend(true, {}, parameters.defaultOptions, parameters.options);
248-
const { path, body, form, formData, qs, method, serviceUrl } = options;
248+
const { path, body, form, formData, qs, method, serviceUrl, axiosOptions } = options;
249249
let { headers, url } = options;
250250

251251
const multipartForm = new FormData();
@@ -329,6 +329,7 @@ export class RequestWrapper {
329329
raxConfig: this.raxConfig,
330330
responseType: options.responseType || 'json',
331331
paramsSerializer: { serialize: (params) => stringify(params) },
332+
...axiosOptions,
332333
};
333334

334335
return this.axiosInstance(requestParams).then(

test/unit/request-wrapper.test.js

+37
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,43 @@ describe('sendRequest', () => {
227227
expect(mockAxiosInstance.mock.calls).toHaveLength(1);
228228
});
229229

230+
it('should include any specified axios request options', async () => {
231+
const parameters = {
232+
defaultOptions: {
233+
body: 'post=body',
234+
formData: '',
235+
qs: {},
236+
method: 'POST',
237+
url: 'https://example.ibm.com/v1',
238+
headers: {
239+
'test-header': 'test-header-value',
240+
},
241+
responseType: 'buffer',
242+
axiosOptions: {
243+
signal: 'mock',
244+
},
245+
},
246+
};
247+
248+
mockAxiosInstance.mockResolvedValue(axiosResolveValue);
249+
250+
const res = await requestWrapperInstance.sendRequest(parameters);
251+
// assert results
252+
expect(mockAxiosInstance.mock.calls[0][0].data).toEqual('post=body');
253+
expect(mockAxiosInstance.mock.calls[0][0].url).toEqual('https://example.ibm.com/v1');
254+
expect(mockAxiosInstance.mock.calls[0][0].headers).toEqual({
255+
'Accept-Encoding': 'gzip',
256+
'test-header': 'test-header-value',
257+
});
258+
expect(mockAxiosInstance.mock.calls[0][0].method).toEqual(parameters.defaultOptions.method);
259+
expect(mockAxiosInstance.mock.calls[0][0].responseType).toEqual(
260+
parameters.defaultOptions.responseType
261+
);
262+
expect(mockAxiosInstance.mock.calls[0][0].signal).toEqual('mock');
263+
expect(res).toEqual(expectedResult);
264+
expect(mockAxiosInstance.mock.calls).toHaveLength(1);
265+
});
266+
230267
it('sendRequest should strip trailing slashes', async () => {
231268
const parameters = {
232269
defaultOptions: {

0 commit comments

Comments
 (0)