UNPKG

@fairmint/canton-node-sdk

Version:
44 lines 1.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PartiesAggregationParamsSchema = void 0; exports.fetchAllParties = fetchAllParties; const zod_1 = require("zod"); const endpoint = '/v2/parties'; const DEFAULT_PAGE_SIZE = 5000; exports.PartiesAggregationParamsSchema = zod_1.z.object({ pageToken: zod_1.z.string().optional(), }); async function fetchAllParties(operation, params) { const validatedParams = operation.validateParams(params, exports.PartiesAggregationParamsSchema); const aggregatedPartyDetails = []; let currentPageToken = validatedParams.pageToken?.trim() ?? ''; // Loop until the API stops returning nextPageToken (or fails to advance) for (;;) { const url = new URL(`${operation.getApiUrl()}${endpoint}`); url.searchParams.set('pageSize', DEFAULT_PAGE_SIZE.toString()); if (currentPageToken.length > 0) { url.searchParams.set('pageToken', currentPageToken); } const response = await operation.makeGetRequest(url.toString(), { contentType: 'application/json', includeBearerToken: true, }); if (response.partyDetails?.length) { aggregatedPartyDetails.push(...response.partyDetails); } const nextTokenRaw = response.nextPageToken; const nextToken = typeof nextTokenRaw === 'string' ? nextTokenRaw.trim() : ''; if (nextToken.length === 0) { break; } if (nextToken === currentPageToken) { throw new Error('ListParties pagination did not advance to a new page token'); } currentPageToken = nextToken; } return { partyDetails: aggregatedPartyDetails, nextPageToken: '', }; } //# sourceMappingURL=aggregate-parties.js.map