angular-odata
Version:
Client side OData typescript library for Angular
240 lines • 9.17 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.Metadata = exports.Index = exports.Base = exports.Callable = void 0;
const core_1 = require("@angular-devkit/core");
const random_1 = require("../../random");
const import_1 = require("./import");
const schematics_1 = require("@angular-devkit/schematics");
const csdl_function_action_1 = require("../metadata/csdl/csdl-function-action");
const utils_1 = require("../utils");
class Callable {
constructor(callable) {
this.callable = callable;
this.callables = [];
this.callables.push(callable);
}
addOverload(callable) {
this.callables.push(callable);
}
name() {
return this.callable.Name;
}
isBound() {
var _a;
return (_a = this.callable.IsBound) !== null && _a !== void 0 ? _a : false;
}
bindingParameter() {
var _a;
return (_a = this.callable.Parameter) === null || _a === void 0 ? void 0 : _a.find((p) => p.Name === csdl_function_action_1.BINDING_PARAMETER_NAME);
}
parameters() {
const parameters = this.callables.reduce((acc, c) => {
var _a;
for (let param of (_a = c.Parameter) !== null && _a !== void 0 ? _a : []) {
if (acc.some((p) => p.Name === param.Name))
continue;
acc.push(param);
}
return acc;
}, []);
const names = parameters.map((p) => p.Name);
const binding = parameters.find((p) => p.Name === csdl_function_action_1.BINDING_PARAMETER_NAME);
const inAllCallables = names.filter((n) => n !== csdl_function_action_1.BINDING_PARAMETER_NAME &&
this.callables.every((c) => { var _a; return ((_a = c.Parameter) !== null && _a !== void 0 ? _a : []).some((p) => p.Name === n); }));
const required = parameters.filter((p) => p.Name !== csdl_function_action_1.BINDING_PARAMETER_NAME && inAllCallables.includes(p.Name));
const optional = parameters.filter((p) => p.Name !== csdl_function_action_1.BINDING_PARAMETER_NAME && !inAllCallables.includes(p.Name));
return {
binding,
required,
optional,
};
}
returnType() {
return this.callable.ReturnType;
}
fullName() {
return this.callable.fullName();
}
resourceFunction() {
const isFunction = this.callable instanceof csdl_function_action_1.CsdlFunction;
const methodName = core_1.strings.camelize(this.callable.Name);
const { binding, required, optional } = this.parameters();
const parameters = [...required, ...optional];
const bindingType = binding !== undefined ? (0, utils_1.toTypescriptType)(binding.Type) : '';
const returnType = this.returnType();
const retType = returnType === undefined ? 'null' : (0, utils_1.toTypescriptType)(returnType.Type);
const bindingMethod = !(binding === null || binding === void 0 ? void 0 : binding.Collection) ? 'entity' : 'entities';
const baseMethod = isFunction ? 'function' : 'action';
const keyParameter = !(binding === null || binding === void 0 ? void 0 : binding.Collection)
? `key: EntityKey<${bindingType}>`
: '';
const key = !(binding === null || binding === void 0 ? void 0 : binding.Collection) ? `key` : '';
const parametersType = parameters.length === 0
? 'null'
: `{${parameters
.map((p) => {
const op = optional.includes(p);
return `${p.Name}${op ? '?' : ''}: ${(0, utils_1.toTypescriptType)(p.Type)}`;
})
.join(', ')}}`;
return `public ${methodName}(${keyParameter}) {
return this.${bindingMethod}(${key}).${baseMethod}<${parametersType}, ${retType}>('${this.fullName()}');
}`;
}
callableFunction() {
const isFunction = this.callable instanceof csdl_function_action_1.CsdlFunction;
const { binding, required, optional } = this.parameters();
const parameters = [...required, ...optional];
const returnType = this.returnType();
const methodResourceName = core_1.strings.camelize(this.callable.Name);
const methodName = core_1.strings.classify(this.callable.Name);
const responseType = returnType === undefined
? 'none'
: (returnType === null || returnType === void 0 ? void 0 : returnType.Collection)
? 'entities'
: (returnType === null || returnType === void 0 ? void 0 : returnType.Type.startsWith('Edm.'))
? 'property'
: 'entity';
const retType = returnType === undefined ? 'null' : (0, utils_1.toTypescriptType)(returnType.Type);
const bindingType = binding !== undefined ? (0, utils_1.toTypescriptType)(binding.Type) : '';
const baseMethod = isFunction ? 'callFunction' : 'callAction';
const parametersCall = parameters.length === 0
? 'null'
: `{${parameters.map((p) => p.Name).join(', ')}}`;
// Method arguments
let args = !(binding === null || binding === void 0 ? void 0 : binding.Collection) ? [`key: EntityKey<${bindingType}>`] : [];
args = [
...args,
...(required.length === 0
? []
: required.map((p) => `${p.Name}: ${(0, utils_1.toTypescriptType)(p.Type)}`)),
];
args = [
...args,
...(optional.length === 0
? []
: optional.map((p) => `${p.Name}?: ${(0, utils_1.toTypescriptType)(p.Type)}`)),
];
const optionsType = returnType !== undefined && returnType.Type.startsWith('Edm.')
? isFunction
? 'ODataOptions & {alias?: boolean}'
: 'ODataOptions'
: isFunction
? `ODataFunctionOptions<${retType}>`
: `ODataActionOptions<${retType}>`;
args.push(`options?: ${optionsType}`);
// Key parameter
const key = !(binding === null || binding === void 0 ? void 0 : binding.Collection) ? `key` : '';
// Render
return `public call${methodName}(${args.join(', ')}) {
return this.${baseMethod}(${parametersCall}, this.${methodResourceName}(${key}), '${responseType}', options);
}`;
}
}
exports.Callable = Callable;
class Base {
constructor(options) {
this.options = options;
this.dependencies = [];
this.callables = [];
}
path() {
const directory = this.directory();
const filename = this.fileName();
return directory !== '' ? directory + `/${filename}` : filename;
}
imports() {
const groups = this.dependencies
.filter((a) => a[1].path() != this.path())
.reduce((acc, i) => {
const path = (0, utils_1.makeRelativePath)(this.directory(), i[1].path());
if (acc[path] === undefined) {
acc[path] = [];
}
acc[path].push(i);
return acc;
}, {});
return Object.entries(groups).map(([path, items]) => {
const names = items.reduce((acc, i) => [...acc, i[0]], []);
return new import_1.Import(names, path);
});
}
cleanImportedNames() {
this.dependencies.forEach((d) => (d[1].importedName = d[1].name()));
}
addDependency(renderable) {
if (this.dependencies.every((d) => d[1] != renderable)) {
var alias = renderable.name();
while (this.dependencies.some((d) => d[0] == alias)) {
alias = (0, random_1.getRandomName)();
}
this.dependencies.push([alias, renderable]);
}
}
addDependencies(renderables) {
renderables.forEach((r) => this.addDependency(r));
}
addCallable(callable) {
this.callables.push(callable);
}
addCallables(callables) {
callables.forEach((r) => this.addCallable(r));
}
}
exports.Base = Base;
class Index extends Base {
constructor(options) {
super(options);
}
template() {
return (0, schematics_1.url)('./files/index');
}
variables() {
return Object.assign({}, this.options);
}
name() {
return '';
}
fileName() {
return 'index';
}
directory() {
return '';
}
fullName() {
return this.name();
}
importTypes() {
return [];
}
}
exports.Index = Index;
class Metadata extends Base {
constructor(options, meta) {
super(options);
this.meta = meta;
}
template() {
return (0, schematics_1.url)('./files/metadata');
}
variables() {
return { content: JSON.stringify(this.meta.toJson(), null, 2) };
}
name() {
return '';
}
fileName() {
return 'metadata';
}
directory() {
return '';
}
fullName() {
return this.name();
}
importTypes() {
return [];
}
}
exports.Metadata = Metadata;
//# sourceMappingURL=base.js.map
;