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C,MAAM,IAAI,MAAM,uEAEpB,MAAM,QAAoB,WAAW,EAAa,GAE5C,EAAW,CAEb,KAAM,KAGN,SAAU,EAAY,SAQ1B,OALI,EAAO,KAAO,EAAmB,QACjC,EAAS,KAAO,OAAO,OAAO,CAAC,EAAG,GAClC,EAAS,KAAK,QAGX,CAAQ,EASb,aAAe,CAAC,EAAa,IAC/B,QAAQ,EAAa,CACjB,KAAM,EACN,UAAW,EAAY,UACvB,SAGR,OAAO,QAAU,CAAE,0BAAc,gBAAS,cAAQ,gBAAS","file":"/npm/springboard-pagination-tool@2.1.0/index.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/springboard-pagination-tool@2.1.0/index.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","const fetch = require('node-fetch').default;\nconst retry = require('p-retry');\n\n/**\n * @typedef {Object} Springboard\n * @property {string} subDomain\n * @property {string} token\n */\n\n/**\n * @typedef {Object} Cursor\n * @description Represents the iterator's current position in the collection.\n * @property {int} page\n * @property {string} subDomain - the Springboard instance that contains this collection\n * @property {string} path - the url to this collection (Relative to the root API endpoint)\n */\n\n/**\n * @param {Springboard} springboard - the instance of Springboard Retail to get the page from.\n * @param  {Cursor} cursor - cursor pointing to the page you wish to retrieve\n * @return {Promise<Object>} deserialized JSON response returned by Springboard Retail.\n */\nconst getRawPage = async (springboard, cursor) => {\n    // The absolute URL of the Springboard Retail collection.\n    let url = `https://${springboard.subDomain}.myspringboard.us/api/${cursor.path}`;\n\n    // Adds a query parameter to the end of the URL.\n    const appendQueryParam = (key, value) =>\n        url += (url.includes('?') ? '&' : '?') + `${key}=${value}`;\n\n    appendQueryParam('page', cursor.page);\n\n    const authorizationHeader = { 'Authorization': `Bearer ${springboard.token}` };\n\n    const fetchPage = () => fetch(url, { headers: authorizationHeader })\n        .then(response => response.json());\n\n    // Retry a couple of times in case some one-off network error occurred.\n    const data = await retry(fetchPage, { retries: 5 });\n\n    if (data.error) throw new Error(data.error);\n\n    return data\n};\n\n/**\n * @typedef {Function} PageConsumer\n * @param {Array} elements - all elements on the the current page\n * @param {Function} cancel - stops iteration over the collection.\n * @returns {undefined} any output returned from the consumer will be ignored\n */\n\n/**\n * Iterates over every page in the collection.\n * @param {Springboard} springboard - the Springboard Retail instance containing this collection.\n * @param path - the path to the collection (relative to the root API endpoint). Can also contain query parameters.\n * @param {PageConsumer} consumer - a function that processes each page in the collection\n * @return {Promise<void>} the returned promise resolves after the consumer has been invoked on each page in the collection.\n */\nconst iteratePages = async (springboard, path, consumer) => {\n    // The page that we are currently on.\n    // Zero if iteration has not started yet.\n    let page = 0;\n\n    // The total number of pages in this collection. There will always be at least once page,\n    // though it may be empty.\n    let pages = 1;\n\n    // Run until we've iterated over every single page.\n    while (page < pages) {\n        const cursor = {\n            page: ++page,\n            subDomain: springboard.subDomain,\n            path\n        };\n\n        const data = await getRawPage(springboard, cursor);\n\n        // Update the pages count. The collection might have expanded or contracted while we iterated\n        // over the elements on the current page.\n        pages = data['pages'];\n\n        // Pass the page to the consumer for processing.\n        {\n            let isCancelled = false;\n            const cancel = () => isCancelled = true;\n\n            await consumer(data.results, cancel);\n\n            if (isCancelled) return\n        }\n    }\n};\n\n\n/**\n * @typedef {Function} ElementConsumer\n * @param {Object} record - a record in the paginated collection.\n * @param {Function} cancel - stops iteration over the collection.\n * @returns {undefined} any output returned from the consumer will be ignored\n */\n\n/**\n * Iterates over each element in a Springboard Retail paginated collection.\n * @param {Springboard} springboard the instance of Springboard to fetch the collection from\n * @param {string} path relative path to the collection\n * @param {ElementConsumer} consumer invoked once for every item in the list\n * @returns {Promise<void>} resolves after all records have been iterated over or a {@link ElementConsumer} cancels iteration.\n */\nconst iterate = (springboard, path, consumer) => {\n    const pageConsumer = async (elements, cancel) => {\n        for (const element of elements) {\n            let isCancelled = false;\n            const innerCancel = () => isCancelled = true;\n\n            await consumer(element, innerCancel);\n\n            if (isCancelled) {\n                // If the ElementConsumer cancelled iteration, we must cancel the page iterator.\n                cancel();\n\n                // The ElementConsumer cancelled iteration, do not pass anymore elements to it.\n                break;\n            }\n        }\n    };\n\n    return iteratePages(springboard, path, pageConsumer);\n};\n\n\n/**\n * Returns all the members of a paginated list at once. Only use this on small list.\n * @param {Springboard} springboard the instance of Springboard to fetch the collection from\n * @param {string} path relative path to the collection\n * @returns {Promise<[]>} the results stored in the collection\n */\nconst getAll = async (springboard, path) => {\n    const compilation = [];\n\n    const consumer = (element, _) => {\n        compilation.push(element)\n    };\n\n    await iterate(springboard, path, consumer);\n\n    return compilation;\n};\n\n/**\n * @typedef {Object} PageData\n * @property {Cursor|null} next - a cursor that points to the next page in the collection. Null if this is the last page.\n *  Pass the cursor to {@link getPage} to get subsequent pages.\n * @property {Array} elements - an array of the elements on this page\n */\n\n/**\n * Returns the collection page that corresponds to the given cursor.\n * @param {Springboard} springboard - the instance of Springboard Retail to get the page from.\n * @param {Cursor} cursor\n * @return {Promise<PageData>}\n */\nconst getPage = async (springboard, cursor) => {\n    if (cursor.subDomain !== springboard.subDomain)\n        throw new Error('The given Springboard instance can\\'t be used to access that cursor.');\n\n    const rawPageData = await getRawPage(springboard, cursor);\n\n    const pageData = {\n        /** @type {Cursor|null} */\n        next: null,\n\n        /** @type {Array} */\n        elements: rawPageData.results\n    };\n\n    if (cursor.page < rawPageData['pages']) {\n        pageData.next = Object.assign({}, cursor);\n        pageData.next.page++;\n    }\n\n    return pageData;\n };\n\n/**\n * Returns the first page of a Springboard Retail collection.\n * @param {Springboard} springboard - the instance of Springboard Retail to fetch the collection from\n * @param {string} path - the relative path to the collection\n * @return {Promise<PageData>}\n */\nconst getFirstPage = (springboard, path) =>\n    getPage(springboard, {\n        page: 1,\n        subDomain: springboard.subDomain,\n        path\n    });\n\nmodule.exports = { iteratePages, iterate, getAll, getPage, getFirstPage };\n"]}