fsm-sdk
Version:
Node.JS sdk to interface with SAP Field Service Management APIs.
186 lines • 8.14 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CoreAPIClient = void 0;
const request_options_factory_1 = require("./core/request-options.factory");
const oauth_service_1 = require("./core/oauth/oauth.service");
const http_service_1 = require("./core/http/http-service");
const account_api_service_1 = require("./core/account/account-api.service");
const translations_api_service_1 = require("./core/translations/translations-api.service");
const service_management_service_1 = require("./core/service-management/service-management.service");
const data_service_service_1 = require("./core/data-service/data-service.service");
const rules_api_service_1 = require("./core/rules/rules-api.service");
class CoreAPIClient {
/**
* The CoreAPIClient
*
* ```typescript
*{
* // [mandatory] your client configuration
* clientIdentifier: '<your-clientIdentifier>',
* clientSecret: '<your-clientSecret>',
* clientVersion: '<your-clientVersion>',
*
* // [optional] oauth grant type, default=password
* authGrantType: 'password' | 'client_credentials' | undefined
*
* // [optional] | [mandatory] if oauth grant type password
* authAccountName: '<your-authAccountName>',
*
* // [optional] | [mandatory] if oauth grant type password
* authUserName: '<your-authUserName>',
*
* // [optional] | [mandatory] if oauth grant type password
* authPassword: '<your-authPassword>',
*
* // [optional] or default=FIRST
* authCompany: '<your-authCompany>',
*
* // [optional] provide verbose logs
* debug: false,
*
* // [optional] enable using custom oauth endpoints see https://api.sap.com/api/cloud_authentication_service_ext/overview
* oauthEndpoint: 'https://eu.fsm.cloud.sap/api/oauth2/v2',
*
* // [optional] client will cache token (helpful for writing integration tests)
* tokenCacheFilePath: './.myToken.json'
*
*}
* ```
* @param _config ClientConfig
* @returns CoreAPIClient
*/
constructor(config) {
this._config_default = {
debug: false,
oauthEndpoint: undefined,
baseUrl: 'https://eu.fsm.cloud.sap',
tokenCacheFilePath: undefined,
clientIdentifier: '<your-clientIdentifier>',
clientSecret: '<your-clientSecret>',
clientVersion: '<your-clientVersion>',
authGrantType: 'password',
authAccountName: undefined,
authUserName: undefined,
authPassword: undefined,
authCompany: undefined
};
this._config = Object.assign(this._config_default, config);
const _http = new http_service_1.HttpService(this._config);
this._auth = new oauth_service_1.OAuthService(_http);
this._accountApi = new account_api_service_1.AccountAPIService(this._config, this._auth);
this._dataServiceAPI = new data_service_service_1.DataServiceAPIService(this._config, _http, this._auth);
this._translationApi = new translations_api_service_1.TranslationApiService(this._config, _http, this._auth);
this._rulesApi = new rules_api_service_1.RulesAPIService(this._config, _http, this._auth);
this._serviceManagementApi = new service_management_service_1.ServiceManagementAPIService(this._config, _http, this._auth);
}
/**
* @param o { legacyFormat: boolean }
* @description Creates a UUID string.
* If `legacyFormat` is true, it returns the UUID without dashes and in uppercase. used by FSM legacy data-cloud APIs APIs.
* If `legacyFormat` is false, it returns the UUID in its standard format.
* @returns string
*/
static createUUID(o = { legacyFormat: true }) {
return request_options_factory_1.RequestOptionsFactory.getUUID(o === null || o === void 0 ? void 0 : o.legacyFormat);
}
/**
* Provides access to the Service Management API for managing service calls and activities.
*
* @returns {ServiceManagementAPIService} Service management API instance with activity, service call, and composite operations.
*/
get serviceManagementAPI() {
return this._serviceManagementApi;
}
/**
* Provides access to the legacy Data Service API (Data Cloud).
*
* @returns {DataServiceAPIService} Data service API instance for legacy data cloud operations.
*/
get dataServiceAPI() {
return this._dataServiceAPI;
}
/**
* Provides access to the Account API for retrieving account and company information.
*
* @returns {AccountAPIService} Account API instance for account and company operations.
*/
get accountAPI() {
return this._accountApi;
}
/**
* Provides access to the Translation API for managing labels and translations.
*
* @returns {TranslationApiService} Translation API instance for label and value operations.
*/
get translationAPI() {
return this._translationApi;
}
/**
* Provides access to the Rules API for managing business rules.
*
* @returns {RulesAPIService} Rules API instance for business rule operations.
*/
get rulesAPI() {
return this._rulesApi;
}
/**
* Executes a login using the current client configuration and retrieves an OAuth token.
*
* Note: Explicit login is not required before other actions; the client will auto-login and refresh tokens as needed.
*
* @returns {Promise<OAuthTokenResponse>} Resolves with the OAuth token response.
*/
login() {
return __awaiter(this, void 0, void 0, function* () {
return this._auth.ensureToken(this._config);
});
}
/**
* Retrieves the current OAuth token, if available.
*
* @returns {Readonly<OAuthTokenResponse> | undefined} The current OAuth token or undefined if not logged in.
*/
getToken() {
return this._auth.getToken();
}
/**
* Sets the OAuth token to be used by the client.
*
* @param {OAuthTokenResponse} token - The OAuth token to set.
* @returns {CoreAPIClient} The current client instance for chaining.
*/
setToken(token) {
this._auth.setToken(token);
return this;
}
/**
* Sets the company to use for authentication, if the token contains multiple companies.
*
* @param {string} companyName - The name of the company to set for authentication.
* @throws {Error} If no token is found, or if the company is not available in the token.
* @returns {CoreAPIClient} The current client instance for chaining.
*/
setAuthCompany(companyName) {
var _a;
const token = this._auth.getToken();
if (!token) {
throw new Error('No token found. Please login first.');
}
if (token.contentType === 'user' && (token.content.companies || []).map(it => it.name.toLocaleLowerCase()).includes(companyName.toLocaleLowerCase()) === false) {
throw new Error(`Company '${companyName}' not found in token. Available companies: ${(_a = token.content.companies) === null || _a === void 0 ? void 0 : _a.map(it => it.name).join(', ')}`);
}
this._config.authCompany = companyName;
return this;
}
}
exports.CoreAPIClient = CoreAPIClient;
//# sourceMappingURL=core-api.client.js.map