Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ const request = {
body,
headers,
requestInterceptor,
responseInterceptor
responseInterceptor,
userFetch
}

Swagger.http(request)
Expand All @@ -98,6 +99,11 @@ Swagger.http({
responseInterceptor: (res: Response) => Response
})

// Custom Fetch
Swagger.http({
userFetch: (url: String, options: Object) => Promise
})

```

Swagger Specification Resolver
Expand Down Expand Up @@ -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.
Expand Down
5 changes: 4 additions & 1 deletion src/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -87,6 +87,9 @@ export function buildRequest({
if (responseInterceptor) {
req.responseInterceptor = responseInterceptor
}
if (userFetch) {
req.userFetch = userFetch
}

// Mostly for testing
if (!operationId) {
Expand Down
3 changes: 2 additions & 1 deletion src/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
31 changes: 31 additions & 0 deletions test/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down