UNPKG

maximo-rest-client

Version:

A simple REST API client for the IBM Maximo platform written in TypeScript.

171 lines 7.32 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const axios_1 = __importDefault(require("axios")); const MaximoClientError_1 = __importDefault(require("./MaximoClientError")); class MaximoClient { constructor(options) { this.maximoOptions = { authorizationScheme: options.authorizationScheme || "MAX_AUTH", host: options.host, lean: true, password: options.password, port: options.port || 9080, protocol: options.protocol || "http", user: options.user, }; this.baseUrl = `${this.maximoOptions.protocol}://${this.maximoOptions.host}:${this.maximoOptions.port}`; } getResources(resourceName, fieldsOrWhere, whereClause) { return __awaiter(this, void 0, void 0, function* () { const axiosInstance = this.session || (yield this.authorize()); let response; let whereString = null; let selectString = null; if (typeof fieldsOrWhere === "string") { whereString = fieldsOrWhere; } else if (Array.isArray(fieldsOrWhere)) { selectString = fieldsOrWhere.join(); whereString = whereClause || null; } try { response = yield axiosInstance.get(`${this.baseUrl}/maximo/oslc/os/${resourceName}`, { params: { ["oslc.select"]: selectString, ["oslc.where"]: whereString, lean: this.maximoOptions.lean ? 1 : 0, }, }); } catch (err) { throw new MaximoClientError_1.default(err.response.status, err.response.statusText, err.response.data, "Failed to get the resources"); } return response.data.member; }); } /** * Retrieves a single resource by its URI * @param resourceUri URI of the resource which to retrieve. * Can be created by using the {@link #getResources | getResources() method} * @param fields Select fields to return in the resource. Equivalent to oslc.select. */ getResource(resourceUri, fields) { return __awaiter(this, void 0, void 0, function* () { const axiosInstance = this.session || (yield this.authorize()); let response; const selectString = fields ? fields.join() : null; try { response = yield axiosInstance.get(`${resourceUri}`, { params: { ["oslc.select"]: selectString, lean: this.maximoOptions.lean ? 1 : 0, }, }); } catch (err) { throw new MaximoClientError_1.default(err.response.status, err.response.statusText, err.response.data, "Failed to get the resource"); } return response.data; }); } /** * Creates a new resource with the specified contents * @param resourceName Name of the resource to create * @param resourceBody Content and fields of the new resource */ createResource(resourceName, resourceBody) { return __awaiter(this, void 0, void 0, function* () { const axiosInstance = this.session || (yield this.authorize()); let response; try { response = yield axiosInstance.post(`${this.baseUrl}/maximo/oslc/os/${resourceName}`, resourceBody, { headers: { "Content-Type": "application/json", }, params: { lean: this.maximoOptions.lean ? 1 : 0, }, }); } catch (err) { throw new MaximoClientError_1.default(err.response.status, err.response.statusText, err.response.data, "Failed to create the resource"); } return response; }); } /** * Updates a resource * @param resourceUri URI of the resource on which to perform updates. * @param resourceBody Content and fields to be updated * @param merge True if you want to set patchtype to MERGE */ updateResource(resourceUri, resourceBody, merge = false) { return __awaiter(this, void 0, void 0, function* () { const axiosInstance = this.session || (yield this.authorize()); let response; try { response = yield axiosInstance.post(resourceUri, resourceBody, { headers: { "Content-Type": "application/json", "patchtype": merge ? "MERGE" : null, "x-method-override": "PATCH", }, params: { lean: this.maximoOptions.lean ? 1 : 0, }, }); } catch (err) { throw new MaximoClientError_1.default(err.response.status, err.response.statusText, err.response.data, "Failed to update the resource"); } return response; }); } /** * Initializes a reusable session */ initializeReusableSession() { return __awaiter(this, void 0, void 0, function* () { this.session = yield this.authorize(); }); } /** * Authorizes the user and returns an `AxiosInstance` with JSESSIONID */ authorize() { return __awaiter(this, void 0, void 0, function* () { let response = null; try { response = yield axios_1.default.get(`${this.baseUrl}/maximo/oslc/login`, { headers: { maxauth: Buffer.from(`${this.maximoOptions.user}:${this.maximoOptions.password}`) .toString("base64"), }, withCredentials: true, }); } catch (err) { throw new MaximoClientError_1.default(err.response.status, err.response.statusText, err.response.data, "Failed to authorize the given user"); } return axios_1.default.create({ headers: { Cookie: response.headers["set-cookie"][0], }, withCredentials: true, }); }); } } exports.MaximoClient = MaximoClient; //# sourceMappingURL=MaximoClient.js.map