UNPKG

4.08 kBJavaScriptView Raw
1import _regeneratorRuntime from "@babel/runtime-corejs2/regenerator";
2import _asyncToGenerator from "@babel/runtime-corejs2/helpers/asyncToGenerator";
3import Http from './http';
4import mapSpec, { plugins } from './specmap';
5import { normalizeSwagger } from './helpers';
6import { ACCEPT_HEADER_VALUE_FOR_DOCUMENTS } from './constants';
7export function makeFetchJSON(http) {
8 var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
9 var requestInterceptor = opts.requestInterceptor,
10 responseInterceptor = opts.responseInterceptor; // Set credentials with 'http.withCredentials' value
11
12 var credentials = http.withCredentials ? 'include' : 'same-origin';
13 return function (docPath) {
14 return http({
15 url: docPath,
16 loadSpec: true,
17 requestInterceptor: requestInterceptor,
18 responseInterceptor: responseInterceptor,
19 headers: {
20 Accept: ACCEPT_HEADER_VALUE_FOR_DOCUMENTS
21 },
22 credentials: credentials
23 }).then(function (res) {
24 return res.body;
25 });
26 };
27} // Wipe out the http cache
28
29export function clearCache() {
30 plugins.refs.clearCache();
31}
32export default function resolve(obj) {
33 var fetch = obj.fetch,
34 spec = obj.spec,
35 url = obj.url,
36 mode = obj.mode,
37 _obj$allowMetaPatches = obj.allowMetaPatches,
38 allowMetaPatches = _obj$allowMetaPatches === void 0 ? true : _obj$allowMetaPatches,
39 pathDiscriminator = obj.pathDiscriminator,
40 modelPropertyMacro = obj.modelPropertyMacro,
41 parameterMacro = obj.parameterMacro,
42 requestInterceptor = obj.requestInterceptor,
43 responseInterceptor = obj.responseInterceptor,
44 skipNormalization = obj.skipNormalization,
45 useCircularStructures = obj.useCircularStructures;
46 var http = obj.http,
47 baseDoc = obj.baseDoc; // @TODO Swagger-UI uses baseDoc instead of url, this is to allow both
48 // need to fix and pick one.
49
50 baseDoc = baseDoc || url; // Provide a default fetch implementation
51 // TODO fetch should be removed, and http used instead
52
53 http = fetch || http || Http;
54
55 if (!spec) {
56 return makeFetchJSON(http, {
57 requestInterceptor: requestInterceptor,
58 responseInterceptor: responseInterceptor
59 })(baseDoc).then(doResolve);
60 }
61
62 return doResolve(spec);
63
64 function doResolve(_spec) {
65 if (baseDoc) {
66 plugins.refs.docCache[baseDoc] = _spec;
67 } // Build a json-fetcher ( ie: give it a URL and get json out )
68
69
70 plugins.refs.fetchJSON = makeFetchJSON(http, {
71 requestInterceptor: requestInterceptor,
72 responseInterceptor: responseInterceptor
73 });
74 var plugs = [plugins.refs];
75
76 if (typeof parameterMacro === 'function') {
77 plugs.push(plugins.parameters);
78 }
79
80 if (typeof modelPropertyMacro === 'function') {
81 plugs.push(plugins.properties);
82 }
83
84 if (mode !== 'strict') {
85 plugs.push(plugins.allOf);
86 } // mapSpec is where the hard work happens, see https://github.com/swagger-api/specmap for more details
87
88
89 return mapSpec({
90 spec: _spec,
91 context: {
92 baseDoc: baseDoc
93 },
94 plugins: plugs,
95 allowMetaPatches: allowMetaPatches,
96 // allows adding .meta patches, which include adding `$$ref`s to the spec
97 pathDiscriminator: pathDiscriminator,
98 // for lazy resolution
99 parameterMacro: parameterMacro,
100 modelPropertyMacro: modelPropertyMacro,
101 useCircularStructures: useCircularStructures
102 }).then(skipNormalization ? /*#__PURE__*/function () {
103 var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(a) {
104 return _regeneratorRuntime.wrap(function _callee$(_context) {
105 while (1) {
106 switch (_context.prev = _context.next) {
107 case 0:
108 return _context.abrupt("return", a);
109
110 case 1:
111 case "end":
112 return _context.stop();
113 }
114 }
115 }, _callee);
116 }));
117
118 return function (_x) {
119 return _ref.apply(this, arguments);
120 };
121 }() : normalizeSwagger);
122 }
123}
\No newline at end of file