Skip to content

Commit a6fcff3

Browse files
authored
Merge pull request #1161 from shockey/bug/ui-3763-ref-interceptors
Interceptors for $ref download
2 parents 848f10f + 4587433 commit a6fcff3

File tree

3 files changed

+69
-8
lines changed

3 files changed

+69
-8
lines changed

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ Swagger.prototype = {
6969
return Swagger.resolve({
7070
spec: this.spec,
7171
url: this.url,
72-
allowMetaPatches: this.allowMetaPatches
72+
allowMetaPatches: this.allowMetaPatches,
73+
requestInterceptor: this.requestInterceptor || null,
74+
responseInterceptor: this.responseInterceptor || null
7375
}).then((obj) => {
7476
this.originalSpec = this.spec
7577
this.spec = obj.spec

src/resolver.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ import Http from './http'
22
import mapSpec, {plugins} from './specmap'
33
import {normalizeSwagger} from './helpers'
44

5-
export function makeFetchJSON(http) {
5+
export function makeFetchJSON(http, opts = {}) {
6+
const {requestInterceptor, responseInterceptor} = opts
67
return (docPath) => {
78
return http({
89
url: docPath,
910
loadSpec: true,
11+
requestInterceptor,
12+
responseInterceptor,
1013
headers: {
1114
Accept: 'application/json'
1215
},
@@ -23,10 +26,17 @@ export function clearCache() {
2326
plugins.refs.clearCache()
2427
}
2528

26-
export default function resolve({
27-
http, fetch, spec, url, baseDoc, mode, allowMetaPatches = true,
28-
modelPropertyMacro, parameterMacro
29-
}) {
29+
export default function resolve(obj) {
30+
const {
31+
fetch, spec, url, mode, allowMetaPatches = true,
32+
modelPropertyMacro, parameterMacro, requestInterceptor,
33+
responseInterceptor
34+
} = obj
35+
36+
let {http, baseDoc} = obj
37+
38+
// console.log(obj)
39+
3040
// @TODO Swagger-UI uses baseDoc instead of url, this is to allow both
3141
// need to fix and pick one.
3242
baseDoc = baseDoc || url
@@ -36,7 +46,7 @@ export default function resolve({
3646
http = fetch || http || Http
3747

3848
if (!spec) {
39-
return makeFetchJSON(http)(baseDoc).then(doResolve)
49+
return makeFetchJSON(http, {requestInterceptor, responseInterceptor})(baseDoc).then(doResolve)
4050
}
4151

4252
return doResolve(spec)
@@ -47,7 +57,7 @@ export default function resolve({
4757
}
4858

4959
// Build a json-fetcher ( ie: give it a URL and get json out )
50-
plugins.refs.fetchJSON = makeFetchJSON(http)
60+
plugins.refs.fetchJSON = makeFetchJSON(http, {requestInterceptor, responseInterceptor})
5161

5262
const plugs = [plugins.refs]
5363

test/index.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,17 +494,27 @@ describe('constructor', () => {
494494

495495
describe('interceptor', function () {
496496
beforeEach(() => {
497+
Swagger.clearCache()
497498
const xapp = xmock()
498499
xapp
499500
.get('http://petstore.swagger.io/v2/swagger.json', () => require('./data/petstore.json'))
500501
.get('http://petstore.swagger.io/v2/pet/3', () => ({id: 3}))
501502
.get('http://petstore.swagger.io/v2/pet/4', () => ({id: 4}))
503+
.get('http://petstore.swagger.io/v2/ref.json', () => ({b: 2}))
504+
.get('http://petstore.swagger.io/v2/base.json', () => (
505+
{
506+
$ref: 'http://petstore.swagger.io/v2/ref.json#b'
507+
}
508+
))
502509
})
503510

504511
it('should support request interceptor', function (cb) {
505512
new Swagger({
506513
url: 'http://petstore.swagger.io/v2/swagger.json',
507514
requestInterceptor: (req) => {
515+
if (req.loadSpec) {
516+
return req
517+
}
508518
req.url = 'http://petstore.swagger.io/v2/pet/4'
509519
}
510520
}).then((client) => {
@@ -528,5 +538,44 @@ describe('constructor', () => {
528538
})
529539
}, cb)
530540
})
541+
542+
it('should support request interceptor when fetching a spec and remote ref', function (cb) {
543+
const spy = createSpy().andCall(a => a)
544+
new Swagger({
545+
url: 'http://petstore.swagger.io/v2/base.json',
546+
requestInterceptor: spy
547+
}).then((client) => {
548+
expect(spy.calls.length).toEqual(2)
549+
cb()
550+
}).catch(cb)
551+
})
552+
553+
it('should support response interceptor when fetching a spec and remote ref', function (cb) {
554+
const spy = createSpy().andCall((a) => {
555+
return a
556+
})
557+
558+
new Swagger({
559+
url: 'http://petstore.swagger.io/v2/base.json',
560+
responseInterceptor: spy
561+
}).then((client) => {
562+
expect(spy.calls.length).toEqual(2)
563+
cb()
564+
}).catch(cb)
565+
})
566+
567+
it('should support request and response interceptor when fetching a spec and remote ref', function (cb) {
568+
const reqSpy = createSpy().andCall(a => a)
569+
const resSpy = createSpy().andCall(a => a)
570+
new Swagger({
571+
url: 'http://petstore.swagger.io/v2/base.json',
572+
responseInterceptor: reqSpy,
573+
requestInterceptor: resSpy
574+
}).then((client) => {
575+
expect(reqSpy.calls.length).toEqual(2)
576+
expect(resSpy.calls.length).toEqual(2)
577+
cb()
578+
}).catch(cb)
579+
})
531580
})
532581
})

0 commit comments

Comments
 (0)