UNPKG

@scalar/api-client

Version:

the open source API testing client

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