@nestbridge/core 1.1.0 → 1.1.1

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,990 +1,915 @@
1
- import { readFileSync, existsSync } from 'node:fs';
2
- import ts from 'typescript';
3
- import { resolve, dirname, relative, extname } from 'node:path';
4
-
5
- const findDecorator = (node, decoratorName) => {
6
- const decorators = ts.getDecorators(node);
7
- if (decorators === void 0) {
8
- return void 0;
9
- }
10
- return decorators.find((decorator) => decoratorNameOf(decorator) === decoratorName);
11
- };
12
- const findAnyDecorator = (node, decoratorNames) => {
13
- const decorators = ts.getDecorators(node);
14
- if (decorators === void 0) {
15
- return void 0;
16
- }
17
- return decorators.find((decorator) => decoratorNames.includes(decoratorNameOf(decorator) ?? ""));
18
- };
19
- const decoratorNameOf = (decorator) => {
20
- const expression = decorator.expression;
21
- if (ts.isCallExpression(expression) && ts.isIdentifier(expression.expression)) {
22
- return expression.expression.text;
23
- }
24
- if (ts.isIdentifier(expression)) {
25
- return expression.text;
26
- }
27
- return void 0;
28
- };
29
- const readStaticStringArgument = (decorator) => {
30
- const expression = decorator.expression;
31
- if (!ts.isCallExpression(expression)) {
32
- return { ok: true, value: void 0 };
33
- }
34
- const [firstArgument] = expression.arguments;
35
- if (firstArgument === void 0) {
36
- return { ok: true, value: void 0 };
37
- }
38
- if (ts.isStringLiteralLike(firstArgument)) {
39
- return { ok: true, value: firstArgument.text };
40
- }
41
- return { ok: false, argumentText: firstArgument.getText() };
42
- };
43
-
44
- const HTTP_METHOD_DECORATOR_NAMES = {
45
- Get: "GET",
46
- Post: "POST",
47
- Put: "PUT",
48
- Patch: "PATCH",
49
- Delete: "DELETE"
50
- };
51
-
52
- const locationOf = (sourceFile, node) => {
53
- const { line, character } = sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile));
54
- return { line: line + 1, column: character + 1 };
55
- };
56
-
57
- const SUPPORTED_PARAMETER_DECORATORS = ["Param", "Query", "Body", "Headers"];
58
- const KNOWN_UNSUPPORTED_PARAMETER_DECORATORS = ["Req", "Res", "Request", "Response"];
59
- const extractParameterDefinition = (parameter, index, context) => {
60
- const decorator = findAnyDecorator(parameter, [
61
- ...SUPPORTED_PARAMETER_DECORATORS,
62
- ...KNOWN_UNSUPPORTED_PARAMETER_DECORATORS
63
- ]);
64
- const location = locationOf(context.sourceFile, parameter);
65
- if (decorator === void 0) {
66
- context.diagnostics.push({
67
- code: "unsupported-parameter",
68
- title: "Unsupported parameter.",
69
- controllerName: context.controllerName,
70
- memberName: context.methodName,
71
- detail: `Parameter "${parameter.name.getText()}" must be decorated with one of @Param, @Query, @Body, or @Headers to be transported through a NestBridge HTTP client.`,
72
- filePath: context.filePath,
73
- line: location.line,
74
- column: location.column
75
- });
76
- return void 0;
77
- }
78
- const decoratorName = decoratorNameOf(decorator);
79
- if (decoratorName !== void 0 && KNOWN_UNSUPPORTED_PARAMETER_DECORATORS.includes(decoratorName)) {
80
- context.diagnostics.push({
81
- code: "unsupported-parameter",
82
- title: "Unsupported parameter.",
83
- controllerName: context.controllerName,
84
- memberName: context.methodName,
85
- detail: `@${decoratorName}() cannot be transported through a NestBridge HTTP client.`,
86
- filePath: context.filePath,
87
- line: location.line,
88
- column: location.column
89
- });
90
- return void 0;
91
- }
92
- if (!ts.isIdentifier(parameter.name)) {
93
- context.diagnostics.push({
94
- code: "unsupported-parameter",
95
- title: "Unsupported parameter.",
96
- controllerName: context.controllerName,
97
- memberName: context.methodName,
98
- detail: "Destructured parameters cannot be transported through a NestBridge HTTP client.",
99
- filePath: context.filePath,
100
- line: location.line,
101
- column: location.column
102
- });
103
- return void 0;
104
- }
105
- const argument = readStaticStringArgument(decorator);
106
- if (!argument.ok) {
107
- context.diagnostics.push({
108
- code: "unsupported-parameter",
109
- title: "Unsupported parameter.",
110
- controllerName: context.controllerName,
111
- memberName: context.methodName,
112
- detail: `@${decoratorName}() arguments must be statically analyzable string literals.`,
113
- found: `@${decoratorName}(${argument.argumentText})`,
114
- filePath: context.filePath,
115
- line: location.line,
116
- column: location.column
117
- });
118
- return void 0;
119
- }
120
- const base = {
121
- index,
122
- parameterName: parameter.name.text
123
- };
124
- if (decoratorName === "Param") {
125
- if (argument.value === void 0) {
126
- context.diagnostics.push({
127
- code: "unsupported-parameter",
128
- title: "Unsupported parameter.",
129
- controllerName: context.controllerName,
130
- memberName: context.methodName,
131
- detail: "A whole @Param() is not supported; provide a route parameter name such as @Param('id').",
132
- filePath: context.filePath,
133
- line: location.line,
134
- column: location.column
135
- });
136
- return void 0;
137
- }
138
- return { ...base, kind: "param", name: argument.value };
139
- }
140
- if (decoratorName === "Headers") {
141
- if (argument.value === void 0) {
142
- context.diagnostics.push({
143
- code: "unsupported-parameter",
144
- title: "Unsupported parameter.",
145
- controllerName: context.controllerName,
146
- memberName: context.methodName,
147
- detail: "A whole @Headers() is not supported; provide a header name such as @Headers('x-example').",
148
- filePath: context.filePath,
149
- line: location.line,
150
- column: location.column
151
- });
152
- return void 0;
153
- }
154
- return { ...base, kind: "header", name: argument.value };
155
- }
156
- if (decoratorName === "Query") {
157
- return { ...base, kind: "query", name: argument.value };
158
- }
159
- return { ...base, kind: "body", name: argument.value };
160
- };
161
-
162
- const PRIMITIVE_SCALAR_TYPES = {
163
- string: "String",
164
- number: "Float",
165
- boolean: "Boolean"
166
- };
167
- const unwrapPromise = (typeNode) => {
168
- if (ts.isTypeReferenceNode(typeNode) && ts.isIdentifier(typeNode.typeName) && typeNode.typeName.text === "Promise" && typeNode.typeArguments?.[0] !== void 0) {
169
- return unwrapPromise(typeNode.typeArguments[0]);
170
- }
171
- return typeNode;
172
- };
173
- const unwrapNullable = (typeNode) => {
174
- if (!ts.isUnionTypeNode(typeNode)) {
175
- return { inner: typeNode, isNullable: false };
176
- }
177
- const nonNullMembers = typeNode.types.filter(
178
- (member) => member.kind !== ts.SyntaxKind.NullKeyword && member.kind !== ts.SyntaxKind.UndefinedKeyword
179
- );
180
- const [onlyMember] = nonNullMembers;
181
- if (nonNullMembers.length !== 1 || onlyMember === void 0) {
182
- return { inner: typeNode, isNullable: nonNullMembers.length !== typeNode.types.length };
183
- }
184
- return { inner: onlyMember, isNullable: true };
185
- };
186
- const unwrapArrayElement = (typeNode) => {
187
- if (ts.isArrayTypeNode(typeNode)) {
188
- return typeNode.elementType;
189
- }
190
- if (ts.isTypeReferenceNode(typeNode) && ts.isIdentifier(typeNode.typeName) && typeNode.typeName.text === "Array" && typeNode.typeArguments?.[0] !== void 0) {
191
- return typeNode.typeArguments[0];
192
- }
193
- return void 0;
194
- };
195
- const identifierTextOf = (typeNode) => {
196
- if (ts.isTypeReferenceNode(typeNode) && ts.isIdentifier(typeNode.typeName)) {
197
- return typeNode.typeName.text;
198
- }
199
- switch (typeNode.kind) {
200
- case ts.SyntaxKind.StringKeyword:
201
- return "string";
202
- case ts.SyntaxKind.NumberKeyword:
203
- return "number";
204
- case ts.SyntaxKind.BooleanKeyword:
205
- return "boolean";
206
- default:
207
- return void 0;
208
- }
209
- };
210
- const unwrapTypeNode = (typeNode) => {
211
- const withoutPromise = unwrapPromise(typeNode);
212
- const { inner: withoutNullable, isNullable } = unwrapNullable(withoutPromise);
213
- const arrayElement = unwrapArrayElement(withoutNullable);
214
- const element = arrayElement === void 0 ? withoutNullable : unwrapNullable(arrayElement).inner;
215
- const identifierText = identifierTextOf(element);
216
- if (identifierText === void 0) {
217
- return void 0;
218
- }
219
- return { identifierText, isArray: arrayElement !== void 0, isNullable };
220
- };
221
- const graphqlScalarNameFor = (identifierText) => PRIMITIVE_SCALAR_TYPES[identifierText] ?? identifierText;
222
- const graphqlTypeStringFor = (unwrapped, explicitTypeName) => {
223
- const baseName = explicitTypeName ?? graphqlScalarNameFor(unwrapped.identifierText);
224
- const wrapped = unwrapped.isArray ? `[${baseName}!]` : baseName;
225
- return unwrapped.isNullable ? wrapped : `${wrapped}!`;
226
- };
227
-
228
- const OBSERVABLE_TYPE_NAME = "Observable";
229
- const STREAMABLE_FILE_TYPE_NAME = "StreamableFile";
230
- const readResponseKind = (returnType) => {
231
- if (returnType === void 0) {
232
- return "json";
233
- }
234
- const unwrapped = unwrapPromise(returnType);
235
- if (!ts.isTypeReferenceNode(unwrapped) || !ts.isIdentifier(unwrapped.typeName)) {
236
- return "json";
237
- }
238
- if (unwrapped.typeName.text === OBSERVABLE_TYPE_NAME) {
239
- return "observable";
240
- }
241
- if (unwrapped.typeName.text === STREAMABLE_FILE_TYPE_NAME) {
242
- return "stream";
243
- }
244
- return "json";
245
- };
246
-
247
- const trimSlashes = (segment) => segment.replace(/^\/+/, "").replace(/\/+$/, "");
248
- const joinRoutePath = (controllerPath, methodPath) => {
249
- const segments = [trimSlashes(controllerPath), trimSlashes(methodPath ?? "")].filter(
250
- (segment) => segment.length > 0
251
- );
252
- return `/${segments.join("/")}`;
253
- };
254
-
255
- const extractMethodDefinition = (method, context) => {
256
- const decorator = findAnyDecorator(method, Object.keys(HTTP_METHOD_DECORATOR_NAMES));
257
- if (decorator === void 0) {
258
- return void 0;
259
- }
260
- const methodName = method.name.getText();
261
- const decoratorName = decoratorNameOf(decorator);
262
- const location = locationOf(context.sourceFile, method);
263
- if (decoratorName === void 0) {
264
- return void 0;
265
- }
266
- const httpMethod = HTTP_METHOD_DECORATOR_NAMES[decoratorName];
267
- if (httpMethod === void 0) {
268
- return void 0;
269
- }
270
- const routeArgument = readStaticStringArgument(decorator);
271
- if (!routeArgument.ok) {
272
- context.diagnostics.push({
273
- code: "unsupported-route",
274
- title: "Unsupported route.",
275
- controllerName: context.controllerName,
276
- memberName: methodName,
277
- detail: "Route paths must be statically analyzable.",
278
- found: `@${decoratorName}(${routeArgument.argumentText})`,
279
- filePath: context.filePath,
280
- line: location.line,
281
- column: location.column
282
- });
283
- return void 0;
284
- }
285
- const parameters = method.parameters.map(
286
- (parameter, index) => extractParameterDefinition(parameter, index, {
287
- sourceFile: context.sourceFile,
288
- filePath: context.filePath,
289
- controllerName: context.controllerName,
290
- methodName,
291
- diagnostics: context.diagnostics
292
- })
293
- );
294
- if (parameters.some((parameter) => parameter === void 0)) {
295
- return void 0;
296
- }
297
- return {
298
- name: methodName,
299
- httpMethod,
300
- path: joinRoutePath(context.controllerPath, routeArgument.value),
301
- parameters: parameters.filter((parameter) => parameter !== void 0),
302
- responseKind: readResponseKind(method.type),
303
- line: location.line,
304
- column: location.column
305
- };
306
- };
307
-
308
- const extractControllerDefinition = (classDeclaration, sourceFile, filePath, diagnostics) => {
309
- const controllerName = classDeclaration.name?.text ?? "(anonymous controller)";
310
- const decorator = findDecorator(classDeclaration, "Controller");
311
- if (decorator === void 0) {
312
- return void 0;
313
- }
314
- const location = locationOf(sourceFile, classDeclaration);
315
- const argument = readStaticStringArgument(decorator);
316
- if (!argument.ok) {
317
- diagnostics.push({
318
- code: "unsupported-route",
319
- title: "Unsupported route.",
320
- controllerName,
321
- memberName: "",
322
- detail: "Controller paths must be statically analyzable.",
323
- found: `@Controller(${argument.argumentText})`,
324
- filePath,
325
- line: location.line,
326
- column: location.column
327
- });
328
- return void 0;
329
- }
330
- const methods = classDeclaration.members.filter((member) => ts.isMethodDeclaration(member)).map(
331
- (method) => extractMethodDefinition(method, {
332
- sourceFile,
333
- filePath,
334
- controllerName,
335
- controllerPath: argument.value ?? "",
336
- diagnostics
337
- })
338
- ).filter((method) => method !== void 0);
339
- return {
340
- name: controllerName,
341
- path: argument.value ?? "",
342
- sourceFile: filePath,
343
- methods
344
- };
345
- };
346
-
347
- const findControllerClasses = (sourceFile) => {
348
- return sourceFile.statements.filter(
349
- (statement) => ts.isClassDeclaration(statement) && findDecorator(statement, "Controller") !== void 0
350
- );
351
- };
352
-
353
- const analyzeControllers = (filePaths) => {
354
- const diagnostics = [];
355
- const controllers = [];
356
- for (const filePath of filePaths) {
357
- const content = readFileSync(filePath, "utf-8");
358
- const sourceFile = ts.createSourceFile(
359
- filePath,
360
- content,
361
- ts.ScriptTarget.ESNext,
362
- true,
363
- ts.ScriptKind.TS
364
- );
365
- for (const classDeclaration of findControllerClasses(sourceFile)) {
366
- const controller = extractControllerDefinition(
367
- classDeclaration,
368
- sourceFile,
369
- filePath,
370
- diagnostics
371
- );
372
- if (controller !== void 0) {
373
- controllers.push(controller);
374
- }
375
- }
376
- }
377
- return { controllers, diagnostics };
378
- };
379
-
380
- const GRAPHQL_OPERATION_DECORATOR_NAMES = {
381
- Query: "query",
382
- Mutation: "mutation"
383
- };
384
-
385
- const readTypeThunkExpression = (thunk) => {
386
- if (!ts.isBlock(thunk.body)) {
387
- return thunk.body;
388
- }
389
- const returnStatement = thunk.body.statements.find(ts.isReturnStatement);
390
- return returnStatement?.expression;
391
- };
392
-
393
- const readArgsTypeOption = (decorator) => {
394
- if (!ts.isCallExpression(decorator.expression)) {
395
- return void 0;
396
- }
397
- const optionsArgument = decorator.expression.arguments.find(ts.isObjectLiteralExpression);
398
- if (optionsArgument === void 0) {
399
- return void 0;
400
- }
401
- const typeProperty = optionsArgument.properties.find(
402
- (property) => ts.isPropertyAssignment(property) && ts.isIdentifier(property.name) && property.name.text === "type"
403
- );
404
- if (typeProperty === void 0) {
405
- return void 0;
406
- }
407
- const thunk = typeProperty.initializer;
408
- if (!ts.isArrowFunction(thunk) && !ts.isFunctionExpression(thunk)) {
409
- return void 0;
410
- }
411
- const returnedExpression = readTypeThunkExpression(thunk);
412
- if (returnedExpression === void 0) {
413
- return void 0;
414
- }
415
- if (ts.isArrayLiteralExpression(returnedExpression)) {
416
- const element = returnedExpression.elements[0];
417
- return element !== void 0 && ts.isIdentifier(element) ? { typeName: element.text, isArray: true } : void 0;
418
- }
419
- return ts.isIdentifier(returnedExpression) ? { typeName: returnedExpression.text, isArray: false } : void 0;
420
- };
421
-
422
- const extractResolverArgumentDefinition = (parameter, index, context) => {
423
- const decorator = findAnyDecorator(parameter, ["Args"]);
424
- const location = locationOf(context.sourceFile, parameter);
425
- if (decorator === void 0) {
426
- context.diagnostics.push({
427
- code: "unsupported-argument",
428
- title: "Unsupported argument.",
429
- controllerName: context.resolverName,
430
- memberName: context.methodName,
431
- detail: `Parameter "${parameter.name.getText()}" must be decorated with @Args to be transported through a NestBridge GraphQL client.`,
432
- filePath: context.filePath,
433
- line: location.line,
434
- column: location.column
435
- });
436
- return void 0;
437
- }
438
- if (!ts.isIdentifier(parameter.name)) {
439
- context.diagnostics.push({
440
- code: "unsupported-argument",
441
- title: "Unsupported argument.",
442
- controllerName: context.resolverName,
443
- memberName: context.methodName,
444
- detail: "Destructured parameters cannot be transported through a NestBridge GraphQL client.",
445
- filePath: context.filePath,
446
- line: location.line,
447
- column: location.column
448
- });
449
- return void 0;
450
- }
451
- const nameArgument = readStaticStringArgument(decorator);
452
- if (!nameArgument.ok) {
453
- context.diagnostics.push({
454
- code: "unsupported-argument",
455
- title: "Unsupported argument.",
456
- controllerName: context.resolverName,
457
- memberName: context.methodName,
458
- detail: "@Args() name must be a statically analyzable string literal.",
459
- found: `@Args(${nameArgument.argumentText})`,
460
- filePath: context.filePath,
461
- line: location.line,
462
- column: location.column
463
- });
464
- return void 0;
465
- }
466
- if (nameArgument.value === void 0) {
467
- context.diagnostics.push({
468
- code: "unsupported-argument",
469
- title: "Unsupported argument.",
470
- controllerName: context.resolverName,
471
- memberName: context.methodName,
472
- detail: "A whole @Args() is not supported; provide an argument name such as @Args('id').",
473
- filePath: context.filePath,
474
- line: location.line,
475
- column: location.column
476
- });
477
- return void 0;
478
- }
479
- if (parameter.type === void 0) {
480
- context.diagnostics.push({
481
- code: "unsupported-argument",
482
- title: "Unsupported argument.",
483
- controllerName: context.resolverName,
484
- memberName: context.methodName,
485
- detail: `@Args() parameter "${parameter.name.text}" needs an explicit type annotation.`,
486
- filePath: context.filePath,
487
- line: location.line,
488
- column: location.column
489
- });
490
- return void 0;
491
- }
492
- const unwrapped = unwrapTypeNode(parameter.type);
493
- if (unwrapped === void 0) {
494
- context.diagnostics.push({
495
- code: "unsupported-argument",
496
- title: "Unsupported argument.",
497
- controllerName: context.resolverName,
498
- memberName: context.methodName,
499
- detail: `@Args() parameter "${parameter.name.text}" has a type NestBridge cannot statically resolve to a GraphQL type.`,
500
- found: parameter.type.getText(),
501
- filePath: context.filePath,
502
- line: location.line,
503
- column: location.column
504
- });
505
- return void 0;
506
- }
507
- const explicitType = readArgsTypeOption(decorator);
508
- return {
509
- index,
510
- parameterName: parameter.name.text,
511
- name: nameArgument.value,
512
- graphqlType: graphqlTypeStringFor(unwrapped, explicitType?.typeName)
513
- };
514
- };
515
-
516
- const parsedFilesByPath = /* @__PURE__ */ new Map();
517
- const parseFile = (filePath) => {
518
- const cached = parsedFilesByPath.get(filePath);
519
- if (cached !== void 0) {
520
- return cached;
521
- }
522
- const content = readFileSync(filePath, "utf-8");
523
- const sourceFile = ts.createSourceFile(
524
- filePath,
525
- content,
526
- ts.ScriptTarget.ESNext,
527
- true,
528
- ts.ScriptKind.TS
529
- );
530
- parsedFilesByPath.set(filePath, sourceFile);
531
- return sourceFile;
532
- };
533
- const resolveModuleFile = (fromFilePath, moduleSpecifierText) => {
534
- if (!moduleSpecifierText.startsWith(".")) {
535
- return void 0;
536
- }
537
- const base = resolve(dirname(fromFilePath), moduleSpecifierText);
538
- const candidates = [`${base}.ts`, `${base}.tsx`, resolve(base, "index.ts")];
539
- return candidates.find((candidate) => existsSync(candidate));
540
- };
541
- const findExportedDeclaration = (sourceFile, name) => sourceFile.statements.find(
542
- (statement) => ts.isClassDeclaration(statement) && statement.name?.text === name
543
- );
544
- const findImportSource = (sourceFile, localName) => {
545
- for (const statement of sourceFile.statements) {
546
- if (!ts.isImportDeclaration(statement) || !ts.isStringLiteralLike(statement.moduleSpecifier)) {
547
- continue;
548
- }
549
- const namedBindings = statement.importClause?.namedBindings;
550
- if (namedBindings === void 0 || !ts.isNamedImports(namedBindings)) {
551
- continue;
552
- }
553
- const element = namedBindings.elements.find((candidate) => candidate.name.text === localName);
554
- if (element !== void 0) {
555
- return {
556
- moduleSpecifierText: statement.moduleSpecifier.text,
557
- importedName: element.propertyName?.text ?? element.name.text
558
- };
559
- }
560
- }
561
- return void 0;
562
- };
563
- const findReExportSource = (sourceFile, name) => {
564
- for (const statement of sourceFile.statements) {
565
- if (!ts.isExportDeclaration(statement) || statement.moduleSpecifier === void 0 || !ts.isStringLiteralLike(statement.moduleSpecifier)) {
566
- continue;
567
- }
568
- if (statement.exportClause === void 0) {
569
- return statement.moduleSpecifier.text;
570
- }
571
- if (ts.isNamedExports(statement.exportClause) && statement.exportClause.elements.some(
572
- (element) => (element.propertyName?.text ?? element.name.text) === name
573
- )) {
574
- return statement.moduleSpecifier.text;
575
- }
576
- }
577
- return void 0;
578
- };
579
- const resolveClassDeclaration = (name, fromSourceFile, fromFilePath, visited = /* @__PURE__ */ new Set()) => {
580
- if (visited.has(fromFilePath)) {
581
- return void 0;
582
- }
583
- visited.add(fromFilePath);
584
- const direct = findExportedDeclaration(fromSourceFile, name);
585
- if (direct !== void 0) {
586
- return { classDeclaration: direct, sourceFile: fromSourceFile, filePath: fromFilePath };
587
- }
588
- const importSource = findImportSource(fromSourceFile, name);
589
- if (importSource !== void 0) {
590
- const resolvedPath = resolveModuleFile(fromFilePath, importSource.moduleSpecifierText);
591
- if (resolvedPath !== void 0) {
592
- const nextSourceFile = parseFile(resolvedPath);
593
- const result = resolveClassDeclaration(
594
- importSource.importedName,
595
- nextSourceFile,
596
- resolvedPath,
597
- visited
598
- );
599
- if (result !== void 0) {
600
- return result;
601
- }
602
- }
603
- }
604
- const reExportModuleSpecifierText = findReExportSource(fromSourceFile, name);
605
- if (reExportModuleSpecifierText !== void 0) {
606
- const resolvedPath = resolveModuleFile(fromFilePath, reExportModuleSpecifierText);
607
- if (resolvedPath !== void 0) {
608
- const nextSourceFile = parseFile(resolvedPath);
609
- return resolveClassDeclaration(name, nextSourceFile, resolvedPath, visited);
610
- }
611
- }
612
- return void 0;
613
- };
614
-
615
- const PRIMITIVE_SCALAR_NAMES$1 = /* @__PURE__ */ new Set(["string", "number", "boolean"]);
616
- const extractSelectionFields = (classDeclaration, sourceFile, filePath, context) => {
617
- const typeName = classDeclaration.name?.text ?? "(anonymous type)";
618
- const location = locationOf(sourceFile, classDeclaration);
619
- if (context.visitedTypeNames.has(typeName)) {
620
- context.diagnostics.push({
621
- code: "circular-type",
622
- title: "Circular type.",
623
- controllerName: context.resolverName,
624
- memberName: context.methodName,
625
- detail: `Type "${typeName}" references itself, directly or indirectly. NestBridge cannot build a finite GraphQL selection set for it.`,
626
- filePath,
627
- line: location.line,
628
- column: location.column
629
- });
630
- return void 0;
631
- }
632
- const nextVisited = new Set(context.visitedTypeNames).add(typeName);
633
- const properties = classDeclaration.members.filter(
634
- (member) => ts.isPropertyDeclaration(member) && findDecorator(member, "Field") !== void 0
635
- );
636
- const fields = [];
637
- for (const property of properties) {
638
- if (!ts.isIdentifier(property.name)) {
639
- continue;
640
- }
641
- const fieldName = property.name.text;
642
- const fieldLocation = locationOf(sourceFile, property);
643
- if (property.type === void 0) {
644
- context.diagnostics.push({
645
- code: "unsupported-return-type",
646
- title: "Unsupported field type.",
647
- controllerName: context.resolverName,
648
- memberName: context.methodName,
649
- detail: `Field "${fieldName}" on "${typeName}" needs an explicit type annotation.`,
650
- filePath,
651
- line: fieldLocation.line,
652
- column: fieldLocation.column
653
- });
654
- return void 0;
655
- }
656
- const unwrapped = unwrapTypeNode(property.type);
657
- if (unwrapped === void 0) {
658
- context.diagnostics.push({
659
- code: "unsupported-return-type",
660
- title: "Unsupported field type.",
661
- controllerName: context.resolverName,
662
- memberName: context.methodName,
663
- detail: `Field "${fieldName}" on "${typeName}" has a type NestBridge cannot statically resolve.`,
664
- found: property.type.getText(),
665
- filePath,
666
- line: fieldLocation.line,
667
- column: fieldLocation.column
668
- });
669
- return void 0;
670
- }
671
- if (PRIMITIVE_SCALAR_NAMES$1.has(unwrapped.identifierText)) {
672
- fields.push({ name: fieldName });
673
- continue;
674
- }
675
- const resolved = resolveClassDeclaration(unwrapped.identifierText, sourceFile, filePath);
676
- if (resolved === void 0) {
677
- fields.push({ name: fieldName });
678
- continue;
679
- }
680
- if (findDecorator(resolved.classDeclaration, "ObjectType") === void 0) {
681
- context.diagnostics.push({
682
- code: "unsupported-return-type",
683
- title: "Unsupported field type.",
684
- controllerName: context.resolverName,
685
- memberName: context.methodName,
686
- detail: `Field "${fieldName}" on "${typeName}" resolves to class "${unwrapped.identifierText}", which is not decorated with @ObjectType(). NestBridge cannot build a GraphQL selection set for it.`,
687
- filePath,
688
- line: fieldLocation.line,
689
- column: fieldLocation.column
690
- });
691
- return void 0;
692
- }
693
- const children = extractSelectionFields(
694
- resolved.classDeclaration,
695
- resolved.sourceFile,
696
- resolved.filePath,
697
- {
698
- ...context,
699
- visitedTypeNames: nextVisited
700
- }
701
- );
702
- if (children === void 0) {
703
- return void 0;
704
- }
705
- fields.push({ name: fieldName, children });
706
- }
707
- return fields;
708
- };
709
-
710
- const readNameFromOptions = (optionsArgument) => {
711
- const nameProperty = optionsArgument.properties.find(
712
- (property) => ts.isPropertyAssignment(property) && ts.isIdentifier(property.name) && property.name.text === "name"
713
- );
714
- if (nameProperty === void 0 || !ts.isStringLiteralLike(nameProperty.initializer)) {
715
- return void 0;
716
- }
717
- return nameProperty.initializer.text;
718
- };
719
- const readGraphqlOperationName = (decorator) => {
720
- const expression = decorator.expression;
721
- if (!ts.isCallExpression(expression)) {
722
- return { ok: true, value: void 0 };
723
- }
724
- let value;
725
- for (const argument of expression.arguments) {
726
- if (ts.isArrowFunction(argument) || ts.isFunctionExpression(argument)) {
727
- continue;
728
- }
729
- if (ts.isStringLiteralLike(argument)) {
730
- value = argument.text;
731
- continue;
732
- }
733
- if (ts.isObjectLiteralExpression(argument)) {
734
- value = readNameFromOptions(argument) ?? value;
735
- continue;
736
- }
737
- return { ok: false, argumentText: argument.getText() };
738
- }
739
- return { ok: true, value };
740
- };
741
-
742
- const readGraphqlReturnTypeOption = (decorator) => {
743
- if (!ts.isCallExpression(decorator.expression)) {
744
- return void 0;
745
- }
746
- const thunk = decorator.expression.arguments.find(
747
- (argument) => ts.isArrowFunction(argument) || ts.isFunctionExpression(argument)
748
- );
749
- if (thunk === void 0) {
750
- return void 0;
751
- }
752
- const returnedExpression = readTypeThunkExpression(thunk);
753
- if (returnedExpression === void 0) {
754
- return void 0;
755
- }
756
- if (ts.isArrayLiteralExpression(returnedExpression)) {
757
- const element = returnedExpression.elements[0];
758
- return element !== void 0 && ts.isIdentifier(element) ? element.text : void 0;
759
- }
760
- return ts.isIdentifier(returnedExpression) ? returnedExpression.text : void 0;
761
- };
762
-
763
- const PRIMITIVE_SCALAR_NAMES = /* @__PURE__ */ new Set(["string", "number", "boolean"]);
764
- const extractResolverMethodDefinition = (method, context) => {
765
- const decorator = findAnyDecorator(method, Object.keys(GRAPHQL_OPERATION_DECORATOR_NAMES));
766
- if (decorator === void 0) {
767
- return void 0;
768
- }
769
- const methodName = method.name.getText();
770
- const decoratorName = decoratorNameOf(decorator);
771
- const location = locationOf(context.sourceFile, method);
772
- if (decoratorName === void 0) {
773
- return void 0;
774
- }
775
- const operationKind = GRAPHQL_OPERATION_DECORATOR_NAMES[decoratorName];
776
- if (operationKind === void 0) {
777
- return void 0;
778
- }
779
- const nameArgument = readGraphqlOperationName(decorator);
780
- if (!nameArgument.ok) {
781
- context.diagnostics.push({
782
- code: "unsupported-route",
783
- title: "Unsupported operation name.",
784
- controllerName: context.resolverName,
785
- memberName: methodName,
786
- detail: "GraphQL operation names must be statically analyzable string literals.",
787
- found: `@${decoratorName}(${nameArgument.argumentText})`,
788
- filePath: context.filePath,
789
- line: location.line,
790
- column: location.column
791
- });
792
- return void 0;
793
- }
794
- const decoratorReturnType = readGraphqlReturnTypeOption(decorator);
795
- const identifierText = decoratorReturnType ?? (method.type === void 0 ? void 0 : unwrapTypeNode(method.type)?.identifierText);
796
- if (identifierText === void 0) {
797
- context.diagnostics.push({
798
- code: "unsupported-return-type",
799
- title: "Unsupported return type.",
800
- controllerName: context.resolverName,
801
- memberName: methodName,
802
- detail: 'GraphQL resolver methods need a type thunk on @Query()/@Mutation() (e.g. "() => UserType") or an explicit return type annotation so NestBridge can build a selection set.',
803
- filePath: context.filePath,
804
- line: location.line,
805
- column: location.column
806
- });
807
- return void 0;
808
- }
809
- let selection = [];
810
- if (!PRIMITIVE_SCALAR_NAMES.has(identifierText.toLowerCase())) {
811
- const resolved = resolveClassDeclaration(identifierText, context.sourceFile, context.filePath);
812
- if (resolved === void 0 && decoratorReturnType === void 0) {
813
- context.diagnostics.push({
814
- code: "unsupported-return-type",
815
- title: "Unsupported return type.",
816
- controllerName: context.resolverName,
817
- memberName: methodName,
818
- detail: `NestBridge could not locate the declaration of type "${identifierText}".`,
819
- filePath: context.filePath,
820
- line: location.line,
821
- column: location.column
822
- });
823
- return void 0;
824
- }
825
- if (resolved !== void 0) {
826
- const fields = extractSelectionFields(
827
- resolved.classDeclaration,
828
- resolved.sourceFile,
829
- resolved.filePath,
830
- {
831
- resolverName: context.resolverName,
832
- methodName,
833
- diagnostics: context.diagnostics,
834
- visitedTypeNames: /* @__PURE__ */ new Set()
835
- }
836
- );
837
- if (fields === void 0) {
838
- return void 0;
839
- }
840
- selection = fields;
841
- }
842
- }
843
- const args = method.parameters.map(
844
- (parameter, index) => extractResolverArgumentDefinition(parameter, index, {
845
- sourceFile: context.sourceFile,
846
- filePath: context.filePath,
847
- resolverName: context.resolverName,
848
- methodName,
849
- diagnostics: context.diagnostics
850
- })
851
- );
852
- if (args.some((argument) => argument === void 0)) {
853
- return void 0;
854
- }
855
- return {
856
- name: methodName,
857
- operationKind,
858
- operationName: nameArgument.value ?? methodName,
859
- arguments: args.filter((argument) => argument !== void 0),
860
- selection,
861
- line: location.line,
862
- column: location.column
863
- };
864
- };
865
-
866
- const extractResolverDefinition = (classDeclaration, sourceFile, filePath, diagnostics) => {
867
- const resolverName = classDeclaration.name?.text ?? "(anonymous resolver)";
868
- const methods = classDeclaration.members.filter((member) => ts.isMethodDeclaration(member)).map(
869
- (method) => extractResolverMethodDefinition(method, {
870
- sourceFile,
871
- filePath,
872
- resolverName,
873
- diagnostics
874
- })
875
- ).filter((method) => method !== void 0);
876
- return {
877
- name: resolverName,
878
- sourceFile: filePath,
879
- methods
880
- };
881
- };
882
-
883
- const findResolverClasses = (sourceFile) => {
884
- return sourceFile.statements.filter(
885
- (statement) => ts.isClassDeclaration(statement) && findDecorator(statement, "Resolver") !== void 0
886
- );
887
- };
888
-
889
- const analyzeResolvers = (filePaths) => {
890
- const diagnostics = [];
891
- const resolvers = [];
892
- for (const filePath of filePaths) {
893
- const content = readFileSync(filePath, "utf-8");
894
- const sourceFile = ts.createSourceFile(
895
- filePath,
896
- content,
897
- ts.ScriptTarget.ESNext,
898
- true,
899
- ts.ScriptKind.TS
900
- );
901
- for (const classDeclaration of findResolverClasses(sourceFile)) {
902
- const resolver = extractResolverDefinition(
903
- classDeclaration,
904
- sourceFile,
905
- filePath,
906
- diagnostics
907
- );
908
- if (resolver !== void 0) {
909
- resolvers.push(resolver);
910
- }
911
- }
912
- }
913
- return { resolvers, diagnostics };
914
- };
915
-
916
- const REMOTE_RESULT_HELPERS = {
917
- json: "RemoteResult",
918
- observable: "RemoteObservableResult",
919
- stream: "RemoteStreamResult"
920
- };
921
- const toPosixPath = (filePath) => filePath.replace(/\\/g, "/");
922
- const importSpecifierFor = (sourceFile, outputFilePath) => {
923
- if (!sourceFile.startsWith(".") && !/^[A-Za-z]:[\\/]/.test(sourceFile) && !sourceFile.startsWith("/")) {
924
- return sourceFile;
925
- }
926
- const relativePath = toPosixPath(relative(dirname(outputFilePath), sourceFile));
927
- return relativePath.startsWith(".") ? relativePath : `./${relativePath}`;
928
- };
929
- const stripExtension = (filePath) => {
930
- const extension = extname(filePath);
931
- return extension.length > 0 ? filePath.slice(0, -extension.length) : filePath;
932
- };
933
- const generateControllerDeclaration = (controller, outputFilePath) => {
934
- const sourceFileSpecifier = importSpecifierFor(controller.sourceFile, outputFilePath);
935
- const importSpecifier = stripExtension(sourceFileSpecifier);
936
- const serverAlias = `__Server${controller.name}`;
937
- const methodDeclarations = controller.methods.map((method) => {
938
- const helperName = REMOTE_RESULT_HELPERS[method.responseKind ?? "json"];
939
- return ` ${method.name}(...args: Parameters<${serverAlias}['${method.name}']>): ${helperName}<${serverAlias}['${method.name}']>;`;
940
- });
941
- const classBody = [" constructor();", "", ...methodDeclarations].join("\n");
942
- const usedHelperNames = new Set(
943
- controller.methods.map((method) => REMOTE_RESULT_HELPERS[method.responseKind ?? "json"])
944
- );
945
- const helperImports = Array.from(
946
- usedHelperNames.size > 0 ? usedHelperNames : ["RemoteResult"]
947
- ).sort();
948
- return [
949
- `import type { ${controller.name} as ${serverAlias} } from '${importSpecifier}';`,
950
- `import type { ${helperImports.join(", ")} } from '@nestbridge/runtime';`,
951
- "",
952
- "/**",
953
- " * Generated by NestBridge — client-facing methods only.",
954
- ` * @see {@link ${sourceFileSpecifier}} for the real controller implementation.`,
955
- " */",
956
- `export declare class ${controller.name} {`,
957
- classBody,
958
- "}",
959
- ""
960
- ].join("\n");
961
- };
962
-
963
- const formatDiagnostic = (diagnostic) => {
964
- const subject = diagnostic.memberName.length > 0 ? `${diagnostic.controllerName}.${diagnostic.memberName}` : diagnostic.controllerName;
965
- const lines = [`[NestBridge] ${diagnostic.title}`, "", subject, "", diagnostic.detail];
966
- if (diagnostic.found !== void 0) {
967
- lines.push("", "Found:", diagnostic.found);
968
- }
969
- if (diagnostic.expected !== void 0) {
970
- lines.push("", "Expected:", diagnostic.expected);
971
- }
972
- lines.push("", `${diagnostic.filePath}:${diagnostic.line}:${diagnostic.column}`);
973
- return lines.join("\n");
974
- };
975
-
976
- class NestBridgeDiagnosticError extends Error {
977
- diagnostic;
978
- constructor(diagnostic) {
979
- super(formatDiagnostic(diagnostic));
980
- this.name = "NestBridgeDiagnosticError";
981
- this.diagnostic = diagnostic;
982
- }
983
- }
984
-
985
- const version = "1.1.0";
986
-
987
- const VERSION = version;
988
-
1
+ import { SyntaxKind, isArrayLiteralExpression, isArrayTypeNode, isArrowFunction, isBlock, isCallExpression, isClassDeclaration, isDecorator, isExportDeclaration, isFunctionExpression, isIdentifier, isImportDeclaration, isMethodDeclaration, isNamedExports, isNamedImports, isObjectLiteralExpression, isPropertyAssignment, isPropertyDeclaration, isReturnStatement, isStringLiteralLikeNode, isTypeReferenceNode, isUnionTypeNode } from "typescript/unstable/ast";
2
+ import { API } from "typescript/unstable/sync";
3
+ import { existsSync } from "node:fs";
4
+ import { dirname, extname, relative, resolve } from "node:path";
5
+ //#region src/analysis/decorator-inspection.ts
6
+ var getDecorators = (node) => {
7
+ const decorators = node.modifiers?.filter(isDecorator);
8
+ return decorators === void 0 || decorators.length === 0 ? void 0 : decorators;
9
+ };
10
+ var findDecorator = (node, decoratorName) => {
11
+ const decorators = getDecorators(node);
12
+ if (decorators === void 0) return;
13
+ return decorators.find((decorator) => decoratorNameOf(decorator) === decoratorName);
14
+ };
15
+ var findAnyDecorator = (node, decoratorNames) => {
16
+ const decorators = getDecorators(node);
17
+ if (decorators === void 0) return;
18
+ return decorators.find((decorator) => decoratorNames.includes(decoratorNameOf(decorator) ?? ""));
19
+ };
20
+ var decoratorNameOf = (decorator) => {
21
+ const expression = decorator.expression;
22
+ if (isCallExpression(expression) && isIdentifier(expression.expression)) return expression.expression.text;
23
+ if (isIdentifier(expression)) return expression.text;
24
+ };
25
+ var readStaticStringArgument = (decorator) => {
26
+ const expression = decorator.expression;
27
+ if (!isCallExpression(expression)) return {
28
+ ok: true,
29
+ value: void 0
30
+ };
31
+ const [firstArgument] = expression.arguments;
32
+ if (firstArgument === void 0) return {
33
+ ok: true,
34
+ value: void 0
35
+ };
36
+ if (isStringLiteralLikeNode(firstArgument)) return {
37
+ ok: true,
38
+ value: firstArgument.text
39
+ };
40
+ return {
41
+ ok: false,
42
+ argumentText: firstArgument.getText()
43
+ };
44
+ };
45
+ //#endregion
46
+ //#region src/models/http-method.ts
47
+ var HTTP_METHOD_DECORATOR_NAMES = {
48
+ Get: "GET",
49
+ Post: "POST",
50
+ Put: "PUT",
51
+ Patch: "PATCH",
52
+ Delete: "DELETE"
53
+ };
54
+ //#endregion
55
+ //#region src/analysis/node-location.ts
56
+ var locationOf = (sourceFile, node) => {
57
+ const { line, character } = sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile));
58
+ return {
59
+ line: line + 1,
60
+ column: character + 1
61
+ };
62
+ };
63
+ //#endregion
64
+ //#region src/analysis/extract-parameter-definition.ts
65
+ var SUPPORTED_PARAMETER_DECORATORS = [
66
+ "Param",
67
+ "Query",
68
+ "Body",
69
+ "Headers"
70
+ ];
71
+ var KNOWN_UNSUPPORTED_PARAMETER_DECORATORS = [
72
+ "Req",
73
+ "Res",
74
+ "Request",
75
+ "Response"
76
+ ];
77
+ var extractParameterDefinition = (parameter, index, context) => {
78
+ const decorator = findAnyDecorator(parameter, [...SUPPORTED_PARAMETER_DECORATORS, ...KNOWN_UNSUPPORTED_PARAMETER_DECORATORS]);
79
+ const location = locationOf(context.sourceFile, parameter);
80
+ if (decorator === void 0) {
81
+ context.diagnostics.push({
82
+ code: "unsupported-parameter",
83
+ title: "Unsupported parameter.",
84
+ controllerName: context.controllerName,
85
+ memberName: context.methodName,
86
+ detail: `Parameter "${parameter.name.getText()}" must be decorated with one of @Param, @Query, @Body, or @Headers to be transported through a NestBridge HTTP client.`,
87
+ filePath: context.filePath,
88
+ line: location.line,
89
+ column: location.column
90
+ });
91
+ return;
92
+ }
93
+ const decoratorName = decoratorNameOf(decorator);
94
+ if (decoratorName !== void 0 && KNOWN_UNSUPPORTED_PARAMETER_DECORATORS.includes(decoratorName)) {
95
+ context.diagnostics.push({
96
+ code: "unsupported-parameter",
97
+ title: "Unsupported parameter.",
98
+ controllerName: context.controllerName,
99
+ memberName: context.methodName,
100
+ detail: `@${decoratorName}() cannot be transported through a NestBridge HTTP client.`,
101
+ filePath: context.filePath,
102
+ line: location.line,
103
+ column: location.column
104
+ });
105
+ return;
106
+ }
107
+ if (!isIdentifier(parameter.name)) {
108
+ context.diagnostics.push({
109
+ code: "unsupported-parameter",
110
+ title: "Unsupported parameter.",
111
+ controllerName: context.controllerName,
112
+ memberName: context.methodName,
113
+ detail: "Destructured parameters cannot be transported through a NestBridge HTTP client.",
114
+ filePath: context.filePath,
115
+ line: location.line,
116
+ column: location.column
117
+ });
118
+ return;
119
+ }
120
+ const argument = readStaticStringArgument(decorator);
121
+ if (!argument.ok) {
122
+ context.diagnostics.push({
123
+ code: "unsupported-parameter",
124
+ title: "Unsupported parameter.",
125
+ controllerName: context.controllerName,
126
+ memberName: context.methodName,
127
+ detail: `@${decoratorName}() arguments must be statically analyzable string literals.`,
128
+ found: `@${decoratorName}(${argument.argumentText})`,
129
+ filePath: context.filePath,
130
+ line: location.line,
131
+ column: location.column
132
+ });
133
+ return;
134
+ }
135
+ const base = {
136
+ index,
137
+ parameterName: parameter.name.text
138
+ };
139
+ if (decoratorName === "Param") {
140
+ if (argument.value === void 0) {
141
+ context.diagnostics.push({
142
+ code: "unsupported-parameter",
143
+ title: "Unsupported parameter.",
144
+ controllerName: context.controllerName,
145
+ memberName: context.methodName,
146
+ detail: "A whole @Param() is not supported; provide a route parameter name such as @Param('id').",
147
+ filePath: context.filePath,
148
+ line: location.line,
149
+ column: location.column
150
+ });
151
+ return;
152
+ }
153
+ return {
154
+ ...base,
155
+ kind: "param",
156
+ name: argument.value
157
+ };
158
+ }
159
+ if (decoratorName === "Headers") {
160
+ if (argument.value === void 0) {
161
+ context.diagnostics.push({
162
+ code: "unsupported-parameter",
163
+ title: "Unsupported parameter.",
164
+ controllerName: context.controllerName,
165
+ memberName: context.methodName,
166
+ detail: "A whole @Headers() is not supported; provide a header name such as @Headers('x-example').",
167
+ filePath: context.filePath,
168
+ line: location.line,
169
+ column: location.column
170
+ });
171
+ return;
172
+ }
173
+ return {
174
+ ...base,
175
+ kind: "header",
176
+ name: argument.value
177
+ };
178
+ }
179
+ if (decoratorName === "Query") return {
180
+ ...base,
181
+ kind: "query",
182
+ name: argument.value
183
+ };
184
+ return {
185
+ ...base,
186
+ kind: "body",
187
+ name: argument.value
188
+ };
189
+ };
190
+ //#endregion
191
+ //#region src/analysis/graphql-scalar-type.ts
192
+ var PRIMITIVE_SCALAR_TYPES = {
193
+ string: "String",
194
+ number: "Float",
195
+ boolean: "Boolean"
196
+ };
197
+ var unwrapPromise = (typeNode) => {
198
+ if (isTypeReferenceNode(typeNode) && isIdentifier(typeNode.typeName) && typeNode.typeName.text === "Promise" && typeNode.typeArguments?.[0] !== void 0) return unwrapPromise(typeNode.typeArguments[0]);
199
+ return typeNode;
200
+ };
201
+ var unwrapNullable = (typeNode) => {
202
+ if (!isUnionTypeNode(typeNode)) return {
203
+ inner: typeNode,
204
+ isNullable: false
205
+ };
206
+ const nonNullMembers = typeNode.types.filter((member) => member.kind !== SyntaxKind.NullKeyword && member.kind !== SyntaxKind.UndefinedKeyword);
207
+ const [onlyMember] = nonNullMembers;
208
+ if (nonNullMembers.length !== 1 || onlyMember === void 0) return {
209
+ inner: typeNode,
210
+ isNullable: nonNullMembers.length !== typeNode.types.length
211
+ };
212
+ return {
213
+ inner: onlyMember,
214
+ isNullable: true
215
+ };
216
+ };
217
+ var unwrapArrayElement = (typeNode) => {
218
+ if (isArrayTypeNode(typeNode)) return typeNode.elementType;
219
+ if (isTypeReferenceNode(typeNode) && isIdentifier(typeNode.typeName) && typeNode.typeName.text === "Array" && typeNode.typeArguments?.[0] !== void 0) return typeNode.typeArguments[0];
220
+ };
221
+ var identifierTextOf = (typeNode) => {
222
+ if (isTypeReferenceNode(typeNode) && isIdentifier(typeNode.typeName)) return typeNode.typeName.text;
223
+ switch (typeNode.kind) {
224
+ case SyntaxKind.StringKeyword: return "string";
225
+ case SyntaxKind.NumberKeyword: return "number";
226
+ case SyntaxKind.BooleanKeyword: return "boolean";
227
+ default: return;
228
+ }
229
+ };
230
+ var unwrapTypeNode = (typeNode) => {
231
+ const { inner: withoutNullable, isNullable } = unwrapNullable(unwrapPromise(typeNode));
232
+ const arrayElement = unwrapArrayElement(withoutNullable);
233
+ const identifierText = identifierTextOf(arrayElement === void 0 ? withoutNullable : unwrapNullable(arrayElement).inner);
234
+ if (identifierText === void 0) return;
235
+ return {
236
+ identifierText,
237
+ isArray: arrayElement !== void 0,
238
+ isNullable
239
+ };
240
+ };
241
+ var graphqlScalarNameFor = (identifierText) => PRIMITIVE_SCALAR_TYPES[identifierText] ?? identifierText;
242
+ var graphqlTypeStringFor = (unwrapped, explicitTypeName) => {
243
+ const baseName = explicitTypeName ?? graphqlScalarNameFor(unwrapped.identifierText);
244
+ const wrapped = unwrapped.isArray ? `[${baseName}!]` : baseName;
245
+ return unwrapped.isNullable ? wrapped : `${wrapped}!`;
246
+ };
247
+ //#endregion
248
+ //#region src/analysis/read-response-kind.ts
249
+ var OBSERVABLE_TYPE_NAME = "Observable";
250
+ var STREAMABLE_FILE_TYPE_NAME = "StreamableFile";
251
+ var readResponseKind = (returnType) => {
252
+ if (returnType === void 0) return "json";
253
+ const unwrapped = unwrapPromise(returnType);
254
+ if (!isTypeReferenceNode(unwrapped) || !isIdentifier(unwrapped.typeName)) return "json";
255
+ if (unwrapped.typeName.text === OBSERVABLE_TYPE_NAME) return "observable";
256
+ if (unwrapped.typeName.text === STREAMABLE_FILE_TYPE_NAME) return "stream";
257
+ return "json";
258
+ };
259
+ //#endregion
260
+ //#region src/analysis/route-path.ts
261
+ var trimSlashes = (segment) => segment.replace(/^\/+/, "").replace(/\/+$/, "");
262
+ var joinRoutePath = (controllerPath, methodPath) => {
263
+ return `/${[trimSlashes(controllerPath), trimSlashes(methodPath ?? "")].filter((segment) => segment.length > 0).join("/")}`;
264
+ };
265
+ //#endregion
266
+ //#region src/analysis/extract-method-definition.ts
267
+ var extractMethodDefinition = (method, context) => {
268
+ const decorator = findAnyDecorator(method, Object.keys(HTTP_METHOD_DECORATOR_NAMES));
269
+ if (decorator === void 0) return;
270
+ const methodName = method.name.getText();
271
+ const decoratorName = decoratorNameOf(decorator);
272
+ const location = locationOf(context.sourceFile, method);
273
+ if (decoratorName === void 0) return;
274
+ const httpMethod = HTTP_METHOD_DECORATOR_NAMES[decoratorName];
275
+ if (httpMethod === void 0) return;
276
+ const routeArgument = readStaticStringArgument(decorator);
277
+ if (!routeArgument.ok) {
278
+ context.diagnostics.push({
279
+ code: "unsupported-route",
280
+ title: "Unsupported route.",
281
+ controllerName: context.controllerName,
282
+ memberName: methodName,
283
+ detail: "Route paths must be statically analyzable.",
284
+ found: `@${decoratorName}(${routeArgument.argumentText})`,
285
+ filePath: context.filePath,
286
+ line: location.line,
287
+ column: location.column
288
+ });
289
+ return;
290
+ }
291
+ const parameters = method.parameters.map((parameter, index) => extractParameterDefinition(parameter, index, {
292
+ sourceFile: context.sourceFile,
293
+ filePath: context.filePath,
294
+ controllerName: context.controllerName,
295
+ methodName,
296
+ diagnostics: context.diagnostics
297
+ }));
298
+ if (parameters.some((parameter) => parameter === void 0)) return;
299
+ return {
300
+ name: methodName,
301
+ httpMethod,
302
+ path: joinRoutePath(context.controllerPath, routeArgument.value),
303
+ parameters: parameters.filter((parameter) => parameter !== void 0),
304
+ responseKind: readResponseKind(method.type),
305
+ line: location.line,
306
+ column: location.column
307
+ };
308
+ };
309
+ //#endregion
310
+ //#region src/analysis/extract-controller-definition.ts
311
+ var extractControllerDefinition = (classDeclaration, sourceFile, filePath, diagnostics) => {
312
+ const controllerName = classDeclaration.name?.text ?? "(anonymous controller)";
313
+ const decorator = findDecorator(classDeclaration, "Controller");
314
+ if (decorator === void 0) return;
315
+ const location = locationOf(sourceFile, classDeclaration);
316
+ const argument = readStaticStringArgument(decorator);
317
+ if (!argument.ok) {
318
+ diagnostics.push({
319
+ code: "unsupported-route",
320
+ title: "Unsupported route.",
321
+ controllerName,
322
+ memberName: "",
323
+ detail: "Controller paths must be statically analyzable.",
324
+ found: `@Controller(${argument.argumentText})`,
325
+ filePath,
326
+ line: location.line,
327
+ column: location.column
328
+ });
329
+ return;
330
+ }
331
+ const methods = classDeclaration.members.filter((member) => isMethodDeclaration(member)).map((method) => extractMethodDefinition(method, {
332
+ sourceFile,
333
+ filePath,
334
+ controllerName,
335
+ controllerPath: argument.value ?? "",
336
+ diagnostics
337
+ })).filter((method) => method !== void 0);
338
+ return {
339
+ name: controllerName,
340
+ path: argument.value ?? "",
341
+ sourceFile: filePath,
342
+ methods
343
+ };
344
+ };
345
+ //#endregion
346
+ //#region src/analysis/find-controller-classes.ts
347
+ var findControllerClasses = (sourceFile) => {
348
+ return sourceFile.statements.filter((statement) => isClassDeclaration(statement) && findDecorator(statement, "Controller") !== void 0);
349
+ };
350
+ //#endregion
351
+ //#region src/analysis/parse-source-file.ts
352
+ var sharedApi;
353
+ var parseSourceFile = (filePath) => {
354
+ sharedApi ??= new API();
355
+ const sourceFile = sharedApi.updateSnapshot({ openFiles: [filePath] }).getDefaultProjectForFile(filePath)?.program.getSourceFile(filePath);
356
+ if (sourceFile === void 0) throw new Error(`Unable to parse source file: ${filePath}`);
357
+ return sourceFile;
358
+ };
359
+ //#endregion
360
+ //#region src/analysis/analyze-controllers.ts
361
+ var analyzeControllers = (filePaths) => {
362
+ const diagnostics = [];
363
+ const controllers = [];
364
+ for (const filePath of filePaths) {
365
+ const sourceFile = parseSourceFile(filePath);
366
+ for (const classDeclaration of findControllerClasses(sourceFile)) {
367
+ const controller = extractControllerDefinition(classDeclaration, sourceFile, filePath, diagnostics);
368
+ if (controller !== void 0) controllers.push(controller);
369
+ }
370
+ }
371
+ return {
372
+ controllers,
373
+ diagnostics
374
+ };
375
+ };
376
+ //#endregion
377
+ //#region src/models/graphql-operation-kind.ts
378
+ var GRAPHQL_OPERATION_DECORATOR_NAMES = {
379
+ Query: "query",
380
+ Mutation: "mutation"
381
+ };
382
+ //#endregion
383
+ //#region src/analysis/read-type-thunk-expression.ts
384
+ var readTypeThunkExpression = (thunk) => {
385
+ if (!isBlock(thunk.body)) return thunk.body;
386
+ return thunk.body.statements.find(isReturnStatement)?.expression;
387
+ };
388
+ //#endregion
389
+ //#region src/analysis/read-args-type-option.ts
390
+ var readArgsTypeOption = (decorator) => {
391
+ if (!isCallExpression(decorator.expression)) return;
392
+ const optionsArgument = decorator.expression.arguments.find(isObjectLiteralExpression);
393
+ if (optionsArgument === void 0) return;
394
+ const typeProperty = optionsArgument.properties.find((property) => isPropertyAssignment(property) && isIdentifier(property.name) && property.name.text === "type");
395
+ if (typeProperty === void 0) return;
396
+ const thunk = typeProperty.initializer;
397
+ if (!isArrowFunction(thunk) && !isFunctionExpression(thunk)) return;
398
+ const returnedExpression = readTypeThunkExpression(thunk);
399
+ if (returnedExpression === void 0) return;
400
+ if (isArrayLiteralExpression(returnedExpression)) {
401
+ const element = returnedExpression.elements[0];
402
+ return element !== void 0 && isIdentifier(element) ? {
403
+ typeName: element.text,
404
+ isArray: true
405
+ } : void 0;
406
+ }
407
+ return isIdentifier(returnedExpression) ? {
408
+ typeName: returnedExpression.text,
409
+ isArray: false
410
+ } : void 0;
411
+ };
412
+ //#endregion
413
+ //#region src/analysis/extract-resolver-argument-definition.ts
414
+ var extractResolverArgumentDefinition = (parameter, index, context) => {
415
+ const decorator = findAnyDecorator(parameter, ["Args"]);
416
+ const location = locationOf(context.sourceFile, parameter);
417
+ if (decorator === void 0) {
418
+ context.diagnostics.push({
419
+ code: "unsupported-argument",
420
+ title: "Unsupported argument.",
421
+ controllerName: context.resolverName,
422
+ memberName: context.methodName,
423
+ detail: `Parameter "${parameter.name.getText()}" must be decorated with @Args to be transported through a NestBridge GraphQL client.`,
424
+ filePath: context.filePath,
425
+ line: location.line,
426
+ column: location.column
427
+ });
428
+ return;
429
+ }
430
+ if (!isIdentifier(parameter.name)) {
431
+ context.diagnostics.push({
432
+ code: "unsupported-argument",
433
+ title: "Unsupported argument.",
434
+ controllerName: context.resolverName,
435
+ memberName: context.methodName,
436
+ detail: "Destructured parameters cannot be transported through a NestBridge GraphQL client.",
437
+ filePath: context.filePath,
438
+ line: location.line,
439
+ column: location.column
440
+ });
441
+ return;
442
+ }
443
+ const nameArgument = readStaticStringArgument(decorator);
444
+ if (!nameArgument.ok) {
445
+ context.diagnostics.push({
446
+ code: "unsupported-argument",
447
+ title: "Unsupported argument.",
448
+ controllerName: context.resolverName,
449
+ memberName: context.methodName,
450
+ detail: "@Args() name must be a statically analyzable string literal.",
451
+ found: `@Args(${nameArgument.argumentText})`,
452
+ filePath: context.filePath,
453
+ line: location.line,
454
+ column: location.column
455
+ });
456
+ return;
457
+ }
458
+ if (nameArgument.value === void 0) {
459
+ context.diagnostics.push({
460
+ code: "unsupported-argument",
461
+ title: "Unsupported argument.",
462
+ controllerName: context.resolverName,
463
+ memberName: context.methodName,
464
+ detail: "A whole @Args() is not supported; provide an argument name such as @Args('id').",
465
+ filePath: context.filePath,
466
+ line: location.line,
467
+ column: location.column
468
+ });
469
+ return;
470
+ }
471
+ if (parameter.type === void 0) {
472
+ context.diagnostics.push({
473
+ code: "unsupported-argument",
474
+ title: "Unsupported argument.",
475
+ controllerName: context.resolverName,
476
+ memberName: context.methodName,
477
+ detail: `@Args() parameter "${parameter.name.text}" needs an explicit type annotation.`,
478
+ filePath: context.filePath,
479
+ line: location.line,
480
+ column: location.column
481
+ });
482
+ return;
483
+ }
484
+ const unwrapped = unwrapTypeNode(parameter.type);
485
+ if (unwrapped === void 0) {
486
+ context.diagnostics.push({
487
+ code: "unsupported-argument",
488
+ title: "Unsupported argument.",
489
+ controllerName: context.resolverName,
490
+ memberName: context.methodName,
491
+ detail: `@Args() parameter "${parameter.name.text}" has a type NestBridge cannot statically resolve to a GraphQL type.`,
492
+ found: parameter.type.getText(),
493
+ filePath: context.filePath,
494
+ line: location.line,
495
+ column: location.column
496
+ });
497
+ return;
498
+ }
499
+ const explicitType = readArgsTypeOption(decorator);
500
+ return {
501
+ index,
502
+ parameterName: parameter.name.text,
503
+ name: nameArgument.value,
504
+ graphqlType: graphqlTypeStringFor(unwrapped, explicitType?.typeName)
505
+ };
506
+ };
507
+ //#endregion
508
+ //#region src/analysis/resolve-type-declaration.ts
509
+ var parsedFilesByPath = /* @__PURE__ */ new Map();
510
+ var parseFile = (filePath) => {
511
+ const cached = parsedFilesByPath.get(filePath);
512
+ if (cached !== void 0) return cached;
513
+ const sourceFile = parseSourceFile(filePath);
514
+ parsedFilesByPath.set(filePath, sourceFile);
515
+ return sourceFile;
516
+ };
517
+ var resolveModuleFile = (fromFilePath, moduleSpecifierText) => {
518
+ if (!moduleSpecifierText.startsWith(".")) return;
519
+ const base = resolve(dirname(fromFilePath), moduleSpecifierText);
520
+ return [
521
+ `${base}.ts`,
522
+ `${base}.tsx`,
523
+ resolve(base, "index.ts")
524
+ ].find((candidate) => existsSync(candidate));
525
+ };
526
+ var findExportedDeclaration = (sourceFile, name) => sourceFile.statements.find((statement) => isClassDeclaration(statement) && statement.name?.text === name);
527
+ var findImportSource = (sourceFile, localName) => {
528
+ for (const statement of sourceFile.statements) {
529
+ if (!isImportDeclaration(statement) || !isStringLiteralLikeNode(statement.moduleSpecifier)) continue;
530
+ const namedBindings = statement.importClause?.namedBindings;
531
+ if (namedBindings === void 0 || !isNamedImports(namedBindings)) continue;
532
+ const element = namedBindings.elements.find((candidate) => candidate.name.text === localName);
533
+ if (element !== void 0) return {
534
+ moduleSpecifierText: statement.moduleSpecifier.text,
535
+ importedName: element.propertyName?.text ?? element.name.text
536
+ };
537
+ }
538
+ };
539
+ var findReExportSource = (sourceFile, name) => {
540
+ for (const statement of sourceFile.statements) {
541
+ if (!isExportDeclaration(statement) || statement.moduleSpecifier === void 0 || !isStringLiteralLikeNode(statement.moduleSpecifier)) continue;
542
+ if (statement.exportClause === void 0) return statement.moduleSpecifier.text;
543
+ if (isNamedExports(statement.exportClause) && statement.exportClause.elements.some((element) => (element.propertyName?.text ?? element.name.text) === name)) return statement.moduleSpecifier.text;
544
+ }
545
+ };
546
+ var resolveClassDeclaration = (name, fromSourceFile, fromFilePath, visited = /* @__PURE__ */ new Set()) => {
547
+ if (visited.has(fromFilePath)) return;
548
+ visited.add(fromFilePath);
549
+ const direct = findExportedDeclaration(fromSourceFile, name);
550
+ if (direct !== void 0) return {
551
+ classDeclaration: direct,
552
+ sourceFile: fromSourceFile,
553
+ filePath: fromFilePath
554
+ };
555
+ const importSource = findImportSource(fromSourceFile, name);
556
+ if (importSource !== void 0) {
557
+ const resolvedPath = resolveModuleFile(fromFilePath, importSource.moduleSpecifierText);
558
+ if (resolvedPath !== void 0) {
559
+ const nextSourceFile = parseFile(resolvedPath);
560
+ const result = resolveClassDeclaration(importSource.importedName, nextSourceFile, resolvedPath, visited);
561
+ if (result !== void 0) return result;
562
+ }
563
+ }
564
+ const reExportModuleSpecifierText = findReExportSource(fromSourceFile, name);
565
+ if (reExportModuleSpecifierText !== void 0) {
566
+ const resolvedPath = resolveModuleFile(fromFilePath, reExportModuleSpecifierText);
567
+ if (resolvedPath !== void 0) return resolveClassDeclaration(name, parseFile(resolvedPath), resolvedPath, visited);
568
+ }
569
+ };
570
+ //#endregion
571
+ //#region src/analysis/extract-selection-fields.ts
572
+ var PRIMITIVE_SCALAR_NAMES$1 = /* @__PURE__ */ new Set([
573
+ "string",
574
+ "number",
575
+ "boolean"
576
+ ]);
577
+ var extractSelectionFields = (classDeclaration, sourceFile, filePath, context) => {
578
+ const typeName = classDeclaration.name?.text ?? "(anonymous type)";
579
+ const location = locationOf(sourceFile, classDeclaration);
580
+ if (context.visitedTypeNames.has(typeName)) {
581
+ context.diagnostics.push({
582
+ code: "circular-type",
583
+ title: "Circular type.",
584
+ controllerName: context.resolverName,
585
+ memberName: context.methodName,
586
+ detail: `Type "${typeName}" references itself, directly or indirectly. NestBridge cannot build a finite GraphQL selection set for it.`,
587
+ filePath,
588
+ line: location.line,
589
+ column: location.column
590
+ });
591
+ return;
592
+ }
593
+ const nextVisited = new Set(context.visitedTypeNames).add(typeName);
594
+ const properties = classDeclaration.members.filter((member) => isPropertyDeclaration(member) && findDecorator(member, "Field") !== void 0);
595
+ const fields = [];
596
+ for (const property of properties) {
597
+ if (!isIdentifier(property.name)) continue;
598
+ const fieldName = property.name.text;
599
+ const fieldLocation = locationOf(sourceFile, property);
600
+ if (property.type === void 0) {
601
+ context.diagnostics.push({
602
+ code: "unsupported-return-type",
603
+ title: "Unsupported field type.",
604
+ controllerName: context.resolverName,
605
+ memberName: context.methodName,
606
+ detail: `Field "${fieldName}" on "${typeName}" needs an explicit type annotation.`,
607
+ filePath,
608
+ line: fieldLocation.line,
609
+ column: fieldLocation.column
610
+ });
611
+ return;
612
+ }
613
+ const unwrapped = unwrapTypeNode(property.type);
614
+ if (unwrapped === void 0) {
615
+ context.diagnostics.push({
616
+ code: "unsupported-return-type",
617
+ title: "Unsupported field type.",
618
+ controllerName: context.resolverName,
619
+ memberName: context.methodName,
620
+ detail: `Field "${fieldName}" on "${typeName}" has a type NestBridge cannot statically resolve.`,
621
+ found: property.type.getText(),
622
+ filePath,
623
+ line: fieldLocation.line,
624
+ column: fieldLocation.column
625
+ });
626
+ return;
627
+ }
628
+ if (PRIMITIVE_SCALAR_NAMES$1.has(unwrapped.identifierText)) {
629
+ fields.push({ name: fieldName });
630
+ continue;
631
+ }
632
+ const resolved = resolveClassDeclaration(unwrapped.identifierText, sourceFile, filePath);
633
+ if (resolved === void 0) {
634
+ fields.push({ name: fieldName });
635
+ continue;
636
+ }
637
+ if (findDecorator(resolved.classDeclaration, "ObjectType") === void 0) {
638
+ context.diagnostics.push({
639
+ code: "unsupported-return-type",
640
+ title: "Unsupported field type.",
641
+ controllerName: context.resolverName,
642
+ memberName: context.methodName,
643
+ detail: `Field "${fieldName}" on "${typeName}" resolves to class "${unwrapped.identifierText}", which is not decorated with @ObjectType(). NestBridge cannot build a GraphQL selection set for it.`,
644
+ filePath,
645
+ line: fieldLocation.line,
646
+ column: fieldLocation.column
647
+ });
648
+ return;
649
+ }
650
+ const children = extractSelectionFields(resolved.classDeclaration, resolved.sourceFile, resolved.filePath, {
651
+ ...context,
652
+ visitedTypeNames: nextVisited
653
+ });
654
+ if (children === void 0) return;
655
+ fields.push({
656
+ name: fieldName,
657
+ children
658
+ });
659
+ }
660
+ return fields;
661
+ };
662
+ //#endregion
663
+ //#region src/analysis/read-graphql-operation-name.ts
664
+ var readNameFromOptions = (optionsArgument) => {
665
+ const nameProperty = optionsArgument.properties.find((property) => isPropertyAssignment(property) && isIdentifier(property.name) && property.name.text === "name");
666
+ if (nameProperty === void 0 || !isStringLiteralLikeNode(nameProperty.initializer)) return;
667
+ return nameProperty.initializer.text;
668
+ };
669
+ var readGraphqlOperationName = (decorator) => {
670
+ const expression = decorator.expression;
671
+ if (!isCallExpression(expression)) return {
672
+ ok: true,
673
+ value: void 0
674
+ };
675
+ let value;
676
+ for (const argument of expression.arguments) {
677
+ if (isArrowFunction(argument) || isFunctionExpression(argument)) continue;
678
+ if (isStringLiteralLikeNode(argument)) {
679
+ value = argument.text;
680
+ continue;
681
+ }
682
+ if (isObjectLiteralExpression(argument)) {
683
+ value = readNameFromOptions(argument) ?? value;
684
+ continue;
685
+ }
686
+ return {
687
+ ok: false,
688
+ argumentText: argument.getText()
689
+ };
690
+ }
691
+ return {
692
+ ok: true,
693
+ value
694
+ };
695
+ };
696
+ //#endregion
697
+ //#region src/analysis/read-graphql-return-type-option.ts
698
+ var readGraphqlReturnTypeOption = (decorator) => {
699
+ if (!isCallExpression(decorator.expression)) return;
700
+ const thunk = decorator.expression.arguments.find((argument) => isArrowFunction(argument) || isFunctionExpression(argument));
701
+ if (thunk === void 0) return;
702
+ const returnedExpression = readTypeThunkExpression(thunk);
703
+ if (returnedExpression === void 0) return;
704
+ if (isArrayLiteralExpression(returnedExpression)) {
705
+ const element = returnedExpression.elements[0];
706
+ return element !== void 0 && isIdentifier(element) ? element.text : void 0;
707
+ }
708
+ return isIdentifier(returnedExpression) ? returnedExpression.text : void 0;
709
+ };
710
+ //#endregion
711
+ //#region src/analysis/extract-resolver-method-definition.ts
712
+ var PRIMITIVE_SCALAR_NAMES = /* @__PURE__ */ new Set([
713
+ "string",
714
+ "number",
715
+ "boolean"
716
+ ]);
717
+ var extractResolverMethodDefinition = (method, context) => {
718
+ const decorator = findAnyDecorator(method, Object.keys(GRAPHQL_OPERATION_DECORATOR_NAMES));
719
+ if (decorator === void 0) return;
720
+ const methodName = method.name.getText();
721
+ const decoratorName = decoratorNameOf(decorator);
722
+ const location = locationOf(context.sourceFile, method);
723
+ if (decoratorName === void 0) return;
724
+ const operationKind = GRAPHQL_OPERATION_DECORATOR_NAMES[decoratorName];
725
+ if (operationKind === void 0) return;
726
+ const nameArgument = readGraphqlOperationName(decorator);
727
+ if (!nameArgument.ok) {
728
+ context.diagnostics.push({
729
+ code: "unsupported-route",
730
+ title: "Unsupported operation name.",
731
+ controllerName: context.resolverName,
732
+ memberName: methodName,
733
+ detail: "GraphQL operation names must be statically analyzable string literals.",
734
+ found: `@${decoratorName}(${nameArgument.argumentText})`,
735
+ filePath: context.filePath,
736
+ line: location.line,
737
+ column: location.column
738
+ });
739
+ return;
740
+ }
741
+ const decoratorReturnType = readGraphqlReturnTypeOption(decorator);
742
+ const identifierText = decoratorReturnType ?? (method.type === void 0 ? void 0 : unwrapTypeNode(method.type)?.identifierText);
743
+ if (identifierText === void 0) {
744
+ context.diagnostics.push({
745
+ code: "unsupported-return-type",
746
+ title: "Unsupported return type.",
747
+ controllerName: context.resolverName,
748
+ memberName: methodName,
749
+ detail: "GraphQL resolver methods need a type thunk on @Query()/@Mutation() (e.g. \"() => UserType\") or an explicit return type annotation so NestBridge can build a selection set.",
750
+ filePath: context.filePath,
751
+ line: location.line,
752
+ column: location.column
753
+ });
754
+ return;
755
+ }
756
+ let selection = [];
757
+ if (!PRIMITIVE_SCALAR_NAMES.has(identifierText.toLowerCase())) {
758
+ const resolved = resolveClassDeclaration(identifierText, context.sourceFile, context.filePath);
759
+ if (resolved === void 0 && decoratorReturnType === void 0) {
760
+ context.diagnostics.push({
761
+ code: "unsupported-return-type",
762
+ title: "Unsupported return type.",
763
+ controllerName: context.resolverName,
764
+ memberName: methodName,
765
+ detail: `NestBridge could not locate the declaration of type "${identifierText}".`,
766
+ filePath: context.filePath,
767
+ line: location.line,
768
+ column: location.column
769
+ });
770
+ return;
771
+ }
772
+ if (resolved !== void 0) {
773
+ const fields = extractSelectionFields(resolved.classDeclaration, resolved.sourceFile, resolved.filePath, {
774
+ resolverName: context.resolverName,
775
+ methodName,
776
+ diagnostics: context.diagnostics,
777
+ visitedTypeNames: /* @__PURE__ */ new Set()
778
+ });
779
+ if (fields === void 0) return;
780
+ selection = fields;
781
+ }
782
+ }
783
+ const args = method.parameters.map((parameter, index) => extractResolverArgumentDefinition(parameter, index, {
784
+ sourceFile: context.sourceFile,
785
+ filePath: context.filePath,
786
+ resolverName: context.resolverName,
787
+ methodName,
788
+ diagnostics: context.diagnostics
789
+ }));
790
+ if (args.some((argument) => argument === void 0)) return;
791
+ return {
792
+ name: methodName,
793
+ operationKind,
794
+ operationName: nameArgument.value ?? methodName,
795
+ arguments: args.filter((argument) => argument !== void 0),
796
+ selection,
797
+ line: location.line,
798
+ column: location.column
799
+ };
800
+ };
801
+ //#endregion
802
+ //#region src/analysis/extract-resolver-definition.ts
803
+ var extractResolverDefinition = (classDeclaration, sourceFile, filePath, diagnostics) => {
804
+ const resolverName = classDeclaration.name?.text ?? "(anonymous resolver)";
805
+ return {
806
+ name: resolverName,
807
+ sourceFile: filePath,
808
+ methods: classDeclaration.members.filter((member) => isMethodDeclaration(member)).map((method) => extractResolverMethodDefinition(method, {
809
+ sourceFile,
810
+ filePath,
811
+ resolverName,
812
+ diagnostics
813
+ })).filter((method) => method !== void 0)
814
+ };
815
+ };
816
+ //#endregion
817
+ //#region src/analysis/find-resolver-classes.ts
818
+ var findResolverClasses = (sourceFile) => {
819
+ return sourceFile.statements.filter((statement) => isClassDeclaration(statement) && findDecorator(statement, "Resolver") !== void 0);
820
+ };
821
+ //#endregion
822
+ //#region src/analysis/analyze-resolvers.ts
823
+ var analyzeResolvers = (filePaths) => {
824
+ const diagnostics = [];
825
+ const resolvers = [];
826
+ for (const filePath of filePaths) {
827
+ const sourceFile = parseSourceFile(filePath);
828
+ for (const classDeclaration of findResolverClasses(sourceFile)) {
829
+ const resolver = extractResolverDefinition(classDeclaration, sourceFile, filePath, diagnostics);
830
+ if (resolver !== void 0) resolvers.push(resolver);
831
+ }
832
+ }
833
+ return {
834
+ resolvers,
835
+ diagnostics
836
+ };
837
+ };
838
+ //#endregion
839
+ //#region src/declarations/generate-controller-declaration.ts
840
+ var REMOTE_RESULT_HELPERS = {
841
+ json: "RemoteResult",
842
+ observable: "RemoteObservableResult",
843
+ stream: "RemoteStreamResult"
844
+ };
845
+ var toPosixPath = (filePath) => filePath.replace(/\\/g, "/");
846
+ var importSpecifierFor = (sourceFile, outputFilePath) => {
847
+ if (!sourceFile.startsWith(".") && !/^[A-Za-z]:[\\/]/.test(sourceFile) && !sourceFile.startsWith("/")) return sourceFile;
848
+ const relativePath = toPosixPath(relative(dirname(outputFilePath), sourceFile));
849
+ return relativePath.startsWith(".") ? relativePath : `./${relativePath}`;
850
+ };
851
+ var stripExtension = (filePath) => {
852
+ const extension = extname(filePath);
853
+ return extension.length > 0 ? filePath.slice(0, -extension.length) : filePath;
854
+ };
855
+ var generateControllerDeclaration = (controller, outputFilePath) => {
856
+ const sourceFileSpecifier = importSpecifierFor(controller.sourceFile, outputFilePath);
857
+ const importSpecifier = stripExtension(sourceFileSpecifier);
858
+ const serverAlias = `__Server${controller.name}`;
859
+ const classBody = [
860
+ " constructor();",
861
+ "",
862
+ ...controller.methods.map((method) => {
863
+ const helperName = REMOTE_RESULT_HELPERS[method.responseKind ?? "json"];
864
+ return ` ${method.name}(...args: Parameters<${serverAlias}['${method.name}']>): ${helperName}<${serverAlias}['${method.name}']>;`;
865
+ })
866
+ ].join("\n");
867
+ const usedHelperNames = new Set(controller.methods.map((method) => REMOTE_RESULT_HELPERS[method.responseKind ?? "json"]));
868
+ const helperImports = Array.from(usedHelperNames.size > 0 ? usedHelperNames : ["RemoteResult"]).sort();
869
+ return [
870
+ `import type { ${controller.name} as ${serverAlias} } from '${importSpecifier}';`,
871
+ `import type { ${helperImports.join(", ")} } from 'nestbridge';`,
872
+ "",
873
+ "/**",
874
+ " * Generated by NestBridge — client-facing methods only.",
875
+ ` * @see {@link ${sourceFileSpecifier}} for the real controller implementation.`,
876
+ " */",
877
+ `export declare class ${controller.name} {`,
878
+ classBody,
879
+ "}",
880
+ ""
881
+ ].join("\n");
882
+ };
883
+ //#endregion
884
+ //#region src/diagnostics/format-diagnostic.ts
885
+ var formatDiagnostic = (diagnostic) => {
886
+ const subject = diagnostic.memberName.length > 0 ? `${diagnostic.controllerName}.${diagnostic.memberName}` : diagnostic.controllerName;
887
+ const lines = [
888
+ `[NestBridge] ${diagnostic.title}`,
889
+ "",
890
+ subject,
891
+ "",
892
+ diagnostic.detail
893
+ ];
894
+ if (diagnostic.found !== void 0) lines.push("", "Found:", diagnostic.found);
895
+ if (diagnostic.expected !== void 0) lines.push("", "Expected:", diagnostic.expected);
896
+ lines.push("", `${diagnostic.filePath}:${diagnostic.line}:${diagnostic.column}`);
897
+ return lines.join("\n");
898
+ };
899
+ //#endregion
900
+ //#region src/diagnostics/nestbridge-diagnostic-error.ts
901
+ var NestBridgeDiagnosticError = class extends Error {
902
+ diagnostic;
903
+ constructor(diagnostic) {
904
+ super(formatDiagnostic(diagnostic));
905
+ this.name = "NestBridgeDiagnosticError";
906
+ this.diagnostic = diagnostic;
907
+ }
908
+ };
909
+ //#endregion
910
+ //#region src/version.ts
911
+ var VERSION = "1.1.1";
912
+ //#endregion
989
913
  export { NestBridgeDiagnosticError, VERSION, analyzeControllers, analyzeResolvers, formatDiagnostic, generateControllerDeclaration };
990
- //# sourceMappingURL=nestbridge.core.js.map
914
+
915
+ //# sourceMappingURL=nestbridge.core.js.map