@scalar/api-client
Version:
the open source API testing client
299 lines (298 loc) • 10.3 kB
JavaScript
import { isDefined as v } from "@scalar/helpers/array/is-defined";
import { getRaw as E } from "@scalar/json-magic/magic-proxy";
import { getResolvedRef as m } from "@scalar/workspace-store/helpers/get-resolved-ref";
import { unpackOverridesProxy as N } from "@scalar/workspace-store/helpers/overrides-proxy";
const D = 10, P = 10, M = "propertyName*", j = (/* @__PURE__ */ new Date()).toISOString(), q = j.split("T")[0], k = j.split("T")[1].split(".")[0], C = {
"date-time": j,
date: q,
email: "hello@example.com",
hostname: "example.com",
// https://tools.ietf.org/html/rfc6531#section-3.3
"idn-email": "jane.doe@example.com",
// https://tools.ietf.org/html/rfc5890#section-2.3.2.3
"idn-hostname": "example.com",
ipv4: "127.0.0.1",
ipv6: "51d4:7fab:bfbf:b7d7:b2cb:d4b4:3dad:d998",
"iri-reference": "/entitiy/1",
// https://tools.ietf.org/html/rfc3987
iri: "https://example.com/entity/123",
"json-pointer": "/nested/objects",
password: "super-secret",
regex: "/[a-z]/",
// https://tools.ietf.org/html/draft-handrews-relative-json-pointer-01
"relative-json-pointer": "1/nested/objects",
// full-time in https://tools.ietf.org/html/rfc3339#section-5.6
time: k,
// either a URI or relative-reference https://tools.ietf.org/html/rfc3986#section-4.1
"uri-reference": "../folder",
"uri-template": "https://example.com/{id}",
uri: "https://example.com",
uuid: "123e4567-e89b-12d3-a456-426614174000",
"object-id": "6592008029c8c3e4dc76256c"
}, w = (t, i = !1, o = "") => "type" in t && t.type === "string" && "format" in t && t.format === "binary" ? new File([""], "filename") : i && "format" in t && t.format ? C[t.format] ?? o : o, A = /* @__PURE__ */ new WeakMap(), S = /* @__PURE__ */ new WeakMap(), I = (t) => {
if (!t)
return;
const i = S.get(t);
if (i)
return i;
if ("required" in t) {
const o = t.required;
if (Array.isArray(o) && o.length > 0) {
const l = new Set(o);
return S.set(t, l), l;
}
}
}, a = (t, i, o) => {
if (typeof i != "object" || i === null)
return i;
const l = E(N(t)), d = A.get(l) ?? /* @__PURE__ */ new Map();
return d && d.set(o, i), A.set(l, d), i;
}, _ = (t) => !!(t.allOf || t.oneOf || t.anyOf), F = (t, i, o, l) => {
if (t.deprecated || l?.mode === "write" && t.readOnly || l?.mode === "read" && t.writeOnly)
return !0;
if (l?.omitEmptyAndOptionalProperties !== !0 || "type" in t && (t.type === "object" || t.type === "array") || _(t) || "examples" in t && Array.isArray(t.examples) && t.examples.length > 0 || "example" in t && t.example !== void 0 || "default" in t && t.default !== void 0 || "const" in t && t.const !== void 0 || "enum" in t && Array.isArray(t.enum) && t.enum.length > 0)
return !1;
const r = o ?? t.title ?? "", e = I(i);
return !(e ? e.has(r) : !1);
}, R = (t, i) => Array.isArray(t) && Array.isArray(i) ? [...t, ...i] : t && typeof t == "object" && i && typeof i == "object" ? { ...t, ...i } : i, L = (t, i, o, l, d) => {
const r = {};
if ("properties" in t && t.properties) {
const e = Object.keys(t.properties), n = o > 3 ? Math.min(P, e.length) : e.length;
for (let p = 0; p < n; p++) {
const x = e[p], u = m(t.properties[x]);
if (!u)
continue;
const s = i?.xml && "xml" in u ? u.xml?.name : void 0, y = c(u, i, {
level: o + 1,
parentSchema: t,
name: x,
seen: l
});
typeof y < "u" && (r[s ?? x] = y);
}
o > 3 && e.length > P && (r["..."] = "[Additional Properties Truncated]");
}
if ("patternProperties" in t && t.patternProperties)
for (const e of Object.keys(t.patternProperties)) {
const n = m(t.patternProperties[e]);
n && (r[e] = c(n, i, {
level: o + 1,
parentSchema: t,
name: e,
seen: l
}));
}
if ("additionalProperties" in t && t.additionalProperties) {
const e = m(t.additionalProperties), n = t.additionalProperties === !0 || typeof t.additionalProperties == "object" && Object.keys(t.additionalProperties).length === 0, p = typeof e == "object" && "x-additionalPropertiesName" in e && typeof e["x-additionalPropertiesName"] == "string" && e["x-additionalPropertiesName"].trim().length > 0 ? `${e["x-additionalPropertiesName"].trim()}*` : M;
r[p] = n ? "anything" : typeof e == "object" ? c(e, i, {
level: o + 1,
seen: l
}) : "anything";
}
if (t.oneOf?.[0])
Object.assign(
r,
c(m(t.oneOf[0]), i, {
level: o + 1,
seen: l
})
);
else if (t.anyOf?.[0])
Object.assign(
r,
c(m(t.anyOf[0]), i, {
level: o + 1,
seen: l
})
);
else if (Array.isArray(t.allOf) && t.allOf.length > 0) {
let e = r;
for (const n of t.allOf) {
const p = c(m(n), i, {
level: o + 1,
parentSchema: t,
seen: l
});
e = R(e, p);
}
e && typeof e == "object" && Object.assign(r, e);
}
if (i?.xml && "xml" in t && t.xml?.name && o === 0) {
const e = {};
return e[t.xml.name] = r, a(t, e, d);
}
return a(t, r, d);
}, X = (t, i, o, l, d) => {
const r = "items" in t ? m(t.items) : void 0, e = r && typeof r == "object" && "xml" in r ? r.xml?.name : void 0, n = !!(i?.xml && "xml" in t && t.xml?.wrapped && e);
if (t.example !== void 0)
return a(t, n ? { [e]: t.example } : t.example, d);
if (r && typeof r == "object") {
if (Array.isArray(r.allOf) && r.allOf.length > 0) {
const s = r.allOf.filter(v), y = m(s[0]);
if (y && typeof y == "object" && "type" in y && y.type === "object") {
const g = c({ type: "object", allOf: s }, i, {
level: o + 1,
parentSchema: t,
seen: l
});
return a(t, n ? [{ [e]: g }] : [g], d);
}
const O = s.map(
(f) => c(m(f), i, {
level: o + 1,
parentSchema: t,
seen: l
})
).filter(v);
return a(
t,
n ? O.map((f) => ({ [e]: f })) : O,
d
);
}
const u = r.anyOf || r.oneOf;
if (u && u.length > 0) {
const s = u[0], y = c(m(s), i, {
level: o + 1,
parentSchema: t,
seen: l
});
return a(t, n ? [{ [e]: y }] : [y], d);
}
}
const p = r && typeof r == "object" && ("type" in r && r.type === "object" || "properties" in r), x = r && typeof r == "object" && ("type" in r && r.type === "array" || "items" in r);
if (r && typeof r == "object" && ("type" in r && r.type || p || x)) {
const u = c(r, i, {
level: o + 1,
seen: l
});
return a(t, n ? [{ [e]: u }] : [u], d);
}
return a(t, [], d);
}, U = (t, i, o) => {
if ("type" in t && t.type && !Array.isArray(t.type))
switch (t.type) {
case "string":
return w(t, i, o ?? "");
case "boolean":
return !0;
case "integer":
return "minimum" in t && typeof t.minimum == "number" ? t.minimum : 1;
case "number":
return "minimum" in t && typeof t.minimum == "number" ? t.minimum : 1;
case "array":
return [];
default:
return;
}
}, W = (t, i, o) => {
if ("type" in t && Array.isArray(t.type)) {
if (t.type.includes("null"))
return null;
const l = t.type[0];
if (l)
switch (l) {
case "string":
return w(t, i, o ?? "");
case "boolean":
return !0;
case "integer":
return "minimum" in t && typeof t.minimum == "number" ? t.minimum : 1;
case "number":
return "minimum" in t && typeof t.minimum == "number" ? t.minimum : 1;
case "null":
return null;
default:
return;
}
}
}, z = (t) => JSON.stringify({
emptyString: t?.emptyString,
xml: t?.xml,
mode: t?.mode,
variables: t?.variables,
omitEmptyAndOptionalProperties: t?.omitEmptyAndOptionalProperties
}), c = (t, i, {
level: o = 0,
parentSchema: l,
name: d,
seen: r = /* @__PURE__ */ new WeakSet()
} = {}) => {
const e = m(t);
if (!v(e))
return;
const n = E(N(e));
if (r.has(n))
return "[Circular Reference]";
r.add(n);
const p = z(i), x = A.get(n)?.get(p);
if (typeof x < "u")
return r.delete(n), x;
if (o > D)
return r.delete(n), "[Max Depth Exceeded]";
const u = !!i?.emptyString;
if (F(e, l, d, i)) {
r.delete(n);
return;
}
if ("x-variable" in e && e["x-variable"]) {
const f = i?.variables?.[e["x-variable"]];
if (f !== void 0)
return "type" in e && (e.type === "number" || e.type === "integer") ? (r.delete(n), a(e, Number(f), p)) : (r.delete(n), a(e, f, p));
}
if (Array.isArray(e.examples) && e.examples.length > 0)
return r.delete(n), a(e, e.examples[0], p);
if (e.example !== void 0)
return r.delete(n), a(e, e.example, p);
if (e.default !== void 0)
return r.delete(n), a(e, e.default, p);
if (e.const !== void 0)
return r.delete(n), a(e, e.const, p);
if (Array.isArray(e.enum) && e.enum.length > 0)
return r.delete(n), a(e, e.enum[0], p);
if ("properties" in e || "type" in e && e.type === "object") {
const f = L(e, i, o, r, p);
return r.delete(n), f;
}
if ("type" in e && e.type === "array" || "items" in e) {
const f = X(e, i, o, r, p);
return r.delete(n), f;
}
const s = U(e, u, i?.emptyString);
if (s !== void 0)
return r.delete(n), a(e, s, p);
const y = e.oneOf || e.anyOf;
if (Array.isArray(y) && y.length > 0) {
for (const f of y) {
const g = m(f);
if (g && (!("type" in g) || g.type !== "null"))
return r.delete(n), a(
e,
c(g, i, {
level: o + 1,
seen: r
}),
p
);
}
return r.delete(n), a(e, null, p);
}
if (Array.isArray(e.allOf) && e.allOf.length > 0) {
let f;
const g = e.allOf;
for (const T of g) {
const b = c(T, i, {
level: o + 1,
parentSchema: e,
seen: r
});
f === void 0 ? f = b : f && typeof f == "object" && b && typeof b == "object" ? f = R(f, b) : b !== void 0 && (f = b);
}
return r.delete(n), a(e, f ?? null, p);
}
const O = W(e, u, i?.emptyString);
return O !== void 0 ? (r.delete(n), a(e, O, p)) : (r.delete(n), a(e, null, p));
};
export {
c as getExampleFromSchema
};