-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
createImports.mts
131 lines (123 loc) · 3.61 KB
/
createImports.mts
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { posix } from "node:path";
import type { Project } from "ts-morph";
import ts from "typescript";
import { modalsFileName, serviceFileName } from "./constants.mjs";
const { join } = posix;
export const createImports = ({
project,
}: {
project: Project;
}) => {
const modelsFile = project
.getSourceFiles()
.find((sourceFile) => sourceFile.getFilePath().includes(modalsFileName));
const serviceFile = project.getSourceFileOrThrow(`${serviceFileName}.ts`);
if (!modelsFile) {
console.warn(`
⚠️ WARNING: No models file found.
This may be an error if \`.components.schemas\` or \`.components.parameters\` is defined in your OpenAPI input.`);
}
const modelNames = modelsFile
? Array.from(modelsFile.getExportedDeclarations().keys())
: [];
const serviceExports = Array.from(
serviceFile.getExportedDeclarations().keys(),
);
const serviceNames = serviceExports;
const imports = [
ts.factory.createImportDeclaration(
undefined,
ts.factory.createImportClause(
false,
undefined,
ts.factory.createNamedImports([
ts.factory.createImportSpecifier(
true,
undefined,
ts.factory.createIdentifier("QueryClient"),
),
ts.factory.createImportSpecifier(
false,
undefined,
ts.factory.createIdentifier("useQuery"),
),
ts.factory.createImportSpecifier(
false,
undefined,
ts.factory.createIdentifier("useSuspenseQuery"),
),
ts.factory.createImportSpecifier(
false,
undefined,
ts.factory.createIdentifier("useMutation"),
),
ts.factory.createImportSpecifier(
false,
undefined,
ts.factory.createIdentifier("UseQueryResult"),
),
ts.factory.createImportSpecifier(
false,
undefined,
ts.factory.createIdentifier("UseQueryOptions"),
),
ts.factory.createImportSpecifier(
false,
undefined,
ts.factory.createIdentifier("UseMutationOptions"),
),
ts.factory.createImportSpecifier(
false,
undefined,
ts.factory.createIdentifier("UseMutationResult"),
),
]),
),
ts.factory.createStringLiteral("@tanstack/react-query"),
undefined,
),
ts.factory.createImportDeclaration(
undefined,
ts.factory.createImportClause(
false,
undefined,
ts.factory.createNamedImports([
// import all class names from service file
...serviceNames.map((serviceName) =>
ts.factory.createImportSpecifier(
false,
undefined,
ts.factory.createIdentifier(serviceName),
),
),
]),
),
ts.factory.createStringLiteral(join("../requests", serviceFileName)),
undefined,
),
];
if (modelsFile) {
// import all the models by name
imports.push(
ts.factory.createImportDeclaration(
undefined,
ts.factory.createImportClause(
false,
undefined,
ts.factory.createNamedImports([
...modelNames.map((modelName) =>
ts.factory.createImportSpecifier(
false,
undefined,
ts.factory.createIdentifier(modelName),
),
),
]),
),
ts.factory.createStringLiteral(join("../requests/", modalsFileName)),
undefined,
),
);
}
return imports;
};