UNPKG

@commercelayer/sdk-utils

Version:
136 lines (134 loc) 4.89 kB
import { headerRateLimits, computeRateLimits } from './chunk-5DLBI7WA.js'; import { sleep } from './chunk-PDAZISCK.js'; import { config } from './chunk-Q7OVA4YI.js'; import { init_default } from './chunk-XGJBRQ3X.js'; // src/all.ts var retrieveAll = async (resourceType, params) => { const cl = init_default().sdk; const client = init_default().api(resourceType); const rrr = cl.addRawResponseReader({ headers: true }); let result = null; let lastId = null; let rateLimit = null; const recordLimit = (params?.limit || 0) > 0 ? params?.limit : void 0; if (recordLimit && params) delete params.limit; const allParams = params || {}; allParams.pageNumber = 1; allParams.pageSize = config.api.page_max_size; allParams.sort = ["id"]; if (!allParams.filters) allParams.filters = {}; do { if (lastId) allParams.filters.id_gt = lastId; if (rateLimit) await sleep(rateLimit.delay); const page = await client.list(allParams); if (result === null) result = page; else result.push(...page); lastId = page.last()?.id; if (!rateLimit) try { const rateLimits = headerRateLimits(rrr.headers); rateLimit = computeRateLimits(rateLimits, resourceType, result.pageCount); if (rateLimit) cl.removeRawResponseReader(); } catch (error) { } } while (result.length < Math.min(recordLimit || result.recordCount, result.recordCount)); if (recordLimit) { result.splice(recordLimit, result.length - recordLimit); } const meta = result.meta; delete meta.currentPage; return result; }; var updateAll = async (resourceType, resource, params) => { const cl = init_default().sdk; const client = init_default().api(resourceType); const rrr = cl.addRawResponseReader({ headers: true }); const result = { total: 0, processed: 0, errors: 0, resources: {} }; let lastId = null; let rateLimit = null; const allParams = params || {}; allParams.pageNumber = 1; allParams.pageSize = config.api.page_max_size; allParams.sort = ["id"]; if (!allParams.filters) allParams.filters = {}; do { if (lastId) allParams.filters.id_gt = lastId; if (rateLimit) await sleep(rateLimit.delay); const page = await client.list(allParams); if (!lastId) result.total = page.recordCount; if (!rateLimit) try { const rateLimits = headerRateLimits(rrr.headers); rateLimit = computeRateLimits(rateLimits, resourceType, result.total + page.pageCount); if (rateLimit) cl.removeRawResponseReader(); } catch (error) { } for (const item of page) { result.resources[item.id] = { success: false }; const resId = result.resources[item.id]; try { const updRes = { ...resource, id: item.id, type: resourceType }; if (rateLimit) await sleep(rateLimit.delay); await client.update(updRes); result.processed++; resId.success = true; } catch (error) { result.errors++; if (cl.isApiError(error)) resId.error = error; resId.errorMessage = error.message; } } lastId = page.last()?.id; } while (result.processed + result.errors < result.total); return result; }; var deleteAll = async (resourceType, params) => { const cl = init_default().sdk; const client = init_default().api(resourceType); const rrr = cl.addRawResponseReader({ headers: true }); const result = { total: 0, processed: 0, errors: 0, resources: {} }; let lastId = null; let rateLimit = null; const allParams = params || {}; allParams.pageNumber = 1; allParams.pageSize = config.api.page_max_size; allParams.sort = ["id"]; if (!allParams.filters) allParams.filters = {}; do { if (lastId) allParams.filters.id_gt = lastId; if (rateLimit) await sleep(rateLimit.delay); const page = await client.list(allParams); if (!lastId) result.total = page.recordCount; if (!rateLimit) try { const rateLimits = headerRateLimits(rrr.headers); rateLimit = computeRateLimits(rateLimits, resourceType, result.total + page.pageCount); if (rateLimit) cl.removeRawResponseReader(); } catch (error) { } for (const item of page) { result.resources[item.id] = { success: false }; const resId = result.resources[item.id]; try { if (rateLimit) await sleep(rateLimit.delay); await client.delete(item.id); result.processed++; resId.success = true; } catch (error) { result.errors++; if (cl.isApiError(error)) resId.error = error; resId.errorMessage = error.message; } } lastId = page.last()?.id; } while (result.processed + result.errors < result.total); return result; }; var all = { retrieveAll, updateAll }; export { all, deleteAll, retrieveAll, updateAll }; //# sourceMappingURL=chunk-M6CJ3UGH.js.map //# sourceMappingURL=chunk-M6CJ3UGH.js.map