@scalar/api-client
Version:
the open source API testing client
303 lines (302 loc) • 10.5 kB
JavaScript
import { isDefined as O } from "@scalar/helpers/array/is-defined";
import { unpackProxyObject as D } from "@scalar/workspace-store/helpers/unpack-proxy";
import { resolve as s } from "@scalar/workspace-store/resolve";
const T = 10, q = "additionalProperty", j = (/* @__PURE__ */ new Date()).toISOString(), C = j.split("T")[0], k = j.split("T")[1].split(".")[0], M = {
"date-time": j,
date: C,
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"
}, I = (e) => {
if (!("propertyNames" in e) || !e.propertyNames)
return;
const i = s.schema(e.propertyNames);
if (i && "enum" in i && Array.isArray(i.enum) && i.enum.length > 0)
return i.enum;
}, S = (e, i = !1, o = "") => "type" in e && e.type === "string" && "format" in e && e.format === "binary" ? "@filename" : i && "format" in e && e.format ? M[e.format] ?? o : o, A = /* @__PURE__ */ new WeakMap(), P = /* @__PURE__ */ new WeakMap(), N = (e) => D(e, { depth: 1 }), R = (e) => {
if (!e)
return;
const i = P.get(e);
if (i)
return i;
if ("required" in e) {
const o = e.required;
if (Array.isArray(o) && o.length > 0) {
const a = new Set(o);
return P.set(e, a), a;
}
}
}, d = (e, i, o) => {
if (typeof i != "object" || i === null)
return i;
const a = N(e), f = A.get(a) ?? /* @__PURE__ */ new Map();
return f && f.set(o, i), A.set(a, f), i;
}, _ = (e) => !!(e.allOf || e.oneOf || e.anyOf), F = (e, i, o, a) => {
if (e.deprecated || a?.mode === "write" && e.readOnly || a?.mode === "read" && e.writeOnly)
return !0;
if (a?.omitEmptyAndOptionalProperties !== !0 || "type" in e && (e.type === "object" || e.type === "array") || _(e) || "examples" in e && Array.isArray(e.examples) && e.examples.length > 0 || "example" in e && e.example !== void 0 || "default" in e && e.default !== void 0 || "const" in e && e.const !== void 0 || "enum" in e && Array.isArray(e.enum) && e.enum.length > 0)
return !1;
const r = o ?? e.title ?? "", t = R(i);
return !(t ? t.has(r) : !1);
}, E = (e, i) => Array.isArray(e) && Array.isArray(i) ? [...e, ...i] : e && typeof e == "object" && i && typeof i == "object" ? { ...e, ...i } : i, L = (e, i, o, a, f) => {
const r = {};
if ("properties" in e && e.properties) {
const t = Object.keys(e.properties), n = t.length;
for (let l = 0; l < n; l++) {
const c = t[l], u = s.schema(e.properties[c]);
if (!u)
continue;
const y = i?.xml && "xml" in u ? u.xml?.name : void 0, m = g(u, i, {
level: o + 1,
parentSchema: e,
name: c,
seen: a
});
typeof m < "u" && (r[y ?? c] = m);
}
}
if ("patternProperties" in e && e.patternProperties)
for (const t of Object.keys(e.patternProperties)) {
const n = s.schema(e.patternProperties[t]);
n && (r[t] = g(n, i, {
level: o + 1,
parentSchema: e,
name: t,
seen: a
}));
}
if ("additionalProperties" in e && e.additionalProperties) {
const t = typeof e.additionalProperties == "boolean" ? e.additionalProperties : s.schema(e.additionalProperties), n = e.additionalProperties === !0 || typeof e.additionalProperties == "object" && Object.keys(e.additionalProperties).length === 0, l = typeof t == "object" && "x-additionalPropertiesName" in t && typeof t["x-additionalPropertiesName"] == "string" && t["x-additionalPropertiesName"].trim().length > 0, c = l ? void 0 : I(e), u = l ? t["x-additionalPropertiesName"].trim() : q, y = n ? "anything" : typeof t == "object" ? g(t, i, {
level: o + 1,
seen: a
}) : "anything";
c && c.length > 0 ? r[String(c[0])] = y : r[u] = y;
}
if (e.oneOf?.[0])
Object.assign(
r,
g(s.schema(e.oneOf[0]), i, {
level: o + 1,
seen: a
})
);
else if (e.anyOf?.[0])
Object.assign(
r,
g(s.schema(e.anyOf[0]), i, {
level: o + 1,
seen: a
})
);
else if (Array.isArray(e.allOf) && e.allOf.length > 0) {
let t = r;
for (const n of e.allOf) {
const l = g(s.schema(n), i, {
level: o + 1,
parentSchema: e,
seen: a
});
t = E(t, l);
}
t && typeof t == "object" && Object.assign(r, t);
}
if (i?.xml && "xml" in e && e.xml?.name && o === 0) {
const t = {};
return t[e.xml.name] = r, d(e, t, f);
}
return d(e, r, f);
}, U = (e, i, o, a, f) => {
const r = "items" in e ? s.schema(e.items) : void 0, t = r && typeof r == "object" && "xml" in r ? r.xml?.name : void 0, n = !!(i?.xml && "xml" in e && e.xml?.wrapped && t);
if (e.example !== void 0)
return d(e, n ? { [t]: e.example } : e.example, f);
if (r && typeof r == "object") {
if (Array.isArray(r.allOf) && r.allOf.length > 0) {
const y = r.allOf.filter(O), m = s.schema(y[0]);
if (m && typeof m == "object" && "type" in m && m.type === "object") {
const b = g({ type: "object", allOf: y }, i, {
level: o + 1,
parentSchema: e,
seen: a
});
return d(e, n ? [{ [t]: b }] : [b], f);
}
const v = y.map(
(p) => g(s.schema(p), i, {
level: o + 1,
parentSchema: e,
seen: a
})
).filter(O);
return d(
e,
n ? v.map((p) => ({ [t]: p })) : v,
f
);
}
const u = r.anyOf || r.oneOf;
if (u && u.length > 0) {
const y = u[0], m = g(s.schema(y), i, {
level: o + 1,
parentSchema: e,
seen: a
});
return d(e, n ? [{ [t]: m }] : [m], f);
}
}
const l = r && typeof r == "object" && ("type" in r && r.type === "object" || "properties" in r), c = r && typeof r == "object" && ("type" in r && r.type === "array" || "items" in r);
if (r && typeof r == "object" && ("type" in r && r.type || l || c)) {
const u = g(r, i, {
level: o + 1,
seen: a
});
return d(e, n ? [{ [t]: u }] : [u], f);
}
return d(e, [], f);
}, W = (e, i, o) => {
if ("type" in e && e.type && !Array.isArray(e.type))
switch (e.type) {
case "string":
return S(e, i, o ?? "");
case "boolean":
return !0;
case "integer":
return "minimum" in e && typeof e.minimum == "number" ? e.minimum : 1;
case "number":
return "minimum" in e && typeof e.minimum == "number" ? e.minimum : 1;
case "array":
return [];
default:
return;
}
}, X = (e, i, o) => {
if ("type" in e && Array.isArray(e.type)) {
if (e.type.includes("null"))
return null;
const a = e.type[0];
if (a)
switch (a) {
case "string":
return S(e, i, o ?? "");
case "boolean":
return !0;
case "integer":
return "minimum" in e && typeof e.minimum == "number" ? e.minimum : 1;
case "number":
return "minimum" in e && typeof e.minimum == "number" ? e.minimum : 1;
case "null":
return null;
default:
return;
}
}
}, z = (e) => JSON.stringify({
emptyString: e?.emptyString,
xml: e?.xml,
mode: e?.mode,
variables: e?.variables,
omitEmptyAndOptionalProperties: e?.omitEmptyAndOptionalProperties
}), g = (e, i, {
level: o = 0,
parentSchema: a,
name: f,
seen: r = /* @__PURE__ */ new WeakSet()
} = {}) => {
const t = s.schema(e);
if (!O(t))
return;
const n = N(t);
if (r.has(n))
return;
r.add(n);
const l = z(i), c = A.get(n)?.get(l);
if (typeof c < "u")
return r.delete(n), c;
if (o > T)
return r.delete(n), "[Max Depth Exceeded]";
const u = !!i?.emptyString;
if (F(t, a, f, i)) {
r.delete(n);
return;
}
if ("x-variable" in t && t["x-variable"]) {
const p = i?.variables?.[t["x-variable"]];
if (p !== void 0)
return "type" in t && (t.type === "number" || t.type === "integer") ? (r.delete(n), d(t, Number(p), l)) : (r.delete(n), d(t, p, l));
}
if (Array.isArray(t.examples) && t.examples.length > 0)
return r.delete(n), d(t, t.examples[0], l);
if (t.example !== void 0)
return r.delete(n), d(t, t.example, l);
if (t.default !== void 0)
return r.delete(n), d(t, t.default, l);
if (t.const !== void 0)
return r.delete(n), d(t, t.const, l);
if (Array.isArray(t.enum) && t.enum.length > 0)
return r.delete(n), d(t, t.enum[0], l);
if ("properties" in t || "type" in t && t.type === "object") {
const p = L(t, i, o, r, l);
return r.delete(n), p;
}
if ("type" in t && t.type === "array" || "items" in t) {
const p = U(t, i, o, r, l);
return r.delete(n), p;
}
const y = W(t, u, i?.emptyString);
if (y !== void 0)
return r.delete(n), d(t, y, l);
const m = t.oneOf || t.anyOf;
if (Array.isArray(m) && m.length > 0) {
for (const p of m) {
const b = s.schema(p);
if (b && (!("type" in b) || b.type !== "null"))
return r.delete(n), d(
t,
g(b, i, {
level: o + 1,
seen: r
}),
l
);
}
return r.delete(n), d(t, null, l);
}
if (Array.isArray(t.allOf) && t.allOf.length > 0) {
let p;
const b = t.allOf;
for (const w of b) {
const x = g(w, i, {
level: o + 1,
parentSchema: t,
seen: r
});
p === void 0 ? p = x : p && typeof p == "object" && x && typeof x == "object" ? p = E(p, x) : x !== void 0 && (p = x);
}
return r.delete(n), d(t, p ?? null, l);
}
const v = X(t, u, i?.emptyString);
return v !== void 0 ? (r.delete(n), d(t, v, l)) : (r.delete(n), d(t, null, l));
};
export {
g as getExampleFromSchema
};