UNPKG

fsm-sdk

Version:

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

337 lines 12.4 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { 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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const request_options_facory_1 = require("./core/request-options.facory"); const auth_service_1 = require("./core/auth.service"); const http_service_1 = require("./core/http-service"); const client_service_1 = require("./core/client.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 * oauthEndpoint: 'https://ds.coresuite.com/api/oauth2/v1', * * // [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: 'https://ds.coresuite.com/api/oauth2/v1', tokenCacheFilePath: undefined, clientIdentifier: '<your-clientIdentifier>', clientSecret: '<your-clientSecret>', clientVersion: '<your-clientVersion>', authGrantType: 'password', authAccountName: undefined, authUserName: undefined, authPassword: undefined, authCompany: undefined }; const _config = Object.assign(this._config_default, config); const _http = new http_service_1.HttpService(_config); const _auth = new auth_service_1.AuthService(_http); this._client = new client_service_1.ClientService(_config, _http, _auth); } /** * Creates UUID * @returns a uuid that can be used as an FSM object id */ static createUUID() { return request_options_facory_1.RequestOptionsFacory.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._client.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._client.getResource(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._client.getResource(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._client.deleteResource(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._client.deleteResource(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._client.postResource(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._client.postResource(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._client.putResource(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._client.putResource(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._client.patchResource(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._client.patchResource(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._client.batch(actions); }); } /** * Will use provided ClientConfig and perform a Login. * * Note: that it is **not required** to explicitly call client.login() * before each client action. The CoreAPIClient will login and **keep a internally token copy** * and will use this **up to its expiration** and **will auto refresh** when needed. * Calling client.login() will NOT result in mutiple http calls to the oauth api. * * related api docs: * https://help.sap.com/viewer/fsm_access_api/Cloud/en-US/oauth-intro.html * * @returns Promise<OauthTokenResponse> */ login() { return __awaiter(this, void 0, void 0, function* () { return this._client.login(); }); } /** * get OauthTokenResponse * @returns Readonly<OauthTokenResponse> | undefined */ getToken() { return this._client.getToken(); } /** * set OauthTokenResponse * @param token OauthTokenResponse * @returns CoreAPIClient */ setToken(token) { this._client.setToken(token); return this; } } exports.CoreAPIClient = CoreAPIClient; //# sourceMappingURL=core-api.client.js.map