@rxap/schematics-ts-morph
Version:
This package provides utilities for manipulating TypeScript code using ts-morph, particularly for Angular and NestJS projects. It offers functions to add, coerce, and modify code elements like classes, methods, decorators, and imports. The package also in
123 lines • 5.26 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CoerceProxyRemoteMethodClass = CoerceProxyRemoteMethodClass;
const schematics_utilities_1 = require("@rxap/schematics-utilities");
const ts_morph_1 = require("@rxap/ts-morph");
const utilities_1 = require("@rxap/utilities");
const ts_morph_2 = require("ts-morph");
const ts_morph_transform_1 = require("../ts-morph-transform");
function CoerceProxyRemoteMethodClass(options) {
const { override, name, tsMorphTransform = utilities_1.noop, sourceType, targetType, proxyMethod, identifier, } = options;
const className = (0, schematics_utilities_1.classify)((0, schematics_utilities_1.CoerceSuffix)(name, 'ProxyMethod'));
const fileName = (0, schematics_utilities_1.CoerceSuffix)(name, '-proxy.method.ts');
return (0, ts_morph_transform_1.TsMorphAngularProjectTransformRule)(options, (project) => {
var _a, _b, _c, _d, _e;
const sourceFile = (0, ts_morph_1.CoerceSourceFile)(project, fileName);
const classDeclaration = (0, ts_morph_1.CoerceClass)(sourceFile, className, {
isExported: true,
decorators: [
{
name: 'Injectable',
arguments: [],
},
{
name: 'RxapRemoteMethod',
arguments: [w => w.quote((0, schematics_utilities_1.CoerceSuffix)(name, '-proxy'))],
},
],
ctors: [
{
parameters: [
{
name: 'method',
type: proxyMethod,
decorators: [
{
name: 'Inject',
arguments: [proxyMethod],
},
],
},
],
statements: ['super(method);'],
},
],
});
classDeclaration.setExtends(w => {
w.write('ProxyRemoteMethod<');
w.write('any,');
if (typeof sourceType === 'string') {
w.write(sourceType);
}
else {
sourceType(w);
}
w.write(', ');
if (typeof targetType === 'string') {
w.write(targetType);
}
else {
targetType(w);
}
w.write('>');
});
(0, ts_morph_1.CoerceImports)(sourceFile, {
moduleSpecifier: '@rxap/pattern',
namedImports: ['Method'],
});
(0, ts_morph_1.CoerceImports)(sourceFile, {
moduleSpecifier: '@angular/core',
namedImports: ['Injectable', 'Inject'],
});
(0, ts_morph_1.CoerceImports)(sourceFile, {
moduleSpecifier: '@rxap/remote-method',
namedImports: ['RxapRemoteMethod', 'ProxyRemoteMethod'],
});
const methodStructure = (_a = tsMorphTransform(project, sourceFile, classDeclaration)) !== null && _a !== void 0 ? _a : {};
(_b = methodStructure.parameters) !== null && _b !== void 0 ? _b : (methodStructure.parameters = [
{
name: 'source',
type: sourceType,
},
]);
if ((identifier === null || identifier === void 0 ? void 0 : identifier.source) === 'route' && !methodStructure.statements) {
(0, ts_morph_1.CoerceClassProperty)(classDeclaration, 'route', {
scope: ts_morph_2.Scope.Protected,
initializer: 'inject(ActivatedRoute)',
isReadonly: true,
});
(0, ts_morph_1.CoerceImports)(sourceFile, {
moduleSpecifier: '@angular/router',
namedImports: ['ActivatedRoute'],
});
(0, ts_morph_1.CoerceImports)(sourceFile, {
moduleSpecifier: '@angular/core',
namedImports: ['inject'],
});
methodStructure.statements = [
`const { ${identifier.property.name} } = this.route.snapshot.params;`,
`return { parameters: { ${identifier.property.name} } };`,
];
}
else {
(_c = methodStructure.statements) !== null && _c !== void 0 ? _c : (methodStructure.statements = ['return source as any;']);
}
(_d = methodStructure.returnType) !== null && _d !== void 0 ? _d : (methodStructure.returnType = w => {
w.write('Promise<');
if (typeof targetType ===
'string') {
w.write(targetType);
}
else {
targetType(w);
}
w.write('>');
});
(_e = methodStructure.isAsync) !== null && _e !== void 0 ? _e : (methodStructure.isAsync = true);
const methodDeclaration = (0, ts_morph_1.CoerceClassMethod)(classDeclaration, 'transformParameters', methodStructure);
if (override) {
methodDeclaration.set(methodStructure);
}
});
}
//# sourceMappingURL=coerce-proxy-remote-method-class.js.map