UNPKG

3.64 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.partialUpdateJson = void 0;
7const chalk_1 = __importDefault(require("chalk"));
8function partialUpdateJson(input, newJson, tag) {
9 let refs = [];
10 const filteredPaths = Object.fromEntries(Object.entries(input.paths).map(([name, value]) => [
11 name,
12 Object.fromEntries(Object.entries(value).filter(([_, { tags }]) => !(tags === null || tags === void 0 ? void 0 : tags.find((item) => tag.find((i) => i === item))))),
13 ]));
14 const paths = Object.assign({}, filteredPaths);
15 Object.entries(newJson.paths).forEach(([endPoint, value]) => {
16 Object.entries(value).forEach(([method, options]) => {
17 if (typeof options !== "object") {
18 return;
19 }
20 if (tag.find((t) => { var _a; return (_a = options.tags) === null || _a === void 0 ? void 0 : _a.includes(t); })) {
21 refs = refs.concat(findRefs(options));
22 if (!paths[endPoint]) {
23 paths[endPoint] = Object.assign({}, newJson.paths[endPoint]);
24 }
25 paths[endPoint][method] = options;
26 }
27 });
28 });
29 refs = findRelatedRef(newJson, refs);
30 const components = replaceComponents(input, newJson, refs);
31 return Object.assign(Object.assign({}, input), { paths,
32 components });
33}
34exports.partialUpdateJson = partialUpdateJson;
35function findRelatedRef(newJson, refs) {
36 try {
37 ["schemas", "requestBodies", "parameters"].map((key) => {
38 var _a;
39 if ((_a = newJson === null || newJson === void 0 ? void 0 : newJson.components) === null || _a === void 0 ? void 0 : _a[key]) {
40 Object.entries(newJson.components[key]).forEach(([name, schema]) => {
41 if (refs.includes(name)) {
42 const schemaRefs = findRefs(schema);
43 const newRefs = schemaRefs.filter((ref) => !refs.includes(ref));
44 if (newRefs.length > 0) {
45 refs = findRelatedRef(newJson, [...refs, ...newRefs]);
46 }
47 }
48 });
49 }
50 });
51 }
52 catch (error) {
53 chalk_1.default.red(error);
54 }
55 return refs;
56}
57function replaceComponents(input, newJson, refs) {
58 const components = Object.assign({}, input.components);
59 ["schemas", "requestBodies", "parameters"].map((key) => {
60 var _a;
61 if ((_a = newJson === null || newJson === void 0 ? void 0 : newJson.components) === null || _a === void 0 ? void 0 : _a[key]) {
62 Object.entries(newJson.components[key]).forEach(([name, schema]) => {
63 if (refs.includes(name)) {
64 if (!components[key]) {
65 components[key] = Object.assign({}, input.components[key]);
66 }
67 components[key][name] = schema;
68 }
69 });
70 }
71 });
72 return components;
73}
74function findRefs(obj) {
75 if (typeof obj !== "object") {
76 return [];
77 }
78 if (Array.isArray(obj)) {
79 return obj.flatMap((value) => {
80 return findRefs(value);
81 });
82 }
83 return Object.entries(obj).flatMap(([key, value]) => {
84 if (key === "$ref") {
85 return [value.replace(/#\/components\/[\w]+\//g, "")];
86 }
87 return findRefs(value);
88 });
89}
90//# sourceMappingURL=updateJson.js.map
\No newline at end of file