UNPKG

@shinkashi/insight-sdk

Version:

Solution Insight SDK for JavaScript

180 lines (179 loc) 8.17 kB
"use strict"; var __asyncValues = (this && this.__asyncValues) || function (o) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var m = o[Symbol.asyncIterator], i; return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } }; var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); } var __asyncDelegator = (this && this.__asyncDelegator) || function (o) { var i, p; return i = {}, verb("next"), verb("throw", function (e) { throw e; }), verb("return"), i[Symbol.iterator] = function () { return this; }, i; function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === "return" } : f ? f(v) : v; } : f; } }; var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var g = generator.apply(thisArg, _arguments || []), i, q = []; return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i; function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; } function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } } function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); } function fulfill(value) { resume("next", value); } function reject(value) { resume("throw", value); } function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.InsightRepository = void 0; const client_1 = require("./client"); class InsightRepository { constructor(client, objPath, updateMethod) { this.client = client; this.objPath = objPath; this.updateMethod = updateMethod; // Change objPath from /identity/groups/A/identity/policy/B to /identity/grous/A/policies/B if (client.prefix.includes('/identity/groups') || client.prefix.includes('/identity/users')) this.objPath = client.prefix + this.objPath.replace('/identity', ''); else this.objPath = client.prefix + this.objPath; } // Hierarchical REST API resource. // Usage Example: cmp.group.within(groupID).policy.add(policyID). within(id) { const credential = { url: this.client.url, apiKey: this.client.apiKey, apiSecret: this.client.apiSecret, headers: this.client.headers }; return new client_1.InsightClient(credential, this.objPath + '/' + id); } async getByID(id, params = {}) { const response = await this.client.ax.get(this.objPath + '/' + id, { params }); if (response.status !== 200) { throw new Error(`API Error ${response.status}: ${response.data}`); } return response.data; } // TODO: This can be merged with find(). // The original find_all() in Insight SDK for Python was to return a flat array not generator, // but I'm not sure if this variation is really needed in this Insight SDK for Javascript. async findAll() { var e_1, _a; const findResp = this.find(); let findAllResp = []; try { for (var findResp_1 = __asyncValues(findResp), findResp_1_1; findResp_1_1 = await findResp_1.next(), !findResp_1_1.done;) { let res = findResp_1_1.value; findAllResp.push(res); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (findResp_1_1 && !findResp_1_1.done && (_a = findResp_1.return)) await _a.call(findResp_1); } finally { if (e_1) throw e_1.error; } } return findAllResp; } find(queryParams = {}) { return __asyncGenerator(this, arguments, function* find_1() { let page = 1; let params = queryParams; while (true) { params.page = page; // console.log('** calling API ', this.objPath, 'with', params); const response = yield __await(this.client.ax.get(this.objPath, { params })); // console.log('** response ', response); if (response.status !== 200) { throw new Error(`API Error ${response.status}: ${response.data}`); } let items; if ('objects' in response.data) { items = response.data['objects']; } else { items = response.data; } if (items.length === 0) break; yield __await(yield* __asyncDelegator(__asyncValues(items))); page++; } return yield __await(undefined); }); } async findOne(params = {}) { const findResp = (await this.find(params).next()).value; return findResp; } async create(obj) { const response = await this.client.ax.post(this.objPath, obj); if (response.status !== 201) { throw new Error(`API Error ${response.status}: ${response.data}`); } return response.data; } async findOrCreate(obj) { const existingObjs = await this.findAll(); const existingObj = existingObjs.find(o => o.name === obj.name); if (existingObj) { return existingObj; } return this.create(obj); } async update(obj) { var _a; const objContent = Object.assign({}, obj); // TODO: These are Policy specific, we should get the fields from T delete objContent.id; delete objContent.customer_id; delete objContent.deprecated_at; delete objContent.create_time; delete objContent.is_public; if (((_a = objContent.document) === null || _a === void 0 ? void 0 : _a.Statement) === null) { objContent.document = { Statement: [] }; } let response; let path = this.objPath; if ('id' in obj) { path += "/" + obj.id; } if (this.updateMethod === 'PATCH') { response = await this.client.ax.patch(path, objContent); } else { response = await this.client.ax.put(path, objContent); } if (response.status !== 200) { throw new Error(`API Error ${response.status}: ${response.data}`); } return response.data; } async delete(id) { if (id === '') { throw new Error('id cannot be empty'); } const response = await this.client.ax.delete(this.objPath + '/' + id); if (!([200, 204].includes(response.status))) { throw new Error(`API Error ${response.status}: ${response.data}`); } return response.data; } async add(id) { const response = await this.client.ax.put(this.objPath + "/" + id); if (response.status !== 204) { throw new Error(`API Error ${response.status}: ${response.data}`); } return response.data; } async remove(id) { const response = await this.client.ax.delete(this.objPath + "/" + id); if (response.status !== 204) { throw new Error(`API Error ${response.status}: ${response.data}`); } return response.data; } } exports.InsightRepository = InsightRepository;