@7nohe/openapi-react-query-codegen 3.2.1 → 4.0.0-beta.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.
Files changed (49) hide show
  1. package/AGENTS.md +22 -19
  2. package/README.md +3 -3
  3. package/dist/cli.d.mts +6 -1
  4. package/dist/client/emitSchemas.d.mts +9 -0
  5. package/dist/client/emitSchemas.mjs +130 -0
  6. package/dist/client/emitSdk.d.mts +18 -0
  7. package/dist/client/emitSdk.mjs +253 -0
  8. package/dist/client/emitTransformers.d.mts +19 -0
  9. package/dist/client/emitTransformers.mjs +556 -0
  10. package/dist/client/emitTypes.d.mts +16 -0
  11. package/dist/client/emitTypes.mjs +204 -0
  12. package/dist/client/index.d.mts +44 -0
  13. package/dist/client/index.mjs +126 -0
  14. package/dist/client/naming.d.mts +41 -0
  15. package/dist/client/naming.mjs +175 -0
  16. package/dist/client/operations.d.mts +103 -0
  17. package/dist/client/operations.mjs +352 -0
  18. package/dist/client/runtime/axios.runtime.ts +706 -0
  19. package/dist/client/runtime/fetch.runtime.ts +795 -0
  20. package/dist/client/schema.d.mts +50 -0
  21. package/dist/client/schema.mjs +648 -0
  22. package/dist/client/spec.d.mts +40 -0
  23. package/dist/client/spec.mjs +387 -0
  24. package/dist/client/tsconfig.d.mts +13 -0
  25. package/dist/client/tsconfig.mjs +56 -0
  26. package/dist/common.d.mts +0 -38
  27. package/dist/common.mjs +0 -87
  28. package/dist/constants.d.mts +0 -2
  29. package/dist/constants.mjs +0 -2
  30. package/dist/createSource.d.mts +8 -7
  31. package/dist/createSource.mjs +14 -17
  32. package/dist/format.mjs +0 -1
  33. package/dist/generate.d.mts +9 -9
  34. package/dist/generate.mjs +45 -107
  35. package/dist/runCli.mjs +7 -2
  36. package/dist/tsmorph/buildQueryHooks.mjs +19 -11
  37. package/dist/tsmorph/generateFiles.mjs +105 -161
  38. package/dist/tsmorph/operationNames.d.mts +2 -13
  39. package/dist/tsmorph/operationNames.mjs +4 -24
  40. package/dist/tsmorph/projectFactory.d.mts +7 -2
  41. package/dist/tsmorph/projectFactory.mjs +16 -9
  42. package/dist/types.d.mts +14 -26
  43. package/package.json +6 -6
  44. package/dist/parseOperations.d.mts +0 -10
  45. package/dist/parseOperations.mjs +0 -164
  46. package/dist/service.d.mts +0 -8
  47. package/dist/service.mjs +0 -98
  48. package/dist/util.d.mts +0 -2
  49. package/dist/util.mjs +0 -16
@@ -0,0 +1,556 @@
1
+ import { HEADER } from "./emitSdk.mjs";
2
+ import { isValidIdentifier, uniqueName } from "./naming.mjs";
3
+ import { classifyResponses } from "./operations.mjs";
4
+ import { componentSchemaKey, discriminatorValues, isDateString, } from "./schema.mjs";
5
+ import { getByPointer, refSiblingSchema } from "./spec.mjs";
6
+ const ALL_KINDS = ["string", "object", "array", "other"];
7
+ const ACTED_KINDS = ["string", "object", "array"];
8
+ function kindOfType(type) {
9
+ return type === "string" || type === "object" || type === "array"
10
+ ? type
11
+ : "other";
12
+ }
13
+ function kindOfValue(value) {
14
+ if (typeof value === "string")
15
+ return "string";
16
+ if (Array.isArray(value))
17
+ return "array";
18
+ return typeof value === "object" && value !== null ? "object" : "other";
19
+ }
20
+ /** The shapes a value of the schema can have. Unknown means any of them. */
21
+ function kindsOf(ctx, schema, depth = 0) {
22
+ if (schema === false)
23
+ return new Set();
24
+ if (!schema || typeof schema !== "object" || depth > 16) {
25
+ return new Set(ALL_KINDS);
26
+ }
27
+ if (typeof schema.$ref === "string") {
28
+ return kindsOf(ctx, getByPointer(ctx.doc, schema.$ref), depth + 1);
29
+ }
30
+ if (schema.const !== undefined)
31
+ return new Set([kindOfValue(schema.const)]);
32
+ if (Array.isArray(schema.enum)) {
33
+ return new Set(schema.enum.map(kindOfValue));
34
+ }
35
+ let kinds;
36
+ if (Array.isArray(schema.type)) {
37
+ kinds = new Set(schema.type.map(kindOfType));
38
+ }
39
+ else if (typeof schema.type === "string") {
40
+ kinds = new Set([kindOfType(schema.type)]);
41
+ }
42
+ else if (schema.properties ||
43
+ schema.patternProperties ||
44
+ schema.additionalProperties !== undefined) {
45
+ kinds = new Set(["object"]);
46
+ }
47
+ else if (schema.items || schema.prefixItems) {
48
+ kinds = new Set(["array"]);
49
+ }
50
+ else {
51
+ kinds = new Set(ALL_KINDS);
52
+ }
53
+ const narrow = (other) => {
54
+ kinds = new Set([...kinds].filter((kind) => other.has(kind)));
55
+ };
56
+ for (const member of Array.isArray(schema.allOf) ? schema.allOf : []) {
57
+ narrow(kindsOf(ctx, member, depth + 1));
58
+ }
59
+ for (const key of ["oneOf", "anyOf"]) {
60
+ if (!Array.isArray(schema[key]) || schema[key].length === 0)
61
+ continue;
62
+ narrow(new Set(schema[key].flatMap((member) => [
63
+ ...kindsOf(ctx, member, depth + 1),
64
+ ])));
65
+ }
66
+ return kinds;
67
+ }
68
+ function all(steps) {
69
+ const present = steps.filter((step) => step !== undefined);
70
+ if (present.length === 0)
71
+ return undefined;
72
+ return present.length === 1 ? present[0] : { kind: "all", steps: present };
73
+ }
74
+ /**
75
+ * Build the plan for a schema, following the same reading of it as the types
76
+ * do. A component is only referred to; unions are told apart later.
77
+ */
78
+ function buildPlan(schema, ctx, where, seen = new Set()) {
79
+ if (!schema || typeof schema !== "object")
80
+ return undefined;
81
+ if (typeof schema.$ref === "string") {
82
+ const siblings = refSiblingSchema(ctx.doc, schema);
83
+ if (siblings) {
84
+ return buildPlan({ allOf: [{ $ref: schema.$ref }, siblings] }, ctx, where, seen);
85
+ }
86
+ const key = componentSchemaKey(schema.$ref);
87
+ if (key !== undefined) {
88
+ return Object.hasOwn(ctx.doc.components?.schemas ?? {}, key)
89
+ ? { kind: "ref", key }
90
+ : undefined;
91
+ }
92
+ if (seen.has(schema.$ref))
93
+ return undefined;
94
+ return buildPlan(getByPointer(ctx.doc, schema.$ref), ctx, where, new Set(seen).add(schema.$ref));
95
+ }
96
+ if (schema.const !== undefined || Array.isArray(schema.enum)) {
97
+ return undefined;
98
+ }
99
+ const hasObjectKeywords = schema.properties ||
100
+ schema.patternProperties ||
101
+ schema.additionalProperties;
102
+ const allOf = Array.isArray(schema.allOf) && schema.allOf.length > 0 ? schema.allOf : [];
103
+ const unionKeys = ["oneOf", "anyOf"].filter((key) => Array.isArray(schema[key]) && schema[key].length > 0);
104
+ if (allOf.length > 0 || unionKeys.length > 0) {
105
+ return all([
106
+ ...allOf.map((member, index) => buildPlan(member, ctx, `${where}/allOf/${index}`, seen)),
107
+ ...unionKeys.map((key) => buildUnion(schema[key], schema.discriminator, ctx, `${where}/${key}`, seen)),
108
+ hasObjectKeywords ? buildObject(schema, ctx, where, seen) : undefined,
109
+ ]);
110
+ }
111
+ if (Array.isArray(schema.type)) {
112
+ return all(schema.type
113
+ .filter((type) => type !== "null")
114
+ .map((type) => buildPlan({ ...schema, type }, ctx, where, seen)));
115
+ }
116
+ switch (schema.type) {
117
+ case "string":
118
+ return isDateString(schema) ? { kind: "date" } : undefined;
119
+ case "array":
120
+ return buildArray(schema, ctx, where, seen);
121
+ case "object":
122
+ return buildObject(schema, ctx, where, seen);
123
+ case undefined:
124
+ if (hasObjectKeywords)
125
+ return buildObject(schema, ctx, where, seen);
126
+ return schema.items || schema.prefixItems
127
+ ? buildArray(schema, ctx, where, seen)
128
+ : undefined;
129
+ default:
130
+ return undefined;
131
+ }
132
+ }
133
+ function buildArray(schema, ctx, where, seen) {
134
+ const prefix = Array.isArray(schema.prefixItems)
135
+ ? schema.prefixItems.map((item, index) => buildPlan(item, ctx, `${where}/prefixItems/${index}`, seen))
136
+ : [];
137
+ const items = buildPlan(schema.items, ctx, `${where}/items`, seen);
138
+ return items || prefix.some((plan) => plan !== undefined)
139
+ ? { kind: "array", prefix, items }
140
+ : undefined;
141
+ }
142
+ function isUsablePattern(pattern) {
143
+ try {
144
+ new RegExp(pattern);
145
+ return true;
146
+ }
147
+ catch {
148
+ return false;
149
+ }
150
+ }
151
+ function buildObject(schema, ctx, where, seen) {
152
+ const declared = Object.keys(schema.properties ?? {});
153
+ const properties = [];
154
+ for (const [name, value] of Object.entries((schema.properties ?? {}))) {
155
+ const plan = buildPlan(value, ctx, `${where}/properties/${name}`, seen);
156
+ if (plan)
157
+ properties.push([name, plan]);
158
+ }
159
+ const patterns = Object.entries((schema.patternProperties ?? {}))
160
+ .filter(([pattern]) => isUsablePattern(pattern))
161
+ .map(([pattern, value]) => [
162
+ pattern,
163
+ buildPlan(value, ctx, `${where}/patternProperties/${pattern}`, seen),
164
+ ]);
165
+ const additional = schema.additionalProperties &&
166
+ typeof schema.additionalProperties === "object"
167
+ ? buildPlan(schema.additionalProperties, ctx, `${where}/additionalProperties`, seen)
168
+ : undefined;
169
+ if (properties.length === 0 &&
170
+ additional === undefined &&
171
+ patterns.every(([, plan]) => plan === undefined)) {
172
+ return undefined;
173
+ }
174
+ return { kind: "object", properties, declared, patterns, additional };
175
+ }
176
+ function buildUnion(options, discriminator, ctx, where, seen) {
177
+ const property = typeof discriminator?.propertyName === "string"
178
+ ? discriminator.propertyName
179
+ : undefined;
180
+ const distinct = new Map();
181
+ for (const option of options)
182
+ distinct.set(JSON.stringify(option), option);
183
+ const members = [...distinct.values()].map((member, index) => ({
184
+ kinds: kindsOf(ctx, member),
185
+ plan: buildPlan(member, ctx, `${where}/${index}`, seen),
186
+ values: property
187
+ ? discriminatorValues(member, discriminator, ctx)
188
+ : undefined,
189
+ }));
190
+ return members.some((member) => member.plan !== undefined)
191
+ ? { kind: "union", members, property, where }
192
+ : undefined;
193
+ }
194
+ /** Rebuild a plan with each part replaced by what `visit` makes of it. */
195
+ function mapPlan(plan, visit) {
196
+ if (plan === undefined)
197
+ return undefined;
198
+ const map = (part) => mapPlan(part, visit);
199
+ switch (plan.kind) {
200
+ case "date":
201
+ case "ref":
202
+ return visit(plan);
203
+ case "array": {
204
+ const prefix = plan.prefix.map(map);
205
+ const items = map(plan.items);
206
+ return items || prefix.some((part) => part !== undefined)
207
+ ? visit({ kind: "array", prefix, items })
208
+ : undefined;
209
+ }
210
+ case "object": {
211
+ const properties = plan.properties.flatMap(([name, part]) => {
212
+ const mapped = map(part);
213
+ return mapped ? [[name, mapped]] : [];
214
+ });
215
+ const patterns = plan.patterns.map(([pattern, part]) => [pattern, map(part)]);
216
+ const additional = map(plan.additional);
217
+ return properties.length > 0 ||
218
+ additional !== undefined ||
219
+ patterns.some(([, part]) => part !== undefined)
220
+ ? visit({ ...plan, properties, patterns, additional })
221
+ : undefined;
222
+ }
223
+ case "all": {
224
+ const steps = plan.steps.map(map).filter((step) => step !== undefined);
225
+ if (steps.length < 2)
226
+ return steps[0];
227
+ return visit({ kind: "all", steps });
228
+ }
229
+ case "union": {
230
+ const members = plan.members.map((member) => ({
231
+ ...member,
232
+ plan: map(member.plan),
233
+ }));
234
+ return members.some((member) => member.plan !== undefined)
235
+ ? visit({ ...plan, members })
236
+ : undefined;
237
+ }
238
+ case "tagged": {
239
+ const cases = plan.cases.flatMap((entry) => {
240
+ const mapped = map(entry.plan);
241
+ return mapped ? [{ values: entry.values, plan: mapped }] : [];
242
+ });
243
+ return cases.length > 0 ? visit({ ...plan, cases }) : undefined;
244
+ }
245
+ }
246
+ }
247
+ /** The components a plan refers to, and whether it holds a date of its own. */
248
+ function inspect(plan) {
249
+ const refs = [];
250
+ let dates = false;
251
+ mapPlan(plan, (part) => {
252
+ if (part.kind === "ref")
253
+ refs.push(part.key);
254
+ if (part.kind === "date")
255
+ dates = true;
256
+ return part;
257
+ });
258
+ return { refs, dates };
259
+ }
260
+ /** The components whose plan reaches a date, directly or through others. */
261
+ function liveKeys(plans) {
262
+ const dependents = new Map();
263
+ const queue = [];
264
+ for (const [key, plan] of plans) {
265
+ const { refs, dates } = inspect(plan);
266
+ if (dates)
267
+ queue.push(key);
268
+ for (const ref of refs) {
269
+ dependents.set(ref, [...(dependents.get(ref) ?? []), key]);
270
+ }
271
+ }
272
+ const live = new Set();
273
+ while (queue.length > 0) {
274
+ const key = queue.pop();
275
+ if (live.has(key))
276
+ continue;
277
+ live.add(key);
278
+ queue.push(...(dependents.get(key) ?? []));
279
+ }
280
+ return live;
281
+ }
282
+ function dropDead(plan, live) {
283
+ return mapPlan(plan, (part) => part.kind === "ref" && !live.has(part.key) ? undefined : part);
284
+ }
285
+ /** The shapes of the value itself, not of what is inside it, a plan changes. */
286
+ function actsOf(plan, byKey) {
287
+ switch (plan.kind) {
288
+ case "date":
289
+ return new Set(["string"]);
290
+ case "array":
291
+ return new Set(["array"]);
292
+ case "object":
293
+ case "tagged":
294
+ return new Set(["object"]);
295
+ case "ref":
296
+ return byKey.get(plan.key) ?? new Set();
297
+ case "all":
298
+ return new Set(plan.steps.flatMap((step) => [...actsOf(step, byKey)]));
299
+ case "union":
300
+ return new Set(plan.members.flatMap((member) => member.plan ? [...actsOf(member.plan, byKey)] : []));
301
+ }
302
+ }
303
+ function actsByKey(plans) {
304
+ const byKey = new Map();
305
+ let changed = true;
306
+ while (changed) {
307
+ changed = false;
308
+ for (const [key, plan] of plans) {
309
+ if (!plan)
310
+ continue;
311
+ const acts = actsOf(plan, byKey);
312
+ if (acts.size !== (byKey.get(key)?.size ?? 0)) {
313
+ byKey.set(key, acts);
314
+ changed = true;
315
+ }
316
+ }
317
+ }
318
+ return byKey;
319
+ }
320
+ /** Keep the parts of a plan that only change values of the allowed shapes. */
321
+ function restrict(plan, allowed, byKey) {
322
+ if (plan.kind === "all") {
323
+ return all(plan.steps.map((step) => restrict(step, allowed, byKey)));
324
+ }
325
+ return [...actsOf(plan, byKey)].every((kind) => allowed.has(kind))
326
+ ? plan
327
+ : undefined;
328
+ }
329
+ /**
330
+ * Tell the members of every union apart. A step only changes the shape it is
331
+ * made for, so members of different shapes simply follow each other. Two
332
+ * members of the same shape need a discriminator, and without one their dates
333
+ * are left alone.
334
+ */
335
+ function resolveUnions(plan, byKey, skipped) {
336
+ return mapPlan(plan, (part) => {
337
+ if (part.kind !== "union")
338
+ return part;
339
+ const allowed = part.members.map(() => new Set(ACTED_KINDS));
340
+ let tagged;
341
+ for (const kind of ACTED_KINDS) {
342
+ const candidates = part.members.flatMap((member, index) => member.kinds.has(kind) ? [index] : []);
343
+ const actors = candidates.filter((index) => {
344
+ const memberPlan = part.members[index].plan;
345
+ return memberPlan !== undefined && actsOf(memberPlan, byKey).has(kind);
346
+ });
347
+ if (actors.length === 0 || candidates.length === 1)
348
+ continue;
349
+ for (const index of actors)
350
+ allowed[index].delete(kind);
351
+ const told = kind === "object" &&
352
+ part.property !== undefined &&
353
+ candidates.every((index) => part.members[index].values !== undefined);
354
+ if (told) {
355
+ const only = new Set(["object"]);
356
+ tagged = {
357
+ kind: "tagged",
358
+ property: part.property,
359
+ cases: actors.flatMap((index) => {
360
+ const member = part.members[index];
361
+ const memberPlan = restrict(member.plan, only, byKey);
362
+ return memberPlan
363
+ ? [{ values: member.values, plan: memberPlan }]
364
+ : [];
365
+ }),
366
+ };
367
+ if (tagged.cases.length === 0)
368
+ tagged = undefined;
369
+ }
370
+ else if (kind !== "string") {
371
+ // A string next to a date is typed `string | Date`, which holds
372
+ // either way. An object or an array is typed as holding `Date`s.
373
+ skipped.add(part.where);
374
+ }
375
+ }
376
+ return all([
377
+ ...part.members.map((member, index) => member.plan ? restrict(member.plan, allowed[index], byKey) : undefined),
378
+ tagged,
379
+ ]);
380
+ });
381
+ }
382
+ function access(target, name) {
383
+ return isValidIdentifier(name)
384
+ ? `${target}.${name}`
385
+ : `${target}[${JSON.stringify(name)}]`;
386
+ }
387
+ function suffix(depth) {
388
+ return depth === 0 ? "" : String(depth + 1);
389
+ }
390
+ /** Statements that turn the dates in `target` into `Date` objects in place. */
391
+ function emitPlan(plan, target, indent, depth, functionNames) {
392
+ const inner = `${indent} `;
393
+ const emit = (part, on, at = inner, level = depth) => emitPlan(part, on, at, level, functionNames);
394
+ switch (plan.kind) {
395
+ case "date":
396
+ return [
397
+ `${indent}if (typeof ${target} === "string") ${target} = new Date(${target});`,
398
+ ];
399
+ case "ref": {
400
+ const call = `${target} = ${functionNames.get(plan.key)}(${target});`;
401
+ // Assigning to a property that is not there would add it.
402
+ return [
403
+ target === "data"
404
+ ? `${indent}${call}`
405
+ : `${indent}if (${target} !== undefined) ${call}`,
406
+ ];
407
+ }
408
+ case "all":
409
+ return plan.steps.flatMap((step) => emit(step, target, indent));
410
+ case "array": {
411
+ const lines = [`${indent}if (Array.isArray(${target})) {`];
412
+ plan.prefix.forEach((part, index) => {
413
+ if (part)
414
+ lines.push(...emit(part, `${target}[${index}]`));
415
+ });
416
+ if (plan.items) {
417
+ const index = `i${suffix(depth)}`;
418
+ lines.push(`${inner}for (let ${index} = ${plan.prefix.length}; ${index} < ${target}.length; ${index}++) {`, ...emit(plan.items, `${target}[${index}]`, `${inner} `, depth + 1), `${inner}}`);
419
+ }
420
+ lines.push(`${indent}}`);
421
+ return lines;
422
+ }
423
+ case "object": {
424
+ const lines = [
425
+ `${indent}if (typeof ${target} === "object" && ${target} !== null && !Array.isArray(${target})) {`,
426
+ ];
427
+ for (const [name, part] of plan.properties) {
428
+ lines.push(...emit(part, access(target, name)));
429
+ }
430
+ const patterned = plan.patterns.filter(([, part]) => part !== undefined);
431
+ if (plan.additional || patterned.length > 0) {
432
+ const key = `key${suffix(depth)}`;
433
+ const body = `${inner} `;
434
+ const test = (pattern) => `new RegExp(${JSON.stringify(pattern)}).test(${key})`;
435
+ lines.push(`${inner}for (const ${key} of Object.keys(${target})) {`);
436
+ if (plan.declared.length > 0) {
437
+ const names = plan.declared.map((name) => JSON.stringify(name));
438
+ lines.push(`${body}if ([${names.join(", ")}].includes(${key})) continue;`);
439
+ }
440
+ for (const [pattern, part] of patterned) {
441
+ lines.push(`${body}if (${test(pattern)}) {`, ...emit(part, `${target}[${key}]`, `${body} `, depth + 1), `${body}}`);
442
+ }
443
+ if (plan.additional) {
444
+ // `additionalProperties` describes the keys no pattern matches.
445
+ const unmatched = plan.patterns
446
+ .map(([pattern]) => `!${test(pattern)}`)
447
+ .join(" && ");
448
+ const at = unmatched ? `${body} ` : body;
449
+ if (unmatched)
450
+ lines.push(`${body}if (${unmatched}) {`);
451
+ lines.push(...emit(plan.additional, `${target}[${key}]`, at, depth + 1));
452
+ if (unmatched)
453
+ lines.push(`${body}}`);
454
+ }
455
+ lines.push(`${inner}}`);
456
+ }
457
+ lines.push(`${indent}}`);
458
+ return lines;
459
+ }
460
+ case "tagged": {
461
+ const lines = [
462
+ `${indent}if (typeof ${target} === "object" && ${target} !== null) {`,
463
+ ];
464
+ plan.cases.forEach((entry, index) => {
465
+ const tests = entry.values
466
+ .map((value) => `${access(target, plan.property)} === ${JSON.stringify(value)}`)
467
+ .join(" || ");
468
+ lines.push(`${inner}${index === 0 ? "" : "} else "}if (${tests}) {`, ...emit(entry.plan, target, `${inner} `));
469
+ });
470
+ lines.push(`${inner}}`, `${indent}}`);
471
+ return lines;
472
+ }
473
+ case "union":
474
+ // Resolved before anything is emitted.
475
+ return [];
476
+ }
477
+ }
478
+ function lowerFirst(name) {
479
+ return name.charAt(0).toLowerCase() + name.slice(1);
480
+ }
481
+ /**
482
+ * Generate `transformers.gen.ts` for `--useDateType`: a function per operation
483
+ * that turns the `date` and `date-time` strings of its successful response
484
+ * into `Date` objects, as the generated types say they are.
485
+ */
486
+ export function emitTransformers(operations, ctx) {
487
+ const schemas = (ctx.doc.components?.schemas ?? {});
488
+ let components = new Map(Object.entries(schemas).map(([key, schema]) => [
489
+ key,
490
+ buildPlan(schema, ctx, `#/components/schemas/${key}`),
491
+ ]));
492
+ let responses = new Map();
493
+ for (const operation of operations) {
494
+ const bodies = classifyResponses(operation)
495
+ .responses.map((response) => response.schema)
496
+ .filter((schema) => schema !== undefined);
497
+ const where = `${operation.method.toUpperCase()} ${operation.path}`;
498
+ responses.set(operation.functionName,
499
+ // Which of several successful responses arrived is not known here.
500
+ buildUnion(bodies, undefined, ctx, `${where} responses`, new Set()));
501
+ }
502
+ const prune = () => {
503
+ const live = liveKeys(components);
504
+ const keep = (plan) => dropDead(plan, live);
505
+ components = new Map([...components].map(([k, plan]) => [k, keep(plan)]));
506
+ responses = new Map([...responses].map(([k, plan]) => [k, keep(plan)]));
507
+ };
508
+ prune();
509
+ const byKey = actsByKey(components);
510
+ const skipped = new Set();
511
+ const resolve = (plan) => resolveUnions(plan, byKey, skipped);
512
+ components = new Map([...components].map(([k, plan]) => [k, resolve(plan)]));
513
+ responses = new Map([...responses].map(([k, plan]) => [k, resolve(plan)]));
514
+ // Dropping an ambiguous union can leave a component with nothing to do.
515
+ prune();
516
+ const used = new Set();
517
+ const byOperation = new Map();
518
+ for (const [name, plan] of responses) {
519
+ if (plan) {
520
+ byOperation.set(name, uniqueName(`${name}ResponseTransformer`, used));
521
+ }
522
+ }
523
+ if (byOperation.size === 0) {
524
+ return { content: undefined, byOperation, skipped: [...skipped] };
525
+ }
526
+ // Only what a response reaches, so that nothing is left unused.
527
+ const reached = new Set();
528
+ const queue = [...responses.values()].flatMap((plan) => inspect(plan).refs);
529
+ while (queue.length > 0) {
530
+ const key = queue.pop();
531
+ if (reached.has(key))
532
+ continue;
533
+ reached.add(key);
534
+ queue.push(...inspect(components.get(key)).refs);
535
+ }
536
+ const functionNames = new Map();
537
+ for (const key of Object.keys(schemas)) {
538
+ if (!reached.has(key))
539
+ continue;
540
+ const typeName = ctx.names.get(key);
541
+ functionNames.set(key, uniqueName(`${lowerFirst(typeName)}SchemaResponseTransformer`, used));
542
+ }
543
+ const body = (plan) => [...emitPlan(plan, "data", " ", 0, functionNames), " return data;"].join("\n");
544
+ const blocks = [];
545
+ for (const [key, name] of functionNames) {
546
+ blocks.push(`const ${name} = (data: any): any => {\n${body(components.get(key))}\n};`);
547
+ }
548
+ for (const [operation, name] of byOperation) {
549
+ blocks.push(`export const ${name} = (data: any): any => {\n${body(responses.get(operation))}\n};`);
550
+ }
551
+ return {
552
+ content: `${HEADER}\n${blocks.join("\n\n")}\n`,
553
+ byOperation,
554
+ skipped: [...skipped],
555
+ };
556
+ }
@@ -0,0 +1,16 @@
1
+ import { type ClientKind } from "./emitSdk.mjs";
2
+ import { type OperationIR } from "./operations.mjs";
3
+ import { type SchemaCtx } from "./schema.mjs";
4
+ import type { Doc } from "./spec.mjs";
5
+ export type EnumsMode = "javascript" | "typescript" | false | undefined;
6
+ export type EmittedTypes = {
7
+ content: string;
8
+ /** Every identifier exported from the generated file. */
9
+ exportedNames: string[];
10
+ };
11
+ /** First server URL with its variables replaced by their defaults. */
12
+ export declare function getBaseUrl(doc: Doc): string | undefined;
13
+ export declare function emitTypes(doc: Doc, operations: OperationIR[], ctx: SchemaCtx, options: {
14
+ enums?: EnumsMode;
15
+ clientKind: ClientKind;
16
+ }): EmittedTypes;