contentful-management
Version:
Client for Contentful's Content Management API
113 lines (110 loc) • 5.87 kB
JavaScript
import { post, get as get$1 } from './raw.js';
import { isSuccessful, shouldRePoll, waitFor } from '../../../common-utils.js';
const create = (http, params, data) => {
return post(http, `/spaces/${params.spaceId}/environments/${params.environmentId}/app_installations/${params.appDefinitionId}/actions/${params.appActionId}/calls`, data);
};
const getCallDetails = (http, params) => {
return get$1(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 {
const result = await getCallDetails(http, { ...params, callId: callId });
// The lambda failed or returned a 404, so we shouldn't re-poll anymore
if (result?.response?.statusCode && !isSuccessful(result?.response?.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();
});
}
const createWithResponse = async (http, params, data) => {
const createResponse = await 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 });
};
// Get structured AppActionCall (status/result/error) via new route that includes app installation context
const get = (http, params) => {
return get$1(http, `/spaces/${params.spaceId}/environments/${params.environmentId}/app_installations/${params.appDefinitionId}/actions/${params.appActionId}/calls/${params.callId}`);
};
// Get raw AppActionCall response (headers/body) for a completed call
const getResponse = (http, params) => {
return get$1(http, `/spaces/${params.spaceId}/environments/${params.environmentId}/app_installations/${params.appDefinitionId}/actions/${params.appActionId}/calls/${params.callId}/response`);
};
async function pollStructuredAppActionCall(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 {
const result = await get(http, { ...params, callId });
// If backend has not yet written the record, keep polling up to retries
// Otherwise, resolve when status is terminal
if (result?.sys.status === 'succeeded' || result?.sys.status === 'failed') {
resolve(result);
}
else if (result?.sys.status === 'processing' && checkCount < retries) {
checkCount++;
await waitFor(retryInterval);
poll();
}
else {
// Status not terminal and no more retries
reject(new Error('The app action result is taking longer than expected to process.'));
}
}
catch (error) {
checkCount++;
if (checkCount > retries) {
reject(new Error('The app action result is taking longer than expected to process.'));
return;
}
// Similar to legacy behavior: transient errors (e.g., 404 during propagation) → re-poll
await waitFor(retryInterval);
poll();
}
};
poll();
});
}
// Create and poll the structured AppActionCall until completion (succeeded/failed)
const createWithResult = async (http, params, data) => {
const createResponse = await post(http, `/spaces/${params.spaceId}/environments/${params.environmentId}/app_installations/${params.appDefinitionId}/actions/${params.appActionId}/calls`, data);
const callId = createResponse.sys.id;
return pollStructuredAppActionCall(http, params, { callId });
};
export { create, createWithResponse, createWithResult, get, getCallDetails, getResponse };
//# sourceMappingURL=app-action-call.js.map