@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
@@ -1,164 +0,0 @@
1
- import { Node, } from "ts-morph";
2
- import ts from "typescript";
3
- import { capitalizeFirstLetter, extractPropertiesFromObjectParam, getNameFromVariable, getShortType, getVariableArrowFunctionParameters, } from "./common.mjs";
4
- import { modelsFileName, serviceFileName } from "./constants.mjs";
5
- import { getServices } from "./service.mjs";
6
- function getPageParamTypeKind(type) {
7
- const types = type.isUnion()
8
- ? type.types.filter((item) => !(item.flags & (ts.TypeFlags.Null | ts.TypeFlags.Undefined)))
9
- : [type];
10
- if (types.length > 0 &&
11
- types.every((item) => item.flags & ts.TypeFlags.StringLike)) {
12
- return "string";
13
- }
14
- if (types.length > 0 &&
15
- types.every((item) => item.flags & ts.TypeFlags.NumberLike)) {
16
- return "number";
17
- }
18
- return "other";
19
- }
20
- /**
21
- * Extract parameter information from a method's variable declaration.
22
- */
23
- function extractParameters(method, pageParam) {
24
- const arrowParams = getVariableArrowFunctionParameters(method);
25
- if (!arrowParams.length) {
26
- return [];
27
- }
28
- return arrowParams.flatMap((param) => {
29
- const paramNodes = extractPropertiesFromObjectParam(param);
30
- return paramNodes
31
- .filter((p) => p.name !== pageParam)
32
- .map((refParam) => ({
33
- name: refParam.name,
34
- typeName: getShortType(refParam.type?.getText() ?? ""),
35
- optional: refParam.optional,
36
- }));
37
- });
38
- }
39
- /**
40
- * Read the operation's Data type name from the first type argument of the
41
- * SDK options parameter (`Options<XData, ThrowOnError>`).
42
- * See OperationInfo.dataTypeName for why the name is read from the signature
43
- * instead of derived from the method name (#213).
44
- */
45
- function getDataTypeNameFromSignature(optionsParam) {
46
- const typeNode = optionsParam?.getTypeNode();
47
- if (!typeNode || !Node.isTypeReference(typeNode))
48
- return undefined;
49
- if (typeNode.getTypeName().getText() !== "Options")
50
- return undefined;
51
- const [dataTypeArg] = typeNode.getTypeArguments();
52
- return dataTypeArg?.getText();
53
- }
54
- /**
55
- * Get paginatable Data types by checking if they have the pageParam in their query property.
56
- * Uses TypeScript compiler API for accurate AST traversal.
57
- * The map is keyed by the Data type name (e.g., "FindPetsData") so callers can
58
- * look it up with the name read from the SDK signature.
59
- */
60
- function getPaginatableMethods(project, pageParam) {
61
- const modelsFile = project
62
- .getSourceFiles()
63
- .find((sf) => sf.getFilePath().includes(modelsFileName));
64
- if (!modelsFile)
65
- return new Map();
66
- const paginatableMethods = new Map();
67
- const typeChecker = project.getTypeChecker().compilerObject;
68
- const modelDeclarations = modelsFile.getExportedDeclarations();
69
- const entries = modelDeclarations.entries();
70
- for (const [key, value] of entries) {
71
- // Check if this is a *Data type (e.g., FindPetsData)
72
- if (!key.endsWith("Data"))
73
- continue;
74
- const node = value[0].compilerNode;
75
- if (!ts.isTypeAliasDeclaration(node))
76
- continue;
77
- const typeAliasDeclaration = node.type;
78
- if (typeAliasDeclaration.kind !== ts.SyntaxKind.TypeLiteral)
79
- continue;
80
- // Look for 'query' property in the type literal
81
- const query = typeAliasDeclaration.members.find((m) => m.kind === ts.SyntaxKind.PropertySignature &&
82
- m.name?.getText() === "query");
83
- if (!query)
84
- continue;
85
- // Check if query type has the pageParam
86
- const queryType = query.type;
87
- if (!queryType || queryType.kind !== ts.SyntaxKind.TypeLiteral)
88
- continue;
89
- const pageParamNode = queryType.members.find((m) => ts.isPropertySignature(m) && m.name?.getText() === pageParam);
90
- if (pageParamNode) {
91
- const pageParamType = pageParamNode.type?.getText(modelsFile.compilerNode);
92
- const resolvedType = typeChecker.getTypeAtLocation(pageParamNode.type ?? pageParamNode);
93
- paginatableMethods.set(key, {
94
- type: pageParamType ?? "unknown",
95
- typeKind: getPageParamTypeKind(resolvedType),
96
- });
97
- }
98
- }
99
- return paginatableMethods;
100
- }
101
- /**
102
- * Parse operations from the OpenAPI-generated service file and return normalized DTOs.
103
- */
104
- export async function parseOperations(project, pageParam) {
105
- const service = await getServices(project);
106
- const { methods } = service;
107
- const paginatableMethods = getPaginatableMethods(project, pageParam);
108
- return methods.map((desc) => {
109
- const methodName = getNameFromVariable(desc.method);
110
- const httpMethod = desc.httpMethodName.toUpperCase();
111
- const parameters = extractParameters(desc.method);
112
- // Use the SDK function's parameter optionality as the authoritative check.
113
- // Generic types like Options<XData, ThrowOnError> may not resolve correctly
114
- // via extractPropertiesFromObjectParam for type alias properties (path, url).
115
- const sdkParams = getVariableArrowFunctionParameters(desc.method);
116
- const allParamsOptional = sdkParams.length === 0 || sdkParams[0].isOptional();
117
- const dataTypeName = getDataTypeNameFromSignature(sdkParams[0]);
118
- const pageParamInfo = dataTypeName
119
- ? paginatableMethods.get(dataTypeName)
120
- : undefined;
121
- const isPaginatable = httpMethod === "GET" && pageParamInfo !== undefined;
122
- return {
123
- methodName,
124
- capitalizedMethodName: capitalizeFirstLetter(methodName),
125
- dataTypeName,
126
- httpMethod,
127
- jsDoc: desc.jsDoc,
128
- isDeprecated: desc.isDeprecated,
129
- parameters,
130
- allParamsOptional,
131
- isPaginatable,
132
- pageParamType: isPaginatable ? pageParamInfo.type : undefined,
133
- pageParamTypeKind: isPaginatable ? pageParamInfo.typeKind : undefined,
134
- };
135
- });
136
- }
137
- /**
138
- * Build generation context from project configuration.
139
- */
140
- export function buildGenerationContext(project, client, pageParam, nextPageParam, initialPageParam, omitInitialPageParam, version) {
141
- const modelsFile = project
142
- .getSourceFiles()
143
- .find((sf) => sf.getFilePath().includes(modelsFileName));
144
- const serviceFile = project
145
- .getSourceFiles()
146
- .find((sf) => sf.getFilePath().includes(serviceFileName));
147
- if (!serviceFile) {
148
- throw new Error("No service node found");
149
- }
150
- const modelNames = modelsFile
151
- ? Array.from(modelsFile.getExportedDeclarations().keys())
152
- : [];
153
- const serviceNames = Array.from(serviceFile.getExportedDeclarations().keys());
154
- return {
155
- client,
156
- modelNames,
157
- serviceNames,
158
- pageParam,
159
- nextPageParam,
160
- initialPageParam,
161
- omitInitialPageParam,
162
- version,
163
- };
164
- }
@@ -1,8 +0,0 @@
1
- import type { Project, SourceFile } from "ts-morph";
2
- import type { FunctionDescription } from "./common.mjs";
3
- export type Service = {
4
- node: SourceFile;
5
- methods: Array<FunctionDescription>;
6
- };
7
- export declare function getServices(project: Project): Promise<Service>;
8
- export declare function getMethodsFromService(node: SourceFile): FunctionDescription[];
package/dist/service.mjs DELETED
@@ -1,98 +0,0 @@
1
- import ts from "typescript";
2
- import { serviceFileName } from "./constants.mjs";
3
- export async function getServices(project) {
4
- const node = project
5
- .getSourceFiles()
6
- .find((sourceFile) => sourceFile.getFilePath().includes(serviceFileName));
7
- if (!node) {
8
- throw new Error("No service node found");
9
- }
10
- const methods = getMethodsFromService(node);
11
- return {
12
- methods,
13
- node,
14
- };
15
- }
16
- export function getMethodsFromService(node) {
17
- const variableStatements = node.getVariableStatements();
18
- // Filter to only exported variable statements that contain arrow functions
19
- const exportedStatements = variableStatements.filter((statement) => {
20
- if (!statement.isExported())
21
- return false;
22
- const declarations = statement.getDeclarations();
23
- return declarations.some((decl) => {
24
- const initializer = decl.getInitializer();
25
- return initializer && ts.isArrowFunction(initializer.compilerNode);
26
- });
27
- });
28
- return exportedStatements.flatMap((variableStatement) => {
29
- const declarations = variableStatement.getDeclarations();
30
- // Filter to only arrow function declarations within the statement
31
- const arrowDeclarations = declarations.filter((declaration) => {
32
- const initializer = declaration.getInitializer();
33
- return initializer && ts.isArrowFunction(initializer.compilerNode);
34
- });
35
- return arrowDeclarations.map((declaration) => {
36
- const initializer = declaration.getInitializerOrThrow();
37
- const compilerNode = initializer.compilerNode;
38
- const arrowBody = compilerNode.body;
39
- // Find the call expression - either from block's return statement or direct expression
40
- let callExpression;
41
- let methodBlockNode;
42
- if (ts.isBlock(arrowBody)) {
43
- // Old style: arrow function with block body
44
- methodBlockNode = arrowBody;
45
- const returnStatement = arrowBody.statements.find(ts.isReturnStatement);
46
- if (!returnStatement) {
47
- throw new Error("Return statement not found");
48
- }
49
- if (!returnStatement.expression) {
50
- throw new Error("Call expression not found");
51
- }
52
- callExpression = returnStatement.expression;
53
- }
54
- else {
55
- // New style: arrow function with expression body (no block)
56
- // The body is a call expression like: (options?.client ?? client).post<...>({...})
57
- callExpression = arrowBody;
58
- }
59
- // Navigate to find the HTTP method name (get, post, put, delete, etc.)
60
- let httpMethodName;
61
- if (ts.isCallExpression(callExpression)) {
62
- const expr = callExpression.expression;
63
- if (ts.isPropertyAccessExpression(expr)) {
64
- httpMethodName = expr.name.text;
65
- }
66
- }
67
- if (!httpMethodName) {
68
- throw new Error("httpMethodName not found");
69
- }
70
- const getAllChildren = (tsNode) => {
71
- const childItems = tsNode.getChildren(node.compilerNode);
72
- if (childItems.length) {
73
- const allChildren = childItems.map(getAllChildren);
74
- return [tsNode].concat(allChildren.flat());
75
- }
76
- return [tsNode];
77
- };
78
- const children = getAllChildren(variableStatement.compilerNode);
79
- // get all JSDoc comments
80
- // this should be an array of 1 or 0
81
- const jsDocs = children
82
- .filter((c) => c.kind === ts.SyntaxKind.JSDoc)
83
- .map((c) => c.getText(node.compilerNode));
84
- // get the first JSDoc comment
85
- const jsDoc = jsDocs?.[0];
86
- const isDeprecated = children.some((c) => c.kind === ts.SyntaxKind.JSDocDeprecatedTag);
87
- const methodDescription = {
88
- node,
89
- method: declaration,
90
- methodBlock: methodBlockNode,
91
- httpMethodName,
92
- jsDoc,
93
- isDeprecated,
94
- };
95
- return methodDescription;
96
- });
97
- });
98
- }
package/dist/util.d.mts DELETED
@@ -1,2 +0,0 @@
1
- import ts from "typescript";
2
- export declare function addJSDocToNode<T extends ts.Node>(node: T, jsDoc: string | undefined): T;
package/dist/util.mjs DELETED
@@ -1,16 +0,0 @@
1
- import ts from "typescript";
2
- export function addJSDocToNode(node, jsDoc) {
3
- if (!jsDoc) {
4
- return node;
5
- }
6
- // replace the first /** with *
7
- // we do this because ts.addSyntheticLeadingComment will add /* to the beginning but we want /**
8
- const removedFirstLine = jsDoc.trim().replace(/^\/\*\*/, "*");
9
- // remove the last */ because ts.addSyntheticLeadingComment will add it
10
- const removedSecondLine = removedFirstLine.replace(/\*\/$/, "");
11
- const split = removedSecondLine.split("\n");
12
- const trimmed = split.map((line) => line.trim());
13
- const joined = trimmed.join("\n");
14
- const nodeWithJSDoc = ts.addSyntheticLeadingComment(node, ts.SyntaxKind.MultiLineCommentTrivia, joined, true);
15
- return nodeWithJSDoc;
16
- }