UNPKG

7.89 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4require("cross-fetch/polyfill");
5var debug_1 = tslib_1.__importDefault(require("debug"));
6var utils_1 = require("../utils");
7var jsonSchema_1 = require("./jsonSchema");
8var schemaId_1 = tslib_1.__importDefault(require("./schemaId"));
9var debug = debug_1.default('dtsgen');
10var ReferenceResolver = (function () {
11 function ReferenceResolver() {
12 this.schemaCache = new Map();
13 this.referenceCache = new Map();
14 }
15 ReferenceResolver.prototype.dereference = function (refId) {
16 var result = this.referenceCache.get(refId);
17 if (result == null) {
18 throw new Error('Target reference is not found: ' + refId);
19 }
20 return result;
21 };
22 ReferenceResolver.prototype.getAllRegisteredSchema = function () {
23 return this.schemaCache.values();
24 };
25 ReferenceResolver.prototype.resolve = function () {
26 return tslib_1.__awaiter(this, void 0, void 0, function () {
27 var e_1, _a, error, _b, _c, _d, key, schema, id, fileId, result, refSchema, e_2, rootSchema, targetSchema, e_1_1;
28 return tslib_1.__generator(this, function (_e) {
29 switch (_e.label) {
30 case 0:
31 debug("resolve reference: reference schema count=" + this.referenceCache.size + ".");
32 error = [];
33 _e.label = 1;
34 case 1:
35 _e.trys.push([1, 10, 11, 12]);
36 _b = tslib_1.__values(this.referenceCache.entries()), _c = _b.next();
37 _e.label = 2;
38 case 2:
39 if (!!_c.done) return [3, 9];
40 _d = tslib_1.__read(_c.value, 2), key = _d[0], schema = _d[1];
41 if (schema != null) {
42 return [3, 8];
43 }
44 id = new schemaId_1.default(key);
45 fileId = id.getFileId();
46 result = this.schemaCache.get(id.getAbsoluteId());
47 if (!(result == null)) return [3, 7];
48 refSchema = this.schemaCache.get(fileId);
49 if (!(refSchema == null)) return [3, 6];
50 if (!id.isFetchable()) {
51 error.push("The $ref target is not exists: " + id.getAbsoluteId());
52 return [3, 8];
53 }
54 _e.label = 3;
55 case 3:
56 _e.trys.push([3, 5, , 6]);
57 debug("fetch remote schema: id=[" + fileId + "].");
58 return [4, this.registerRemoteSchema(fileId)];
59 case 4:
60 _e.sent();
61 return [3, 6];
62 case 5:
63 e_2 = _e.sent();
64 error.push("Fail to fetch the $ref target: " + id.getAbsoluteId() + ", " + e_2);
65 return [3, 8];
66 case 6:
67 result = this.schemaCache.get(id.getAbsoluteId());
68 _e.label = 7;
69 case 7:
70 debug("resolve reference: ref=[" + id.getAbsoluteId() + "]");
71 if (result != null) {
72 this.referenceCache.set(id.getAbsoluteId(), result);
73 }
74 else {
75 if (id.existsJsonPointerHash()) {
76 rootSchema = this.schemaCache.get(fileId);
77 if (rootSchema == null) {
78 error.push("The $ref targets root is not found: " + id.getAbsoluteId());
79 return [3, 8];
80 }
81 targetSchema = jsonSchema_1.getSubSchema(rootSchema, id.getJsonPointerHash(), id);
82 this.addSchema(targetSchema);
83 this.registerSchema(targetSchema);
84 this.referenceCache.set(id.getAbsoluteId(), targetSchema);
85 }
86 else {
87 error.push("The $ref target is not found: " + id.getAbsoluteId());
88 return [3, 8];
89 }
90 }
91 _e.label = 8;
92 case 8:
93 _c = _b.next();
94 return [3, 2];
95 case 9: return [3, 12];
96 case 10:
97 e_1_1 = _e.sent();
98 e_1 = { error: e_1_1 };
99 return [3, 12];
100 case 11:
101 try {
102 if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
103 }
104 finally { if (e_1) throw e_1.error; }
105 return [7];
106 case 12:
107 if (error.length > 0) {
108 throw new Error(error.join('\n'));
109 }
110 return [2];
111 }
112 });
113 });
114 };
115 ReferenceResolver.prototype.registerRemoteSchema = function (url) {
116 return tslib_1.__awaiter(this, void 0, void 0, function () {
117 var res, body, content, schema;
118 return tslib_1.__generator(this, function (_a) {
119 switch (_a.label) {
120 case 0: return [4, fetch(url)];
121 case 1:
122 res = _a.sent();
123 return [4, res.text()];
124 case 2:
125 body = _a.sent();
126 if (!res.ok) {
127 throw new Error("Error on fetch from url(" + url + "): " + res.status + ", " + body);
128 }
129 content = utils_1.parseFileContent(body, url);
130 schema = jsonSchema_1.parseSchema(content, url);
131 this.registerSchema(schema);
132 return [2];
133 }
134 });
135 });
136 };
137 ReferenceResolver.prototype.registerSchema = function (schema) {
138 var _this = this;
139 debug("register schema: schemaId=[" + schema.id.getAbsoluteId() + "].");
140 jsonSchema_1.searchAllSubSchema(schema, function (subSchema) {
141 _this.addSchema(subSchema);
142 }, function (refId) {
143 _this.addReference(refId);
144 });
145 };
146 ReferenceResolver.prototype.addSchema = function (schema) {
147 var id = schema.id;
148 var key = id.getAbsoluteId();
149 if (!this.schemaCache.has(key)) {
150 debug(" add schema: id=" + key);
151 this.schemaCache.set(key, schema);
152 if (schema.rootSchema == null) {
153 var fileId = id.getFileId();
154 if (!this.schemaCache.has(fileId)) {
155 this.schemaCache.set(fileId, schema);
156 }
157 }
158 }
159 };
160 ReferenceResolver.prototype.addReference = function (refId) {
161 if (!this.referenceCache.has(refId.getAbsoluteId())) {
162 debug(" add reference: id=" + refId.getAbsoluteId());
163 this.referenceCache.set(refId.getAbsoluteId(), undefined);
164 }
165 };
166 ReferenceResolver.prototype.clear = function () {
167 debug('clear resolver cache.');
168 this.schemaCache.clear();
169 this.referenceCache.clear();
170 };
171 return ReferenceResolver;
172}());
173exports.default = ReferenceResolver;
174//# sourceMappingURL=referenceResolver.js.map
\No newline at end of file