@nestia/sdk 2.5.0-dev.20240130 → 2.5.0-dev.20240130-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.
@@ -1,357 +1,357 @@
1
- import ts from "typescript";
2
- import { ExpressionFactory } from "typia/lib/factories/ExpressionFactory";
3
- import { IdentifierFactory } from "typia/lib/factories/IdentifierFactory";
4
- import { LiteralFactory } from "typia/lib/factories/LiteralFactory";
5
- import { StatementFactory } from "typia/lib/factories/StatementFactory";
6
- import { TypeFactory } from "typia/lib/factories/TypeFactory";
7
-
8
- import { INestiaConfig } from "../../INestiaConfig";
9
- import { IRoute } from "../../structures/IRoute";
10
- import { ImportDictionary } from "../../utils/ImportDictionary";
11
- import { SdkAliasCollection } from "./SdkAliasCollection";
12
- import { SdkImportWizard } from "./SdkImportWizard";
13
- import { SdkTypeProgrammer } from "./SdkTypeProgrammer";
14
-
15
- export namespace SdkSimulationProgrammer {
16
- export const random =
17
- (checker: ts.TypeChecker) =>
18
- (config: INestiaConfig) =>
19
- (importer: ImportDictionary) =>
20
- (route: IRoute): ts.VariableStatement =>
21
- constant("random")(
22
- ts.factory.createArrowFunction(
23
- undefined,
24
- undefined,
25
- [
26
- ts.factory.createParameterDeclaration(
27
- undefined,
28
- undefined,
29
- "g",
30
- ts.factory.createToken(ts.SyntaxKind.QuestionToken),
31
- ts.factory.createTypeReferenceNode(
32
- ts.factory.createIdentifier("Partial"),
33
- [
34
- ts.factory.createTypeReferenceNode(
35
- `${SdkImportWizard.typia(importer)}.IRandomGenerator`,
36
- ),
37
- ],
38
- ),
39
- ),
40
- ],
41
- undefined,
42
- undefined,
43
- ts.factory.createCallExpression(
44
- IdentifierFactory.access(
45
- ts.factory.createIdentifier(SdkImportWizard.typia(importer)),
46
- )("random"),
47
- [SdkAliasCollection.responseBody(checker)(config)(importer)(route)],
48
- [ts.factory.createIdentifier("g")],
49
- ),
50
- ),
51
- );
52
-
53
- export const simulate =
54
- (config: INestiaConfig) =>
55
- (importer: ImportDictionary) =>
56
- (
57
- route: IRoute,
58
- props: {
59
- headers: IRoute.IParameter | undefined;
60
- query: IRoute.IParameter | undefined;
61
- input: IRoute.IParameter | undefined;
62
- },
63
- ): ts.VariableStatement => {
64
- const output: boolean =
65
- config.propagate === true || route.output.typeName !== "void";
66
- const caller = () =>
67
- ts.factory.createCallExpression(
68
- ts.factory.createIdentifier("random"),
69
- undefined,
70
- [
71
- ts.factory.createConditionalExpression(
72
- ts.factory.createLogicalAnd(
73
- ts.factory.createStrictEquality(
74
- ts.factory.createStringLiteral("object"),
75
- ts.factory.createTypeOfExpression(
76
- ts.factory.createIdentifier("connection.simulate"),
77
- ),
78
- ),
79
- ts.factory.createStrictInequality(
80
- ts.factory.createNull(),
81
- ts.factory.createIdentifier("connection.simulate"),
82
- ),
83
- ),
84
- undefined,
85
- ts.factory.createIdentifier("connection.simulate"),
86
- undefined,
87
- ts.factory.createIdentifier("undefined"),
88
- ),
89
- ],
90
- );
91
-
92
- return constant("simulate")(
93
- ts.factory.createArrowFunction(
94
- undefined,
95
- undefined,
96
- [
97
- IdentifierFactory.parameter(
98
- "connection",
99
- ts.factory.createTypeReferenceNode(
100
- SdkImportWizard.IConnection(importer),
101
- route.parameters.some(
102
- (p) => p.category === "headers" && p.field === undefined,
103
- )
104
- ? [
105
- ts.factory.createTypeReferenceNode(
106
- `${route.name}.Headers`,
107
- ),
108
- ]
109
- : [],
110
- ),
111
- ),
112
- ...route.parameters
113
- .filter((p) => p.category !== "headers")
114
- .map((p) =>
115
- ts.factory.createParameterDeclaration(
116
- [],
117
- undefined,
118
- p.name,
119
- p.optional
120
- ? ts.factory.createToken(ts.SyntaxKind.QuestionToken)
121
- : undefined,
122
- config.primitive !== false &&
123
- (p === props.query || p === props.input)
124
- ? ts.factory.createTypeReferenceNode(
125
- `${route.name}.${p === props.query ? "Query" : "Input"}`,
126
- )
127
- : getTypeName(config)(importer)(p),
128
- ),
129
- ),
130
- ],
131
- ts.factory.createTypeReferenceNode(output ? "Output" : "void"),
132
- undefined,
133
- ts.factory.createBlock(
134
- [
135
- ...assert(config)(importer)(route),
136
- ts.factory.createReturnStatement(
137
- config.propagate
138
- ? ts.factory.createObjectLiteralExpression(
139
- [
140
- ts.factory.createPropertyAssignment(
141
- "success",
142
- ts.factory.createTrue(),
143
- ),
144
- ts.factory.createPropertyAssignment(
145
- "status",
146
- ExpressionFactory.number(
147
- route.status ??
148
- (route.method === "POST" ? 201 : 200),
149
- ),
150
- ),
151
- ts.factory.createPropertyAssignment(
152
- "headers",
153
- LiteralFactory.generate({
154
- "Content-Type": route.output.contentType,
155
- }),
156
- ),
157
- ts.factory.createPropertyAssignment("data", caller()),
158
- ],
159
- true,
160
- )
161
- : caller(),
162
- ),
163
- ],
164
- true,
165
- ),
166
- ),
167
- );
168
- };
169
-
170
- const assert =
171
- (config: INestiaConfig) =>
172
- (importer: ImportDictionary) =>
173
- (route: IRoute): ts.Statement[] => {
174
- const parameters = route.parameters.filter(
175
- (p) => p.category !== "headers",
176
- );
177
- if (parameters.length === 0) return [];
178
-
179
- const typia = SdkImportWizard.typia(importer);
180
- const validator = StatementFactory.constant(
181
- "assert",
182
- ts.factory.createCallExpression(
183
- IdentifierFactory.access(
184
- ts.factory.createIdentifier(
185
- importer.internal({
186
- file: `${config.output}/utils/NestiaSimulator.ts`,
187
- instance: "NestiaSimulator",
188
- type: false,
189
- }),
190
- ),
191
- )("assert"),
192
- undefined,
193
- [
194
- ts.factory.createObjectLiteralExpression(
195
- [
196
- ts.factory.createPropertyAssignment(
197
- "method",
198
- ts.factory.createIdentifier("METADATA.method"),
199
- ),
200
- ts.factory.createPropertyAssignment(
201
- "host",
202
- ts.factory.createIdentifier("connection.host"),
203
- ),
204
- ts.factory.createPropertyAssignment(
205
- "path",
206
- ts.factory.createCallExpression(
207
- ts.factory.createIdentifier("path"),
208
- undefined,
209
- route.parameters
210
- .filter(
211
- (p) => p.category === "param" || p.category === "query",
212
- )
213
- .map((p) => ts.factory.createIdentifier(p.name)),
214
- ),
215
- ),
216
- ts.factory.createPropertyAssignment(
217
- "contentType",
218
- ts.factory.createIdentifier(
219
- JSON.stringify(route.output.contentType),
220
- ),
221
- ),
222
- ],
223
- true,
224
- ),
225
- ],
226
- ),
227
- );
228
- const individual = parameters
229
- .map((p) =>
230
- ts.factory.createCallExpression(
231
- (() => {
232
- const base = IdentifierFactory.access(
233
- ts.factory.createIdentifier("assert"),
234
- )(p.category);
235
- if (p.category !== "param") return base;
236
- return ts.factory.createCallExpression(base, undefined, [
237
- ts.factory.createStringLiteral(p.name),
238
- ]);
239
- })(),
240
- undefined,
241
- [
242
- ts.factory.createArrowFunction(
243
- undefined,
244
- undefined,
245
- [],
246
- undefined,
247
- undefined,
248
- ts.factory.createCallExpression(
249
- IdentifierFactory.access(ts.factory.createIdentifier(typia))(
250
- "assert",
251
- ),
252
- undefined,
253
- [
254
- ts.factory.createIdentifier(
255
- p.category === "headers" ? "connection.headers" : p.name,
256
- ),
257
- ],
258
- ),
259
- ),
260
- ],
261
- ),
262
- )
263
- .map(ts.factory.createExpressionStatement);
264
-
265
- return [
266
- validator,
267
- ...(config.propagate !== true
268
- ? individual
269
- : [tryAndCatch(importer)(individual)]),
270
- ];
271
- };
272
-
273
- const tryAndCatch =
274
- (importer: ImportDictionary) => (individual: ts.Statement[]) =>
275
- ts.factory.createTryStatement(
276
- ts.factory.createBlock(individual, true),
277
- ts.factory.createCatchClause(
278
- "exp",
279
- ts.factory.createBlock(
280
- [
281
- ts.factory.createIfStatement(
282
- ts.factory.createLogicalNot(
283
- ts.factory.createCallExpression(
284
- IdentifierFactory.access(
285
- ts.factory.createIdentifier(
286
- SdkImportWizard.typia(importer),
287
- ),
288
- )("is"),
289
- [
290
- ts.factory.createTypeReferenceNode(
291
- SdkImportWizard.HttpError(importer),
292
- ),
293
- ],
294
- [ts.factory.createIdentifier("exp")],
295
- ),
296
- ),
297
- ts.factory.createThrowStatement(
298
- ts.factory.createIdentifier("exp"),
299
- ),
300
- ),
301
- ts.factory.createReturnStatement(
302
- ts.factory.createAsExpression(
303
- ts.factory.createObjectLiteralExpression(
304
- [
305
- ts.factory.createPropertyAssignment(
306
- "success",
307
- ts.factory.createFalse(),
308
- ),
309
- ts.factory.createPropertyAssignment(
310
- "status",
311
- ts.factory.createIdentifier("exp.status"),
312
- ),
313
- ts.factory.createPropertyAssignment(
314
- "headers",
315
- ts.factory.createIdentifier("exp.headers"),
316
- ),
317
- ts.factory.createPropertyAssignment(
318
- "data",
319
- ts.factory.createIdentifier("exp.toJSON().message"),
320
- ),
321
- ],
322
- true,
323
- ),
324
- TypeFactory.keyword("any"),
325
- ),
326
- ),
327
- ],
328
- true,
329
- ),
330
- ),
331
- undefined,
332
- );
333
- }
334
-
335
- const constant = (name: string) => (expression: ts.Expression) =>
336
- ts.factory.createVariableStatement(
337
- [ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
338
- ts.factory.createVariableDeclarationList(
339
- [
340
- ts.factory.createVariableDeclaration(
341
- ts.factory.createIdentifier(name),
342
- undefined,
343
- undefined,
344
- expression,
345
- ),
346
- ],
347
- ts.NodeFlags.Const,
348
- ),
349
- );
350
-
351
- const getTypeName =
352
- (config: INestiaConfig) =>
353
- (importer: ImportDictionary) =>
354
- (p: IRoute.IParameter | IRoute.IOutput) =>
355
- p.metadata
356
- ? SdkTypeProgrammer.decode(config)(importer)(p.metadata)
357
- : ts.factory.createTypeReferenceNode(p.typeName);
1
+ import ts from "typescript";
2
+ import { ExpressionFactory } from "typia/lib/factories/ExpressionFactory";
3
+ import { IdentifierFactory } from "typia/lib/factories/IdentifierFactory";
4
+ import { LiteralFactory } from "typia/lib/factories/LiteralFactory";
5
+ import { StatementFactory } from "typia/lib/factories/StatementFactory";
6
+ import { TypeFactory } from "typia/lib/factories/TypeFactory";
7
+
8
+ import { INestiaConfig } from "../../INestiaConfig";
9
+ import { IRoute } from "../../structures/IRoute";
10
+ import { ImportDictionary } from "../../utils/ImportDictionary";
11
+ import { SdkAliasCollection } from "./SdkAliasCollection";
12
+ import { SdkImportWizard } from "./SdkImportWizard";
13
+ import { SdkTypeProgrammer } from "./SdkTypeProgrammer";
14
+
15
+ export namespace SdkSimulationProgrammer {
16
+ export const random =
17
+ (checker: ts.TypeChecker) =>
18
+ (config: INestiaConfig) =>
19
+ (importer: ImportDictionary) =>
20
+ (route: IRoute): ts.VariableStatement =>
21
+ constant("random")(
22
+ ts.factory.createArrowFunction(
23
+ undefined,
24
+ undefined,
25
+ [
26
+ ts.factory.createParameterDeclaration(
27
+ undefined,
28
+ undefined,
29
+ "g",
30
+ ts.factory.createToken(ts.SyntaxKind.QuestionToken),
31
+ ts.factory.createTypeReferenceNode(
32
+ ts.factory.createIdentifier("Partial"),
33
+ [
34
+ ts.factory.createTypeReferenceNode(
35
+ `${SdkImportWizard.typia(importer)}.IRandomGenerator`,
36
+ ),
37
+ ],
38
+ ),
39
+ ),
40
+ ],
41
+ undefined,
42
+ undefined,
43
+ ts.factory.createCallExpression(
44
+ IdentifierFactory.access(
45
+ ts.factory.createIdentifier(SdkImportWizard.typia(importer)),
46
+ )("random"),
47
+ [SdkAliasCollection.responseBody(checker)(config)(importer)(route)],
48
+ [ts.factory.createIdentifier("g")],
49
+ ),
50
+ ),
51
+ );
52
+
53
+ export const simulate =
54
+ (config: INestiaConfig) =>
55
+ (importer: ImportDictionary) =>
56
+ (
57
+ route: IRoute,
58
+ props: {
59
+ headers: IRoute.IParameter | undefined;
60
+ query: IRoute.IParameter | undefined;
61
+ input: IRoute.IParameter | undefined;
62
+ },
63
+ ): ts.VariableStatement => {
64
+ const output: boolean =
65
+ config.propagate === true || route.output.typeName !== "void";
66
+ const caller = () =>
67
+ ts.factory.createCallExpression(
68
+ ts.factory.createIdentifier("random"),
69
+ undefined,
70
+ [
71
+ ts.factory.createConditionalExpression(
72
+ ts.factory.createLogicalAnd(
73
+ ts.factory.createStrictEquality(
74
+ ts.factory.createStringLiteral("object"),
75
+ ts.factory.createTypeOfExpression(
76
+ ts.factory.createIdentifier("connection.simulate"),
77
+ ),
78
+ ),
79
+ ts.factory.createStrictInequality(
80
+ ts.factory.createNull(),
81
+ ts.factory.createIdentifier("connection.simulate"),
82
+ ),
83
+ ),
84
+ undefined,
85
+ ts.factory.createIdentifier("connection.simulate"),
86
+ undefined,
87
+ ts.factory.createIdentifier("undefined"),
88
+ ),
89
+ ],
90
+ );
91
+
92
+ return constant("simulate")(
93
+ ts.factory.createArrowFunction(
94
+ undefined,
95
+ undefined,
96
+ [
97
+ IdentifierFactory.parameter(
98
+ "connection",
99
+ ts.factory.createTypeReferenceNode(
100
+ SdkImportWizard.IConnection(importer),
101
+ route.parameters.some(
102
+ (p) => p.category === "headers" && p.field === undefined,
103
+ )
104
+ ? [
105
+ ts.factory.createTypeReferenceNode(
106
+ `${route.name}.Headers`,
107
+ ),
108
+ ]
109
+ : [],
110
+ ),
111
+ ),
112
+ ...route.parameters
113
+ .filter((p) => p.category !== "headers")
114
+ .map((p) =>
115
+ ts.factory.createParameterDeclaration(
116
+ [],
117
+ undefined,
118
+ p.name,
119
+ p.optional
120
+ ? ts.factory.createToken(ts.SyntaxKind.QuestionToken)
121
+ : undefined,
122
+ config.primitive !== false &&
123
+ (p === props.query || p === props.input)
124
+ ? ts.factory.createTypeReferenceNode(
125
+ `${route.name}.${p === props.query ? "Query" : "Input"}`,
126
+ )
127
+ : getTypeName(config)(importer)(p),
128
+ ),
129
+ ),
130
+ ],
131
+ ts.factory.createTypeReferenceNode(output ? "Output" : "void"),
132
+ undefined,
133
+ ts.factory.createBlock(
134
+ [
135
+ ...assert(config)(importer)(route),
136
+ ts.factory.createReturnStatement(
137
+ config.propagate
138
+ ? ts.factory.createObjectLiteralExpression(
139
+ [
140
+ ts.factory.createPropertyAssignment(
141
+ "success",
142
+ ts.factory.createTrue(),
143
+ ),
144
+ ts.factory.createPropertyAssignment(
145
+ "status",
146
+ ExpressionFactory.number(
147
+ route.status ??
148
+ (route.method === "POST" ? 201 : 200),
149
+ ),
150
+ ),
151
+ ts.factory.createPropertyAssignment(
152
+ "headers",
153
+ LiteralFactory.generate({
154
+ "Content-Type": route.output.contentType,
155
+ }),
156
+ ),
157
+ ts.factory.createPropertyAssignment("data", caller()),
158
+ ],
159
+ true,
160
+ )
161
+ : caller(),
162
+ ),
163
+ ],
164
+ true,
165
+ ),
166
+ ),
167
+ );
168
+ };
169
+
170
+ const assert =
171
+ (config: INestiaConfig) =>
172
+ (importer: ImportDictionary) =>
173
+ (route: IRoute): ts.Statement[] => {
174
+ const parameters = route.parameters.filter(
175
+ (p) => p.category !== "headers",
176
+ );
177
+ if (parameters.length === 0) return [];
178
+
179
+ const typia = SdkImportWizard.typia(importer);
180
+ const validator = StatementFactory.constant(
181
+ "assert",
182
+ ts.factory.createCallExpression(
183
+ IdentifierFactory.access(
184
+ ts.factory.createIdentifier(
185
+ importer.internal({
186
+ file: `${config.output}/utils/NestiaSimulator.ts`,
187
+ instance: "NestiaSimulator",
188
+ type: false,
189
+ }),
190
+ ),
191
+ )("assert"),
192
+ undefined,
193
+ [
194
+ ts.factory.createObjectLiteralExpression(
195
+ [
196
+ ts.factory.createPropertyAssignment(
197
+ "method",
198
+ ts.factory.createIdentifier("METADATA.method"),
199
+ ),
200
+ ts.factory.createPropertyAssignment(
201
+ "host",
202
+ ts.factory.createIdentifier("connection.host"),
203
+ ),
204
+ ts.factory.createPropertyAssignment(
205
+ "path",
206
+ ts.factory.createCallExpression(
207
+ ts.factory.createIdentifier("path"),
208
+ undefined,
209
+ route.parameters
210
+ .filter(
211
+ (p) => p.category === "param" || p.category === "query",
212
+ )
213
+ .map((p) => ts.factory.createIdentifier(p.name)),
214
+ ),
215
+ ),
216
+ ts.factory.createPropertyAssignment(
217
+ "contentType",
218
+ ts.factory.createIdentifier(
219
+ JSON.stringify(route.output.contentType),
220
+ ),
221
+ ),
222
+ ],
223
+ true,
224
+ ),
225
+ ],
226
+ ),
227
+ );
228
+ const individual = parameters
229
+ .map((p) =>
230
+ ts.factory.createCallExpression(
231
+ (() => {
232
+ const base = IdentifierFactory.access(
233
+ ts.factory.createIdentifier("assert"),
234
+ )(p.category);
235
+ if (p.category !== "param") return base;
236
+ return ts.factory.createCallExpression(base, undefined, [
237
+ ts.factory.createStringLiteral(p.name),
238
+ ]);
239
+ })(),
240
+ undefined,
241
+ [
242
+ ts.factory.createArrowFunction(
243
+ undefined,
244
+ undefined,
245
+ [],
246
+ undefined,
247
+ undefined,
248
+ ts.factory.createCallExpression(
249
+ IdentifierFactory.access(ts.factory.createIdentifier(typia))(
250
+ "assert",
251
+ ),
252
+ undefined,
253
+ [
254
+ ts.factory.createIdentifier(
255
+ p.category === "headers" ? "connection.headers" : p.name,
256
+ ),
257
+ ],
258
+ ),
259
+ ),
260
+ ],
261
+ ),
262
+ )
263
+ .map(ts.factory.createExpressionStatement);
264
+
265
+ return [
266
+ validator,
267
+ ...(config.propagate !== true
268
+ ? individual
269
+ : [tryAndCatch(importer)(individual)]),
270
+ ];
271
+ };
272
+
273
+ const tryAndCatch =
274
+ (importer: ImportDictionary) => (individual: ts.Statement[]) =>
275
+ ts.factory.createTryStatement(
276
+ ts.factory.createBlock(individual, true),
277
+ ts.factory.createCatchClause(
278
+ "exp",
279
+ ts.factory.createBlock(
280
+ [
281
+ ts.factory.createIfStatement(
282
+ ts.factory.createLogicalNot(
283
+ ts.factory.createCallExpression(
284
+ IdentifierFactory.access(
285
+ ts.factory.createIdentifier(
286
+ SdkImportWizard.typia(importer),
287
+ ),
288
+ )("is"),
289
+ [
290
+ ts.factory.createTypeReferenceNode(
291
+ SdkImportWizard.HttpError(importer),
292
+ ),
293
+ ],
294
+ [ts.factory.createIdentifier("exp")],
295
+ ),
296
+ ),
297
+ ts.factory.createThrowStatement(
298
+ ts.factory.createIdentifier("exp"),
299
+ ),
300
+ ),
301
+ ts.factory.createReturnStatement(
302
+ ts.factory.createAsExpression(
303
+ ts.factory.createObjectLiteralExpression(
304
+ [
305
+ ts.factory.createPropertyAssignment(
306
+ "success",
307
+ ts.factory.createFalse(),
308
+ ),
309
+ ts.factory.createPropertyAssignment(
310
+ "status",
311
+ ts.factory.createIdentifier("exp.status"),
312
+ ),
313
+ ts.factory.createPropertyAssignment(
314
+ "headers",
315
+ ts.factory.createIdentifier("exp.headers"),
316
+ ),
317
+ ts.factory.createPropertyAssignment(
318
+ "data",
319
+ ts.factory.createIdentifier("exp.toJSON().message"),
320
+ ),
321
+ ],
322
+ true,
323
+ ),
324
+ TypeFactory.keyword("any"),
325
+ ),
326
+ ),
327
+ ],
328
+ true,
329
+ ),
330
+ ),
331
+ undefined,
332
+ );
333
+ }
334
+
335
+ const constant = (name: string) => (expression: ts.Expression) =>
336
+ ts.factory.createVariableStatement(
337
+ [ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
338
+ ts.factory.createVariableDeclarationList(
339
+ [
340
+ ts.factory.createVariableDeclaration(
341
+ ts.factory.createIdentifier(name),
342
+ undefined,
343
+ undefined,
344
+ expression,
345
+ ),
346
+ ],
347
+ ts.NodeFlags.Const,
348
+ ),
349
+ );
350
+
351
+ const getTypeName =
352
+ (config: INestiaConfig) =>
353
+ (importer: ImportDictionary) =>
354
+ (p: IRoute.IParameter | IRoute.IOutput) =>
355
+ p.metadata
356
+ ? SdkTypeProgrammer.decode(config)(importer)(p.metadata)
357
+ : ts.factory.createTypeReferenceNode(p.typeName);