@7nohe/openapi-react-query-codegen 0.0.0-5d64da85eea9585abb7c08251a30050eda95ede2 → 0.0.0-9ceedcab457bf6d8c278c7f1e2a12a78f980aa7d

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.
@@ -44,10 +44,10 @@ export const createExports = (service, project, pageParam, nextPageParam, initia
44
44
  const allGetQueries = allGet.map((m) => createUseQuery(m, pageParam, nextPageParam, initialPageParam, paginatableMethods, modelNames));
45
45
  const allPrefetchQueries = allGet.map((m) => createPrefetchOrEnsure({ ...m, functionType: "prefetch", modelNames }));
46
46
  const allEnsureQueries = allGet.map((m) => createPrefetchOrEnsure({ ...m, functionType: "ensure", modelNames }));
47
- const allPostMutations = allPost.map((m) => createUseMutation(m));
48
- const allPutMutations = allPut.map((m) => createUseMutation(m));
49
- const allPatchMutations = allPatch.map((m) => createUseMutation(m));
50
- const allDeleteMutations = allDelete.map((m) => createUseMutation(m));
47
+ const allPostMutations = allPost.map((m) => createUseMutation({ ...m, modelNames }));
48
+ const allPutMutations = allPut.map((m) => createUseMutation({ ...m, modelNames }));
49
+ const allPatchMutations = allPatch.map((m) => createUseMutation({ ...m, modelNames }));
50
+ const allDeleteMutations = allDelete.map((m) => createUseMutation({ ...m, modelNames }));
51
51
  const allQueries = [...allGetQueries];
52
52
  const allMutations = [
53
53
  ...allPostMutations,
@@ -1,5 +1,5 @@
1
1
  import ts from "typescript";
2
- import { BuildCommonTypeName, TContext, TData, TError, capitalizeFirstLetter, extractPropertiesFromObjectParam, getNameFromVariable, getShortType, getVariableArrowFunctionParameters, } from "./common.mjs";
2
+ import { BuildCommonTypeName, EqualsOrGreaterThanToken, TContext, TData, TError, capitalizeFirstLetter, getNameFromVariable, getVariableArrowFunctionParameters, } from "./common.mjs";
3
3
  import { addJSDocToNode } from "./util.mjs";
4
4
  /**
5
5
  * Awaited<ReturnType<typeof myClass.myMethod>>
@@ -11,7 +11,7 @@ function generateAwaitedReturnType({ methodName, }) {
11
11
  ]),
12
12
  ]);
13
13
  }
14
- export const createUseMutation = ({ method, jsDoc }) => {
14
+ export const createUseMutation = ({ method, jsDoc, modelNames, }) => {
15
15
  const methodName = getNameFromVariable(method);
16
16
  const awaitedResponseDataType = generateAwaitedReturnType({
17
17
  methodName,
@@ -19,12 +19,12 @@ export const createUseMutation = ({ method, jsDoc }) => {
19
19
  const mutationResult = ts.factory.createTypeAliasDeclaration([ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)], ts.factory.createIdentifier(`${capitalizeFirstLetter(methodName)}MutationResult`), undefined, awaitedResponseDataType);
20
20
  const responseDataType = ts.factory.createTypeParameterDeclaration(undefined, TData, undefined, ts.factory.createTypeReferenceNode(BuildCommonTypeName(mutationResult.name)));
21
21
  const methodParameters = getVariableArrowFunctionParameters(method).length !== 0
22
- ? ts.factory.createTypeLiteralNode(getVariableArrowFunctionParameters(method).flatMap((param) => {
23
- const paramNodes = extractPropertiesFromObjectParam(param);
24
- return paramNodes.map((refParam) => ts.factory.createPropertySignature(undefined, ts.factory.createIdentifier(refParam.name), refParam.optional
25
- ? ts.factory.createToken(ts.SyntaxKind.QuestionToken)
26
- : undefined, ts.factory.createTypeReferenceNode(getShortType(refParam.type?.getText(param) ?? ""))));
27
- }))
22
+ ? ts.factory.createTypeReferenceNode(ts.factory.createIdentifier("Options"), [
23
+ ts.factory.createTypeReferenceNode(modelNames.includes(`${capitalizeFirstLetter(methodName)}Data`)
24
+ ? `${capitalizeFirstLetter(methodName)}Data`
25
+ : "unknown"),
26
+ ts.factory.createLiteralTypeNode(ts.factory.createTrue()),
27
+ ])
28
28
  : ts.factory.createKeywordTypeNode(ts.SyntaxKind.VoidKeyword);
29
29
  const exportHook = ts.factory.createVariableStatement([ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)], ts.factory.createVariableDeclarationList([
30
30
  ts.factory.createVariableDeclaration(ts.factory.createIdentifier(`use${capitalizeFirstLetter(methodName)}`), undefined, undefined, ts.factory.createArrowFunction(undefined, ts.factory.createNodeArray([
@@ -48,22 +48,14 @@ export const createUseMutation = ({ method, jsDoc }) => {
48
48
  ts.factory.createTypeReferenceNode(TContext),
49
49
  ], [
50
50
  ts.factory.createObjectLiteralExpression([
51
- ts.factory.createPropertyAssignment(ts.factory.createIdentifier("mutationFn"), ts.factory.createArrowFunction(undefined, undefined, getVariableArrowFunctionParameters(method).length !== 0
52
- ? [
53
- ts.factory.createParameterDeclaration(undefined, undefined, ts.factory.createObjectBindingPattern(getVariableArrowFunctionParameters(method).flatMap((param) => {
54
- const paramNodes = extractPropertiesFromObjectParam(param);
55
- return paramNodes.map((refParam) => ts.factory.createBindingElement(undefined, undefined, ts.factory.createIdentifier(refParam.name), undefined));
56
- })), undefined, undefined, undefined),
57
- ]
58
- : [], undefined, ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), ts.factory.createAsExpression(ts.factory.createAsExpression(ts.factory.createCallExpression(ts.factory.createIdentifier(methodName), undefined, getVariableArrowFunctionParameters(method)
59
- .length !== 0
60
- ? [
61
- ts.factory.createObjectLiteralExpression(getVariableArrowFunctionParameters(method).flatMap((params) => {
62
- const paramNodes = extractPropertiesFromObjectParam(params);
63
- return paramNodes.map((refParam) => ts.factory.createShorthandPropertyAssignment(refParam.name));
64
- })),
65
- ]
66
- : []), ts.factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword)), ts.factory.createTypeReferenceNode(ts.factory.createIdentifier("Promise"), [ts.factory.createTypeReferenceNode(TData)])))),
51
+ ts.factory.createPropertyAssignment(ts.factory.createIdentifier("mutationFn"),
52
+ // (clientOptions) => addPet(clientOptions).then(response => response.data as TData) as unknown as Promise<TData>
53
+ ts.factory.createArrowFunction(undefined, undefined, [
54
+ ts.factory.createParameterDeclaration(undefined, undefined, ts.factory.createIdentifier("clientOptions"), undefined, undefined, undefined),
55
+ ], undefined, EqualsOrGreaterThanToken, ts.factory.createAsExpression(ts.factory.createAsExpression(ts.factory.createCallExpression(ts.factory.createIdentifier(methodName), undefined, getVariableArrowFunctionParameters(method).length >
56
+ 0
57
+ ? [ts.factory.createIdentifier("clientOptions")]
58
+ : undefined), ts.factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword)), ts.factory.createTypeReferenceNode(ts.factory.createIdentifier("Promise"), [ts.factory.createTypeReferenceNode(TData)])))),
67
59
  ts.factory.createSpreadAssignment(ts.factory.createIdentifier("options")),
68
60
  ]),
69
61
  ]))),
@@ -160,7 +160,7 @@ export function createQueryHook({ queryString, suffix, responseDataType, request
160
160
  ]
161
161
  : // { ...clientOptions }
162
162
  getVariableArrowFunctionParameters(method)
163
- .length
163
+ .length > 0
164
164
  ? [
165
165
  ts.factory.createObjectLiteralExpression([
166
166
  ts.factory.createSpreadAssignment(ts.factory.createIdentifier("clientOptions")),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@7nohe/openapi-react-query-codegen",
3
- "version": "0.0.0-5d64da85eea9585abb7c08251a30050eda95ede2",
3
+ "version": "0.0.0-9ceedcab457bf6d8c278c7f1e2a12a78f980aa7d",
4
4
  "description": "OpenAPI React Query Codegen",
5
5
  "bin": {
6
6
  "openapi-rq": "dist/cli.mjs"