ty-fetch 0.0.2-beta.5 → 0.0.2-beta.6
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/generate-types.d.ts +1 -0
- package/dist/generate-types.js +25 -2
- package/package.json +1 -1
package/dist/generate-types.d.ts
CHANGED
package/dist/generate-types.js
CHANGED
|
@@ -81,9 +81,10 @@ function generateDtsContent(domainSpecs) {
|
|
|
81
81
|
if (!operation?.responses)
|
|
82
82
|
continue;
|
|
83
83
|
const successResp = operation.responses["200"] ?? operation.responses["201"];
|
|
84
|
-
|
|
84
|
+
const jsonContent = successResp?.content?.["application/json"];
|
|
85
|
+
if (!jsonContent?.schema && !jsonContent?.example)
|
|
85
86
|
continue;
|
|
86
|
-
const schema =
|
|
87
|
+
const schema = jsonContent.schema ?? inferSchemaFromExample(jsonContent.example);
|
|
87
88
|
const fullUrl = `${baseUrl}${basePath}${path}`;
|
|
88
89
|
const baseName = sanitizeTypeName(domain, path, method);
|
|
89
90
|
const count = typeNameCounter.get(baseName) ?? 0;
|
|
@@ -157,6 +158,28 @@ function resolveRef(spec, ref) {
|
|
|
157
158
|
return undefined;
|
|
158
159
|
return spec.components?.schemas?.[match[1]];
|
|
159
160
|
}
|
|
161
|
+
/** Infer an OpenAPI schema from a JSON example value. */
|
|
162
|
+
function inferSchemaFromExample(example) {
|
|
163
|
+
if (example === null || example === undefined)
|
|
164
|
+
return { type: "string" };
|
|
165
|
+
if (typeof example === "string")
|
|
166
|
+
return { type: "string" };
|
|
167
|
+
if (typeof example === "number")
|
|
168
|
+
return { type: "number" };
|
|
169
|
+
if (typeof example === "boolean")
|
|
170
|
+
return { type: "boolean" };
|
|
171
|
+
if (Array.isArray(example)) {
|
|
172
|
+
return { type: "array", items: example.length > 0 ? inferSchemaFromExample(example[0]) : { type: "string" } };
|
|
173
|
+
}
|
|
174
|
+
if (typeof example === "object") {
|
|
175
|
+
const properties = {};
|
|
176
|
+
for (const [key, value] of Object.entries(example)) {
|
|
177
|
+
properties[key] = inferSchemaFromExample(value);
|
|
178
|
+
}
|
|
179
|
+
return { type: "object", properties };
|
|
180
|
+
}
|
|
181
|
+
return { type: "string" };
|
|
182
|
+
}
|
|
160
183
|
function schemaToType(schema, resolver, depth) {
|
|
161
184
|
// Depth limit to avoid huge nested types
|
|
162
185
|
if (depth > 4)
|