fsm-sdk
Version:
Node.JS sdk to interface with SAP Field Service Management APIs.
71 lines • 2.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RequestOptionsFactory = void 0;
const uuid_1 = require("uuid");
const all_dto_versions_constant_1 = require("./all-dto-versions.constant");
class RequestOptionsFactory {
static getUUID() {
return uuid_1.v4().replace(/\-/g, '');
}
static stringify(o) {
return Object.keys(o).map((key) => `${encodeURIComponent(key)}=${encodeURIComponent(o[key])}`).join('&');
}
static getBaseUrl(config) {
return config.baseUrl;
}
static getDataApiUriFor(config, resourceName, resourceId = null, externalId = null) {
const identifier = [
(resourceId ? `/${resourceId}` : '').trim(),
(externalId && !resourceId ? `/externalId/${externalId}` : '').trim()
].join('').trim();
return `${this.getBaseUrl(config)}/api/data/v4/${resourceName}${identifier}`;
}
/**
* map of DTO objects and versions
* { ['<DTOName>']: number }
* Note: DTOName is case sensitive
*/
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 || 'unknown',
'X-Client-Version': config.clientVersion || 'unknown',
'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' }, RequestOptionsFactory.getRequestXHeaders(config));
}
static getRequestAccountQueryParams(token, config) {
const [firstCompany] = token.contentType === 'user'
&& token.content.companies || [];
const company = config.authCompany
? config.authCompany
: firstCompany === null || firstCompany === void 0 ? void 0 : firstCompany.name;
const user = config.authUserName
? config.authUserName
: token.contentType === 'user'
? token.content.user_name
: undefined;
return {
user,
company,
account: config.authAccountName,
};
}
}
exports.RequestOptionsFactory = RequestOptionsFactory;
//# sourceMappingURL=request-options.factory.js.map