UNPKG

albxrmtypesgen

Version:

A TypeScript Declaration Generator for Dynamics 365 Forms

225 lines 8.88 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getLocalChoices = exports.getChoicesByEnvironment = exports.getChoicesBySolution = exports.getAttributeMeta = exports.getFormsBySolution = exports.getFormsForEntities = exports.getForms = exports.CallApi = void 0; const node_fetch_1 = __importDefault(require("node-fetch")); const node_localstorage_1 = require("node-localstorage"); const localStorage = new node_localstorage_1.LocalStorage('./scratch', 500 * 1024 * 1024); const initHeader = (accessToken) => ({ Authorization: `Bearer ${accessToken}`, Accept: 'application/json', 'Content-Type': 'application/json; charset=utf-8', 'OData-MaxVersion': '4.0', 'OData-Version': '4.0', }); const autoRetryFetch = async (url, init) => { let response = await node_fetch_1.default(url, init); const time = parseInt(response.headers.get('Retry-After') || '0', 10); if (time > 0) { console.log('retry-after {0} {1}', time, url); await new Promise((resolve) => setTimeout(resolve, time)); response = await autoRetryFetch(url, init); } return response; }; const CallApi = async (authToken, url) => { try { const response = await autoRetryFetch(`${url}/api/data/v9.2/contacts?$top=1`, { headers: initHeader(authToken.accessToken), method: 'GET', }); const json = await response.json(); return json; } catch (err) { console.log(`Fetch Error: ${err}`); return err; } }; exports.CallApi = CallApi; const getForms = async (authToken, url) => { try { const response = await autoRetryFetch(`${url}/api/data/v9.2/systemforms?` + '$select=description,formjson,formid,name,formactivationstate,type,objecttypecode&' + '$filter=(Microsoft.Dynamics.CRM.In(PropertyName=%27type%27,PropertyValues=[%272%27,%277%27]))', { headers: initHeader(authToken.accessToken), method: 'GET', }); const json = await response.json(); return json; } catch (err) { console.log(`Fetch Error: ${err}`); return err; } }; exports.getForms = getForms; const getFormsForEntities = async (authToken, url, entities) => { try { console.log(`Entities: ${entities}`); const entitynames = entities.split(','); const entitiesparam = entitynames.map((value) => `"${value}"`).join(','); const response = await autoRetryFetch(`${url}/api/data/v9.2/systemforms?` + '$select=description,formjson,formid,name,formactivationstate,type,objecttypecode&' + '$filter=(Microsoft.Dynamics.CRM.In(PropertyName=%27type%27,PropertyValues=[%272%27,%277%27])%20' + `and%20Microsoft.Dynamics.CRM.In(PropertyName=%27objecttypecodename%27,PropertyValues=[${entitiesparam}]))`, { headers: initHeader(authToken.accessToken), method: 'GET', }); const json = await response.json(); return json; } catch (err) { console.log(`Fetch Error: ${err}`); return err; } }; exports.getFormsForEntities = getFormsForEntities; const getFormsBySolution = async (authToken, url, solution) => { const fetchXml = ` <fetch no-lock='true'> <entity name='systemform'> <attribute name='description' /> <attribute name='formjson' /> <attribute name='formid' /> <attribute name='name' /> <attribute name='formactivationstate' /> <attribute name='type' /> <attribute name='objecttypecode' /> <filter> <condition attribute="type" operator="in" > <value>2</value> <value>7</value> </condition> </filter> <link-entity name='solutioncomponent' from='objectid' to='formid'> <filter>' <condition attribute='componenttype' operator='eq' value='60'/> </filter>' <link-entity name='solution' from='solutionid' to='solutionid'> <filter> <condition attribute='uniquename' operator='eq' value='${solution}'/> </filter> </link-entity> </link-entity> </entity> </fetch>`; try { const response = await autoRetryFetch(`${url}/api/data/v9.2/systemforms?fetchXml=${encodeURIComponent(fetchXml)}`, { headers: initHeader(authToken.accessToken), method: 'GET', }); const json = await response.json(); return json; } catch (err) { console.log(`Fetch Error: ${err}`); return err; } }; exports.getFormsBySolution = getFormsBySolution; const attributeMetaDataCache = { get: (entity) => { const cache = localStorage.getItem(`attributeMetaData_${entity}`); if (cache) { return JSON.parse(cache); } return undefined; }, set: (entity, data) => { localStorage.setItem(`attributeMetaData_${entity}`, JSON.stringify(data)); }, }; const getAttributeMeta = async (entity, authToken, url) => { try { const cache = attributeMetaDataCache.get(entity); if (cache) { console.log(`getting attribute metadata for the ${entity} entity from cache`); return cache; } console.log(`getting attribute metadata for the ${entity} entity`); const response = await autoRetryFetch(`${url}/api/data/v9.2/EntityDefinitions(LogicalName='${entity}')?` + '$select=LogicalName,SchemaName&$expand=Attributes($select=LogicalName,SchemaName,AttributeType)', { headers: initHeader(authToken.accessToken), method: 'GET', }); const json = await response.json(); attributeMetaDataCache.set(entity, json); return json; } catch (err) { console.log(err); console.log(`Fetch Error: ${err}`); return err; } }; exports.getAttributeMeta = getAttributeMeta; const getChoicesBySolution = async (authToken, url, solution) => { try { const response = await autoRetryFetch(`${url}/api/data/v9.2/GlobalOptionSetDefinitions`, { headers: initHeader(authToken.accessToken), method: 'GET', }); const responseAny = await response.json(); if (responseAny.error) { console.error(responseAny.error); } const json = responseAny.value; const responseSolutions = await autoRetryFetch(`${url}/api/data/v9.2/solutioncomponents?$select=objectid&$filter=(componenttype eq 9) and (solutionid/uniquename eq '${solution}')`, { headers: initHeader(authToken.accessToken), method: 'GET', }); const responseSolutionsAny = await responseSolutions.json(); if (responseSolutionsAny.error) { console.error(responseSolutionsAny.error); } const jsonSolutions = responseSolutionsAny.value; return json.filter((O) => jsonSolutions.some((S) => O.MetadataId === S.objectid)); } catch (err) { console.log(`Fetch Error: ${err}`); return err; } }; exports.getChoicesBySolution = getChoicesBySolution; const getChoicesByEnvironment = async (authToken, url) => { try { const response = await autoRetryFetch(`${url}/api/data/v9.2/GlobalOptionSetDefinitions`, { headers: initHeader(authToken.accessToken), method: 'GET', }); const responseAny = await response.json(); if (responseAny.error) { console.error(responseAny.error); } const json = responseAny.value; return json.filter((O) => O.Name !== null && O.Name !== ''); } catch (err) { console.log(`Fetch Error: ${err}`); return err; } }; exports.getChoicesByEnvironment = getChoicesByEnvironment; const getLocalChoices = async (entity, authToken, url) => { try { const response = await autoRetryFetch(`${url}api/data/v9.0/EntityDefinitions(LogicalName='${entity}')/Attributes/Microsoft.Dynamics.CRM.PicklistAttributeMetadata?$select=LogicalName&$expand=OptionSet($select=Options,IsGlobal)`, { headers: initHeader(authToken.accessToken), method: 'GET', }); const responseAny = await response.json(); if (responseAny.error) { console.error(responseAny.error); } const json = responseAny.value; return json.filter((O) => O.OptionSet.IsGlobal === false); } catch (err) { console.log(`Fetch Error: ${err}`); return err; } }; exports.getLocalChoices = getLocalChoices; //# sourceMappingURL=queries.js.map