diff --git a/README.md b/README.md index b67f27861..1763abeb6 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,8 @@ const request = { body, headers, requestInterceptor, - responseInterceptor + responseInterceptor, + userFetch } Swagger.http(request) @@ -98,6 +99,11 @@ Swagger.http({ responseInterceptor: (res: Response) => Response }) +// Custom Fetch +Swagger.http({ + userFetch: (url: String, options: Object) => Promise +}) + ``` Swagger Specification Resolver @@ -132,6 +138,7 @@ const params = { responseContentType, (http), // You can also override the HTTP client completely + (userFetch), // Alternatively you can just override the fetch method (if you want to use request.js or some other HttpAgent) } // Creates a request object compatible with HTTP client interface. diff --git a/src/execute.js b/src/execute.js index 29fe01e1d..6b59246bb 100755 --- a/src/execute.js +++ b/src/execute.js @@ -66,7 +66,7 @@ export function execute({ export function buildRequest({ spec, operationId, parameters, securities, requestContentType, responseContentType, parameterBuilders, scheme, - requestInterceptor, responseInterceptor, contextUrl + requestInterceptor, responseInterceptor, contextUrl, userFetch }) { parameterBuilders = parameterBuilders || PARAMETER_BUILDERS @@ -87,6 +87,9 @@ export function buildRequest({ if (responseInterceptor) { req.responseInterceptor = responseInterceptor } + if (userFetch) { + req.userFetch = userFetch + } // Mostly for testing if (!operationId) { diff --git a/src/http.js b/src/http.js index ae79f9600..4707733dd 100644 --- a/src/http.js +++ b/src/http.js @@ -37,7 +37,8 @@ export default function http(url, request = {}) { delete request.headers['Content-Type'] } - return fetch(request.url, request).then((res) => { // eslint-disable-line no-undef + // eslint-disable-next-line no-undef + return (request.userFetch || fetch)(request.url, request).then((res) => { const serialized = self.serializeRes(res, url, request).then((_res) => { if (request.responseInterceptor) { _res = request.responseInterceptor(_res) || _res diff --git a/src/interfaces.js b/src/interfaces.js index df4914a84..979186ac2 100644 --- a/src/interfaces.js +++ b/src/interfaces.js @@ -18,7 +18,7 @@ export function makeExecute(swaggerJs = {}) { return ({pathName, method, operationId}) => (parameters, opts = {}) => { return swaggerJs.execute({ spec: swaggerJs.spec, - ...pick(swaggerJs, 'requestInterceptor', 'responseInterceptor'), + ...pick(swaggerJs, 'requestInterceptor', 'responseInterceptor', 'userFetch'), pathName, method, parameters, diff --git a/test/execute.js b/test/execute.js index 92e3407b3..c5e82bfb2 100644 --- a/test/execute.js +++ b/test/execute.js @@ -130,6 +130,37 @@ describe('execute', () => { }) }) + it('should execute a simple get request with user-defined fetch', () => { + // Given + const spec = { + host: 'swagger.io', + schemes: ['https'], + paths: { + '/one': { + get: { + operationId: 'getMe' + } + } + } + } + + const spy = createSpy().andReturn(Promise.resolve()) + + execute({ + userFetch: spy, + spec, + operationId: 'getMe' + }) + expect(spy.calls.length).toEqual(1) + expect(spy.calls[0].arguments[1]).toEqual({ + method: 'GET', + url: 'https://swagger.io/one', + credentials: 'same-origin', + headers: { }, + userFetch: spy + }) + }) + it('should include values for query parameters', function () { // Given const spec = {