@7nohe/openapi-react-query-codegen 3.1.0 → 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 (55) hide show
  1. package/AGENTS.md +207 -0
  2. package/README.md +7 -3
  3. package/dist/check.d.mts +59 -0
  4. package/dist/check.mjs +162 -0
  5. package/dist/cli.d.mts +6 -1
  6. package/dist/cli.mjs +4 -31
  7. package/dist/client/emitSchemas.d.mts +9 -0
  8. package/dist/client/emitSchemas.mjs +130 -0
  9. package/dist/client/emitSdk.d.mts +18 -0
  10. package/dist/client/emitSdk.mjs +253 -0
  11. package/dist/client/emitTransformers.d.mts +19 -0
  12. package/dist/client/emitTransformers.mjs +556 -0
  13. package/dist/client/emitTypes.d.mts +16 -0
  14. package/dist/client/emitTypes.mjs +204 -0
  15. package/dist/client/index.d.mts +44 -0
  16. package/dist/client/index.mjs +126 -0
  17. package/dist/client/naming.d.mts +41 -0
  18. package/dist/client/naming.mjs +175 -0
  19. package/dist/client/operations.d.mts +103 -0
  20. package/dist/client/operations.mjs +352 -0
  21. package/dist/client/runtime/axios.runtime.ts +706 -0
  22. package/dist/client/runtime/fetch.runtime.ts +795 -0
  23. package/dist/client/schema.d.mts +50 -0
  24. package/dist/client/schema.mjs +648 -0
  25. package/dist/client/spec.d.mts +40 -0
  26. package/dist/client/spec.mjs +387 -0
  27. package/dist/client/tsconfig.d.mts +13 -0
  28. package/dist/client/tsconfig.mjs +56 -0
  29. package/dist/common.d.mts +0 -38
  30. package/dist/common.mjs +0 -87
  31. package/dist/constants.d.mts +0 -2
  32. package/dist/constants.mjs +0 -2
  33. package/dist/createSource.d.mts +8 -7
  34. package/dist/createSource.mjs +14 -17
  35. package/dist/errors.d.mts +43 -0
  36. package/dist/errors.mjs +61 -0
  37. package/dist/format.mjs +0 -1
  38. package/dist/generate.d.mts +25 -3
  39. package/dist/generate.mjs +83 -83
  40. package/dist/runCli.d.mts +20 -0
  41. package/dist/runCli.mjs +105 -0
  42. package/dist/tsmorph/buildQueryHooks.mjs +19 -11
  43. package/dist/tsmorph/generateFiles.mjs +105 -161
  44. package/dist/tsmorph/operationNames.d.mts +2 -13
  45. package/dist/tsmorph/operationNames.mjs +4 -24
  46. package/dist/tsmorph/projectFactory.d.mts +7 -2
  47. package/dist/tsmorph/projectFactory.mjs +16 -9
  48. package/dist/types.d.mts +14 -26
  49. package/package.json +8 -8
  50. package/dist/parseOperations.d.mts +0 -10
  51. package/dist/parseOperations.mjs +0 -164
  52. package/dist/service.d.mts +0 -8
  53. package/dist/service.mjs +0 -98
  54. package/dist/util.d.mts +0 -2
  55. package/dist/util.mjs +0 -16
@@ -3,163 +3,80 @@ import { buildDefaultResponseType, buildInfiniteClientOptionsType, buildInfinite
3
3
  import { buildUseMutationHook } from "./buildMutationHooks.mjs";
4
4
  import { buildEnsureQueryDataFn, buildPrefetchFn, buildPrefetchInfiniteQueryFn, buildUseInfiniteQueryHook, buildUseQueryHook, buildUseSuspenseInfiniteQueryHook, buildUseSuspenseQueryHook, } from "./buildQueryHooks.mjs";
5
5
  import { buildInfiniteQueryOptionsFn, buildQueryOptionsFn, } from "./buildQueryOptions.mjs";
6
- import { buildCommonFileImports, buildHookFileImports, buildQueryOptionsFileImports, createGenerationProject, } from "./projectFactory.mjs";
6
+ import { buildCommonFileImports, buildHookFileImports, buildQueryOptionsFileImports, createGenerationProject, relativeModule, } from "./projectFactory.mjs";
7
7
  /**
8
- * Add one variable statement per operation, skipping the ones the builder
8
+ * Build one variable statement per operation, skipping the ones the builder
9
9
  * declines. A builder returns null when the operation is out of its scope —
10
10
  * every infinite-query builder returns null for non-paginatable operations,
11
11
  * which is how the paginatable subset gets selected.
12
12
  */
13
- function addStatements(sourceFile, operations, build) {
13
+ function collectStatements(operations, build) {
14
+ const statements = [];
14
15
  for (const op of operations) {
15
16
  const statement = build(op);
16
17
  if (statement) {
17
- sourceFile.addVariableStatement(statement);
18
+ statements.push(statement);
18
19
  }
19
20
  }
21
+ return statements;
20
22
  }
21
23
  /**
22
- * Generate the index.ts file content.
23
- * The content is constant, so no ts-morph project is needed.
24
+ * ts-morph separates statements of different kinds with a blank line when
25
+ * they are added one at a time but not when they are added as a batch, so
26
+ * restore the separator where the kind changes.
24
27
  */
25
- function generateIndexFile() {
26
- return `export * from "./common";\nexport * from "./queries";\nexport * from "./queryOptions";\n`;
28
+ function separateKinds(statements) {
29
+ return statements.map((statement, index) => index > 0 && statements[index - 1].kind !== statement.kind
30
+ ? { ...statement, leadingTrivia: `\n${statement.leadingTrivia ?? ""}` }
31
+ : statement);
27
32
  }
33
+ const IDENTIFIER_RE = /[\p{ID_Start}_$][\p{ID_Continue}$]*/gu;
28
34
  /**
29
- * Generate the common.ts file content.
35
+ * Create a generated file. Statements go in as one batch so ts-morph reparses
36
+ * the file once, and only the model and service names the body actually
37
+ * mentions are imported: importing thousands of unused names forced every
38
+ * edit to rescan a huge import list and made the later import organising pass
39
+ * slow on large specifications.
30
40
  */
31
- function generateCommonFile(operations, ctx) {
41
+ function createFile(name, ctx, buildImports, statements) {
32
42
  const project = createGenerationProject();
33
- const sourceFile = project.createSourceFile(`${OpenApiRqFiles.common}.ts`, undefined, { overwrite: true });
34
- // Add imports
35
- sourceFile.addImportDeclarations(buildCommonFileImports(ctx));
36
- // Group operations by HTTP method
37
- const getOperations = operations.filter((op) => op.httpMethod === "GET");
38
- const mutationOperations = operations.filter((op) => ["POST", "PUT", "PATCH", "DELETE"].includes(op.httpMethod));
39
- // Add query types and keys
40
- for (const op of getOperations) {
41
- sourceFile.addTypeAlias(buildDefaultResponseType(op));
42
- sourceFile.addTypeAlias(buildQueryResultType(op));
43
- sourceFile.addVariableStatement(buildQueryKeyConst(op));
44
- sourceFile.addVariableStatement(buildQueryKeyFn(op));
45
- }
46
- // Add dedicated infinite query types and keys for paginatable operations
47
- for (const op of getOperations.filter((o) => o.isPaginatable)) {
48
- sourceFile.addTypeAlias(buildInfiniteClientOptionsType(op, ctx));
49
- sourceFile.addVariableStatement(buildInfiniteQueryKeyConst(op));
50
- sourceFile.addVariableStatement(buildInfiniteQueryKeyFn(op));
51
- }
52
- // Add mutation types and keys
53
- for (const op of mutationOperations) {
54
- sourceFile.addTypeAlias(buildMutationResultType(op));
55
- sourceFile.addVariableStatement(buildMutationKeyConst(op));
56
- sourceFile.addVariableStatement(buildMutationKeyFn(op));
57
- }
58
- return sourceFile.getFullText();
59
- }
60
- /**
61
- * Generate the queries.ts file content.
62
- */
63
- function generateQueriesFile(operations, ctx) {
64
- const project = createGenerationProject();
65
- const sourceFile = project.createSourceFile(`${OpenApiRqFiles.queries}.ts`, undefined, { overwrite: true });
66
- // Add imports
67
- sourceFile.addImportDeclarations(buildHookFileImports(ctx));
68
- // Group operations
69
- const getOperations = operations.filter((op) => op.httpMethod === "GET");
70
- const mutationOperations = operations.filter((op) => ["POST", "PUT", "PATCH", "DELETE"].includes(op.httpMethod));
71
- // Add useQuery hooks
72
- for (const op of getOperations) {
73
- sourceFile.addVariableStatement(buildUseQueryHook(op, ctx));
74
- }
75
- // Add useMutation hooks
76
- for (const op of mutationOperations) {
77
- sourceFile.addVariableStatement(buildUseMutationHook(op, ctx));
78
- }
43
+ const sourceFile = project.createSourceFile(`${name}.ts`, undefined, {
44
+ overwrite: true,
45
+ });
46
+ sourceFile.addStatements(statements);
47
+ const used = new Set(sourceFile.getFullText().match(IDENTIFIER_RE));
48
+ sourceFile.insertImportDeclarations(0, buildImports({
49
+ ...ctx,
50
+ modelNames: ctx.modelNames.filter((model) => used.has(model)),
51
+ serviceNames: ctx.serviceNames.filter((service) => used.has(service)),
52
+ }));
79
53
  return sourceFile.getFullText();
80
54
  }
81
55
  /**
82
- * Generate the queryOptions.ts file content.
56
+ * The content of index.ts, which needs no ts-morph project.
83
57
  */
84
- function generateQueryOptionsFile(operations, ctx) {
85
- const project = createGenerationProject();
86
- const sourceFile = project.createSourceFile(`${OpenApiRqFiles.queryOptions}.ts`, undefined, { overwrite: true });
87
- // Add imports
88
- sourceFile.addImportDeclarations(buildQueryOptionsFileImports(ctx));
89
- // Only GET operations have query options
90
- const getOperations = operations.filter((op) => op.httpMethod === "GET");
91
- for (const op of getOperations) {
92
- sourceFile.addVariableStatement(buildQueryOptionsFn(op));
93
- }
94
- // Add infiniteQueryOptions factories
95
- addStatements(sourceFile, getOperations, (op) => buildInfiniteQueryOptionsFn(op, ctx));
96
- return sourceFile.getFullText();
58
+ function indexFile(ctx) {
59
+ return [
60
+ OpenApiRqFiles.common,
61
+ OpenApiRqFiles.queries,
62
+ OpenApiRqFiles.queryOptions,
63
+ ]
64
+ .map((name) => `export * from "${relativeModule(`./${name}`, ctx)}";\n`)
65
+ .join("");
97
66
  }
98
- /**
99
- * Generate the suspense.ts file content.
100
- */
101
- function generateSuspenseFile(operations, ctx) {
102
- const project = createGenerationProject();
103
- const sourceFile = project.createSourceFile(`${OpenApiRqFiles.suspense}.ts`, undefined, { overwrite: true });
104
- // Add imports
105
- sourceFile.addImportDeclarations(buildHookFileImports(ctx));
106
- // Only GET operations for suspense
107
- const getOperations = operations.filter((op) => op.httpMethod === "GET");
108
- // Add useSuspenseQuery hooks
109
- for (const op of getOperations) {
110
- sourceFile.addVariableStatement(buildUseSuspenseQueryHook(op, ctx));
67
+ function commonStatements(queries, mutations, ctx) {
68
+ const statements = [];
69
+ for (const op of queries) {
70
+ statements.push(buildDefaultResponseType(op), buildQueryResultType(op), buildQueryKeyConst(op), buildQueryKeyFn(op));
111
71
  }
112
- // Add useSuspenseInfiniteQuery hooks
113
- addStatements(sourceFile, getOperations, (op) => buildUseSuspenseInfiniteQueryHook(op, ctx));
114
- return sourceFile.getFullText();
115
- }
116
- /**
117
- * Generate the infiniteQueries.ts file content.
118
- */
119
- function generateInfiniteQueriesFile(operations, ctx) {
120
- const project = createGenerationProject();
121
- const sourceFile = project.createSourceFile(`${OpenApiRqFiles.infiniteQueries}.ts`, undefined, { overwrite: true });
122
- // Add imports
123
- sourceFile.addImportDeclarations(buildHookFileImports(ctx));
124
- // Only GET operations can be paginatable
125
- const getOperations = operations.filter((op) => op.httpMethod === "GET");
126
- // Add useInfiniteQuery hooks
127
- addStatements(sourceFile, getOperations, (op) => buildUseInfiniteQueryHook(op, ctx));
128
- return sourceFile.getFullText();
129
- }
130
- /**
131
- * Generate the prefetch.ts file content.
132
- */
133
- function generatePrefetchFile(operations, ctx) {
134
- const project = createGenerationProject();
135
- const sourceFile = project.createSourceFile(`${OpenApiRqFiles.prefetch}.ts`, undefined, { overwrite: true });
136
- // Add imports
137
- sourceFile.addImportDeclarations(buildHookFileImports(ctx));
138
- // Only GET operations for prefetch
139
- const getOperations = operations.filter((op) => op.httpMethod === "GET");
140
- // Add prefetch functions
141
- for (const op of getOperations) {
142
- sourceFile.addVariableStatement(buildPrefetchFn(op));
72
+ // Dedicated infinite query types and keys for paginatable operations
73
+ for (const op of queries.filter((query) => query.isPaginatable)) {
74
+ statements.push(buildInfiniteClientOptionsType(op, ctx), buildInfiniteQueryKeyConst(op), buildInfiniteQueryKeyFn(op));
143
75
  }
144
- // Add prefetchInfiniteQuery functions
145
- addStatements(sourceFile, getOperations, (op) => buildPrefetchInfiniteQueryFn(op, ctx));
146
- return sourceFile.getFullText();
147
- }
148
- /**
149
- * Generate the ensureQueryData.ts file content.
150
- */
151
- function generateEnsureQueryDataFile(operations, ctx) {
152
- const project = createGenerationProject();
153
- const sourceFile = project.createSourceFile(`${OpenApiRqFiles.ensureQueryData}.ts`, undefined, { overwrite: true });
154
- // Add imports
155
- sourceFile.addImportDeclarations(buildHookFileImports(ctx));
156
- // Only GET operations for ensure
157
- const getOperations = operations.filter((op) => op.httpMethod === "GET");
158
- // Add ensureQueryData functions
159
- for (const op of getOperations) {
160
- sourceFile.addVariableStatement(buildEnsureQueryDataFn(op));
76
+ for (const op of mutations) {
77
+ statements.push(buildMutationResultType(op), buildMutationKeyConst(op), buildMutationKeyFn(op));
161
78
  }
162
- return sourceFile.getFullText();
79
+ return separateKinds(statements);
163
80
  }
164
81
  /**
165
82
  * Add the generated header comment to file content.
@@ -172,38 +89,65 @@ function addHeaderComment(content, version) {
172
89
  * Generate all files using ts-morph.
173
90
  */
174
91
  export function generateAllFiles(operations, ctx) {
92
+ const queries = operations.filter((op) => op.httpMethod === "GET");
93
+ const mutations = operations.filter((op) => ["POST", "PUT", "PATCH", "DELETE"].includes(op.httpMethod));
94
+ const files = [
95
+ [
96
+ OpenApiRqFiles.common,
97
+ buildCommonFileImports,
98
+ commonStatements(queries, mutations, ctx),
99
+ ],
100
+ [
101
+ OpenApiRqFiles.queries,
102
+ buildHookFileImports,
103
+ [
104
+ ...queries.map((op) => buildUseQueryHook(op, ctx)),
105
+ ...mutations.map((op) => buildUseMutationHook(op, ctx)),
106
+ ],
107
+ ],
108
+ [
109
+ OpenApiRqFiles.queryOptions,
110
+ buildQueryOptionsFileImports,
111
+ [
112
+ ...queries.map((op) => buildQueryOptionsFn(op)),
113
+ ...collectStatements(queries, (op) => buildInfiniteQueryOptionsFn(op, ctx)),
114
+ ],
115
+ ],
116
+ [
117
+ OpenApiRqFiles.suspense,
118
+ buildHookFileImports,
119
+ [
120
+ ...queries.map((op) => buildUseSuspenseQueryHook(op, ctx)),
121
+ ...collectStatements(queries, (op) => buildUseSuspenseInfiniteQueryHook(op, ctx)),
122
+ ],
123
+ ],
124
+ [
125
+ OpenApiRqFiles.infiniteQueries,
126
+ buildHookFileImports,
127
+ collectStatements(queries, (op) => buildUseInfiniteQueryHook(op, ctx)),
128
+ ],
129
+ [
130
+ OpenApiRqFiles.prefetch,
131
+ buildHookFileImports,
132
+ [
133
+ ...queries.map((op) => buildPrefetchFn(op)),
134
+ ...collectStatements(queries, (op) => buildPrefetchInfiniteQueryFn(op, ctx)),
135
+ ],
136
+ ],
137
+ [
138
+ OpenApiRqFiles.ensureQueryData,
139
+ buildHookFileImports,
140
+ queries.map((op) => buildEnsureQueryDataFn(op)),
141
+ ],
142
+ ];
175
143
  return [
176
144
  {
177
145
  name: `${OpenApiRqFiles.index}.ts`,
178
- content: addHeaderComment(generateIndexFile(), ctx.version),
179
- },
180
- {
181
- name: `${OpenApiRqFiles.common}.ts`,
182
- content: addHeaderComment(generateCommonFile(operations, ctx), ctx.version),
183
- },
184
- {
185
- name: `${OpenApiRqFiles.queries}.ts`,
186
- content: addHeaderComment(generateQueriesFile(operations, ctx), ctx.version),
187
- },
188
- {
189
- name: `${OpenApiRqFiles.queryOptions}.ts`,
190
- content: addHeaderComment(generateQueryOptionsFile(operations, ctx), ctx.version),
191
- },
192
- {
193
- name: `${OpenApiRqFiles.suspense}.ts`,
194
- content: addHeaderComment(generateSuspenseFile(operations, ctx), ctx.version),
195
- },
196
- {
197
- name: `${OpenApiRqFiles.infiniteQueries}.ts`,
198
- content: addHeaderComment(generateInfiniteQueriesFile(operations, ctx), ctx.version),
199
- },
200
- {
201
- name: `${OpenApiRqFiles.prefetch}.ts`,
202
- content: addHeaderComment(generatePrefetchFile(operations, ctx), ctx.version),
203
- },
204
- {
205
- name: `${OpenApiRqFiles.ensureQueryData}.ts`,
206
- content: addHeaderComment(generateEnsureQueryDataFile(operations, ctx), ctx.version),
146
+ content: addHeaderComment(indexFile(ctx), ctx.version),
207
147
  },
148
+ ...files.map(([name, imports, statements]) => ({
149
+ name: `${name}.ts`,
150
+ content: addHeaderComment(createFile(name, ctx, imports, statements), ctx.version),
151
+ })),
208
152
  ];
209
153
  }
@@ -1,16 +1,5 @@
1
1
  import type { GenerationContext, OperationInfo } from "../types.mjs";
2
- /**
3
- * Resolve the generated Data type name for an operation, falling back to
4
- * unknown when the SDK signature exposes no Data type.
5
- * See OperationInfo.dataTypeName for why the name is read from the signature
6
- * instead of derived from the method name (#213).
7
- */
2
+ /** The generated Data type name, or unknown when there is none. */
8
3
  export declare function getDataTypeName(op: OperationInfo): string;
9
- /**
10
- * Get the error type string based on client type.
11
- * The Error type shares its stem with the Data type — hey-api mints both from
12
- * the operationId (see OperationInfo.dataTypeName, #213) — so the stem comes
13
- * from `dataTypeName` rather than the method name. The modelNames probe stays
14
- * because operations without error responses have no generated Error type.
15
- */
4
+ /** The error type of an operation's hooks, wrapped for axios. */
16
5
  export declare function getErrorType(op: OperationInfo, ctx: GenerationContext): string;
@@ -1,29 +1,9 @@
1
- /**
2
- * Resolve the generated Data type name for an operation, falling back to
3
- * unknown when the SDK signature exposes no Data type.
4
- * See OperationInfo.dataTypeName for why the name is read from the signature
5
- * instead of derived from the method name (#213).
6
- */
1
+ /** The generated Data type name, or unknown when there is none. */
7
2
  export function getDataTypeName(op) {
8
3
  return op.dataTypeName ?? "unknown";
9
4
  }
10
- /**
11
- * Get the error type string based on client type.
12
- * The Error type shares its stem with the Data type — hey-api mints both from
13
- * the operationId (see OperationInfo.dataTypeName, #213) — so the stem comes
14
- * from `dataTypeName` rather than the method name. The modelNames probe stays
15
- * because operations without error responses have no generated Error type.
16
- */
5
+ /** The error type of an operation's hooks, wrapped for axios. */
17
6
  export function getErrorType(op, ctx) {
18
- const stem = op.dataTypeName
19
- ? op.dataTypeName.replace(/Data$/, "")
20
- : op.capitalizedMethodName;
21
- const errorTypeName = `${stem}Error`;
22
- const errorType = ctx.modelNames.includes(errorTypeName)
23
- ? errorTypeName
24
- : "unknown";
25
- if (ctx.client === "@hey-api/client-axios") {
26
- return `AxiosError<${errorType}>`;
27
- }
28
- return errorType;
7
+ const errorType = op.errorTypeName ?? "unknown";
8
+ return ctx.client === "axios" ? `AxiosError<${errorType}>` : errorType;
29
9
  }
@@ -5,12 +5,17 @@ import type { GenerationContext } from "../types.mjs";
5
5
  * Uses consistent formatting settings to match existing output.
6
6
  */
7
7
  export declare function createGenerationProject(): Project;
8
+ /**
9
+ * A relative module specifier, with the extension the consuming project's
10
+ * module resolution asks for.
11
+ */
12
+ export declare function relativeModule(path: string, ctx?: Pick<GenerationContext, "importExtension">): string;
8
13
  /**
9
14
  * Build import structure for the Options type.
10
15
  * sdk.gen re-exports Options extended with `client` and `meta`, which the
11
16
  * base client Options lacks; hooks must accept those properties.
12
17
  */
13
- export declare function buildClientImport(_ctx: GenerationContext): ImportDeclarationStructure;
18
+ export declare function buildClientImport(ctx: GenerationContext): ImportDeclarationStructure;
14
19
  /**
15
20
  * Build import structure for TanStack Query.
16
21
  */
@@ -36,7 +41,7 @@ export declare function buildAxiosErrorImport(): ImportDeclarationStructure;
36
41
  /**
37
42
  * Build import for Common namespace.
38
43
  */
39
- export declare function buildCommonImport(): ImportDeclarationStructure;
44
+ export declare function buildCommonImport(ctx?: Pick<GenerationContext, "importExtension">): ImportDeclarationStructure;
40
45
  /**
41
46
  * Build all imports needed for the common file.
42
47
  */
@@ -17,15 +17,22 @@ export function createGenerationProject() {
17
17
  },
18
18
  });
19
19
  }
20
+ /**
21
+ * A relative module specifier, with the extension the consuming project's
22
+ * module resolution asks for.
23
+ */
24
+ export function relativeModule(path, ctx) {
25
+ return `${path}${ctx?.importExtension ?? ""}`;
26
+ }
20
27
  /**
21
28
  * Build import structure for the Options type.
22
29
  * sdk.gen re-exports Options extended with `client` and `meta`, which the
23
30
  * base client Options lacks; hooks must accept those properties.
24
31
  */
25
- export function buildClientImport(_ctx) {
32
+ export function buildClientImport(ctx) {
26
33
  return {
27
34
  kind: StructureKind.ImportDeclaration,
28
- moduleSpecifier: "../requests/sdk.gen",
35
+ moduleSpecifier: relativeModule("../requests/sdk.gen", ctx),
29
36
  namedImports: [{ name: "Options", isTypeOnly: true }],
30
37
  };
31
38
  }
@@ -78,7 +85,7 @@ export function buildQueryOptionsImport() {
78
85
  export function buildServiceImport(ctx) {
79
86
  return {
80
87
  kind: StructureKind.ImportDeclaration,
81
- moduleSpecifier: "../requests/sdk.gen",
88
+ moduleSpecifier: relativeModule("../requests/sdk.gen", ctx),
82
89
  namedImports: ctx.serviceNames.map((name) => ({ name })),
83
90
  };
84
91
  }
@@ -93,7 +100,7 @@ export function buildModelImport(ctx) {
93
100
  }
94
101
  return {
95
102
  kind: StructureKind.ImportDeclaration,
96
- moduleSpecifier: "../requests/types.gen",
103
+ moduleSpecifier: relativeModule("../requests/types.gen", ctx),
97
104
  isTypeOnly: true,
98
105
  namedImports: ctx.modelNames.map((name) => ({ name })),
99
106
  };
@@ -111,10 +118,10 @@ export function buildAxiosErrorImport() {
111
118
  /**
112
119
  * Build import for Common namespace.
113
120
  */
114
- export function buildCommonImport() {
121
+ export function buildCommonImport(ctx) {
115
122
  return {
116
123
  kind: StructureKind.ImportDeclaration,
117
- moduleSpecifier: "./common",
124
+ moduleSpecifier: relativeModule("./common", ctx),
118
125
  namespaceImport: "Common",
119
126
  };
120
127
  }
@@ -131,7 +138,7 @@ export function buildCommonFileImports(ctx) {
131
138
  if (modelImport) {
132
139
  imports.push(modelImport);
133
140
  }
134
- if (ctx.client === "@hey-api/client-axios") {
141
+ if (ctx.client === "axios") {
135
142
  imports.push(buildAxiosErrorImport());
136
143
  }
137
144
  return imports;
@@ -140,7 +147,7 @@ export function buildCommonFileImports(ctx) {
140
147
  * Build all imports needed for hook files (queries, suspense, infinite).
141
148
  */
142
149
  export function buildHookFileImports(ctx) {
143
- return [buildCommonImport(), ...buildCommonFileImports(ctx)];
150
+ return [buildCommonImport(ctx), ...buildCommonFileImports(ctx)];
144
151
  }
145
152
  /**
146
153
  * Build all imports needed for the queryOptions file.
@@ -149,7 +156,7 @@ export function buildHookFileImports(ctx) {
149
156
  */
150
157
  export function buildQueryOptionsFileImports(ctx) {
151
158
  const imports = [
152
- buildCommonImport(),
159
+ buildCommonImport(ctx),
153
160
  buildQueryOptionsImport(),
154
161
  buildClientImport(ctx),
155
162
  buildServiceImport(ctx),
package/dist/types.d.mts CHANGED
@@ -7,28 +7,19 @@ export interface OperationInfo {
7
7
  methodName: string;
8
8
  /** Capitalized method name (e.g., "FindPets") */
9
9
  capitalizedMethodName: string;
10
+ /** Generated Data type name of the operation (e.g., "FindPetsData"). */
11
+ dataTypeName?: string;
10
12
  /**
11
- * Generated Data type name read from the SDK function's own
12
- * `Options<XData, ThrowOnError>` signature (e.g., "FindPetsData").
13
- * Not derivable from the method name: for digit-leading operationIds
14
- * hey-api prefixes the function but strips the digits from the type (#213).
15
- * Undefined when the SDK signature exposes no Data type.
16
- *
17
- * The rule this encodes: names hey-api owns (Data, Error) must be read from
18
- * the SDK signature or share this field's stem; names this codegen mints
19
- * itself (DefaultResponse, MutationResult, key fns) may be derived from
20
- * capitalizedMethodName because they anchor back to the SDK via
21
- * `typeof methodName`.
13
+ * Name of the operation's error type alias (e.g., "FindPetsError"), or
14
+ * undefined when the operation declares no error responses with a body.
15
+ * A colliding name gets a number appended, so this is not always the data
16
+ * type's stem plus "Error".
22
17
  */
23
- dataTypeName?: string;
18
+ errorTypeName?: string;
24
19
  /** HTTP method (e.g., "GET", "POST", "PUT", "PATCH", "DELETE") */
25
20
  httpMethod: string;
26
21
  /** JSDoc comment string (if present) */
27
22
  jsDoc?: string;
28
- /** Whether the operation is deprecated */
29
- isDeprecated: boolean;
30
- /** Parameter information for the operation */
31
- parameters: OperationParameter[];
32
23
  /** Whether all parameters are optional */
33
24
  allParamsOptional: boolean;
34
25
  /** Whether this operation supports pagination (for infinite queries) */
@@ -39,21 +30,13 @@ export interface OperationInfo {
39
30
  pageParamTypeKind?: PageParamTypeKind;
40
31
  }
41
32
  export type PageParamTypeKind = "string" | "number" | "other";
42
- export interface OperationParameter {
43
- /** Parameter name */
44
- name: string;
45
- /** TypeScript type as string */
46
- typeName: string;
47
- /** Whether this parameter is optional */
48
- optional: boolean;
49
- }
50
33
  /**
51
34
  * Context for generating hooks and utilities.
52
35
  * Contains shared information needed across all generators.
53
36
  */
54
37
  export interface GenerationContext {
55
- /** Client type: "@hey-api/client-fetch" or "@hey-api/client-axios" */
56
- client: "@hey-api/client-fetch" | "@hey-api/client-axios";
38
+ /** Client type: "fetch" or "axios" */
39
+ client: "fetch" | "axios";
57
40
  /** Model type names exported from the models file */
58
41
  modelNames: string[];
59
42
  /** Service function names exported from the service file */
@@ -68,6 +51,11 @@ export interface GenerationContext {
68
51
  omitInitialPageParam: boolean;
69
52
  /** Package version for generated comment */
70
53
  version: string;
54
+ /**
55
+ * Extension of relative import specifiers: ".js" when the consuming project
56
+ * resolves modules the Node way (`node16` / `nodenext`), else none.
57
+ */
58
+ importExtension?: "" | ".js";
71
59
  }
72
60
  /**
73
61
  * Generated output for a single file.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@7nohe/openapi-react-query-codegen",
3
- "version": "3.1.0",
3
+ "version": "4.0.0-beta.2",
4
4
  "description": "OpenAPI React Query Codegen",
5
5
  "bin": {
6
6
  "openapi-rq": "dist/cli.mjs"
@@ -24,7 +24,8 @@
24
24
  "homepage": "https://github.com/7nohe/openapi-react-query-codegen",
25
25
  "bugs": "https://github.com/7nohe/openapi-react-query-codegen/issues",
26
26
  "files": [
27
- "dist"
27
+ "dist",
28
+ "AGENTS.md"
28
29
  ],
29
30
  "keywords": [
30
31
  "codegen",
@@ -33,14 +34,13 @@
33
34
  "openapi",
34
35
  "swagger",
35
36
  "typescript",
36
- "openapi-typescript-codegen",
37
- "@hey-api/openapi-ts"
37
+ "openapi-typescript-codegen"
38
38
  ],
39
39
  "license": "MIT",
40
40
  "author": "Daiki Urata (@7nohe)",
41
41
  "dependencies": {
42
- "@hey-api/openapi-ts": "0.99.0",
43
- "cross-spawn": "^7.0.3"
42
+ "cross-spawn": "^7.0.3",
43
+ "yaml": "2.9.1"
44
44
  },
45
45
  "devDependencies": {
46
46
  "@biomejs/biome": "^2.5.4",
@@ -48,9 +48,9 @@
48
48
  "@types/node": "^22.20.1",
49
49
  "@types/semver": "^7.7.1",
50
50
  "@vitest/coverage-v8": "^4.1.12",
51
+ "axios": "1.20.0",
51
52
  "commander": "^15.0.0",
52
53
  "lefthook": "^2.1.10",
53
- "rimraf": "^6.1.3",
54
54
  "ts-morph": "^28.0.0",
55
55
  "typescript": "^6.0.3",
56
56
  "vite": "^7.3.7",
@@ -67,7 +67,7 @@
67
67
  "pnpm": ">=9"
68
68
  },
69
69
  "scripts": {
70
- "build": "rimraf dist && tsc -p tsconfig.json",
70
+ "build": "node scripts/clean.mjs dist && tsc -p tsconfig.json && node scripts/copy-runtime.mjs",
71
71
  "lint": "biome check .",
72
72
  "lint:fix": "biome check --write .",
73
73
  "preview:react": "npm run build && npm -C examples/react-app run generate:api",
@@ -1,10 +0,0 @@
1
- import { type Project } from "ts-morph";
2
- import type { GenerationContext, OperationInfo } from "./types.mjs";
3
- /**
4
- * Parse operations from the OpenAPI-generated service file and return normalized DTOs.
5
- */
6
- export declare function parseOperations(project: Project, pageParam: string): Promise<OperationInfo[]>;
7
- /**
8
- * Build generation context from project configuration.
9
- */
10
- export declare function buildGenerationContext(project: Project, client: GenerationContext["client"], pageParam: string, nextPageParam: string, initialPageParam: string, omitInitialPageParam: boolean, version: string): GenerationContext;