UNPKG

contentful-management

Version:
69 lines (67 loc) 4.37 kB
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } import * as raw from './raw'; import { isSuccessful, shouldRePoll, waitFor } from '../../../common-utils'; export const create = (http, params, data) => { return raw.post(http, `/spaces/${params.spaceId}/environments/${params.environmentId}/app_installations/${params.appDefinitionId}/actions/${params.appActionId}/calls`, data); }; export const getCallDetails = (http, params) => { return raw.get(http, `/spaces/${params.spaceId}/environments/${params.environmentId}/actions/${params.appActionId}/calls/${params.callId}`); }; const APP_ACTION_CALL_RETRY_INTERVAL = 2000; const APP_ACTION_CALL_RETRIES = 15; async function callAppActionResult(http, params, { callId }) { let checkCount = 1; const retryInterval = params.retryInterval || APP_ACTION_CALL_RETRY_INTERVAL; const retries = params.retries || APP_ACTION_CALL_RETRIES; return new Promise((resolve, reject) => { const poll = async () => { try { var _result$response, _result$response2; const result = await getCallDetails(http, _objectSpread(_objectSpread({}, params), {}, { callId: callId })); // The lambda failed or returned a 404, so we shouldn't re-poll anymore if (result !== null && result !== void 0 && (_result$response = result.response) !== null && _result$response !== void 0 && _result$response.statusCode && !isSuccessful(result === null || result === void 0 ? void 0 : (_result$response2 = result.response) === null || _result$response2 === void 0 ? void 0 : _result$response2.statusCode)) { const error = new Error('App action not found or lambda fails'); reject(error); } else if (isSuccessful(result.statusCode)) { resolve(result); } // The logs are not ready yet. Continue waiting for them else if (shouldRePoll(result.statusCode) && checkCount < retries) { checkCount++; await waitFor(retryInterval); poll(); } // If the response status code is not successful and is not a status code that should be repolled, reject with an error immediately else { const error = new Error('The app action response is taking longer than expected to process.'); reject(error); } } catch (error) { checkCount++; if (checkCount > retries) { reject(new Error('The app action response is taking longer than expected to process.')); return; } // If `appActionCalls.getCallDetails` throws, we re-poll as it might mean that the lambda result is not available in the webhook logs yet await waitFor(retryInterval); poll(); } }; poll(); }); } export const createWithResponse = async (http, params, data) => { const createResponse = await raw.post(http, `/spaces/${params.spaceId}/environments/${params.environmentId}/app_installations/${params.appDefinitionId}/actions/${params.appActionId}/calls`, data); const callId = createResponse.sys.id; return callAppActionResult(http, params, { callId }); };