@cisco-meraki/dashboard-api-tools
Version:
Typescript library for interacting with Meraki's public API
82 lines (81 loc) • 3.83 kB
JavaScript
;
var __awaiter = (this && this.__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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.batchedApiRequest = void 0;
const apiUtils_1 = require("./apiUtils");
const sleep = (ms) => {
return new Promise((res) => setTimeout(res, ms));
};
const makeFailResponseObj = (apiResponse) => {
//action batch itself failed;
//apiResponse will have errors;
//formatting it here for consistent error returns
const status = apiResponse.data.status;
const error = {
errors: status.errors,
ok: status.completed,
statusCode: apiResponse.statusCode,
statusText: apiResponse.statusText,
};
return error;
};
const batchedApiRequest = (orgId, actions, authOptions, opts) => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b, _c, _d, _e, _f, _g, _h;
const url = `/api/v1/organizations/${orgId}/actionBatches`;
const data = {
confirmed: true,
synchronous: !!(opts === null || opts === void 0 ? void 0 : opts.synchronous),
actions,
};
let apiResp;
// try to make a post actionBatch request
try {
apiResp = yield (0, apiUtils_1.apiRequest)("POST", url, data, authOptions);
if ((_b = (_a = apiResp.data) === null || _a === void 0 ? void 0 : _a.status) === null || _b === void 0 ? void 0 : _b.completed) {
return apiResp;
}
else if ((_d = (_c = apiResp.data) === null || _c === void 0 ? void 0 : _c.status) === null || _d === void 0 ? void 0 : _d.failed) {
return Promise.reject(makeFailResponseObj(apiResp));
}
}
catch (failedResponse) {
return Promise.reject(failedResponse);
}
// now we check the actionBatch status to see if things have changed since it was neither failed nor completed (aka pending)
const interval = (opts === null || opts === void 0 ? void 0 : opts.interval) || 500; //ms
const endTime = Date.now() + ((opts === null || opts === void 0 ? void 0 : opts.maxPollingTime) || 12000); //ms
while (Date.now() <= endTime) {
try {
const apiCheckResp = yield (0, apiUtils_1.apiRequest)("GET", `/api/v1/organizations/${orgId}/actionBatches/${apiResp.data.id}`);
if ((_f = (_e = apiCheckResp.data) === null || _e === void 0 ? void 0 : _e.status) === null || _f === void 0 ? void 0 : _f.completed) {
return apiCheckResp;
}
else if ((_h = (_g = apiCheckResp.data) === null || _g === void 0 ? void 0 : _g.status) === null || _h === void 0 ? void 0 : _h.failed) {
return Promise.reject(makeFailResponseObj(apiCheckResp));
}
else {
yield sleep(interval);
}
}
catch (failedResponse) {
return Promise.reject(failedResponse);
}
}
const maxPollingErrorMsg = "Your updates have been submitted and are still pending. Try reloading the page.";
const error = {
errors: [maxPollingErrorMsg],
ok: false,
statusCode: 200,
statusText: "max timeout",
};
return Promise.reject(error);
});
exports.batchedApiRequest = batchedApiRequest;