Skip to content

Commit

Permalink
feat(request): use options.request if provided
Browse files Browse the repository at this point in the history
  • Loading branch information
rstachof committed Oct 30, 2024
1 parent 1950a41 commit 48ecb76
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 47 deletions.
9 changes: 7 additions & 2 deletions packages/@webex/http-core/src/request/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@ export default function request(options) {
options.upload = new EventEmitter();

return intercept(options, options.interceptors, 'Request')
.then((...args) => _request(options, ...args))
.then((...args) => {
console.log('XD', ...args);
// if provided own request function, use that instead
if (options.request) {
return options.request(options, ...args);
}

return _request(options, ...args);
})
.then((...args) => {
return intercept(options, options.interceptors.slice().reverse(), 'Response', ...args);
});
}
75 changes: 35 additions & 40 deletions packages/@webex/http-core/src/request/request.shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,51 +49,46 @@ export default function _request(options) {
`start http ${options.method ? options.method : 'request'} to ${options.uri}`
);

if (options.iframeRequest) {
const result = options.iframeRequest(params);
resolve(result);
} else {
const x = xhr(params, (error, response) => {
/* istanbul ignore next */
if (error) {
const x = xhr(params, (error, response) => {
/* istanbul ignore next */
if (error) {
options.logger.warn(
`XHR error for ${options.method || 'request'} to ${options.uri} :`,
error
);
}

/* istanbul ignore else */
if (response) {
if (response.statusCode >= 400) {
options.logger.warn(
`XHR error for ${options.method || 'request'} to ${options.uri} :`,
error
`http ${options.method ? options.method : 'request'} to ${options.uri} result: ${
response.statusCode
}`
);
}

/* istanbul ignore else */
if (response) {
if (response.statusCode >= 400) {
options.logger.warn(
`http ${options.method ? options.method : 'request'} to ${options.uri} result: ${
response.statusCode
}`
);
} else {
options.logger.debug(
`http ${options.method ? options.method : 'request'} to ${options.uri} result: ${
response.statusCode
}`
);
}
response.options = options;
processResponseJson(response, params);
resolve(response);
} else {
resolve({
statusCode: 0,
options,
headers: options.headers,
method: options.method,
url: options.uri,
body: error,
});
options.logger.debug(
`http ${options.method ? options.method : 'request'} to ${options.uri} result: ${
response.statusCode
}`
);
}
});
response.options = options;
processResponseJson(response, params);
resolve(response);
} else {
resolve({
statusCode: 0,
options,
headers: options.headers,
method: options.method,
url: options.uri,
body: error,
});
}
});

x.onprogress = options.download.emit.bind(options.download, 'progress');
}
x.onprogress = options.download.emit.bind(options.download, 'progress');
}).catch((error) => {
options.logger.warn(error);

Expand Down
1 change: 0 additions & 1 deletion packages/@webex/http-core/test/unit/spec/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {assert} from '@webex/test-helper-chai';
import sinon from 'sinon';

import * as utils from '@webex/http-core/src/request/utils';
import WebexTrackingIdInterceptor from '@webex/webex-core/src/interceptors/webex-tracking-id';
import UserAgentInterceptor from '@webex/webex-core/src/interceptors/webex-user-agent';
import {protoprepareFetchOptions, setTimingsAndFetch} from '@webex/http-core/src/index';
Expand Down
58 changes: 58 additions & 0 deletions packages/@webex/http-core/test/unit/spec/request/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import {assert} from '@webex/test-helper-chai';
import sinon from 'sinon';
import {EventEmitter} from 'events';
import request from '@webex/http-core/src/request';
import * as requestModule from '../../../../src/request/request';
import * as utils from '../../../../src/request/utils';

describe('request', () => {
let interceptStub;
let requestStub;

beforeEach(() => {
interceptStub = sinon.spy(utils, 'intercept');
requestStub = sinon.stub(requestModule, 'default');
});

afterEach(() => {
sinon.restore();
});

it('should modify options and call _request if no custom request function is provided', async () => {
const options = {
url: 'http://example.com',
headers: {},
interceptors: [],
};

requestStub.resolves('response');

const result = await request(options);

assert.strictEqual(result, 'response');
assert.strictEqual(options.uri, 'http://example.com');
assert.isNull(options.url);
assert.deepEqual(options.headers, {});
assert.instanceOf(options.download, EventEmitter);
assert.instanceOf(options.upload, EventEmitter);
assert.isTrue(interceptStub.calledTwice);
assert.isTrue(requestStub.calledOnceWith(options));
});

it('should use custom request function if provided', async () => {
const customRequest = sinon.stub().resolves('custom response');
const options = {
url: 'http://example.com',
headers: {},
interceptors: [],
request: customRequest,
};

const result = await request(options);

assert.strictEqual(result, 'custom response');
assert.isTrue(customRequest.calledOnceWith(options));
assert.isTrue(interceptStub.calledTwice);
assert.isFalse(requestStub.called);
});
});
6 changes: 3 additions & 3 deletions packages/@webex/internal-plugin-conversation/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,9 @@ registerInternalPlugin('conversation', Conversation, {
});
},
},
],
// .concat(decryptionTransforms)
// .concat(encryptionTransforms),
]
.concat(decryptionTransforms)
.concat(encryptionTransforms),
},
config,
});
Expand Down
2 changes: 1 addition & 1 deletion packages/@webex/webex-core/src/webex-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const interceptors = {
ServiceInterceptor: undefined,
UserAgentInterceptor: UserAgentInterceptor.create,
WebexUserAgentInterceptor: WebexUserAgentInterceptor.create,
// AuthInterceptor: AuthInterceptor.create,
AuthInterceptor: AuthInterceptor.create,
KmsDryErrorInterceptor: undefined,
PayloadTransformerInterceptor: PayloadTransformerInterceptor.create,
ConversationInterceptor: undefined,
Expand Down

0 comments on commit 48ecb76

Please sign in to comment.