-
Notifications
You must be signed in to change notification settings - Fork 760
/
Copy pathresolver.js
115 lines (99 loc) · 3.07 KB
/
resolver.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import Http from './http';
import mapSpec, { plugins } from './specmap';
import { normalizeSwagger } from './helpers';
import { ACCEPT_HEADER_VALUE_FOR_DOCUMENTS } from './constants';
export function makeFetchJSON(http, opts = {}) {
const { requestInterceptor, responseInterceptor } = opts;
// Set credentials with 'http.withCredentials' value
const credentials = http.withCredentials ? 'include' : 'same-origin';
return (docPath) =>
http({
url: docPath,
loadSpec: true,
requestInterceptor,
responseInterceptor,
headers: {
Accept: ACCEPT_HEADER_VALUE_FOR_DOCUMENTS,
},
credentials,
}).then((res) => res.body);
}
// Wipe out the http cache
export function clearCache() {
plugins.refs.clearCache();
}
export function makeFetchRaw(http, opts = {}) {
const { requestInterceptor, responseInterceptor } = opts;
// Set credentials with 'http.withCredentials' value
const credentials = http.withCredentials ? 'include' : 'same-origin';
return (docPath) =>
http({
url: docPath,
loadSpec: true,
requestInterceptor,
responseInterceptor,
credentials,
}).then((res) => res.text);
}
export default function resolve(obj) {
const {
fetch,
spec,
url,
mode,
allowMetaPatches = true,
pathDiscriminator,
modelPropertyMacro,
parameterMacro,
requestInterceptor,
responseInterceptor,
skipNormalization,
useCircularStructures,
} = obj;
let { http, baseDoc } = obj;
// @TODO Swagger-UI uses baseDoc instead of url, this is to allow both
// need to fix and pick one.
baseDoc = baseDoc || url;
// Provide a default fetch implementation
// TODO fetch should be removed, and http used instead
http = fetch || http || Http;
if (!spec) {
return makeFetchJSON(http, { requestInterceptor, responseInterceptor })(baseDoc).then(
doResolve
);
}
return doResolve(spec);
function doResolve(_spec) {
if (baseDoc) {
plugins.refs.docCache[baseDoc] = _spec;
}
// Build a json-fetcher ( ie: give it a URL and get json out )
plugins.refs.fetchJSON = makeFetchJSON(http, { requestInterceptor, responseInterceptor });
// Build a raw-fetcher ( ie: give it a URL and get raw text out )
plugins.externalValue.fetchRaw = makeFetchRaw(http, {
requestInterceptor,
responseInterceptor,
});
const plugs = [plugins.refs, plugins.externalValue];
if (typeof parameterMacro === 'function') {
plugs.push(plugins.parameters);
}
if (typeof modelPropertyMacro === 'function') {
plugs.push(plugins.properties);
}
if (mode !== 'strict') {
plugs.push(plugins.allOf);
}
// mapSpec is where the hard work happens
return mapSpec({
spec: _spec,
context: { baseDoc },
plugins: plugs,
allowMetaPatches, // allows adding .meta patches, which include adding `$$ref`s to the spec
pathDiscriminator, // for lazy resolution
parameterMacro,
modelPropertyMacro,
useCircularStructures,
}).then(skipNormalization ? async (a) => a : normalizeSwagger);
}
}