@nestbridge/vite 0.1.1 → 1.1.0
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.
- package/README.md +13 -9
- package/build/nestbridge.vite.d.ts +1 -0
- package/build/nestbridge.vite.js +774 -36
- package/build/nestbridge.vite.js.map +1 -1
- package/package.json +1 -1
package/build/nestbridge.vite.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { readFileSync, mkdirSync, writeFileSync, globSync } from 'node:fs';
|
|
1
|
+
import { readFileSync, existsSync, mkdirSync, writeFileSync, globSync } from 'node:fs';
|
|
2
2
|
import ts from 'typescript';
|
|
3
|
-
import { relative, dirname, extname,
|
|
3
|
+
import { relative, dirname, extname, resolve, join } from 'node:path';
|
|
4
4
|
import { normalizePath } from 'vite';
|
|
5
5
|
|
|
6
6
|
const findDecorator = (node, decoratorName) => {
|
|
@@ -160,6 +160,91 @@ const extractParameterDefinition = (parameter, index, context) => {
|
|
|
160
160
|
return { ...base, kind: "body", name: argument.value };
|
|
161
161
|
};
|
|
162
162
|
|
|
163
|
+
const PRIMITIVE_SCALAR_TYPES = {
|
|
164
|
+
string: "String",
|
|
165
|
+
number: "Float",
|
|
166
|
+
boolean: "Boolean"
|
|
167
|
+
};
|
|
168
|
+
const unwrapPromise = (typeNode) => {
|
|
169
|
+
if (ts.isTypeReferenceNode(typeNode) && ts.isIdentifier(typeNode.typeName) && typeNode.typeName.text === "Promise" && typeNode.typeArguments?.[0] !== void 0) {
|
|
170
|
+
return unwrapPromise(typeNode.typeArguments[0]);
|
|
171
|
+
}
|
|
172
|
+
return typeNode;
|
|
173
|
+
};
|
|
174
|
+
const unwrapNullable = (typeNode) => {
|
|
175
|
+
if (!ts.isUnionTypeNode(typeNode)) {
|
|
176
|
+
return { inner: typeNode, isNullable: false };
|
|
177
|
+
}
|
|
178
|
+
const nonNullMembers = typeNode.types.filter(
|
|
179
|
+
(member) => member.kind !== ts.SyntaxKind.NullKeyword && member.kind !== ts.SyntaxKind.UndefinedKeyword
|
|
180
|
+
);
|
|
181
|
+
const [onlyMember] = nonNullMembers;
|
|
182
|
+
if (nonNullMembers.length !== 1 || onlyMember === void 0) {
|
|
183
|
+
return { inner: typeNode, isNullable: nonNullMembers.length !== typeNode.types.length };
|
|
184
|
+
}
|
|
185
|
+
return { inner: onlyMember, isNullable: true };
|
|
186
|
+
};
|
|
187
|
+
const unwrapArrayElement = (typeNode) => {
|
|
188
|
+
if (ts.isArrayTypeNode(typeNode)) {
|
|
189
|
+
return typeNode.elementType;
|
|
190
|
+
}
|
|
191
|
+
if (ts.isTypeReferenceNode(typeNode) && ts.isIdentifier(typeNode.typeName) && typeNode.typeName.text === "Array" && typeNode.typeArguments?.[0] !== void 0) {
|
|
192
|
+
return typeNode.typeArguments[0];
|
|
193
|
+
}
|
|
194
|
+
return void 0;
|
|
195
|
+
};
|
|
196
|
+
const identifierTextOf = (typeNode) => {
|
|
197
|
+
if (ts.isTypeReferenceNode(typeNode) && ts.isIdentifier(typeNode.typeName)) {
|
|
198
|
+
return typeNode.typeName.text;
|
|
199
|
+
}
|
|
200
|
+
switch (typeNode.kind) {
|
|
201
|
+
case ts.SyntaxKind.StringKeyword:
|
|
202
|
+
return "string";
|
|
203
|
+
case ts.SyntaxKind.NumberKeyword:
|
|
204
|
+
return "number";
|
|
205
|
+
case ts.SyntaxKind.BooleanKeyword:
|
|
206
|
+
return "boolean";
|
|
207
|
+
default:
|
|
208
|
+
return void 0;
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
const unwrapTypeNode = (typeNode) => {
|
|
212
|
+
const withoutPromise = unwrapPromise(typeNode);
|
|
213
|
+
const { inner: withoutNullable, isNullable } = unwrapNullable(withoutPromise);
|
|
214
|
+
const arrayElement = unwrapArrayElement(withoutNullable);
|
|
215
|
+
const element = arrayElement === void 0 ? withoutNullable : unwrapNullable(arrayElement).inner;
|
|
216
|
+
const identifierText = identifierTextOf(element);
|
|
217
|
+
if (identifierText === void 0) {
|
|
218
|
+
return void 0;
|
|
219
|
+
}
|
|
220
|
+
return { identifierText, isArray: arrayElement !== void 0, isNullable };
|
|
221
|
+
};
|
|
222
|
+
const graphqlScalarNameFor = (identifierText) => PRIMITIVE_SCALAR_TYPES[identifierText] ?? identifierText;
|
|
223
|
+
const graphqlTypeStringFor = (unwrapped, explicitTypeName) => {
|
|
224
|
+
const baseName = explicitTypeName ?? graphqlScalarNameFor(unwrapped.identifierText);
|
|
225
|
+
const wrapped = unwrapped.isArray ? `[${baseName}!]` : baseName;
|
|
226
|
+
return unwrapped.isNullable ? wrapped : `${wrapped}!`;
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
const OBSERVABLE_TYPE_NAME = "Observable";
|
|
230
|
+
const STREAMABLE_FILE_TYPE_NAME = "StreamableFile";
|
|
231
|
+
const readResponseKind = (returnType) => {
|
|
232
|
+
if (returnType === void 0) {
|
|
233
|
+
return "json";
|
|
234
|
+
}
|
|
235
|
+
const unwrapped = unwrapPromise(returnType);
|
|
236
|
+
if (!ts.isTypeReferenceNode(unwrapped) || !ts.isIdentifier(unwrapped.typeName)) {
|
|
237
|
+
return "json";
|
|
238
|
+
}
|
|
239
|
+
if (unwrapped.typeName.text === OBSERVABLE_TYPE_NAME) {
|
|
240
|
+
return "observable";
|
|
241
|
+
}
|
|
242
|
+
if (unwrapped.typeName.text === STREAMABLE_FILE_TYPE_NAME) {
|
|
243
|
+
return "stream";
|
|
244
|
+
}
|
|
245
|
+
return "json";
|
|
246
|
+
};
|
|
247
|
+
|
|
163
248
|
const trimSlashes = (segment) => segment.replace(/^\/+/, "").replace(/\/+$/, "");
|
|
164
249
|
const joinRoutePath = (controllerPath, methodPath) => {
|
|
165
250
|
const segments = [trimSlashes(controllerPath), trimSlashes(methodPath ?? "")].filter(
|
|
@@ -215,6 +300,7 @@ const extractMethodDefinition = (method, context) => {
|
|
|
215
300
|
httpMethod,
|
|
216
301
|
path: joinRoutePath(context.controllerPath, routeArgument.value),
|
|
217
302
|
parameters: parameters.filter((parameter) => parameter !== void 0),
|
|
303
|
+
responseKind: readResponseKind(method.type),
|
|
218
304
|
line: location.line,
|
|
219
305
|
column: location.column
|
|
220
306
|
};
|
|
@@ -292,6 +378,547 @@ const analyzeControllers = (filePaths) => {
|
|
|
292
378
|
return { controllers, diagnostics };
|
|
293
379
|
};
|
|
294
380
|
|
|
381
|
+
const GRAPHQL_OPERATION_DECORATOR_NAMES = {
|
|
382
|
+
Query: "query",
|
|
383
|
+
Mutation: "mutation"
|
|
384
|
+
};
|
|
385
|
+
|
|
386
|
+
const readTypeThunkExpression = (thunk) => {
|
|
387
|
+
if (!ts.isBlock(thunk.body)) {
|
|
388
|
+
return thunk.body;
|
|
389
|
+
}
|
|
390
|
+
const returnStatement = thunk.body.statements.find(ts.isReturnStatement);
|
|
391
|
+
return returnStatement?.expression;
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
const readArgsTypeOption = (decorator) => {
|
|
395
|
+
if (!ts.isCallExpression(decorator.expression)) {
|
|
396
|
+
return void 0;
|
|
397
|
+
}
|
|
398
|
+
const optionsArgument = decorator.expression.arguments.find(ts.isObjectLiteralExpression);
|
|
399
|
+
if (optionsArgument === void 0) {
|
|
400
|
+
return void 0;
|
|
401
|
+
}
|
|
402
|
+
const typeProperty = optionsArgument.properties.find(
|
|
403
|
+
(property) => ts.isPropertyAssignment(property) && ts.isIdentifier(property.name) && property.name.text === "type"
|
|
404
|
+
);
|
|
405
|
+
if (typeProperty === void 0) {
|
|
406
|
+
return void 0;
|
|
407
|
+
}
|
|
408
|
+
const thunk = typeProperty.initializer;
|
|
409
|
+
if (!ts.isArrowFunction(thunk) && !ts.isFunctionExpression(thunk)) {
|
|
410
|
+
return void 0;
|
|
411
|
+
}
|
|
412
|
+
const returnedExpression = readTypeThunkExpression(thunk);
|
|
413
|
+
if (returnedExpression === void 0) {
|
|
414
|
+
return void 0;
|
|
415
|
+
}
|
|
416
|
+
if (ts.isArrayLiteralExpression(returnedExpression)) {
|
|
417
|
+
const element = returnedExpression.elements[0];
|
|
418
|
+
return element !== void 0 && ts.isIdentifier(element) ? { typeName: element.text, isArray: true } : void 0;
|
|
419
|
+
}
|
|
420
|
+
return ts.isIdentifier(returnedExpression) ? { typeName: returnedExpression.text, isArray: false } : void 0;
|
|
421
|
+
};
|
|
422
|
+
|
|
423
|
+
const extractResolverArgumentDefinition = (parameter, index, context) => {
|
|
424
|
+
const decorator = findAnyDecorator(parameter, ["Args"]);
|
|
425
|
+
const location = locationOf(context.sourceFile, parameter);
|
|
426
|
+
if (decorator === void 0) {
|
|
427
|
+
context.diagnostics.push({
|
|
428
|
+
code: "unsupported-argument",
|
|
429
|
+
title: "Unsupported argument.",
|
|
430
|
+
controllerName: context.resolverName,
|
|
431
|
+
memberName: context.methodName,
|
|
432
|
+
detail: `Parameter "${parameter.name.getText()}" must be decorated with @Args to be transported through a NestBridge GraphQL client.`,
|
|
433
|
+
filePath: context.filePath,
|
|
434
|
+
line: location.line,
|
|
435
|
+
column: location.column
|
|
436
|
+
});
|
|
437
|
+
return void 0;
|
|
438
|
+
}
|
|
439
|
+
if (!ts.isIdentifier(parameter.name)) {
|
|
440
|
+
context.diagnostics.push({
|
|
441
|
+
code: "unsupported-argument",
|
|
442
|
+
title: "Unsupported argument.",
|
|
443
|
+
controllerName: context.resolverName,
|
|
444
|
+
memberName: context.methodName,
|
|
445
|
+
detail: "Destructured parameters cannot be transported through a NestBridge GraphQL client.",
|
|
446
|
+
filePath: context.filePath,
|
|
447
|
+
line: location.line,
|
|
448
|
+
column: location.column
|
|
449
|
+
});
|
|
450
|
+
return void 0;
|
|
451
|
+
}
|
|
452
|
+
const nameArgument = readStaticStringArgument(decorator);
|
|
453
|
+
if (!nameArgument.ok) {
|
|
454
|
+
context.diagnostics.push({
|
|
455
|
+
code: "unsupported-argument",
|
|
456
|
+
title: "Unsupported argument.",
|
|
457
|
+
controllerName: context.resolverName,
|
|
458
|
+
memberName: context.methodName,
|
|
459
|
+
detail: "@Args() name must be a statically analyzable string literal.",
|
|
460
|
+
found: `@Args(${nameArgument.argumentText})`,
|
|
461
|
+
filePath: context.filePath,
|
|
462
|
+
line: location.line,
|
|
463
|
+
column: location.column
|
|
464
|
+
});
|
|
465
|
+
return void 0;
|
|
466
|
+
}
|
|
467
|
+
if (nameArgument.value === void 0) {
|
|
468
|
+
context.diagnostics.push({
|
|
469
|
+
code: "unsupported-argument",
|
|
470
|
+
title: "Unsupported argument.",
|
|
471
|
+
controllerName: context.resolverName,
|
|
472
|
+
memberName: context.methodName,
|
|
473
|
+
detail: "A whole @Args() is not supported; provide an argument name such as @Args('id').",
|
|
474
|
+
filePath: context.filePath,
|
|
475
|
+
line: location.line,
|
|
476
|
+
column: location.column
|
|
477
|
+
});
|
|
478
|
+
return void 0;
|
|
479
|
+
}
|
|
480
|
+
if (parameter.type === void 0) {
|
|
481
|
+
context.diagnostics.push({
|
|
482
|
+
code: "unsupported-argument",
|
|
483
|
+
title: "Unsupported argument.",
|
|
484
|
+
controllerName: context.resolverName,
|
|
485
|
+
memberName: context.methodName,
|
|
486
|
+
detail: `@Args() parameter "${parameter.name.text}" needs an explicit type annotation.`,
|
|
487
|
+
filePath: context.filePath,
|
|
488
|
+
line: location.line,
|
|
489
|
+
column: location.column
|
|
490
|
+
});
|
|
491
|
+
return void 0;
|
|
492
|
+
}
|
|
493
|
+
const unwrapped = unwrapTypeNode(parameter.type);
|
|
494
|
+
if (unwrapped === void 0) {
|
|
495
|
+
context.diagnostics.push({
|
|
496
|
+
code: "unsupported-argument",
|
|
497
|
+
title: "Unsupported argument.",
|
|
498
|
+
controllerName: context.resolverName,
|
|
499
|
+
memberName: context.methodName,
|
|
500
|
+
detail: `@Args() parameter "${parameter.name.text}" has a type NestBridge cannot statically resolve to a GraphQL type.`,
|
|
501
|
+
found: parameter.type.getText(),
|
|
502
|
+
filePath: context.filePath,
|
|
503
|
+
line: location.line,
|
|
504
|
+
column: location.column
|
|
505
|
+
});
|
|
506
|
+
return void 0;
|
|
507
|
+
}
|
|
508
|
+
const explicitType = readArgsTypeOption(decorator);
|
|
509
|
+
return {
|
|
510
|
+
index,
|
|
511
|
+
parameterName: parameter.name.text,
|
|
512
|
+
name: nameArgument.value,
|
|
513
|
+
graphqlType: graphqlTypeStringFor(unwrapped, explicitType?.typeName)
|
|
514
|
+
};
|
|
515
|
+
};
|
|
516
|
+
|
|
517
|
+
const parsedFilesByPath = /* @__PURE__ */ new Map();
|
|
518
|
+
const parseFile = (filePath) => {
|
|
519
|
+
const cached = parsedFilesByPath.get(filePath);
|
|
520
|
+
if (cached !== void 0) {
|
|
521
|
+
return cached;
|
|
522
|
+
}
|
|
523
|
+
const content = readFileSync(filePath, "utf-8");
|
|
524
|
+
const sourceFile = ts.createSourceFile(
|
|
525
|
+
filePath,
|
|
526
|
+
content,
|
|
527
|
+
ts.ScriptTarget.ESNext,
|
|
528
|
+
true,
|
|
529
|
+
ts.ScriptKind.TS
|
|
530
|
+
);
|
|
531
|
+
parsedFilesByPath.set(filePath, sourceFile);
|
|
532
|
+
return sourceFile;
|
|
533
|
+
};
|
|
534
|
+
const resolveModuleFile = (fromFilePath, moduleSpecifierText) => {
|
|
535
|
+
if (!moduleSpecifierText.startsWith(".")) {
|
|
536
|
+
return void 0;
|
|
537
|
+
}
|
|
538
|
+
const base = resolve(dirname(fromFilePath), moduleSpecifierText);
|
|
539
|
+
const candidates = [`${base}.ts`, `${base}.tsx`, resolve(base, "index.ts")];
|
|
540
|
+
return candidates.find((candidate) => existsSync(candidate));
|
|
541
|
+
};
|
|
542
|
+
const findExportedDeclaration = (sourceFile, name) => sourceFile.statements.find(
|
|
543
|
+
(statement) => ts.isClassDeclaration(statement) && statement.name?.text === name
|
|
544
|
+
);
|
|
545
|
+
const findImportSource = (sourceFile, localName) => {
|
|
546
|
+
for (const statement of sourceFile.statements) {
|
|
547
|
+
if (!ts.isImportDeclaration(statement) || !ts.isStringLiteralLike(statement.moduleSpecifier)) {
|
|
548
|
+
continue;
|
|
549
|
+
}
|
|
550
|
+
const namedBindings = statement.importClause?.namedBindings;
|
|
551
|
+
if (namedBindings === void 0 || !ts.isNamedImports(namedBindings)) {
|
|
552
|
+
continue;
|
|
553
|
+
}
|
|
554
|
+
const element = namedBindings.elements.find((candidate) => candidate.name.text === localName);
|
|
555
|
+
if (element !== void 0) {
|
|
556
|
+
return {
|
|
557
|
+
moduleSpecifierText: statement.moduleSpecifier.text,
|
|
558
|
+
importedName: element.propertyName?.text ?? element.name.text
|
|
559
|
+
};
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
return void 0;
|
|
563
|
+
};
|
|
564
|
+
const findReExportSource = (sourceFile, name) => {
|
|
565
|
+
for (const statement of sourceFile.statements) {
|
|
566
|
+
if (!ts.isExportDeclaration(statement) || statement.moduleSpecifier === void 0 || !ts.isStringLiteralLike(statement.moduleSpecifier)) {
|
|
567
|
+
continue;
|
|
568
|
+
}
|
|
569
|
+
if (statement.exportClause === void 0) {
|
|
570
|
+
return statement.moduleSpecifier.text;
|
|
571
|
+
}
|
|
572
|
+
if (ts.isNamedExports(statement.exportClause) && statement.exportClause.elements.some(
|
|
573
|
+
(element) => (element.propertyName?.text ?? element.name.text) === name
|
|
574
|
+
)) {
|
|
575
|
+
return statement.moduleSpecifier.text;
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
return void 0;
|
|
579
|
+
};
|
|
580
|
+
const resolveClassDeclaration = (name, fromSourceFile, fromFilePath, visited = /* @__PURE__ */ new Set()) => {
|
|
581
|
+
if (visited.has(fromFilePath)) {
|
|
582
|
+
return void 0;
|
|
583
|
+
}
|
|
584
|
+
visited.add(fromFilePath);
|
|
585
|
+
const direct = findExportedDeclaration(fromSourceFile, name);
|
|
586
|
+
if (direct !== void 0) {
|
|
587
|
+
return { classDeclaration: direct, sourceFile: fromSourceFile, filePath: fromFilePath };
|
|
588
|
+
}
|
|
589
|
+
const importSource = findImportSource(fromSourceFile, name);
|
|
590
|
+
if (importSource !== void 0) {
|
|
591
|
+
const resolvedPath = resolveModuleFile(fromFilePath, importSource.moduleSpecifierText);
|
|
592
|
+
if (resolvedPath !== void 0) {
|
|
593
|
+
const nextSourceFile = parseFile(resolvedPath);
|
|
594
|
+
const result = resolveClassDeclaration(
|
|
595
|
+
importSource.importedName,
|
|
596
|
+
nextSourceFile,
|
|
597
|
+
resolvedPath,
|
|
598
|
+
visited
|
|
599
|
+
);
|
|
600
|
+
if (result !== void 0) {
|
|
601
|
+
return result;
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
const reExportModuleSpecifierText = findReExportSource(fromSourceFile, name);
|
|
606
|
+
if (reExportModuleSpecifierText !== void 0) {
|
|
607
|
+
const resolvedPath = resolveModuleFile(fromFilePath, reExportModuleSpecifierText);
|
|
608
|
+
if (resolvedPath !== void 0) {
|
|
609
|
+
const nextSourceFile = parseFile(resolvedPath);
|
|
610
|
+
return resolveClassDeclaration(name, nextSourceFile, resolvedPath, visited);
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
return void 0;
|
|
614
|
+
};
|
|
615
|
+
|
|
616
|
+
const PRIMITIVE_SCALAR_NAMES$1 = /* @__PURE__ */ new Set(["string", "number", "boolean"]);
|
|
617
|
+
const extractSelectionFields = (classDeclaration, sourceFile, filePath, context) => {
|
|
618
|
+
const typeName = classDeclaration.name?.text ?? "(anonymous type)";
|
|
619
|
+
const location = locationOf(sourceFile, classDeclaration);
|
|
620
|
+
if (context.visitedTypeNames.has(typeName)) {
|
|
621
|
+
context.diagnostics.push({
|
|
622
|
+
code: "circular-type",
|
|
623
|
+
title: "Circular type.",
|
|
624
|
+
controllerName: context.resolverName,
|
|
625
|
+
memberName: context.methodName,
|
|
626
|
+
detail: `Type "${typeName}" references itself, directly or indirectly. NestBridge cannot build a finite GraphQL selection set for it.`,
|
|
627
|
+
filePath,
|
|
628
|
+
line: location.line,
|
|
629
|
+
column: location.column
|
|
630
|
+
});
|
|
631
|
+
return void 0;
|
|
632
|
+
}
|
|
633
|
+
const nextVisited = new Set(context.visitedTypeNames).add(typeName);
|
|
634
|
+
const properties = classDeclaration.members.filter(
|
|
635
|
+
(member) => ts.isPropertyDeclaration(member) && findDecorator(member, "Field") !== void 0
|
|
636
|
+
);
|
|
637
|
+
const fields = [];
|
|
638
|
+
for (const property of properties) {
|
|
639
|
+
if (!ts.isIdentifier(property.name)) {
|
|
640
|
+
continue;
|
|
641
|
+
}
|
|
642
|
+
const fieldName = property.name.text;
|
|
643
|
+
const fieldLocation = locationOf(sourceFile, property);
|
|
644
|
+
if (property.type === void 0) {
|
|
645
|
+
context.diagnostics.push({
|
|
646
|
+
code: "unsupported-return-type",
|
|
647
|
+
title: "Unsupported field type.",
|
|
648
|
+
controllerName: context.resolverName,
|
|
649
|
+
memberName: context.methodName,
|
|
650
|
+
detail: `Field "${fieldName}" on "${typeName}" needs an explicit type annotation.`,
|
|
651
|
+
filePath,
|
|
652
|
+
line: fieldLocation.line,
|
|
653
|
+
column: fieldLocation.column
|
|
654
|
+
});
|
|
655
|
+
return void 0;
|
|
656
|
+
}
|
|
657
|
+
const unwrapped = unwrapTypeNode(property.type);
|
|
658
|
+
if (unwrapped === void 0) {
|
|
659
|
+
context.diagnostics.push({
|
|
660
|
+
code: "unsupported-return-type",
|
|
661
|
+
title: "Unsupported field type.",
|
|
662
|
+
controllerName: context.resolverName,
|
|
663
|
+
memberName: context.methodName,
|
|
664
|
+
detail: `Field "${fieldName}" on "${typeName}" has a type NestBridge cannot statically resolve.`,
|
|
665
|
+
found: property.type.getText(),
|
|
666
|
+
filePath,
|
|
667
|
+
line: fieldLocation.line,
|
|
668
|
+
column: fieldLocation.column
|
|
669
|
+
});
|
|
670
|
+
return void 0;
|
|
671
|
+
}
|
|
672
|
+
if (PRIMITIVE_SCALAR_NAMES$1.has(unwrapped.identifierText)) {
|
|
673
|
+
fields.push({ name: fieldName });
|
|
674
|
+
continue;
|
|
675
|
+
}
|
|
676
|
+
const resolved = resolveClassDeclaration(unwrapped.identifierText, sourceFile, filePath);
|
|
677
|
+
if (resolved === void 0) {
|
|
678
|
+
fields.push({ name: fieldName });
|
|
679
|
+
continue;
|
|
680
|
+
}
|
|
681
|
+
if (findDecorator(resolved.classDeclaration, "ObjectType") === void 0) {
|
|
682
|
+
context.diagnostics.push({
|
|
683
|
+
code: "unsupported-return-type",
|
|
684
|
+
title: "Unsupported field type.",
|
|
685
|
+
controllerName: context.resolverName,
|
|
686
|
+
memberName: context.methodName,
|
|
687
|
+
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.`,
|
|
688
|
+
filePath,
|
|
689
|
+
line: fieldLocation.line,
|
|
690
|
+
column: fieldLocation.column
|
|
691
|
+
});
|
|
692
|
+
return void 0;
|
|
693
|
+
}
|
|
694
|
+
const children = extractSelectionFields(
|
|
695
|
+
resolved.classDeclaration,
|
|
696
|
+
resolved.sourceFile,
|
|
697
|
+
resolved.filePath,
|
|
698
|
+
{
|
|
699
|
+
...context,
|
|
700
|
+
visitedTypeNames: nextVisited
|
|
701
|
+
}
|
|
702
|
+
);
|
|
703
|
+
if (children === void 0) {
|
|
704
|
+
return void 0;
|
|
705
|
+
}
|
|
706
|
+
fields.push({ name: fieldName, children });
|
|
707
|
+
}
|
|
708
|
+
return fields;
|
|
709
|
+
};
|
|
710
|
+
|
|
711
|
+
const readNameFromOptions = (optionsArgument) => {
|
|
712
|
+
const nameProperty = optionsArgument.properties.find(
|
|
713
|
+
(property) => ts.isPropertyAssignment(property) && ts.isIdentifier(property.name) && property.name.text === "name"
|
|
714
|
+
);
|
|
715
|
+
if (nameProperty === void 0 || !ts.isStringLiteralLike(nameProperty.initializer)) {
|
|
716
|
+
return void 0;
|
|
717
|
+
}
|
|
718
|
+
return nameProperty.initializer.text;
|
|
719
|
+
};
|
|
720
|
+
const readGraphqlOperationName = (decorator) => {
|
|
721
|
+
const expression = decorator.expression;
|
|
722
|
+
if (!ts.isCallExpression(expression)) {
|
|
723
|
+
return { ok: true, value: void 0 };
|
|
724
|
+
}
|
|
725
|
+
let value;
|
|
726
|
+
for (const argument of expression.arguments) {
|
|
727
|
+
if (ts.isArrowFunction(argument) || ts.isFunctionExpression(argument)) {
|
|
728
|
+
continue;
|
|
729
|
+
}
|
|
730
|
+
if (ts.isStringLiteralLike(argument)) {
|
|
731
|
+
value = argument.text;
|
|
732
|
+
continue;
|
|
733
|
+
}
|
|
734
|
+
if (ts.isObjectLiteralExpression(argument)) {
|
|
735
|
+
value = readNameFromOptions(argument) ?? value;
|
|
736
|
+
continue;
|
|
737
|
+
}
|
|
738
|
+
return { ok: false, argumentText: argument.getText() };
|
|
739
|
+
}
|
|
740
|
+
return { ok: true, value };
|
|
741
|
+
};
|
|
742
|
+
|
|
743
|
+
const readGraphqlReturnTypeOption = (decorator) => {
|
|
744
|
+
if (!ts.isCallExpression(decorator.expression)) {
|
|
745
|
+
return void 0;
|
|
746
|
+
}
|
|
747
|
+
const thunk = decorator.expression.arguments.find(
|
|
748
|
+
(argument) => ts.isArrowFunction(argument) || ts.isFunctionExpression(argument)
|
|
749
|
+
);
|
|
750
|
+
if (thunk === void 0) {
|
|
751
|
+
return void 0;
|
|
752
|
+
}
|
|
753
|
+
const returnedExpression = readTypeThunkExpression(thunk);
|
|
754
|
+
if (returnedExpression === void 0) {
|
|
755
|
+
return void 0;
|
|
756
|
+
}
|
|
757
|
+
if (ts.isArrayLiteralExpression(returnedExpression)) {
|
|
758
|
+
const element = returnedExpression.elements[0];
|
|
759
|
+
return element !== void 0 && ts.isIdentifier(element) ? element.text : void 0;
|
|
760
|
+
}
|
|
761
|
+
return ts.isIdentifier(returnedExpression) ? returnedExpression.text : void 0;
|
|
762
|
+
};
|
|
763
|
+
|
|
764
|
+
const PRIMITIVE_SCALAR_NAMES = /* @__PURE__ */ new Set(["string", "number", "boolean"]);
|
|
765
|
+
const extractResolverMethodDefinition = (method, context) => {
|
|
766
|
+
const decorator = findAnyDecorator(method, Object.keys(GRAPHQL_OPERATION_DECORATOR_NAMES));
|
|
767
|
+
if (decorator === void 0) {
|
|
768
|
+
return void 0;
|
|
769
|
+
}
|
|
770
|
+
const methodName = method.name.getText();
|
|
771
|
+
const decoratorName = decoratorNameOf(decorator);
|
|
772
|
+
const location = locationOf(context.sourceFile, method);
|
|
773
|
+
if (decoratorName === void 0) {
|
|
774
|
+
return void 0;
|
|
775
|
+
}
|
|
776
|
+
const operationKind = GRAPHQL_OPERATION_DECORATOR_NAMES[decoratorName];
|
|
777
|
+
if (operationKind === void 0) {
|
|
778
|
+
return void 0;
|
|
779
|
+
}
|
|
780
|
+
const nameArgument = readGraphqlOperationName(decorator);
|
|
781
|
+
if (!nameArgument.ok) {
|
|
782
|
+
context.diagnostics.push({
|
|
783
|
+
code: "unsupported-route",
|
|
784
|
+
title: "Unsupported operation name.",
|
|
785
|
+
controllerName: context.resolverName,
|
|
786
|
+
memberName: methodName,
|
|
787
|
+
detail: "GraphQL operation names must be statically analyzable string literals.",
|
|
788
|
+
found: `@${decoratorName}(${nameArgument.argumentText})`,
|
|
789
|
+
filePath: context.filePath,
|
|
790
|
+
line: location.line,
|
|
791
|
+
column: location.column
|
|
792
|
+
});
|
|
793
|
+
return void 0;
|
|
794
|
+
}
|
|
795
|
+
const decoratorReturnType = readGraphqlReturnTypeOption(decorator);
|
|
796
|
+
const identifierText = decoratorReturnType ?? (method.type === void 0 ? void 0 : unwrapTypeNode(method.type)?.identifierText);
|
|
797
|
+
if (identifierText === void 0) {
|
|
798
|
+
context.diagnostics.push({
|
|
799
|
+
code: "unsupported-return-type",
|
|
800
|
+
title: "Unsupported return type.",
|
|
801
|
+
controllerName: context.resolverName,
|
|
802
|
+
memberName: methodName,
|
|
803
|
+
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.',
|
|
804
|
+
filePath: context.filePath,
|
|
805
|
+
line: location.line,
|
|
806
|
+
column: location.column
|
|
807
|
+
});
|
|
808
|
+
return void 0;
|
|
809
|
+
}
|
|
810
|
+
let selection = [];
|
|
811
|
+
if (!PRIMITIVE_SCALAR_NAMES.has(identifierText.toLowerCase())) {
|
|
812
|
+
const resolved = resolveClassDeclaration(identifierText, context.sourceFile, context.filePath);
|
|
813
|
+
if (resolved === void 0 && decoratorReturnType === void 0) {
|
|
814
|
+
context.diagnostics.push({
|
|
815
|
+
code: "unsupported-return-type",
|
|
816
|
+
title: "Unsupported return type.",
|
|
817
|
+
controllerName: context.resolverName,
|
|
818
|
+
memberName: methodName,
|
|
819
|
+
detail: `NestBridge could not locate the declaration of type "${identifierText}".`,
|
|
820
|
+
filePath: context.filePath,
|
|
821
|
+
line: location.line,
|
|
822
|
+
column: location.column
|
|
823
|
+
});
|
|
824
|
+
return void 0;
|
|
825
|
+
}
|
|
826
|
+
if (resolved !== void 0) {
|
|
827
|
+
const fields = extractSelectionFields(
|
|
828
|
+
resolved.classDeclaration,
|
|
829
|
+
resolved.sourceFile,
|
|
830
|
+
resolved.filePath,
|
|
831
|
+
{
|
|
832
|
+
resolverName: context.resolverName,
|
|
833
|
+
methodName,
|
|
834
|
+
diagnostics: context.diagnostics,
|
|
835
|
+
visitedTypeNames: /* @__PURE__ */ new Set()
|
|
836
|
+
}
|
|
837
|
+
);
|
|
838
|
+
if (fields === void 0) {
|
|
839
|
+
return void 0;
|
|
840
|
+
}
|
|
841
|
+
selection = fields;
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
const args = method.parameters.map(
|
|
845
|
+
(parameter, index) => extractResolverArgumentDefinition(parameter, index, {
|
|
846
|
+
sourceFile: context.sourceFile,
|
|
847
|
+
filePath: context.filePath,
|
|
848
|
+
resolverName: context.resolverName,
|
|
849
|
+
methodName,
|
|
850
|
+
diagnostics: context.diagnostics
|
|
851
|
+
})
|
|
852
|
+
);
|
|
853
|
+
if (args.some((argument) => argument === void 0)) {
|
|
854
|
+
return void 0;
|
|
855
|
+
}
|
|
856
|
+
return {
|
|
857
|
+
name: methodName,
|
|
858
|
+
operationKind,
|
|
859
|
+
operationName: nameArgument.value ?? methodName,
|
|
860
|
+
arguments: args.filter((argument) => argument !== void 0),
|
|
861
|
+
selection,
|
|
862
|
+
line: location.line,
|
|
863
|
+
column: location.column
|
|
864
|
+
};
|
|
865
|
+
};
|
|
866
|
+
|
|
867
|
+
const extractResolverDefinition = (classDeclaration, sourceFile, filePath, diagnostics) => {
|
|
868
|
+
const resolverName = classDeclaration.name?.text ?? "(anonymous resolver)";
|
|
869
|
+
const methods = classDeclaration.members.filter((member) => ts.isMethodDeclaration(member)).map(
|
|
870
|
+
(method) => extractResolverMethodDefinition(method, {
|
|
871
|
+
sourceFile,
|
|
872
|
+
filePath,
|
|
873
|
+
resolverName,
|
|
874
|
+
diagnostics
|
|
875
|
+
})
|
|
876
|
+
).filter((method) => method !== void 0);
|
|
877
|
+
return {
|
|
878
|
+
name: resolverName,
|
|
879
|
+
sourceFile: filePath,
|
|
880
|
+
methods
|
|
881
|
+
};
|
|
882
|
+
};
|
|
883
|
+
|
|
884
|
+
const findResolverClasses = (sourceFile) => {
|
|
885
|
+
return sourceFile.statements.filter(
|
|
886
|
+
(statement) => ts.isClassDeclaration(statement) && findDecorator(statement, "Resolver") !== void 0
|
|
887
|
+
);
|
|
888
|
+
};
|
|
889
|
+
|
|
890
|
+
const analyzeResolvers = (filePaths) => {
|
|
891
|
+
const diagnostics = [];
|
|
892
|
+
const resolvers = [];
|
|
893
|
+
for (const filePath of filePaths) {
|
|
894
|
+
const content = readFileSync(filePath, "utf-8");
|
|
895
|
+
const sourceFile = ts.createSourceFile(
|
|
896
|
+
filePath,
|
|
897
|
+
content,
|
|
898
|
+
ts.ScriptTarget.ESNext,
|
|
899
|
+
true,
|
|
900
|
+
ts.ScriptKind.TS
|
|
901
|
+
);
|
|
902
|
+
for (const classDeclaration of findResolverClasses(sourceFile)) {
|
|
903
|
+
const resolver = extractResolverDefinition(
|
|
904
|
+
classDeclaration,
|
|
905
|
+
sourceFile,
|
|
906
|
+
filePath,
|
|
907
|
+
diagnostics
|
|
908
|
+
);
|
|
909
|
+
if (resolver !== void 0) {
|
|
910
|
+
resolvers.push(resolver);
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
return { resolvers, diagnostics };
|
|
915
|
+
};
|
|
916
|
+
|
|
917
|
+
const REMOTE_RESULT_HELPERS = {
|
|
918
|
+
json: "RemoteResult",
|
|
919
|
+
observable: "RemoteObservableResult",
|
|
920
|
+
stream: "RemoteStreamResult"
|
|
921
|
+
};
|
|
295
922
|
const toPosixPath = (filePath) => filePath.replace(/\\/g, "/");
|
|
296
923
|
const importSpecifierFor = (sourceFile, outputFilePath) => {
|
|
297
924
|
if (!sourceFile.startsWith(".") && !/^[A-Za-z]:[\\/]/.test(sourceFile) && !sourceFile.startsWith("/")) {
|
|
@@ -308,13 +935,20 @@ const generateControllerDeclaration = (controller, outputFilePath) => {
|
|
|
308
935
|
const sourceFileSpecifier = importSpecifierFor(controller.sourceFile, outputFilePath);
|
|
309
936
|
const importSpecifier = stripExtension(sourceFileSpecifier);
|
|
310
937
|
const serverAlias = `__Server${controller.name}`;
|
|
311
|
-
const methodDeclarations = controller.methods.map(
|
|
312
|
-
|
|
313
|
-
)
|
|
938
|
+
const methodDeclarations = controller.methods.map((method) => {
|
|
939
|
+
const helperName = REMOTE_RESULT_HELPERS[method.responseKind ?? "json"];
|
|
940
|
+
return ` ${method.name}(...args: Parameters<${serverAlias}['${method.name}']>): ${helperName}<${serverAlias}['${method.name}']>;`;
|
|
941
|
+
});
|
|
314
942
|
const classBody = [" constructor();", "", ...methodDeclarations].join("\n");
|
|
943
|
+
const usedHelperNames = new Set(
|
|
944
|
+
controller.methods.map((method) => REMOTE_RESULT_HELPERS[method.responseKind ?? "json"])
|
|
945
|
+
);
|
|
946
|
+
const helperImports = Array.from(
|
|
947
|
+
usedHelperNames.size > 0 ? usedHelperNames : ["RemoteResult"]
|
|
948
|
+
).sort();
|
|
315
949
|
return [
|
|
316
950
|
`import type { ${controller.name} as ${serverAlias} } from '${importSpecifier}';`,
|
|
317
|
-
`import type {
|
|
951
|
+
`import type { ${helperImports.join(", ")} } from '@nestbridge/runtime';`,
|
|
318
952
|
"",
|
|
319
953
|
"/**",
|
|
320
954
|
" * Generated by NestBridge — client-facing methods only.",
|
|
@@ -396,7 +1030,7 @@ const generatePathExpression = (method) => {
|
|
|
396
1030
|
|
|
397
1031
|
const generateQueryExpression = (parameters) => mergeNamedParametersExpression(parameters.filter((parameter) => parameter.kind === "query"));
|
|
398
1032
|
|
|
399
|
-
const generateMethod = (method) => {
|
|
1033
|
+
const generateMethod$1 = (method) => {
|
|
400
1034
|
const requestProperties = [
|
|
401
1035
|
`method: ${JSON.stringify(method.httpMethod)}`,
|
|
402
1036
|
`path: ${generatePathExpression(method)}`
|
|
@@ -413,6 +1047,9 @@ const generateMethod = (method) => {
|
|
|
413
1047
|
if (headersExpression !== void 0) {
|
|
414
1048
|
requestProperties.push(`headers: ${headersExpression}`);
|
|
415
1049
|
}
|
|
1050
|
+
if (method.responseKind === "stream") {
|
|
1051
|
+
requestProperties.push(`responseType: "blob"`);
|
|
1052
|
+
}
|
|
416
1053
|
return [
|
|
417
1054
|
` ${method.name}(${generateParameterList(method.parameters)}) {`,
|
|
418
1055
|
" return request({",
|
|
@@ -422,7 +1059,7 @@ const generateMethod = (method) => {
|
|
|
422
1059
|
].join("\n");
|
|
423
1060
|
};
|
|
424
1061
|
const generateControllerModule = (controller) => {
|
|
425
|
-
const methods = controller.methods.map(generateMethod).join("\n\n");
|
|
1062
|
+
const methods = controller.methods.map(generateMethod$1).join("\n\n");
|
|
426
1063
|
return [
|
|
427
1064
|
"import { request } from '@nestbridge/runtime';",
|
|
428
1065
|
"",
|
|
@@ -435,6 +1072,63 @@ const generateControllerModule = (controller) => {
|
|
|
435
1072
|
].join("\n");
|
|
436
1073
|
};
|
|
437
1074
|
|
|
1075
|
+
const capitalize = (value) => `${value.charAt(0).toUpperCase()}${value.slice(1)}`;
|
|
1076
|
+
const renderSelectionSet = (fields) => {
|
|
1077
|
+
const rendered = fields.map(
|
|
1078
|
+
(field) => field.children === void 0 ? field.name : `${field.name} ${renderSelectionSet(field.children)}`
|
|
1079
|
+
);
|
|
1080
|
+
return `{ ${rendered.join(" ")} }`;
|
|
1081
|
+
};
|
|
1082
|
+
const generateGraphqlDocument = (method) => {
|
|
1083
|
+
const variableDeclarations = method.arguments.map((argument) => `$${argument.name}: ${argument.graphqlType}`).join(", ");
|
|
1084
|
+
const fieldArguments = method.arguments.map((argument) => `${argument.name}: $${argument.name}`).join(", ");
|
|
1085
|
+
const variablesClause = variableDeclarations.length > 0 ? `(${variableDeclarations})` : "";
|
|
1086
|
+
const fieldArgumentsClause = fieldArguments.length > 0 ? `(${fieldArguments})` : "";
|
|
1087
|
+
const fieldSelection = method.selection.length > 0 ? ` ${renderSelectionSet(method.selection)}` : "";
|
|
1088
|
+
return `${method.operationKind} ${capitalize(method.name)}${variablesClause} { ${method.operationName}${fieldArgumentsClause}${fieldSelection} }`;
|
|
1089
|
+
};
|
|
1090
|
+
|
|
1091
|
+
const generateResolverArgumentList = (resolverArguments) => [...resolverArguments].sort((a, b) => a.index - b.index).map((argument) => argument.parameterName).join(", ");
|
|
1092
|
+
|
|
1093
|
+
const generateVariablesExpression = (resolverArguments) => {
|
|
1094
|
+
if (resolverArguments.length === 0) {
|
|
1095
|
+
return void 0;
|
|
1096
|
+
}
|
|
1097
|
+
const entries = resolverArguments.map(
|
|
1098
|
+
(argument) => `${JSON.stringify(argument.name)}: ${argument.parameterName}`
|
|
1099
|
+
);
|
|
1100
|
+
return `{ ${entries.join(", ")} }`;
|
|
1101
|
+
};
|
|
1102
|
+
const generateMethod = (method) => {
|
|
1103
|
+
const requestProperties = [`document: ${JSON.stringify(generateGraphqlDocument(method))}`];
|
|
1104
|
+
const variablesExpression = generateVariablesExpression(method.arguments);
|
|
1105
|
+
if (variablesExpression !== void 0) {
|
|
1106
|
+
requestProperties.push(`variables: ${variablesExpression}`);
|
|
1107
|
+
}
|
|
1108
|
+
return [
|
|
1109
|
+
` async ${method.name}(${generateResolverArgumentList(method.arguments)}) {`,
|
|
1110
|
+
" const result = await graphqlRequest({",
|
|
1111
|
+
...requestProperties.map((property) => ` ${property},`),
|
|
1112
|
+
" });",
|
|
1113
|
+
"",
|
|
1114
|
+
` return result.${method.operationName};`,
|
|
1115
|
+
" }"
|
|
1116
|
+
].join("\n");
|
|
1117
|
+
};
|
|
1118
|
+
const generateResolverModule = (resolver) => {
|
|
1119
|
+
const methods = resolver.methods.map(generateMethod).join("\n\n");
|
|
1120
|
+
return [
|
|
1121
|
+
"import { graphqlRequest } from '@nestbridge/runtime';",
|
|
1122
|
+
"",
|
|
1123
|
+
`export class ${resolver.name} {`,
|
|
1124
|
+
" constructor() {}",
|
|
1125
|
+
"",
|
|
1126
|
+
methods,
|
|
1127
|
+
"}",
|
|
1128
|
+
""
|
|
1129
|
+
].join("\n");
|
|
1130
|
+
};
|
|
1131
|
+
|
|
438
1132
|
const stripLeadingParentSegments = (relativePath) => {
|
|
439
1133
|
const segments = relativePath.split(/[\\/]/);
|
|
440
1134
|
const firstOwnedIndex = segments.findIndex((segment) => segment !== "..");
|
|
@@ -458,30 +1152,46 @@ const writeControllerDeclarations = (controllers, root, outputDir) => {
|
|
|
458
1152
|
}
|
|
459
1153
|
};
|
|
460
1154
|
|
|
461
|
-
const
|
|
1155
|
+
const writeResolverDeclarations = (resolvers, root, outputDir) => {
|
|
1156
|
+
for (const resolver of resolvers) {
|
|
1157
|
+
const outputFilePath = mirroredDeclarationPath(root, outputDir, resolver.sourceFile);
|
|
1158
|
+
mkdirSync(dirname(outputFilePath), { recursive: true });
|
|
1159
|
+
writeFileSync(outputFilePath, generateControllerDeclaration(resolver, outputFilePath), "utf-8");
|
|
1160
|
+
}
|
|
1161
|
+
};
|
|
1162
|
+
|
|
1163
|
+
const discoverFiles = (patterns, root) => {
|
|
462
1164
|
const matches = globSync([...patterns], { cwd: root });
|
|
463
1165
|
return [...new Set(matches.map((match) => resolve(root, match)))].sort();
|
|
464
1166
|
};
|
|
465
1167
|
|
|
466
1168
|
const resolveNestBridgeOptions = (options) => ({
|
|
467
1169
|
controllers: Array.isArray(options.controllers) ? options.controllers : [options.controllers],
|
|
1170
|
+
resolvers: options.resolvers === void 0 ? [] : Array.isArray(options.resolvers) ? options.resolvers : [options.resolvers],
|
|
468
1171
|
baseURL: options.baseURL,
|
|
469
1172
|
debug: options.debug ?? false,
|
|
470
1173
|
outputDir: options.outputDir ?? ".nestbridge"
|
|
471
1174
|
});
|
|
472
1175
|
|
|
473
|
-
const VIRTUAL_MODULE_PREFIX = "virtual:nestbridge/controller/";
|
|
474
|
-
const RESOLVED_VIRTUAL_MODULE_PREFIX = `\0${VIRTUAL_MODULE_PREFIX}`;
|
|
475
|
-
const encodeControllerVirtualId = (absoluteControllerPath) => `${VIRTUAL_MODULE_PREFIX}${encodeURIComponent(absoluteControllerPath)}`;
|
|
1176
|
+
const VIRTUAL_MODULE_PREFIX$1 = "virtual:nestbridge/controller/";
|
|
1177
|
+
const RESOLVED_VIRTUAL_MODULE_PREFIX$1 = `\0${VIRTUAL_MODULE_PREFIX$1}`;
|
|
1178
|
+
const encodeControllerVirtualId = (absoluteControllerPath) => `${VIRTUAL_MODULE_PREFIX$1}${encodeURIComponent(absoluteControllerPath)}`;
|
|
476
1179
|
const resolvedControllerVirtualId = (virtualId) => `\0${virtualId}`;
|
|
477
|
-
const isResolvedControllerVirtualId = (id) => id.startsWith(RESOLVED_VIRTUAL_MODULE_PREFIX);
|
|
478
|
-
const decodeControllerVirtualId = (resolvedId) => decodeURIComponent(resolvedId.slice(RESOLVED_VIRTUAL_MODULE_PREFIX.length));
|
|
1180
|
+
const isResolvedControllerVirtualId = (id) => id.startsWith(RESOLVED_VIRTUAL_MODULE_PREFIX$1);
|
|
1181
|
+
const decodeControllerVirtualId = (resolvedId) => decodeURIComponent(resolvedId.slice(RESOLVED_VIRTUAL_MODULE_PREFIX$1.length));
|
|
1182
|
+
|
|
1183
|
+
const VIRTUAL_MODULE_PREFIX = "virtual:nestbridge/resolver/";
|
|
1184
|
+
const RESOLVED_VIRTUAL_MODULE_PREFIX = `\0${VIRTUAL_MODULE_PREFIX}`;
|
|
1185
|
+
const encodeResolverVirtualId = (absoluteResolverPath) => `${VIRTUAL_MODULE_PREFIX}${encodeURIComponent(absoluteResolverPath)}`;
|
|
1186
|
+
const resolvedResolverVirtualId = (virtualId) => `\0${virtualId}`;
|
|
1187
|
+
const isResolvedResolverVirtualId = (id) => id.startsWith(RESOLVED_VIRTUAL_MODULE_PREFIX);
|
|
1188
|
+
const decodeResolverVirtualId = (resolvedId) => decodeURIComponent(resolvedId.slice(RESOLVED_VIRTUAL_MODULE_PREFIX.length));
|
|
479
1189
|
|
|
480
1190
|
const createControllerRegistry = (patterns, root) => {
|
|
481
1191
|
const controllersByFile = /* @__PURE__ */ new Map();
|
|
482
1192
|
const refresh = () => {
|
|
483
1193
|
controllersByFile.clear();
|
|
484
|
-
const files =
|
|
1194
|
+
const files = discoverFiles(patterns, root);
|
|
485
1195
|
const { controllers, diagnostics } = analyzeControllers(files);
|
|
486
1196
|
for (const controller of controllers) {
|
|
487
1197
|
controllersByFile.set(normalizePath(controller.sourceFile), controller);
|
|
@@ -496,10 +1206,30 @@ const createControllerRegistry = (patterns, root) => {
|
|
|
496
1206
|
};
|
|
497
1207
|
};
|
|
498
1208
|
|
|
1209
|
+
const createResolverRegistry = (patterns, root) => {
|
|
1210
|
+
const resolversByFile = /* @__PURE__ */ new Map();
|
|
1211
|
+
const refresh = () => {
|
|
1212
|
+
resolversByFile.clear();
|
|
1213
|
+
const files = discoverFiles(patterns, root);
|
|
1214
|
+
const { resolvers, diagnostics } = analyzeResolvers(files);
|
|
1215
|
+
for (const resolver of resolvers) {
|
|
1216
|
+
resolversByFile.set(normalizePath(resolver.sourceFile), resolver);
|
|
1217
|
+
}
|
|
1218
|
+
return { resolvers, diagnostics };
|
|
1219
|
+
};
|
|
1220
|
+
return {
|
|
1221
|
+
refresh,
|
|
1222
|
+
get: (filePath) => resolversByFile.get(filePath),
|
|
1223
|
+
has: (filePath) => resolversByFile.has(filePath),
|
|
1224
|
+
all: () => [...resolversByFile.values()]
|
|
1225
|
+
};
|
|
1226
|
+
};
|
|
1227
|
+
|
|
499
1228
|
const nestBridge = (options) => {
|
|
500
1229
|
const resolvedOptions = resolveNestBridgeOptions(options);
|
|
501
1230
|
let viteConfig;
|
|
502
|
-
let
|
|
1231
|
+
let controllerRegistry;
|
|
1232
|
+
let resolverRegistry;
|
|
503
1233
|
const log = (message) => {
|
|
504
1234
|
if (resolvedOptions.debug) {
|
|
505
1235
|
console.log(`[nestbridge] ${message}`);
|
|
@@ -522,18 +1252,22 @@ const nestBridge = (options) => {
|
|
|
522
1252
|
}
|
|
523
1253
|
};
|
|
524
1254
|
const runAnalysis = (reporter) => {
|
|
525
|
-
const { controllers, diagnostics } =
|
|
526
|
-
|
|
1255
|
+
const { controllers, diagnostics: controllerDiagnostics } = controllerRegistry.refresh();
|
|
1256
|
+
const { resolvers, diagnostics: resolverDiagnostics } = resolverRegistry.refresh();
|
|
1257
|
+
reportDiagnostics([...controllerDiagnostics, ...resolverDiagnostics], reporter);
|
|
527
1258
|
writeControllerDeclarations(controllers, viteConfig.root, resolvedOptions.outputDir);
|
|
528
|
-
|
|
529
|
-
|
|
1259
|
+
writeResolverDeclarations(resolvers, viteConfig.root, resolvedOptions.outputDir);
|
|
1260
|
+
log(
|
|
1261
|
+
`analyzed ${controllers.length} controller(s), ${resolvers.length} resolver(s), ${controllerDiagnostics.length + resolverDiagnostics.length} diagnostic(s)`
|
|
1262
|
+
);
|
|
530
1263
|
};
|
|
531
1264
|
return {
|
|
532
1265
|
name: "nestbridge",
|
|
533
1266
|
enforce: "pre",
|
|
534
1267
|
configResolved(config) {
|
|
535
1268
|
viteConfig = config;
|
|
536
|
-
|
|
1269
|
+
controllerRegistry = createControllerRegistry(resolvedOptions.controllers, config.root);
|
|
1270
|
+
resolverRegistry = createResolverRegistry(resolvedOptions.resolvers, config.root);
|
|
537
1271
|
},
|
|
538
1272
|
buildStart() {
|
|
539
1273
|
runAnalysis(this);
|
|
@@ -547,38 +1281,42 @@ const nestBridge = (options) => {
|
|
|
547
1281
|
return null;
|
|
548
1282
|
}
|
|
549
1283
|
const absolutePath = normalizePath(resolved.id);
|
|
550
|
-
if (
|
|
551
|
-
return
|
|
1284
|
+
if (controllerRegistry.has(absolutePath)) {
|
|
1285
|
+
return resolvedControllerVirtualId(encodeControllerVirtualId(absolutePath));
|
|
1286
|
+
}
|
|
1287
|
+
if (resolverRegistry.has(absolutePath)) {
|
|
1288
|
+
return resolvedResolverVirtualId(encodeResolverVirtualId(absolutePath));
|
|
552
1289
|
}
|
|
553
|
-
return
|
|
1290
|
+
return null;
|
|
554
1291
|
},
|
|
555
1292
|
load(id) {
|
|
556
|
-
if (
|
|
557
|
-
|
|
1293
|
+
if (isResolvedControllerVirtualId(id)) {
|
|
1294
|
+
const controller = controllerRegistry.get(decodeControllerVirtualId(id));
|
|
1295
|
+
return controller === void 0 ? null : generateControllerModule(controller);
|
|
1296
|
+
}
|
|
1297
|
+
if (isResolvedResolverVirtualId(id)) {
|
|
1298
|
+
const resolver = resolverRegistry.get(decodeResolverVirtualId(id));
|
|
1299
|
+
return resolver === void 0 ? null : generateResolverModule(resolver);
|
|
558
1300
|
}
|
|
559
|
-
|
|
560
|
-
return controller === void 0 ? null : generateControllerModule(controller);
|
|
1301
|
+
return null;
|
|
561
1302
|
},
|
|
562
1303
|
handleHotUpdate(ctx) {
|
|
563
1304
|
const normalizedFile = normalizePath(ctx.file);
|
|
564
|
-
const matchedBefore =
|
|
565
|
-
const matchesNow =
|
|
566
|
-
resolvedOptions.controllers,
|
|
567
|
-
viteConfig.root
|
|
568
|
-
).includes(normalizedFile);
|
|
1305
|
+
const matchedBefore = controllerRegistry.has(normalizedFile) || resolverRegistry.has(normalizedFile);
|
|
1306
|
+
const matchesNow = discoverFiles(resolvedOptions.controllers, viteConfig.root).includes(normalizedFile) || discoverFiles(resolvedOptions.resolvers, viteConfig.root).includes(normalizedFile);
|
|
569
1307
|
if (!matchedBefore && !matchesNow) {
|
|
570
1308
|
return void 0;
|
|
571
1309
|
}
|
|
572
1310
|
runAnalysis(devReporter);
|
|
573
|
-
log(`
|
|
574
|
-
const virtualId = resolvedControllerVirtualId(encodeControllerVirtualId(normalizedFile));
|
|
1311
|
+
log(`nestbridge source changed: ${normalizedFile}`);
|
|
1312
|
+
const virtualId = controllerRegistry.has(normalizedFile) ? resolvedControllerVirtualId(encodeControllerVirtualId(normalizedFile)) : resolvedResolverVirtualId(encodeResolverVirtualId(normalizedFile));
|
|
575
1313
|
const moduleNode = ctx.server.moduleGraph.getModuleById(virtualId);
|
|
576
1314
|
return moduleNode === void 0 ? void 0 : [moduleNode, ...ctx.modules];
|
|
577
1315
|
}
|
|
578
1316
|
};
|
|
579
1317
|
};
|
|
580
1318
|
|
|
581
|
-
const version = "
|
|
1319
|
+
const version = "1.1.0";
|
|
582
1320
|
|
|
583
1321
|
const VERSION = version;
|
|
584
1322
|
|