forked from 7nohe/openapi-react-query-codegen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateExports.ts
More file actions
74 lines (69 loc) · 2.75 KB
/
createExports.ts
File metadata and controls
74 lines (69 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import ts, { JSDoc } from "typescript";
import { sync } from "glob";
import { join } from "path";
import fs from "fs";
import { createUseQuery } from "./createUseQuery";
import { createUseMutation } from "./createUseMutation";
export const createExports = (generatedClientsPath: string) => {
const services = sync(
join(generatedClientsPath, "services", "*.ts").replace(/\\/g, "/")
);
const nodes = services.map((servicePath) =>
ts.createSourceFile(
servicePath, // fileName
fs.readFileSync(join(process.cwd(), servicePath), "utf8"),
ts.ScriptTarget.Latest // langugeVersion
)
);
return [
...nodes
.map((node) => {
const klass = node
.getChildren()[0]
.getChildren()
.find(
(child) => child.kind === ts.SyntaxKind.ClassDeclaration
) as ts.ClassDeclaration;
const className = klass.name?.getText(node)!;
const methods = klass.members.filter(
(node) => node.kind === ts.SyntaxKind.MethodDeclaration
) as ts.MethodDeclaration[];
return methods
.map((method) => {
const methodBlock = method
.getChildren(node)
.find((child) => child.kind === ts.SyntaxKind.Block) as ts.Block;
const returnStatement = methodBlock.statements.find(
(s) => s.kind === ts.SyntaxKind.ReturnStatement
) as ts.ReturnStatement;
const callExpression =
returnStatement.expression as ts.CallExpression;
const properties = (
callExpression.arguments[1] as ts.ObjectLiteralExpression
).properties as unknown as ts.PropertyAssignment[];
const httpMethodName = properties
.find((p) => p.name?.getText(node) === "method")
?.initializer?.getText(node)!;
const getAllChildren = (tsNode: ts.Node): Array<ts.Node> => {
const childItems = tsNode.getChildren(node);
if (childItems.length) {
const allChildren = childItems.map(getAllChildren);
return [tsNode].concat(allChildren.flat());
}
return [tsNode];
}
const children = getAllChildren(method);
const jsDoc = children.filter((c) => c.kind === ts.SyntaxKind.JSDoc).map((c) => {
return (c as JSDoc).comment
});
const hasDeprecated = children
.some((c) => c.kind === ts.SyntaxKind.JSDocDeprecatedTag);
return httpMethodName === "'GET'"
? createUseQuery(node, className, method, jsDoc, hasDeprecated)
: createUseMutation(node, className, method, jsDoc, hasDeprecated);
})
.flat();
})
.flat(),
];
};