@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 +41 -16
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +41 -16
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -92,6 +92,19 @@ var OpenAPIParser = class {
|
|
|
92
92
|
};
|
|
93
93
|
|
|
94
94
|
// src/parser/SchemaResolver.ts
|
|
95
|
+
var SUPPORTED_CONTENT_TYPES = [
|
|
96
|
+
"application/json",
|
|
97
|
+
"application/geo+json",
|
|
98
|
+
// GeoJSON (ORS, mapping APIs)
|
|
99
|
+
"application/vnd.api+json",
|
|
100
|
+
// JSON:API
|
|
101
|
+
"application/ld+json",
|
|
102
|
+
// JSON-LD
|
|
103
|
+
"application/problem+json",
|
|
104
|
+
// RFC 7807 problem details
|
|
105
|
+
"*/*"
|
|
106
|
+
// wildcard fallback
|
|
107
|
+
];
|
|
95
108
|
var SchemaResolver = class {
|
|
96
109
|
/**
|
|
97
110
|
* @param document - Fully dereferenced OpenAPI document. Pass the result of
|
|
@@ -124,16 +137,19 @@ var SchemaResolver = class {
|
|
|
124
137
|
const extractSchema = (responseCode) => {
|
|
125
138
|
const response = responses[responseCode];
|
|
126
139
|
if (!response) return void 0;
|
|
127
|
-
const
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
140
|
+
for (const contentType of SUPPORTED_CONTENT_TYPES) {
|
|
141
|
+
const content = response.content?.[contentType];
|
|
142
|
+
if (!content?.schema) continue;
|
|
143
|
+
const schema = content.schema;
|
|
144
|
+
if ("$ref" in schema) {
|
|
145
|
+
console.warn(
|
|
146
|
+
`[SchemaResolver] Unresolved $ref in schema for ${path3}. Ensure the document is fully dereferenced before passing to SchemaResolver.`
|
|
147
|
+
);
|
|
148
|
+
return schema;
|
|
149
|
+
}
|
|
134
150
|
return schema;
|
|
135
151
|
}
|
|
136
|
-
return
|
|
152
|
+
return void 0;
|
|
137
153
|
};
|
|
138
154
|
const preferred = extractSchema(preferredResponseCode);
|
|
139
155
|
if (preferred) return preferred;
|
|
@@ -544,9 +560,12 @@ var CollectionProviderGenerator = class {
|
|
|
544
560
|
const resolver = new SchemaResolver(this.document);
|
|
545
561
|
const schema = resolver.getResponseSchema(endpoint, method);
|
|
546
562
|
if (!schema) {
|
|
547
|
-
|
|
563
|
+
console.warn(
|
|
564
|
+
` > No response schema found for ${method.toUpperCase()} ${endpoint} \u2014 falling back to z.unknown()`
|
|
565
|
+
);
|
|
548
566
|
}
|
|
549
|
-
const isArray = schema.type === "array";
|
|
567
|
+
const isArray = schema != null ? schema.type === "array" : false;
|
|
568
|
+
const unknownFallback = schema == null;
|
|
550
569
|
const schemaName = `${this.capitalize(collectionName)}Schema`;
|
|
551
570
|
const params = {
|
|
552
571
|
providerName,
|
|
@@ -557,6 +576,7 @@ var CollectionProviderGenerator = class {
|
|
|
557
576
|
baseUrl,
|
|
558
577
|
schemaName,
|
|
559
578
|
isArray,
|
|
579
|
+
unknownFallback,
|
|
560
580
|
bare
|
|
561
581
|
};
|
|
562
582
|
if (auth !== void 0) {
|
|
@@ -578,13 +598,17 @@ var CollectionProviderGenerator = class {
|
|
|
578
598
|
auth,
|
|
579
599
|
schemaName,
|
|
580
600
|
isArray,
|
|
601
|
+
unknownFallback,
|
|
581
602
|
bare
|
|
582
603
|
} = params;
|
|
583
604
|
const authHeader = this.generateAuthHeader(auth);
|
|
584
605
|
const arraySchemaName = `${schemaName.replace(/Schema$/, "")}ArraySchema`;
|
|
585
|
-
const validationSchema = isArray ? arraySchemaName : schemaName;
|
|
586
|
-
const imports = bare ? "" : `import type { CollectionProvider, CollectionItem } from '@stackwright/collections';
|
|
587
|
-
import {
|
|
606
|
+
const validationSchema = unknownFallback ? "z.unknown()" : isArray ? arraySchemaName : schemaName;
|
|
607
|
+
const imports = bare ? "" : unknownFallback ? `import type { CollectionProvider, CollectionItem } from '@stackwright/collections';
|
|
608
|
+
import { z } from 'zod';
|
|
609
|
+
|
|
610
|
+
` : `import type { CollectionProvider, CollectionItem } from '@stackwright/collections';
|
|
611
|
+
import { ${isArray ? arraySchemaName : schemaName} } from './schemas';
|
|
588
612
|
|
|
589
613
|
`;
|
|
590
614
|
return `${imports}/**
|
|
@@ -648,7 +672,7 @@ export class ${providerName} implements CollectionProvider {
|
|
|
648
672
|
* Get a single item by slug
|
|
649
673
|
*/
|
|
650
674
|
async get(slug: string): Promise<CollectionItem | null> {
|
|
651
|
-
${this.generateGetMethod(endpoint, slugField, isArray, collectionName, schemaName)}
|
|
675
|
+
${this.generateGetMethod(endpoint, slugField, isArray, collectionName, schemaName, unknownFallback)}
|
|
652
676
|
}
|
|
653
677
|
|
|
654
678
|
/**
|
|
@@ -684,7 +708,8 @@ export class ${providerName} implements CollectionProvider {
|
|
|
684
708
|
/**
|
|
685
709
|
* Generate get method implementation
|
|
686
710
|
*/
|
|
687
|
-
generateGetMethod(endpoint, slugField, isArray, collectionName, schemaName) {
|
|
711
|
+
generateGetMethod(endpoint, slugField, isArray, collectionName, schemaName, unknownFallback = false) {
|
|
712
|
+
const validationExpr = unknownFallback ? "z.unknown()" : schemaName;
|
|
688
713
|
if (endpoint.includes("{id}") || endpoint.includes(":id")) {
|
|
689
714
|
const detailEndpoint = endpoint.replace("{id}", "${slug}").replace(":id", "${slug}");
|
|
690
715
|
return `const url = \`\${this.baseUrl}${detailEndpoint}\`;
|
|
@@ -702,7 +727,7 @@ export class ${providerName} implements CollectionProvider {
|
|
|
702
727
|
}
|
|
703
728
|
|
|
704
729
|
const data = await response.json();
|
|
705
|
-
const validated = ${
|
|
730
|
+
const validated = ${validationExpr}.parse(data);
|
|
706
731
|
|
|
707
732
|
return this.toCollectionItem(validated);`;
|
|
708
733
|
} else {
|