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.
@@ -20,6 +20,7 @@ interface OpenAPIOperation {
20
20
  responses?: Record<string, {
21
21
  content?: Record<string, {
22
22
  schema?: OpenAPISchema;
23
+ example?: unknown;
23
24
  }>;
24
25
  description?: string;
25
26
  }>;
@@ -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
- if (!successResp?.content?.["application/json"]?.schema)
84
+ const jsonContent = successResp?.content?.["application/json"];
85
+ if (!jsonContent?.schema && !jsonContent?.example)
85
86
  continue;
86
- const schema = successResp.content["application/json"].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)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ty-fetch",
3
- "version": "0.0.2-beta.5",
3
+ "version": "0.0.2-beta.6",
4
4
  "main": "index.js",
5
5
  "types": "index.d.ts",
6
6
  "exports": {