UNPKG

@ventum-digital/plugin-fetch

Version:

Functions for fetching data from SailPoint IdentityIQ REST API

123 lines (121 loc) 6 kB
/******/ // The require scope /******/ var __webpack_require__ = {}; /******/ /************************************************************************/ /******/ /* webpack/runtime/define property getters */ /******/ (() => { /******/ // define getter functions for harmony exports /******/ __webpack_require__.d = (exports, definition) => { /******/ for(var key in definition) { /******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) { /******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] }); /******/ } /******/ } /******/ }; /******/ })(); /******/ /******/ /* webpack/runtime/hasOwnProperty shorthand */ /******/ (() => { /******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop)) /******/ })(); /******/ /******/ /* webpack/runtime/make namespace object */ /******/ (() => { /******/ // define __esModule on exports /******/ __webpack_require__.r = (exports) => { /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); /******/ } /******/ Object.defineProperty(exports, '__esModule', { value: true }); /******/ }; /******/ })(); /******/ /************************************************************************/ var __webpack_exports__ = {}; /*!************************!*\ !*** ./build/index.js ***! \************************/ __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ getRestUrl: () => (/* binding */ getRestUrl), /* harmony export */ getUrlFactory: () => (/* binding */ getUrlFactory), /* harmony export */ pluginFetch: () => (/* binding */ pluginFetch), /* harmony export */ pluginFetchKey: () => (/* binding */ pluginFetchKey) /* harmony export */ }); // noinspection JSUnusedGlobalSymbols var __awaiter = (undefined && undefined.__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()); }); }; /** * Performs an HTTP request using the Fetch API with additional plugin-specific configurations. * * @param {string} url - The URL to which the request is sent. You can use {@link getUrlFactory}. * @param {RequestInit} options - The options used to configure the HTTP request, such as method, headers, and body. * @return {Promise<any>} A promise that resolves with the parsed JSON response if the request is successful or rejects with an error if the request fails. * @throws {Error} If the response is not ok (status code outside the range 200-299), an error is thrown with the status and response message. */ function pluginFetch(url_1) { return __awaiter(this, arguments, void 0, function* (url, options = {}) { let buildOptions = Object.assign({ method: "GET" }, options); buildOptions.headers = Object.assign({ // @ts-ignore PluginHelper "X-XSRF-TOKEN": PluginHelper.getCsrfToken() }, buildOptions.headers); const response = yield fetch(url, buildOptions); let responseJson = yield response.json(); if (!response.ok) { throw new Error(`HTTP error! Status: ${response.status} Response: ${JSON.stringify(responseJson)}`); } return responseJson; }); } /** * Fetches data from the provided URL using {@link pluginFetch} and retrieves the specified key from the response. * * @param {string} url - The URL to fetch the data from. You can use {@link getUrlFactory}. * @param {string} key - The key to extract from the fetched response. * @return {Promise<any>} A promise that resolves to the value associated with the specified key in the response, or an empty string if an error occurs. */ function pluginFetchKey(url, key) { return __awaiter(this, void 0, void 0, function* () { try { const response = yield pluginFetch(url); return response[key]; } catch (e) { console.error("Couldn't get key", e); return ""; } }); } /** * Generates a URL creation function for a specific plugin. * * @param {string} pluginName - The name of the plugin for which the URL factory is being created. * @return {(path: string) => string} - A function that takes a path and returns the complete URL for the given plugin. Leading slashes in the path are removed. */ function getUrlFactory(pluginName) { return function getUrl(path) { // Remove leading slash from path if it exists path = path.replace(/^\//, ""); // @ts-ignore PluginHelper return PluginHelper.getPluginRestUrl(`${pluginName}/${path}`); }; } function getRestUrl(path) { // Remove leading slash from path if it exists path = path.replace(/^\//, ""); // @ts-ignore SailPoint return SailPoint.getRelativeUrl(`/ui/rest/${path}`); } var __webpack_exports__getRestUrl = __webpack_exports__.getRestUrl var __webpack_exports__getUrlFactory = __webpack_exports__.getUrlFactory var __webpack_exports__pluginFetch = __webpack_exports__.pluginFetch var __webpack_exports__pluginFetchKey = __webpack_exports__.pluginFetchKey export { __webpack_exports__getRestUrl as getRestUrl, __webpack_exports__getUrlFactory as getUrlFactory, __webpack_exports__pluginFetch as pluginFetch, __webpack_exports__pluginFetchKey as pluginFetchKey }; //# sourceMappingURL=index.js.map