@odata2ts/odata2ts
Version:
Flexible generator to produce various TypeScript artefacts (from simple model interfaces to complete odata clients) from OData metadata files
246 lines • 12.4 kB
JavaScript
import { camelCase, constantCase, kebabCase, pascalCase, snakeCase } from "change-case";
import { NamingStrategies } from "../NamingModel.js";
function getNamingStrategyImpl(strategy) {
switch (strategy) {
case NamingStrategies.CAMEL_CASE:
return camelCase;
case NamingStrategies.PASCAL_CASE:
return pascalCase;
case NamingStrategies.CONSTANT_CASE:
return constantCase;
case NamingStrategies.SNAKE_CASE:
return snakeCase;
default:
return undefined;
}
}
const noopNamingFunction = (value, options) => {
return ((options === null || options === void 0 ? void 0 : options.prefix) || "") + value + ((options === null || options === void 0 ? void 0 : options.suffix) || "");
};
export class NamingHelper {
constructor(options, mainServiceName, namespaces) {
var _a;
this.getServiceName = (name) => {
const opts = this.options.services;
return this.getName(name, this.namingFunction(opts === null || opts === void 0 ? void 0 : opts.namingStrategy), opts);
};
this.getCollectionServiceName = (name) => {
var _a;
const opts = this.options.services;
const strategy = this.namingFunction(opts === null || opts === void 0 ? void 0 : opts.namingStrategy);
const result = this.getName(name, strategy, opts === null || opts === void 0 ? void 0 : opts.collection);
return ((_a = opts === null || opts === void 0 ? void 0 : opts.collection) === null || _a === void 0 ? void 0 : _a.applyServiceNaming) ? this.getName(result, strategy, opts) : result;
};
this.getPrivatePropName = (name) => {
var _a;
const opts = (_a = this.options.services) === null || _a === void 0 ? void 0 : _a.privateProps;
return this.getName(name, this.namingFunction(opts === null || opts === void 0 ? void 0 : opts.namingStrategy), opts);
};
if (!options) {
throw new Error("NamingHelper: Options must be supplied!");
}
if (!(mainServiceName === null || mainServiceName === void 0 ? void 0 : mainServiceName.trim())) {
throw new Error("NamingHelper: MainServiceName must be supplied!");
}
if (!(namespaces === null || namespaces === void 0 ? void 0 : namespaces.length)) {
namespaces = [[mainServiceName]];
}
this.allowModelPropRenaming = (_a = options.allowRenaming) !== null && _a !== void 0 ? _a : false;
this.options = options.naming;
this.mainServiceName = mainServiceName;
this.namespacePrefixes = namespaces
.reduce((accu, [ns, alias]) => {
accu.push(ns);
if (alias) {
accu.push(alias);
}
return accu;
}, [])
.map((sn) => sn + ".")
.sort((a, b) => (a.length === b.length ? 0 : a.length > b.length ? -1 : 1));
}
/**
* The prefix used to reference model or enum types in this schema.
*
* @returns service prefix
*/
includesServicePrefix(name) {
for (let prefix of this.namespacePrefixes) {
if (name.startsWith(prefix)) {
return true;
}
}
return false;
}
/**
* The OData service name as it was found and is used in metadata file.
*
* @returns
*/
getODataServiceName() {
return this.mainServiceName;
}
getFileNames() {
var _a, _b;
return {
model: this.getFileName((_a = this.options.models) === null || _a === void 0 ? void 0 : _a.fileName),
qObject: this.getFileName((_b = this.options.queryObjects) === null || _b === void 0 ? void 0 : _b.fileName),
service: this.getMainServiceName(),
};
}
getFileName(opts) {
return this.getName(this.mainServiceName, this.namingFunction(opts === null || opts === void 0 ? void 0 : opts.namingStrategy), opts);
}
getFileNameService(name) {
const opts = this.options.services;
return this.getName(name, this.namingFunction(opts === null || opts === void 0 ? void 0 : opts.namingStrategy), opts);
}
stripServicePrefix(token) {
const found = this.namespacePrefixes.find((prefix) => token.startsWith(prefix));
return found ? token.replace(found, "") : token;
}
getNameAndServicePrefix(token) {
const prefix = this.namespacePrefixes.find((prefix) => token.startsWith(prefix));
const name = prefix ? token.replace(prefix, "") : token;
return [name, prefix === null || prefix === void 0 ? void 0 : prefix.substring(0, (prefix === null || prefix === void 0 ? void 0 : prefix.length) - 1)];
}
namingFunction(strategy) {
const strategyFn = getNamingStrategyImpl(strategy);
if (!strategyFn || !this.allowModelPropRenaming) {
return noopNamingFunction;
}
return (value, options) => {
const prefix = options === null || options === void 0 ? void 0 : options.prefix;
const suffix = options === null || options === void 0 ? void 0 : options.suffix;
const isPrefixSpecialChar = prefix === null || prefix === void 0 ? void 0 : prefix.startsWith("_");
const isSuffixSpecialChar = suffix === null || suffix === void 0 ? void 0 : suffix.endsWith("_");
let result = strategyFn((prefix ? prefix + "_" : "") + value + (suffix ? "_" + suffix : ""));
if (isPrefixSpecialChar) {
result = "_" + result;
}
if (isSuffixSpecialChar) {
result = result + "_";
}
return result;
};
}
getName(name, strategy, options) {
return strategy(this.stripServicePrefix(name), options);
}
getModelNamingStrategy() {
var _a;
return this.namingFunction((_a = this.options.models) === null || _a === void 0 ? void 0 : _a.namingStrategy);
}
getModelPropNamingStrategy() {
var _a;
return this.namingFunction((_a = this.options.models) === null || _a === void 0 ? void 0 : _a.propNamingStrategy);
}
getQObjectNamingStrategy() {
var _a;
return this.namingFunction((_a = this.options.queryObjects) === null || _a === void 0 ? void 0 : _a.namingStrategy);
}
getQObjectPropNamingStrategy() {
var _a;
return this.namingFunction((_a = this.options.queryObjects) === null || _a === void 0 ? void 0 : _a.propNamingStrategy);
}
getOperationNamingStrategy() {
var _a, _b;
return this.namingFunction((_b = (_a = this.options.services) === null || _a === void 0 ? void 0 : _a.operations) === null || _b === void 0 ? void 0 : _b.namingStrategy);
}
getModelName(name) {
return this.getName(name, this.getModelNamingStrategy(), this.options.models);
}
getModelPropName(name) {
return this.getName(name, this.getModelPropNamingStrategy());
}
getEnumName(name) {
return this.getName(name, this.getModelNamingStrategy(), this.options.models);
}
getEditableModelName(name) {
var _a;
let options = (_a = this.options.models) === null || _a === void 0 ? void 0 : _a.editableModels;
const result = this.getName(name, this.getModelNamingStrategy(), options);
return (options === null || options === void 0 ? void 0 : options.applyModelNaming)
? this.getName(result, this.getModelNamingStrategy(), this.options.models)
: result;
}
getIdModelName(name) {
var _a;
let options = (_a = this.options.models) === null || _a === void 0 ? void 0 : _a.idModels;
const result = this.getName(name, this.getModelNamingStrategy(), options);
return (options === null || options === void 0 ? void 0 : options.applyModelNaming)
? this.getName(result, this.getModelNamingStrategy(), this.options.models)
: result;
}
getOperationParamsModelName(operationName, boundEntity) {
var _a;
const settings = (_a = this.options.models) === null || _a === void 0 ? void 0 : _a.operationParamModels;
const result = this.getName(operationName, this.getModelNamingStrategy(), settings);
const name = (settings === null || settings === void 0 ? void 0 : settings.applyModelNaming)
? this.getName(result, this.getModelNamingStrategy(), this.options.models)
: result;
return this.getPrefixedName(name, boundEntity);
}
getQName(name) {
return this.getName(name, this.getQObjectNamingStrategy(), this.options.queryObjects);
}
getQBaseName(name) {
const opts = this.options.queryObjects;
const strategy = this.getQObjectNamingStrategy();
const result = this.getName(name, strategy, opts.baseType);
return opts.baseType.applyQNaming ? this.getName(result, strategy, opts) : result;
}
getQPropName(name) {
return this.getName(name, this.getQObjectPropNamingStrategy());
}
getQIdFunctionName(name) {
var _a;
const opts = (_a = this.options.queryObjects) === null || _a === void 0 ? void 0 : _a.idFunctions;
const result = this.getName(name, this.getQObjectNamingStrategy(), opts);
return this.getName(result, this.getQObjectNamingStrategy(), this.options.queryObjects);
}
getQFunctionName(operationName, boundEntity) {
var _a;
const opts = (_a = this.options.queryObjects) === null || _a === void 0 ? void 0 : _a.operations;
const result = this.getName(operationName, this.getQObjectNamingStrategy(), (opts === null || opts === void 0 ? void 0 : opts.function) || opts);
const name = this.getName(result, this.getQObjectNamingStrategy(), this.options.queryObjects);
return this.getPrefixedName(name, boundEntity);
}
getQActionName(operationName, boundEntity) {
var _a;
const opts = (_a = this.options.queryObjects) === null || _a === void 0 ? void 0 : _a.operations;
const result = this.getName(operationName, this.getQObjectNamingStrategy(), (opts === null || opts === void 0 ? void 0 : opts.action) || opts);
const name = this.getName(result, this.getQObjectNamingStrategy(), this.options.queryObjects);
return this.getPrefixedName(name, boundEntity);
}
getMainServiceName() {
var _a, _b, _c, _d;
const name = this.getODataServiceName();
const opts = this.options.services;
const strategy = this.namingFunction((_b = (_a = opts === null || opts === void 0 ? void 0 : opts.main) === null || _a === void 0 ? void 0 : _a.namingStrategy) !== null && _b !== void 0 ? _b : (((_c = opts === null || opts === void 0 ? void 0 : opts.main) === null || _c === void 0 ? void 0 : _c.applyServiceNaming) ? opts.namingStrategy : undefined));
const result = this.getName(name, strategy, opts === null || opts === void 0 ? void 0 : opts.main);
return ((_d = opts === null || opts === void 0 ? void 0 : opts.main) === null || _d === void 0 ? void 0 : _d.applyServiceNaming) ? this.getName(result, strategy, opts) : result;
}
getPrefixedName(name, boundEntity) {
return boundEntity ? boundEntity + "_" + name : name;
}
getFunctionName(operationName) {
var _a;
const opts = (_a = this.options.services) === null || _a === void 0 ? void 0 : _a.operations;
return this.getName(operationName, this.getOperationNamingStrategy(), (opts === null || opts === void 0 ? void 0 : opts.function) || opts);
}
getActionName(operationName) {
var _a;
const opts = (_a = this.options.services) === null || _a === void 0 ? void 0 : _a.operations;
return this.getName(operationName, this.getOperationNamingStrategy(), (opts === null || opts === void 0 ? void 0 : opts.action) || opts);
}
getRelatedServiceGetter(name) {
var _a;
const opts = (_a = this.options.services) === null || _a === void 0 ? void 0 : _a.relatedServiceGetter;
return this.getName(name, this.namingFunction(opts === null || opts === void 0 ? void 0 : opts.namingStrategy), opts);
}
getFolderPath(namespace, name) {
return `${kebabCase(namespace)}/${kebabCase(name)}`;
}
}
//# sourceMappingURL=NamingHelper.js.map