From 3c0c3773e4e69e7f560d3b92fad8e1005321156a Mon Sep 17 00:00:00 2001 From: Robert Schuh Date: Fri, 30 Jun 2017 13:46:53 +0200 Subject: [PATCH 1/5] Add userFetch interfaces for users to replace the fetch method --- src/execute.js | 5 ++++- src/http.js | 2 +- src/interfaces.js | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/execute.js b/src/execute.js index 8d3607983..f6b2dee20 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 a701387dc..c3681f3da 100644 --- a/src/http.js +++ b/src/http.js @@ -37,7 +37,7 @@ export default function http(url, request = {}) { delete request.headers['Content-Type'] } - return fetch(request.url, request).then((res) => { + 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 64b9f5809..a3376abb8 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, From cb5ac310c4625e8cc22b7c71faf30d0f1892577d Mon Sep 17 00:00:00 2001 From: Robert Schuh Date: Fri, 30 Jun 2017 14:21:54 +0200 Subject: [PATCH 2/5] add really basic test --- test/execute.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/test/execute.js b/test/execute.js index c6529104b..b824e7868 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 = { From 8ef06943df8e5e803f75c0e5bc9f87f5717a60fd Mon Sep 17 00:00:00 2001 From: Robert Schuh Date: Fri, 30 Jun 2017 14:31:38 +0200 Subject: [PATCH 3/5] remove ; --- src/execute.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/execute.js b/src/execute.js index f6b2dee20..bec803a99 100755 --- a/src/execute.js +++ b/src/execute.js @@ -88,7 +88,7 @@ export function buildRequest({ req.responseInterceptor = responseInterceptor } if (userFetch) { - req.userFetch = userFetch; + req.userFetch = userFetch } // Mostly for testing From 287c6f0833b0450a98a1dd7d377a85f10d1c45a4 Mon Sep 17 00:00:00 2001 From: Robert Schuh Date: Wed, 19 Jul 2017 20:51:09 +0200 Subject: [PATCH 4/5] fix line length --- src/http.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/http.js b/src/http.js index 29a4609af..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 (request.userFetch || 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 From 4a3d63e5a12d96b88bf52d452d4e6fbdce9b618b Mon Sep 17 00:00:00 2001 From: Robert Schuh Date: Wed, 26 Jul 2017 00:02:00 +0200 Subject: [PATCH 5/5] Add a bit of userFetch docs --- README.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) 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.