fsm-sdk
Version:
Node.JS sdk to interface with SAP Field Service Management APIs.
115 lines • 4.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RequestOptionsFactory = void 0;
const uuid_1 = require("uuid");
const all_dto_versions_constant_1 = require("./data-service/all-dto-versions.constant");
class RequestOptionsFactory {
static getUUID(legacyFormat) {
const id = uuid_1.v4();
return legacyFormat
? id.replace(/\-/g, '').toUpperCase()
: id;
}
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 getContextXHeaders(token, config) {
const { accountId, accountName, companyId, companyName, userId, } = this.resolveContext(token, config);
return Object.entries({
'X-Account-Id': accountId,
'X-Account-Name': accountName,
'X-Company-Id': companyId,
'X-Company-Name': companyName,
'X-User-Id': userId,
}).reduce((headers, [key, value]) => (!value ? headers : Object.assign(Object.assign({}, headers), { [key]: value })), {});
}
static getRequestContentType() {
return {
'Accept': 'application/json',
'Content-Type': 'application/json',
};
}
static getRequestHeaders(token, config) {
return Object.assign(Object.assign(Object.assign({ 'Authorization': `${token.token_type} ${token.access_token}` }, this.getRequestXHeaders(config)), this.getContextXHeaders(token, config)), this.getRequestContentType());
}
static resolveContext(token, config) {
const companies = token.contentType === 'user'
&& token.content.companies || []; // todo: find a way to provide companies for other content types
// will pick company by config.authCompany or first company in the list
const selcetedCompany = !!config.authCompany
? companies.find(c => c.name === config.authCompany)
: companies[0];
const companyName = selcetedCompany
? selcetedCompany.name
: config.authCompany;
const companyId = selcetedCompany
? selcetedCompany.id
: undefined; // todo find a way to provide companyId for other content types
const userName = config.authUserName
? config.authUserName
: token.contentType === 'user'
? token.content.user_name
: undefined;
const userId = token.contentType === 'user'
? token.content.user_id
: undefined; // todo find a way to provide userId for other content types
const accountName = config.authAccountName;
const accountId = token.contentType === 'user'
? token.content.account_id
: undefined; // todo find a way to provide accountId for other content types
return {
accountId,
accountName,
companyId,
companyName,
userId,
userName
};
}
static getRequestAccountQueryParams(token, config) {
const { companyName, userName, accountName } = RequestOptionsFactory.resolveContext(token, config);
return {
user: userName,
company: companyName,
account: accountName,
};
}
}
exports.RequestOptionsFactory = RequestOptionsFactory;
//# sourceMappingURL=request-options.factory.js.map