fsm-sdk
Version:
Node.JS sdk to interface with SAP Field Service Management APIs.
382 lines • 14.9 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 data_api_service_1 = require("./core/data/data-api.service");
const account_api_service_1 = require("./core/account/account-api.service");
const query_api_service_1 = require("./core/query/query-api.service");
const batch_api_service_1 = require("./core/batch/batch-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._queryAPi = new query_api_service_1.QueryApiService(this._config, _http, this._auth);
this._batchApi = new batch_api_service_1.BatchAPIService(this._config, _http, this._auth);
this._accountApi = new account_api_service_1.AccountAPIService(this._config, this._auth);
this._dataApi = new data_api_service_1.DataApiService(this._config, _http, this._auth);
}
/**
* Creates UUID
* @returns a uuid that can be used as an FSM object id
*/
static createUUID() {
return request_options_factory_1.RequestOptionsFactory.getUUID().toUpperCase();
}
/**
* Here, you can input and execute the CoreSQL query.
*
* related api docs:
* https://help.sap.com/viewer/fsm_query_api/Cloud/en-US/query-api-intro.html
*
* @param coreSQL valid CoreSQL
* @param dtoNames DTOName[]
* @returns Promise<{ data: DTO[] }>
*/
query(coreSQL, dtoNames) {
return __awaiter(this, void 0, void 0, function* () {
return this._queryAPi.query(coreSQL, dtoNames);
});
}
/**
* Retrieving Lists of Objects by Id
*
* related api docs:
* https://help.sap.com/viewer/fsm_data_api/Cloud/en-US
*
* @param resourceName DTOName
* @param resourceId uuid as string
* @returns Promise<ClientResponse<DTO>>
*/
getById(resourceName, resourceId) {
return __awaiter(this, void 0, void 0, function* () {
return this._dataApi.getById(resourceName, { resourceId });
});
}
/**
* Retrieving Lists of Objects by ExternalId
*
* Note: [useExternalIds=true] option will be used
* referenced resources will not be a uid-string or null but of object or null
* containing id and externalId if present
* ```typescript
* [referenced-resource] : { id: string, externalId? : string } | null
* ```
*
* related api docs:
* https://help.sap.com/viewer/fsm_data_api/Cloud/en-US
*
* @param resourceName DTOName
* @param externalId externalId as string
* @returns Promise<ClientResponse<DTO>>
*/
getByExternalId(resourceName, externalId) {
return __awaiter(this, void 0, void 0, function* () {
return this._dataApi.getById(resourceName, { externalId }, { useExternalIds: true });
});
}
/**
* Deletes Existing Object by Id
* related api docs:
* https://help.sap.com/viewer/fsm_data_api/Cloud/en-US
*
* @param resourceName DTOName
* @param resource { id: string, lastChanged: number }
* @returns
*/
deleteById(resourceName, resource) {
return __awaiter(this, void 0, void 0, function* () {
const { id, lastChanged } = resource;
return this._dataApi.deleteById(resourceName, { resourceId: id }, lastChanged);
});
}
/**
* Deletes Existing Object by ExternalId
* related api docs:
* https://help.sap.com/viewer/fsm_data_api/Cloud/en-US
*
* @param resourceName DTOName
* @param resource { id: string, lastChanged: number }
* @returns
*/
deleteByExternalId(resourceName, resource) {
return __awaiter(this, void 0, void 0, function* () {
const { externalId, lastChanged } = resource;
return this._dataApi.deleteById(resourceName, { externalId }, lastChanged);
});
}
/**
* Creating Objects by Id
*
* related api docs:
* https://help.sap.com/viewer/fsm_data_api/Cloud/en-US
*
* @param resourceName DTOName
* @param resource should contain in the body the ENTIRE updated resource
* @returns Promise<ClientResponse<DTO>>
*/
post(resourceName, resource) {
return __awaiter(this, void 0, void 0, function* () {
return this._dataApi.post(resourceName, resource);
});
}
/**
* Creating Objects by ExternalId
*
* Note: [useExternalIds=true] option will be used
* referenced resources will not be a uid-string or null but of object or null
* containing id and externalId if present
* ```typescript
* [referenced-resource] : { id: string, externalId? : string } | null
* ```
*
* related api docs:
* https://help.sap.com/viewer/fsm_data_api/Cloud/en-US
*
* @param resourceName DTOName
* @param resource should contain in the body the ENTIRE updated resource
* @returns Promise<ClientResponse<DTO>>
*/
postByExternalId(resourceName, resource) {
return __awaiter(this, void 0, void 0, function* () {
return this._dataApi.post(resourceName, resource, { useExternalIds: true });
});
}
/**
* Updating Existing Objects by Id
*
* related api docs:
* https://help.sap.com/viewer/fsm_data_api/Cloud/en-US
*
* @param resourceName resourceName
* @param resource should contain in the body the ENTIRE updated resource
* @returns Promise<ClientResponse<DTO>>
*/
put(resourceName, resource) {
return __awaiter(this, void 0, void 0, function* () {
return this._dataApi.put(resourceName, { resourceId: resource.id }, resource);
});
}
/**
* Updating Existing Objects by ExternalId
*
* Note: [useExternalIds=true] option will be used
* referenced resources will not be a uid-string or null but of object or null
* containing id and externalId if present
* ```typescript
* [referenced-resource] : { id: string, externalId? : string } | null
* ```
*
* related api docs:
* https://help.sap.com/viewer/fsm_data_api/Cloud/en-US
*
* @param resourceName resourceName
* @param resource should contain in the resource the ENTIRE updated resource
* @returns Promise<ClientResponse<DTO>>
*/
putByExternalId(resourceName, resource) {
return __awaiter(this, void 0, void 0, function* () {
return this._dataApi.put(resourceName, { externalId: resource.externalId }, resource, { useExternalIds: true });
});
}
/**
* Updating Existing Objects by Id
* should contain [id] in the body the entire updated resource
*
* related api docs:
* https://help.sap.com/viewer/fsm_data_api/Cloud/en-US
*
* @param resourceName resourceName
* @param resource should contain in the body the [id] & [FIELDS THAT YOU WANT TO UPDATE]
* @returns Promise<ClientResponse<DTO>>
*/
patch(resourceName, resource) {
return __awaiter(this, void 0, void 0, function* () {
return this._dataApi.patch(resourceName, { resourceId: resource.id }, resource);
});
}
/**
* Updating Existing Objects by ExternalId
* should contain [ExternalId] in the resource the entire updated resource
*
* Note: [useExternalIds=true] option will be used
* referenced resources will not be a uid-string or null but of object or null
* containing id and externalId if present
* ```typescript
* [referenced-resource] : { id: string, externalId? : string } | null
* ```
*
* related api docs:
* https://help.sap.com/viewer/fsm_data_api/Cloud/en-US
*
* @param resourceName resourceName
* @param resource should contain in the resource the [externalId] & [FIELDS THAT YOU WANT TO UPDATE]
* @returns Promise<ClientResponse<DTO>>
*/
patchByExternalId(resourceName, resource) {
return __awaiter(this, void 0, void 0, function* () {
return this._dataApi.patch(resourceName, { externalId: resource.externalId }, resource, { useExternalIds: true });
});
}
/**
* Batch request with transational support
* requests will be executed in oder of the actions array.
*
* Example:
* ```typescript
* const actions = [
* new CreateAction('ServiceCall', { ... }),
* new UpdateAction('ServiceCall', { id, lastChanged ... }),
* new DeleteAction('ServiceCall', { id, lastChanged ... })
* ];
* await client.batch(actions)
* ```
*
* related api docs:
* https://help.sap.com/viewer/fsm_data_api/Cloud/en-US/batch-api-intro.html
*
* @param actions BatchAction | CreateAction | UpdateAction | DeleteAction
* @returns Promise<BatchResponseJson<T>[]>
*/
batch(actions) {
return __awaiter(this, void 0, void 0, function* () {
return this._batchApi.batch(actions);
});
}
/**
* 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;
}
/**
* Retrieves all accounts accessible to the authenticated user/client.
*
* @returns {Promise<Account[]>} A promise resolving to an array of accounts.
*/
getAccounts() {
return __awaiter(this, void 0, void 0, function* () {
return this._accountApi.getAccounts();
});
}
/**
* Retrieves all companies associated with a specific account.
*
* @param {number} accountId - The ID of the account.
* @returns {Promise<Company[]>} A promise resolving to an array of companies for the given account.
*/
getCompaniesByAccountId(accountId) {
return __awaiter(this, void 0, void 0, function* () {
return this._accountApi.getCompaniesByAccount(accountId);
});
}
}
exports.CoreAPIClient = CoreAPIClient;
//# sourceMappingURL=core-api.client.js.map