Skip to content

Commit bb7f355

Browse files
committed
fix: handle case for no method parameters
1 parent 2757088 commit bb7f355

File tree

3 files changed

+39
-31
lines changed

3 files changed

+39
-31
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
},
88
"scripts": {
99
"build": "tsc -p tsconfig.json",
10-
"preview": "npm run build && npm -C examples/react-app run generate:clients"
10+
"preview": "npm run build && npm -C examples/react-app run generate:api"
1111
},
1212
"keywords": [],
1313
"author": "Daiki Urata (@7nohe)",

src/createUseMutation.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export const createUseMutation = (
5353
ts.factory.createKeywordTypeNode(
5454
ts.SyntaxKind.UnknownKeyword
5555
),
56-
ts.factory.createTypeLiteralNode(
56+
method.parameters.length !== 0 ? ts.factory.createTypeLiteralNode(
5757
method.parameters.map((param) => {
5858
return ts.factory.createPropertySignature(
5959
undefined,
@@ -64,6 +64,8 @@ export const createUseMutation = (
6464
param.type
6565
);
6666
})
67+
) : ts.factory.createKeywordTypeNode(
68+
ts.SyntaxKind.VoidKeyword
6769
),
6870
ts.factory.createKeywordTypeNode(
6971
ts.SyntaxKind.UnknownKeyword
@@ -87,7 +89,7 @@ export const createUseMutation = (
8789
ts.factory.createArrowFunction(
8890
undefined,
8991
undefined,
90-
[
92+
method.parameters.length !== 0 ? [
9193
ts.factory.createParameterDeclaration(
9294
undefined,
9395
undefined,
@@ -108,7 +110,7 @@ export const createUseMutation = (
108110
undefined,
109111
undefined
110112
),
111-
],
113+
] : [],
112114
undefined,
113115
ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken),
114116
ts.factory.createCallExpression(

src/createUseQuery.ts

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,38 @@ export const createUseQuery = (
77
method: ts.MethodDeclaration
88
) => {
99
const methodName = method.name?.getText(node)!;
10+
let requestParam = [];
11+
if (method.parameters.length !== 0) {
12+
requestParam.push(
13+
ts.factory.createParameterDeclaration(
14+
undefined,
15+
undefined,
16+
undefined,
17+
ts.factory.createObjectBindingPattern(
18+
method.parameters.map((param) =>
19+
ts.factory.createBindingElement(
20+
undefined,
21+
undefined,
22+
ts.factory.createIdentifier(param.name.getText(node)),
23+
undefined
24+
)
25+
)
26+
),
27+
undefined,
28+
ts.factory.createTypeLiteralNode(
29+
method.parameters.map((param) =>
30+
ts.factory.createPropertySignature(
31+
undefined,
32+
ts.factory.createIdentifier(param.name.getText(node)),
33+
undefined,
34+
param.type
35+
)
36+
)
37+
),
38+
undefined
39+
),
40+
);
41+
}
1042
return ts.factory.createVariableStatement(
1143
[ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
1244
ts.factory.createVariableDeclarationList(
@@ -21,33 +53,7 @@ export const createUseQuery = (
2153
undefined,
2254
undefined,
2355
[
24-
ts.factory.createParameterDeclaration(
25-
undefined,
26-
undefined,
27-
undefined,
28-
ts.factory.createObjectBindingPattern(
29-
method.parameters.map((param) =>
30-
ts.factory.createBindingElement(
31-
undefined,
32-
undefined,
33-
ts.factory.createIdentifier(param.name.getText(node)),
34-
undefined
35-
)
36-
)
37-
),
38-
undefined,
39-
ts.factory.createTypeLiteralNode(
40-
method.parameters.map((param) =>
41-
ts.factory.createPropertySignature(
42-
undefined,
43-
ts.factory.createIdentifier(param.name.getText(node)),
44-
undefined,
45-
param.type
46-
)
47-
)
48-
),
49-
undefined
50-
),
56+
...requestParam,
5157
ts.factory.createParameterDeclaration(
5258
undefined,
5359
undefined,

0 commit comments

Comments
 (0)