@hey-api/shared 0.1.1 → 0.1.2

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/index.mjs CHANGED
@@ -2038,14 +2038,14 @@ var PluginInstance = class {
2038
2038
  symbol(name, symbol) {
2039
2039
  const symbolIn = {
2040
2040
  ...symbol,
2041
- exportFrom: symbol?.exportFrom ?? (!symbol?.external && this.context.config.output.indexFile && this.config.exportFromIndex ? ["index"] : void 0),
2042
- getFilePath: symbol?.getFilePath ?? this.getSymbolFilePath.bind(this),
2043
2041
  meta: {
2044
2042
  pluginName: path.isAbsolute(this.name) ? "custom" : this.name,
2045
2043
  ...symbol?.meta
2046
2044
  },
2047
2045
  name
2048
2046
  };
2047
+ if (symbolIn.getExportFromFilePath === void 0) symbolIn.getExportFromFilePath = this.getSymbolExportFromFilePath.bind(this);
2048
+ if (symbolIn.getFilePath === void 0) symbolIn.getFilePath = this.getSymbolFilePath.bind(this);
2049
2049
  for (const hook of this.eventHooks["symbol:register:before"]) hook({
2050
2050
  plugin: this,
2051
2051
  symbol: symbolIn
@@ -2100,6 +2100,22 @@ var PluginInstance = class {
2100
2100
  pluginName: this.name
2101
2101
  });
2102
2102
  }
2103
+ getSymbolExportFromFilePath(symbol) {
2104
+ const hooks = [this.config["~hooks"]?.symbols, this.context.config.parser.hooks.symbols];
2105
+ for (const hook of hooks) {
2106
+ const result = hook?.getExportFromFilePath?.(symbol);
2107
+ if (result !== void 0) return result;
2108
+ }
2109
+ const entryFile = this.context.config.output.indexFile ?? this.context.config.output.entryFile;
2110
+ if (symbol.external || !entryFile) return;
2111
+ const includeInEntry = this.config.exportFromIndex ?? this.config.includeInEntry;
2112
+ if (typeof includeInEntry === "boolean" && !includeInEntry || typeof includeInEntry === "function" && !includeInEntry(symbol)) return;
2113
+ const language = symbol.node?.language;
2114
+ if (!language) return;
2115
+ const moduleEntryName = this.gen.moduleEntryNames[language];
2116
+ if (!moduleEntryName) return;
2117
+ return [moduleEntryName];
2118
+ }
2103
2119
  getSymbolFilePath(symbol) {
2104
2120
  const hooks = [this.config["~hooks"]?.symbols, this.context.config.parser.hooks.symbols];
2105
2121
  for (const hook of hooks) {
@@ -3564,6 +3580,92 @@ const removeOriginalSplitSchemas = ({ logger, originalSchemas, spec, split }) =>
3564
3580
  event.timeEnd();
3565
3581
  };
3566
3582
  /**
3583
+ * Create writable variants of parent schemas that have discriminators
3584
+ * and are referenced by split schemas.
3585
+ */
3586
+ function splitDiscriminatorSchemas({ config, existingNames, schemasPointerNamespace, spec, split }) {
3587
+ const schemasObj = getSchemasObject(spec);
3588
+ if (!schemasObj) return;
3589
+ const parentSchemasToSplit = /* @__PURE__ */ new Map();
3590
+ for (const [name, schema] of Object.entries(split.schemas)) {
3591
+ const pointer = `${schemasPointerNamespace}${name}`;
3592
+ const originalPointer = split.reverseMapping[pointer];
3593
+ if (originalPointer) {
3594
+ const mapping = split.mapping[originalPointer];
3595
+ if (mapping) {
3596
+ const contextVariant = mapping.read === pointer ? "read" : mapping.write === pointer ? "write" : null;
3597
+ if (contextVariant && schema && typeof schema === "object" && "allOf" in schema && schema.allOf instanceof Array) {
3598
+ for (const comp of schema.allOf) if (comp && typeof comp === "object" && "$ref" in comp && typeof comp.$ref === "string") {
3599
+ const refPath = jsonPointerToPath(comp.$ref);
3600
+ const schemaName = refPath[refPath.length - 1];
3601
+ if (typeof schemaName === "string" && schemaName in schemasObj) {
3602
+ const resolvedSchema = schemasObj[schemaName];
3603
+ if (resolvedSchema && typeof resolvedSchema === "object" && "discriminator" in resolvedSchema && resolvedSchema.discriminator && typeof resolvedSchema.discriminator === "object" && "mapping" in resolvedSchema.discriminator && resolvedSchema.discriminator.mapping && typeof resolvedSchema.discriminator.mapping === "object") {
3604
+ if (!parentSchemasToSplit.has(comp.$ref)) parentSchemasToSplit.set(comp.$ref, /* @__PURE__ */ new Set());
3605
+ parentSchemasToSplit.get(comp.$ref).add(contextVariant);
3606
+ }
3607
+ }
3608
+ }
3609
+ }
3610
+ }
3611
+ }
3612
+ }
3613
+ const parentSchemaVariants = /* @__PURE__ */ new Map();
3614
+ for (const [parentRef, contexts] of parentSchemasToSplit) {
3615
+ const refPath = jsonPointerToPath(parentRef);
3616
+ const parentName = refPath[refPath.length - 1];
3617
+ if (typeof parentName !== "string" || !(parentName in schemasObj)) continue;
3618
+ const parentSchema = schemasObj[parentName];
3619
+ if (!parentSchema || typeof parentSchema !== "object") continue;
3620
+ const variants = {};
3621
+ for (const context of contexts) {
3622
+ const variantSchema = deepClone(parentSchema);
3623
+ if ("discriminator" in variantSchema && variantSchema.discriminator && typeof variantSchema.discriminator === "object" && "mapping" in variantSchema.discriminator && variantSchema.discriminator.mapping && typeof variantSchema.discriminator.mapping === "object") {
3624
+ const mapping = variantSchema.discriminator.mapping;
3625
+ const updatedMapping = {};
3626
+ for (const [discriminatorValue, originalRef] of Object.entries(mapping)) {
3627
+ const map = split.mapping[originalRef];
3628
+ if (map) if (context === "read" && map.read) updatedMapping[discriminatorValue] = map.read;
3629
+ else if (context === "write" && map.write) updatedMapping[discriminatorValue] = map.write;
3630
+ else updatedMapping[discriminatorValue] = originalRef;
3631
+ else updatedMapping[discriminatorValue] = originalRef;
3632
+ }
3633
+ variantSchema.discriminator.mapping = updatedMapping;
3634
+ }
3635
+ if (context === "write") {
3636
+ const writeName = getUniqueComponentName({
3637
+ base: applyNaming(parentName, config.requests),
3638
+ components: existingNames
3639
+ });
3640
+ existingNames.add(writeName);
3641
+ split.schemas[writeName] = variantSchema;
3642
+ variants.write = `${schemasPointerNamespace}${writeName}`;
3643
+ }
3644
+ }
3645
+ parentSchemaVariants.set(parentRef, variants);
3646
+ }
3647
+ for (const [name, schema] of Object.entries(split.schemas)) {
3648
+ const pointer = `${schemasPointerNamespace}${name}`;
3649
+ const originalPointer = split.reverseMapping[pointer];
3650
+ if (!originalPointer) continue;
3651
+ const mapping = split.mapping[originalPointer];
3652
+ if (!mapping) continue;
3653
+ const contextVariant = mapping.read === pointer ? "read" : mapping.write === pointer ? "write" : null;
3654
+ if (contextVariant && schema && typeof schema === "object") {
3655
+ if ("allOf" in schema && schema.allOf instanceof Array) for (let i = 0; i < schema.allOf.length; i++) {
3656
+ const comp = schema.allOf[i];
3657
+ if (comp && typeof comp === "object" && "$ref" in comp && typeof comp.$ref === "string") {
3658
+ const variants = parentSchemaVariants.get(comp.$ref);
3659
+ if (variants) {
3660
+ if (contextVariant === "write" && variants.write) comp.$ref = variants.write;
3661
+ else if (contextVariant === "read" && variants.read) comp.$ref = variants.read;
3662
+ }
3663
+ }
3664
+ }
3665
+ }
3666
+ }
3667
+ }
3668
+ /**
3567
3669
  * Splits schemas with both 'read' and 'write' scopes into read/write variants.
3568
3670
  * Returns the new schemas and a mapping from original pointer to new variant pointers.
3569
3671
  *
@@ -3634,6 +3736,13 @@ const splitSchemas = ({ config, graph: graph$1, logger, spec }) => {
3634
3736
  split.reverseMapping[readPointer] = pointer;
3635
3737
  split.reverseMapping[writePointer] = pointer;
3636
3738
  }
3739
+ splitDiscriminatorSchemas({
3740
+ config,
3741
+ existingNames,
3742
+ schemasPointerNamespace,
3743
+ spec,
3744
+ split
3745
+ });
3637
3746
  event.timeEnd();
3638
3747
  return split;
3639
3748
  };
@@ -3774,6 +3883,26 @@ const updateRefsInSpec = ({ logger, spec, split }) => {
3774
3883
  else if (nextContext === "write" && map.write) node[key] = map.write;
3775
3884
  else if (!nextContext && map.read) node[key] = map.read;
3776
3885
  }
3886
+ } else if (key === "discriminator" && typeof value === "object" && value !== null) {
3887
+ if ("mapping" in value && value.mapping && typeof value.mapping === "object") {
3888
+ const updatedMapping = {};
3889
+ for (const [discriminatorValue, originalRef] of Object.entries(value.mapping)) {
3890
+ const map = split.mapping[originalRef];
3891
+ if (map) if (nextContext === "read" && map.read) updatedMapping[discriminatorValue] = map.read;
3892
+ else if (nextContext === "write" && map.write) updatedMapping[discriminatorValue] = map.write;
3893
+ else updatedMapping[discriminatorValue] = originalRef;
3894
+ else updatedMapping[discriminatorValue] = originalRef;
3895
+ }
3896
+ value.mapping = updatedMapping;
3897
+ }
3898
+ walk$1({
3899
+ context: nextContext,
3900
+ currentPointer: nextPointer,
3901
+ inSchema,
3902
+ node: value,
3903
+ path: [...path$1, key],
3904
+ visited
3905
+ });
3777
3906
  } else walk$1({
3778
3907
  context: nextContext,
3779
3908
  currentPointer: nextPointer,