UNPKG

contentful-management

Version:
42 lines (40 loc) 1.31 kB
/* eslint-disable @typescript-eslint/ban-ts-comment */ import { toPlainObject } from 'contentful-sdk-core'; import copy from 'fast-copy'; /** * @private */ export const wrapCollection = fn => (makeRequest, data, ...rest) => { const collectionData = toPlainObject(copy(data)); // @ts-expect-error collectionData.items = collectionData.items.map(entity => fn(makeRequest, entity, ...rest)); // @ts-expect-error return collectionData; }; /** * @private * Function for endpoints allowing `?cursor=true` wrapping the call * to ensure the correct return type for cursor based pagination * when `cursor: true`. */ export const withOptionalCursorApi = fn => { return function (args) { return fn.call(this, args); }; }; export const wrapCursorPaginatedCollection = fn => (makeRequest, data, ...rest) => { const collectionData = toPlainObject(copy(data)); // @ts-expect-error collectionData.items = collectionData.items.map(entity => fn(makeRequest, entity, ...rest)); // @ts-expect-error return collectionData; }; export function isSuccessful(statusCode) { return statusCode < 300; } export function shouldRePoll(statusCode) { return [404, 422, 429, 400].includes(statusCode); } export async function waitFor(ms = 1000) { return new Promise(resolve => setTimeout(resolve, ms)); }