auth0
Version:
Auth0 Node.js SDK for the Management API v2.
177 lines (176 loc) • 8.41 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
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 = Object.create((typeof AsyncIterator === "function" ? AsyncIterator : Object).prototype), verb("next"), verb("throw"), verb("return", awaitReturn), i[Symbol.asyncIterator] = function () { return this; }, i;
function awaitReturn(f) { return function (v) { return Promise.resolve(v).then(f, reject); }; }
function verb(n, f) { if (g[n]) { i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; if (f) i[n] = f(i[n]); } }
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]); }
};
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); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CustomPager = void 0;
/**
* A custom pager for paginated API responses where the pagination logic
* must be implemented by the SDK author.
*
* SDK authors provide a parser callback to extract items and determine
* pagination state from responses.
*
* @template TItem The type of the items in the page.
* @template TRequest The type of the request object.
* @template TResponse The type of the API response.
*/
class CustomPager {
constructor(args) {
this.response = args.response;
this.rawResponse = args.rawResponse;
this.data = args.items;
this._hasNextPage = args.hasNextPage;
this._hasPreviousPage = args.hasPreviousPage;
this.nextRequest = args.nextRequest;
this.previousRequest = args.previousRequest;
this.context = args.context;
this.parser = args.parser;
}
/**
* @returns whether there is a next page to load
*/
hasNextPage() {
return this._hasNextPage;
}
/**
* @returns whether there is a previous page to load
*/
hasPreviousPage() {
return this._hasPreviousPage;
}
/**
* Retrieves the next page of results.
* @returns this pager with updated data
* @throws Error if there is no next page
*/
getNextPage() {
return __awaiter(this, void 0, void 0, function* () {
if (!this._hasNextPage || !this.nextRequest) {
throw new Error("No next page available");
}
const { data, rawResponse } = yield this.context.sendRequest(this.nextRequest).withRawResponse();
const parsed = yield this.parser(this.nextRequest, { data, rawResponse });
this.response = data;
this.rawResponse = rawResponse;
this.data = parsed.items;
this._hasNextPage = parsed.hasNextPage;
this._hasPreviousPage = parsed.hasPreviousPage;
this.context.currentRequest = this.nextRequest;
this.nextRequest = parsed.nextRequest;
this.previousRequest = parsed.previousRequest;
return this;
});
}
/**
* Retrieves the previous page of results.
* @returns this pager with updated data
* @throws Error if there is no previous page
*/
getPreviousPage() {
return __awaiter(this, void 0, void 0, function* () {
if (!this._hasPreviousPage || !this.previousRequest) {
throw new Error("No previous page available");
}
const { data, rawResponse } = yield this.context.sendRequest(this.previousRequest).withRawResponse();
const parsed = yield this.parser(this.previousRequest, { data, rawResponse });
this.response = data;
this.rawResponse = rawResponse;
this.data = parsed.items;
this._hasNextPage = parsed.hasNextPage;
this._hasPreviousPage = parsed.hasPreviousPage;
this.context.currentRequest = this.previousRequest;
this.nextRequest = parsed.nextRequest;
this.previousRequest = parsed.previousRequest;
return this;
});
}
iterMessages() {
return __asyncGenerator(this, arguments, function* iterMessages_1() {
for (const item of this.data) {
yield yield __await(item);
}
while (this.hasNextPage()) {
yield __await(this.getNextPage());
for (const item of this.data) {
yield yield __await(item);
}
}
});
}
[Symbol.asyncIterator]() {
return __asyncGenerator(this, arguments, function* _a() {
var _b, e_1, _c, _d;
try {
for (var _e = true, _f = __asyncValues(this.iterMessages()), _g; _g = yield __await(_f.next()), _b = _g.done, !_b; _e = true) {
_d = _g.value;
_e = false;
const message = _d;
yield yield __await(message);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (!_e && !_b && (_c = _f.return)) yield __await(_c.call(_f));
}
finally { if (e_1) throw e_1.error; }
}
});
}
/**
* Creates a CustomPager by making the initial request and parsing the response.
*
* @param args.sendRequest Function to send a request and get a response
* @param args.initialRequest The initial request to start pagination
* @param args.parse The parser function to extract items and pagination state
* @returns A new CustomPager instance
*/
static create(args) {
return __awaiter(this, void 0, void 0, function* () {
const { data, rawResponse } = yield args.sendRequest(args.initialRequest).withRawResponse();
const parsed = yield args.parse(args.initialRequest, { data, rawResponse });
return new CustomPager({
response: data,
rawResponse,
items: parsed.items,
hasNextPage: parsed.hasNextPage,
hasPreviousPage: parsed.hasPreviousPage,
nextRequest: parsed.nextRequest,
previousRequest: parsed.previousRequest,
context: {
sendRequest: args.sendRequest,
currentRequest: args.initialRequest,
},
parser: args.parse,
});
});
}
}
exports.CustomPager = CustomPager;