UNPKG

@controlplane/cli

Version:

Control Plane Corporation CLI

59 lines 2.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RestClient = void 0; /** Query parameter that makes the API validate a write and return its result without persisting it */ const DRY_RUN_PARAM = 'dryRun'; class RestClient { constructor(axios) { this.axios = axios; this.dryRun = false; } /** * does a POST, follows the location header and retrieves it * @param path * @param body */ async create(path, body, cfg) { // A dry run answers with the resource itself and no location header to follow if (this.dryRun) { return this.post(path, body, cfg); } let res = await this.axios.post(path, body, cfg); let link = res.headers.location; res = await this.axios.get(link); return res.data; } async delete(path, cfg) { await this.axios.delete(path, cfg); } async patch(path, body, cfg) { const res = await this.axios.patch(path, body, this.writeConfig(cfg)); return res.data; } async get(path, cfg) { const res = await this.axios.get(path, cfg); return res.data; } async post(path, body, cfg) { const res = await this.axios.post(path, body, this.writeConfig(cfg)); return res.data; } async put(path, body, cfg) { const res = await this.axios.put(path, body, this.writeConfig(cfg)); return res.data; } /** * Adds the dry run query parameter to a write request while the client is in dry run mode. * * @param {AxiosRequestConfig | undefined} cfg - (Optional) The request configuration of the call site. * @returns {AxiosRequestConfig | undefined} The configuration to send the write with. */ writeConfig(cfg) { if (!this.dryRun) { return cfg; } return { ...cfg, params: { ...cfg === null || cfg === void 0 ? void 0 : cfg.params, [DRY_RUN_PARAM]: true } }; } } exports.RestClient = RestClient; //# sourceMappingURL=client.js.map