UNPKG

fsm-sdk

Version:

Node.JS sdk to interface with SAP Field Service Management APIs.

63 lines 2.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const uuid_1 = require("uuid"); const all_dto_versions_constant_1 = require("./all-dto-versions.constant"); class RequestOptionsFacory { static getUUID() { return uuid_1.v4().replace(/\-/g, ''); } static stringify(o) { return Object.keys(o).map((key) => `${encodeURIComponent(key)}=${encodeURIComponent(o[key])}`).join('&'); } static getDataApiUriFor(token, resourceName, resourceId = null, externalId = null) { const identifier = [ (resourceId ? `/${resourceId}` : '').trim(), (externalId && !resourceId ? `/externalId/${externalId}` : '').trim() ].join('').trim(); return `${token.cluster_url}/api/data/v4/${resourceName}${identifier}`; } /** * map of DTO objects and versions * { ['<DTOName>']: number } * Note: DTOName is case sensitiv */ static getAllDTOVersions() { return all_dto_versions_constant_1.ALL_DTO_VERSIONS; } static getDTOVersionsString(DTONames) { return DTONames .map(name => { if (!all_dto_versions_constant_1.ALL_DTO_VERSIONS[name]) { throw new Error(`no DTO version found for ${name}`); } return `${name}.${all_dto_versions_constant_1.ALL_DTO_VERSIONS[name]}`; }).join(';'); } static getRequestXHeaders(config) { const requestId = uuid_1.v4().replace(/\-/g, ''); return { 'X-Client-Id': config.clientIdentifier, 'X-Client-Version': config.clientVersion, 'X-Request-ID': requestId, 'X-B3-TraceId': requestId }; } static getRequestHeaders(token, config) { return Object.assign({ 'Authorization': `${token.token_type} ${token.access_token}`, 'Accept': 'application/json' }, RequestOptionsFacory.getRequestXHeaders(config)); } static getRequestAccountQueryParams(token, config) { if (!token.companies || !token.companies.length) { throw new Error('no compnay found on given account'); } const [selectedCompany] = token.companies; return { account: config.authAccountName, user: config.authUserName, company: config.authCompany ? config.authCompany : selectedCompany.name, }; } } exports.RequestOptionsFacory = RequestOptionsFacory; //# sourceMappingURL=request-options.facory.js.map