@scalar/api-reference
Version:
Generate beautiful API references from OpenAPI documents
136 lines (135 loc) • 4.17 kB
JavaScript
import { objectKeys as m } from "@scalar/helpers/object/object-keys";
import { getResolvedRef as p } from "@scalar/workspace-store/helpers/get-resolved-ref";
import { isArraySchema as O } from "@scalar/workspace-store/schemas/v3.1/strict/type-guards";
const l = (e, f) => {
if (!e?.allOf?.length || !Array.isArray(e.allOf))
return f || {};
const s = {}, { allOf: r, ...i } = e;
for (const o of e.allOf) {
if (!o || typeof o != "object")
continue;
const t = p(o);
if (t.allOf) {
const n = l(t);
c(s, n);
continue;
}
c(s, t);
}
if (Object.keys(i).length > 0 && c(s, i, !0), f && typeof f == "object")
if (f.allOf) {
const o = l(f);
c(s, o, !0);
} else
c(s, f, !0);
return s;
}, c = (e, f, s = !1) => {
const r = m(f);
if (r.length !== 0)
for (const i of r) {
const o = p(f[i]);
if (o !== void 0)
if (i === "required")
Array.isArray(o) && o.length > 0 && (e.required?.length ? e.required = [.../* @__PURE__ */ new Set([...e.required, ...o])] : e.required = o.slice());
else if (i === "properties")
o && typeof o == "object" && (e.properties || (e.properties = {}), a(e.properties, o));
else if (i === "items") {
const t = p(o);
if (t)
if (O(f))
if (e.items || (e.items = {}), t.allOf) {
const n = l(t);
Object.assign(e.items, n);
} else
y(p(e.items), t);
else if (t.allOf) {
const n = l(t);
"properties" in n && ("properties" in e || (e.properties = {}), "properties" in e && a(e.properties, n.properties));
} else "items" in e || (e.items = t);
} else if (i === "enum")
Array.isArray(o) && o.length > 0 && (e.enum = [.../* @__PURE__ */ new Set([...e.enum || [], ...o])]);
else if (i === "oneOf" || i === "anyOf") {
if (Array.isArray(o)) {
"properties" in e || (e.properties = {});
for (const t of o) {
const n = p(t);
n.properties && "properties" in e && a(e.properties, n.properties);
}
}
} else {
if (i === "allOf")
continue;
(s || e[i] === void 0) && (e[i] = o);
}
}
}, a = (e, f) => {
const s = Object.keys(f ?? {});
if (!(!f || !e || s.length === 0))
for (const r of s) {
const i = p(f[r]);
if (!i || typeof i != "object") {
e[r] = i;
continue;
}
if (!e[r]) {
i.allOf ? e[r] = l(i) : O(i) && p(i.items)?.allOf ? e[r] = {
...i,
items: l(p(i.items))
} : e[r] = f[r];
continue;
}
const o = p(e[r]);
if (i.allOf)
e[r] = l({ allOf: [o, ...i.allOf] });
else if (O(i) && O(o) && i.items) {
const t = p(o.items);
e[r] = {
...o,
type: "array",
items: t ? h(t, p(i.items)) : p(i.items)
};
} else if ("properties" in o && "properties" in i) {
const t = { ...o, ...i };
t.properties = { ...o.properties }, a(t.properties, i.properties), e[r] = t;
} else
e[r] = { ...i, ...o };
}
}, y = (e, f) => {
if (f.allOf || e.allOf) {
const s = [];
if (e.allOf)
for (const i of e.allOf)
s.push(p(i));
else
s.push(e);
if (f.allOf)
for (const i of f.allOf)
s.push(p(i));
else
s.push(f);
const r = l({ allOf: s });
Object.assign(e, r);
return;
}
Object.assign(e, f), "properties" in e && "properties" in f && a(e.properties, f.properties);
}, h = (e, f) => {
if (e.allOf || f.allOf) {
const r = [];
if (e.allOf)
for (const i of e.allOf)
r.push(p(i));
else
r.push(e);
if (f.allOf)
for (const i of f.allOf)
r.push(p(i));
else
r.push(f);
return l({ allOf: r });
}
const s = { ...e, ...f };
return "properties" in e && "properties" in f && (s.properties = { ...e.properties }, a(s.properties, f.properties)), s;
};
export {
l as mergeAllOfSchemas
};