dynamics-meta
Version:
Library for download MetaData from Dynamics365
259 lines (256 loc) • 10.1 kB
JavaScript
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __objRest = (source, exclude) => {
var target = {};
for (var prop in source)
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
target[prop] = source[prop];
if (source != null && __getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(source)) {
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
target[prop] = source[prop];
}
return target;
};
var __async = (__this, __arguments, generator) => {
return new Promise((resolve, reject) => {
var fulfilled = (value) => {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
};
var rejected = (value) => {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
};
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
step((generator = generator.apply(__this, __arguments)).next());
});
};
// src/types/api.ts
var FieldType = /* @__PURE__ */ ((FieldType2) => {
FieldType2["String"] = "String";
FieldType2["Lookup"] = "Lookup";
FieldType2["Memo"] = "Memo";
FieldType2["Virtual"] = "Virtual";
FieldType2["DateTime"] = "DateTime";
FieldType2["Boolean"] = "Boolean";
FieldType2["Picklist"] = "Picklist";
FieldType2["Number"] = "Number";
FieldType2["Owner"] = "Owner";
FieldType2["State"] = "State";
FieldType2["Status"] = "Status";
FieldType2["PartyList"] = "PartyList";
FieldType2["Integer"] = "Integer";
FieldType2["Choice"] = "Choice";
FieldType2["EntityName"] = "EntityName";
FieldType2["Uniqueidentifier"] = "Uniqueidentifier";
return FieldType2;
})(FieldType || {});
// src/auth.ts
import axios from "axios";
var getRequest = (Authorization, baseURL) => {
return (props) => __async(void 0, null, function* () {
const _a = props, { method = "get", tries } = _a, rest = __objRest(_a, ["method", "tries"]);
const request = () => axios.request(__spreadProps(__spreadValues({
baseURL,
method
}, rest), {
headers: { Authorization }
}));
if (tries) {
const sendRequest = (tries2) => __async(void 0, null, function* () {
try {
return yield request();
} catch (e) {
if (e.code === "ECONNABORTED" && tries2 > 0) {
return yield sendRequest(tries2 - 1);
} else {
throw e;
}
}
});
return sendRequest(tries);
}
return request();
});
};
// src/build.ts
var splitArray = (array, LIMIT = 10) => Array.from(new Array(Math.ceil(array.length / LIMIT)).keys()).map((k) => array.slice(k * LIMIT, k * LIMIT + 25));
var systemBasedFields = ["ownerid", "createdon", "createdby", "modifiedon"];
var optionBasedTypes = [
"#Microsoft.Dynamics.CRM.PicklistAttributeMetadata",
"#Microsoft.Dynamics.CRM.BooleanAttributeMetadata",
"#Microsoft.Dynamics.CRM.MultiSelectPicklistAttributeMetadata"
];
var getBuildFunction = (token, url, config) => {
const request = getRequest(token, url);
const targets = /* @__PURE__ */ new Set();
const getFieldsData = (logicalName) => request({
url: `EntityDefinitions(LogicalName='${logicalName}')/Attributes`,
timeout: 5e3,
tries: 3
});
const getJoinParams = (names) => __async(void 0, null, function* () {
return (yield Promise.all(splitArray(names, 20).map((names2) => request({
url: `EntityDefinitions`,
timeout: 5e3,
tries: 3,
params: {
$select: "LogicalName,LogicalCollectionName,PrimaryIdAttribute,PrimaryNameAttribute,DisplayName",
$filter: names2.map((name) => `LogicalName eq '${name}'`).join(" or ")
}
})))).map((v) => v.data.value).flat();
});
const getEntityMeta = (names) => request({
url: `EntityDefinitions`,
params: {
$select: "LogicalName,LogicalCollectionName,PrimaryIdAttribute,PrimaryNameAttribute,PrimaryImageAttribute,DisplayName,DisplayCollectionName,IsActivity,SchemaName,ObjectTypeCode,OwnershipType,IsCustomEntity",
$filter: names.map((name) => `LogicalName eq '${name}'`).join(" or ")
},
timeout: 5e3,
tries: 3
});
const getListItems = (entityLogicalName, field, type) => request({
url: `EntityDefinitions(LogicalName='${entityLogicalName}')/Attributes(LogicalName='${field}')/${type.slice(1)}`,
params: {
$select: "LogicalName",
$expand: "OptionSet"
},
timeout: 5e3,
tries: 3
});
const getOptions = (type, fullType, logicalName, fieldName) => __async(void 0, null, function* () {
if (["Picklist" /* Picklist */, "Virtual" /* Virtual */, "Boolean" /* Boolean */, "Status" /* Status */, "State" /* State */].includes(type) && fullType) {
try {
const {
data: {
OptionSet: { Options, FalseOption, TrueOption }
}
} = yield getListItems(logicalName, fieldName, fullType);
if (type === "Boolean" /* Boolean */ && FalseOption && TrueOption) {
return [
["true", TrueOption.Label.UserLocalizedLabel.Label],
["false", FalseOption.Label.UserLocalizedLabel.Label]
];
} else {
return Options.map((v) => [`${v.Value}`, v.Label.UserLocalizedLabel.Label]);
}
} catch (e) {
console.log(e);
}
} else {
return [];
}
});
const getMeta = (entityName) => __async(void 0, null, function* () {
const {
data: { value: fieldsMeta }
} = yield getFieldsData(config[entityName].name);
const columns = config[entityName].columns.concat(...systemBasedFields);
const options = {};
try {
yield Promise.all(
fieldsMeta.filter(
(v) => {
var _a;
return optionBasedTypes.includes(v["@odata.type"]) && ((_a = config[entityName]) == null ? void 0 : _a.columns).includes(v.LogicalName);
}
).map((_0) => __async(void 0, [_0], function* ({ LogicalName, AttributeType, "@odata.type": type }) {
options[LogicalName] = yield getOptions(AttributeType, type, config[entityName].name, LogicalName);
}))
);
} catch (e) {
console.log(e);
}
fieldsMeta.filter((v) => columns.includes(v.LogicalName) && v.Targets).map((v) => v.Targets).flat().forEach((v) => targets.add(v));
return fieldsMeta.filter((v) => columns.includes(v.LogicalName)).reduce(
(acc, next) => {
var _a;
return __spreadProps(__spreadValues({}, acc), {
[next.LogicalName]: {
label: next.DisplayName.UserLocalizedLabel.Label,
description: next.Description.UserLocalizedLabel.Label,
extraType: next["@odata.type"],
type: next.AttributeType,
format: next.Format,
formatName: (_a = next.FormatName) == null ? void 0 : _a.Value,
targets: next.Targets,
options: options[next.LogicalName],
maxLength: next.MaxLength,
minValue: next.MinValue,
maxValue: next.MaxValue
}
});
},
{}
);
});
return () => __async(void 0, null, function* () {
const mainKeys = Object.keys(config);
const entitiesMeta = Object.fromEntries(
(yield getEntityMeta(mainKeys.map((entityName) => config[entityName].name))).data.value.map((_a) => {
var _b = _a, { LogicalName, DisplayName, LogicalCollectionName, IsActivity, DisplayCollectionName, SchemaName } = _b, rest = __objRest(_b, ["LogicalName", "DisplayName", "LogicalCollectionName", "IsActivity", "DisplayCollectionName", "SchemaName"]);
var _a2, _b2;
return [
LogicalName,
__spreadValues({
logicalName: LogicalName,
url: LogicalCollectionName,
fields: {},
displayName: (_a2 = DisplayName == null ? void 0 : DisplayName.UserLocalizedLabel) == null ? void 0 : _a2.Label,
displayCollectionName: (_b2 = DisplayCollectionName == null ? void 0 : DisplayCollectionName.UserLocalizedLabel) == null ? void 0 : _b2.Label,
isActivity: IsActivity,
schemaName: SchemaName
}, rest)
];
})
);
const meta = Object.fromEntries(mainKeys.map((key) => [key, entitiesMeta[config[key].name]]));
const fields = Object.fromEntries(
yield Promise.all(mainKeys.map((entityName) => __async(void 0, null, function* () {
return [entityName, yield getMeta(entityName)];
})))
);
const targetsResponse = yield getJoinParams([...targets.values()]);
const targetCollections = Object.fromEntries(
targetsResponse.map(
(_c) => {
var _d = _c, { LogicalName, DisplayName } = _d, rest = __objRest(_d, ["LogicalName", "DisplayName"]);
var _a;
return [LogicalName, __spreadValues({ LogicalName, DisplayName: (_a = DisplayName == null ? void 0 : DisplayName.UserLocalizedLabel) == null ? void 0 : _a.Label }, rest)];
}
)
);
return { meta: Object.fromEntries(
Object.entries(meta).map(([key, values]) => [key, __spreadProps(__spreadValues({}, values), { fields: fields[key] })])
), targetCollections };
});
};
export {
FieldType,
getBuildFunction
};
//# sourceMappingURL=index.mjs.map