@cloudpdf/contract 3.0.0-next.10 → 3.0.0-next.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/openapi.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // src/openapi.ts
2
- import { EngineErrorPayloadSchema as EngineErrorPayloadSchema2 } from "@embedpdf/engine-core/wire";
2
+ import { EngineErrorPayloadSchema as EngineErrorPayloadSchema2, PdfActionWireComponents } from "@embedpdf/engine-core/wire";
3
3
  import { z as z2 } from "zod";
4
- import { zodToJsonSchema } from "zod-to-json-schema";
4
+ import { ignoreOverride, zodToJsonSchema } from "zod-to-json-schema";
5
5
 
6
6
  // src/index.ts
7
7
  import {
@@ -1325,11 +1325,21 @@ var SECURITY_SCHEME = {
1325
1325
  };
1326
1326
  var ADMIN_ERROR_REF = "#/components/schemas/AdminErrorPayload";
1327
1327
  var ENGINE_ERROR_REF = "#/components/schemas/EngineErrorPayload";
1328
+ var SHARED_COMPONENT_SCHEMAS = PdfActionWireComponents;
1329
+ var SHARED_COMPONENT_BY_DEFINITION = new Map(
1330
+ Object.entries(SHARED_COMPONENT_SCHEMAS).map(([name, schema]) => [schema._def, name])
1331
+ );
1332
+ var SHARED_COMPONENT_REFS = new Set(
1333
+ Object.keys(SHARED_COMPONENT_SCHEMAS).map((name) => `#/components/schemas/${name}`)
1334
+ );
1328
1335
  function buildAdminOpenApiDocument(opts) {
1329
1336
  const paths = {};
1330
1337
  const schemas = {};
1331
1338
  registerSchema(schemas, "AdminErrorPayload", AdminErrorPayloadSchema);
1332
1339
  registerSchema(schemas, "EngineErrorPayload", EngineErrorPayloadSchema2);
1340
+ for (const [name, schema] of Object.entries(SHARED_COMPONENT_SCHEMAS)) {
1341
+ registerSchema(schemas, name, schema);
1342
+ }
1333
1343
  for (const op of Object.values(allOperations)) {
1334
1344
  assertDocsGroupsCover(op);
1335
1345
  const openApiPath = toOpenApiPath(op.path);
@@ -1545,11 +1555,50 @@ function registerSchema(schemas, name, schema) {
1545
1555
  const referenced = zodToJsonSchema(schema, {
1546
1556
  target: "openApi3",
1547
1557
  $refStrategy: "root",
1548
- basePath: ["#", ...componentPath]
1558
+ basePath: ["#", ...componentPath],
1559
+ override: (definition) => {
1560
+ const sharedName = SHARED_COMPONENT_BY_DEFINITION.get(definition);
1561
+ if (!sharedName || sharedName === name) return ignoreOverride;
1562
+ return { $ref: `#/components/schemas/${sharedName}` };
1563
+ }
1549
1564
  });
1550
- schemas[name] = breakAncestorReferences(referenced, componentPath);
1565
+ const canonical = collapseSharedReferenceAliases(referenced, referenced, componentPath);
1566
+ schemas[name] = breakAncestorReferences(canonical, componentPath);
1551
1567
  return { $ref: `#/components/schemas/${name}` };
1552
1568
  }
1569
+ function collapseSharedReferenceAliases(value, component, componentPath) {
1570
+ if (Array.isArray(value)) {
1571
+ return value.map((child) => collapseSharedReferenceAliases(child, component, componentPath));
1572
+ }
1573
+ if (!value || typeof value !== "object") return value;
1574
+ const record = value;
1575
+ if (Object.keys(record).length === 1 && typeof record.$ref === "string") {
1576
+ const target = resolveComponentReference(component, record.$ref, componentPath);
1577
+ if (target && typeof target === "object" && !Array.isArray(target)) {
1578
+ const targetRecord = target;
1579
+ if (Object.keys(targetRecord).length === 1 && typeof targetRecord.$ref === "string" && SHARED_COMPONENT_REFS.has(targetRecord.$ref)) {
1580
+ return { $ref: targetRecord.$ref };
1581
+ }
1582
+ }
1583
+ }
1584
+ return Object.fromEntries(
1585
+ Object.entries(record).map(([key, child]) => [
1586
+ key,
1587
+ collapseSharedReferenceAliases(child, component, componentPath)
1588
+ ])
1589
+ );
1590
+ }
1591
+ function resolveComponentReference(component, reference, componentPath) {
1592
+ if (!reference.startsWith("#/")) return void 0;
1593
+ const target = reference.slice(2).split("/").map((segment) => segment.replaceAll("~1", "/").replaceAll("~0", "~"));
1594
+ if (target.length < componentPath.length || !componentPath.every((segment, index) => target[index] === segment)) {
1595
+ return void 0;
1596
+ }
1597
+ return target.slice(componentPath.length).reduce((current, segment) => {
1598
+ if (!current || typeof current !== "object") return void 0;
1599
+ return current[segment];
1600
+ }, component);
1601
+ }
1553
1602
  function breakAncestorReferences(value, path) {
1554
1603
  if (Array.isArray(value)) {
1555
1604
  return value.map((child, index) => breakAncestorReferences(child, [...path, String(index)]));