UNPKG

@jesseditson/dnsimple

Version:

A Node.JS client for the DNSimple API.

65 lines (64 loc) 3.91 kB
"use strict"; var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); } var __asyncValues = (this && this.__asyncValues) || function (o) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var m = o[Symbol.asyncIterator], i; return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } }; var __asyncDelegator = (this && this.__asyncDelegator) || function (o) { var i, p; return i = {}, verb("next"), verb("throw", function (e) { throw e; }), verb("return"), i[Symbol.iterator] = function () { return this; }, i; function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === "return" } : f ? f(v) : v; } : f; } }; var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var g = generator.apply(thisArg, _arguments || []), i, q = []; return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i; function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; } function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } } function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); } function fulfill(value) { resume("next", value); } function reject(value) { resume("throw", value); } function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.paginate = void 0; /** * Async generator function to assist with paginated APIs. * Use this to iterate through individual items of a paginated API with ease. * Provide a callback that makes the request with the provided page number, and this function will take care of calling the API and yielding the individual items, allowing continuous iteration of all items across all pages like a single list. * * @example Iterate through all items of a paginated API: * ``` * const client = new Dnsimple({}); * const paginated = paginate(page => client.certificates.listCertificates(1000, "bingo.pizza", { sort: "name:asc", page })); * for await (const certificate of paginated) { * console.log("Certificate", certificate.name, "expires at", certificate.expires_at); * } * ``` * * @example Most paginated API methods also have a helper submethod that will invoke this function automatically with the appropriate arguments: * ``` * const client = new Dnsimple({}); * const paginated = client.certificates.listCertificates.paginate(1000, "bingo.pizza", { sort: "name:asc" }); * for await (const certificate of paginated) { * console.log("Certificate", certificate.name, "expires at", certificate.expires_at); * } * ``` */ function paginate(fn) { return __asyncGenerator(this, arguments, function* paginate_1() { let page = 1; while (true) { const res = yield __await(fn(page)); yield __await(yield* __asyncDelegator(__asyncValues(res.data))); if (page >= res.pagination.total_pages) { break; } page++; } }); } exports.paginate = paginate;