@stackwright-pro/openapi 0.3.0-alpha.4 → 0.3.0-alpha.5

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.js CHANGED
@@ -104,6 +104,19 @@ var OpenAPIParser = class {
104
104
  };
105
105
 
106
106
  // src/parser/SchemaResolver.ts
107
+ var SUPPORTED_CONTENT_TYPES = [
108
+ "application/json",
109
+ "application/geo+json",
110
+ // GeoJSON (ORS, mapping APIs)
111
+ "application/vnd.api+json",
112
+ // JSON:API
113
+ "application/ld+json",
114
+ // JSON-LD
115
+ "application/problem+json",
116
+ // RFC 7807 problem details
117
+ "*/*"
118
+ // wildcard fallback
119
+ ];
107
120
  var SchemaResolver = class {
108
121
  /**
109
122
  * @param document - Fully dereferenced OpenAPI document. Pass the result of
@@ -136,16 +149,19 @@ var SchemaResolver = class {
136
149
  const extractSchema = (responseCode) => {
137
150
  const response = responses[responseCode];
138
151
  if (!response) return void 0;
139
- const content = response.content?.["application/json"];
140
- const schema = content?.schema;
141
- if (!schema) return void 0;
142
- if ("$ref" in schema) {
143
- console.warn(
144
- `[SchemaResolver] Unresolved $ref in schema for ${path3}. Ensure the document is fully dereferenced before passing to SchemaResolver.`
145
- );
152
+ for (const contentType of SUPPORTED_CONTENT_TYPES) {
153
+ const content = response.content?.[contentType];
154
+ if (!content?.schema) continue;
155
+ const schema = content.schema;
156
+ if ("$ref" in schema) {
157
+ console.warn(
158
+ `[SchemaResolver] Unresolved $ref in schema for ${path3}. Ensure the document is fully dereferenced before passing to SchemaResolver.`
159
+ );
160
+ return schema;
161
+ }
146
162
  return schema;
147
163
  }
148
- return schema;
164
+ return void 0;
149
165
  };
150
166
  const preferred = extractSchema(preferredResponseCode);
151
167
  if (preferred) return preferred;
@@ -556,9 +572,12 @@ var CollectionProviderGenerator = class {
556
572
  const resolver = new SchemaResolver(this.document);
557
573
  const schema = resolver.getResponseSchema(endpoint, method);
558
574
  if (!schema) {
559
- throw new Error(`No response schema found for ${method.toUpperCase()} ${endpoint}`);
575
+ console.warn(
576
+ ` > No response schema found for ${method.toUpperCase()} ${endpoint} \u2014 falling back to z.unknown()`
577
+ );
560
578
  }
561
- const isArray = schema.type === "array";
579
+ const isArray = schema != null ? schema.type === "array" : false;
580
+ const unknownFallback = schema == null;
562
581
  const schemaName = `${this.capitalize(collectionName)}Schema`;
563
582
  const params = {
564
583
  providerName,
@@ -569,6 +588,7 @@ var CollectionProviderGenerator = class {
569
588
  baseUrl,
570
589
  schemaName,
571
590
  isArray,
591
+ unknownFallback,
572
592
  bare
573
593
  };
574
594
  if (auth !== void 0) {
@@ -590,13 +610,17 @@ var CollectionProviderGenerator = class {
590
610
  auth,
591
611
  schemaName,
592
612
  isArray,
613
+ unknownFallback,
593
614
  bare
594
615
  } = params;
595
616
  const authHeader = this.generateAuthHeader(auth);
596
617
  const arraySchemaName = `${schemaName.replace(/Schema$/, "")}ArraySchema`;
597
- const validationSchema = isArray ? arraySchemaName : schemaName;
598
- const imports = bare ? "" : `import type { CollectionProvider, CollectionItem } from '@stackwright/collections';
599
- import { ${isArray ? arraySchemaName : schemaName}} } from './schemas';
618
+ const validationSchema = unknownFallback ? "z.unknown()" : isArray ? arraySchemaName : schemaName;
619
+ const imports = bare ? "" : unknownFallback ? `import type { CollectionProvider, CollectionItem } from '@stackwright/collections';
620
+ import { z } from 'zod';
621
+
622
+ ` : `import type { CollectionProvider, CollectionItem } from '@stackwright/collections';
623
+ import { ${isArray ? arraySchemaName : schemaName} } from './schemas';
600
624
 
601
625
  `;
602
626
  return `${imports}/**
@@ -660,7 +684,7 @@ export class ${providerName} implements CollectionProvider {
660
684
  * Get a single item by slug
661
685
  */
662
686
  async get(slug: string): Promise<CollectionItem | null> {
663
- ${this.generateGetMethod(endpoint, slugField, isArray, collectionName, schemaName)}
687
+ ${this.generateGetMethod(endpoint, slugField, isArray, collectionName, schemaName, unknownFallback)}
664
688
  }
665
689
 
666
690
  /**
@@ -696,7 +720,8 @@ export class ${providerName} implements CollectionProvider {
696
720
  /**
697
721
  * Generate get method implementation
698
722
  */
699
- generateGetMethod(endpoint, slugField, isArray, collectionName, schemaName) {
723
+ generateGetMethod(endpoint, slugField, isArray, collectionName, schemaName, unknownFallback = false) {
724
+ const validationExpr = unknownFallback ? "z.unknown()" : schemaName;
700
725
  if (endpoint.includes("{id}") || endpoint.includes(":id")) {
701
726
  const detailEndpoint = endpoint.replace("{id}", "${slug}").replace(":id", "${slug}");
702
727
  return `const url = \`\${this.baseUrl}${detailEndpoint}\`;
@@ -714,7 +739,7 @@ export class ${providerName} implements CollectionProvider {
714
739
  }
715
740
 
716
741
  const data = await response.json();
717
- const validated = ${schemaName}.parse(data);
742
+ const validated = ${validationExpr}.parse(data);
718
743
 
719
744
  return this.toCollectionItem(validated);`;
720
745
  } else {