From 41ce160a2423c9d28ddac1f835d771fe03887068 Mon Sep 17 00:00:00 2001 From: Paulius Kigas <876738+pauliokas@users.noreply.github.com> Date: Mon, 17 Feb 2025 14:25:45 +0200 Subject: [PATCH] Paginate over template results --- src/commands/templates/helpers.ts | 18 ++++++++++++++++++ src/commands/templates/pull.ts | 3 ++- src/commands/templates/push.ts | 9 +++++++-- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/commands/templates/helpers.ts b/src/commands/templates/helpers.ts index f672788..ca57f43 100644 --- a/src/commands/templates/helpers.ts +++ b/src/commands/templates/helpers.ts @@ -4,6 +4,8 @@ import { readJsonSync, readFileSync, existsSync } from 'fs-extra' import traverse from 'traverse' import dirTree from 'directory-tree' import { TemplateManifest, MetaFileTraverse, MetaFile } from '../../types' +import { ServerClient } from 'postmark' +import { Templates } from 'postmark/dist/client/models' /** * Parses templates folder and files @@ -102,3 +104,19 @@ export function sameContent( return str1 === str2 } + +export async function fetchAllTemplates(client: ServerClient) { + const pageSize = 300 + let offset = 0 + let totalCount = Infinity + let templates: Templates['Templates'] = [] + + while (offset < totalCount) { + const response = await client.getTemplates({ count: pageSize, offset }) + totalCount = response.TotalCount + offset += response.Templates.length + templates.push(...response.Templates) + } + + return { Templates: templates, TotalCount: totalCount } +} diff --git a/src/commands/templates/pull.ts b/src/commands/templates/pull.ts index 9b3bc89..c2c46b6 100644 --- a/src/commands/templates/pull.ts +++ b/src/commands/templates/pull.ts @@ -15,6 +15,7 @@ import { logError, fatalError, } from '../../utils' +import { fetchAllTemplates } from './helpers' export const command = 'pull [options]' export const desc = 'Pull templates from a server to ' @@ -106,7 +107,7 @@ async function fetchTemplateList(options: TemplateListOptions) { } try { - const templates = await client.getTemplates({ count: 300 }) + const templates = await fetchAllTemplates(client) if (templates.TotalCount === 0) { spinner.stop() diff --git a/src/commands/templates/push.ts b/src/commands/templates/push.ts index b1b3bd7..22e9bc4 100644 --- a/src/commands/templates/push.ts +++ b/src/commands/templates/push.ts @@ -18,7 +18,12 @@ import { logError, } from '../../utils' -import { createManifest, sameContent, templatesDiff } from './helpers' +import { + createManifest, + fetchAllTemplates, + sameContent, + templatesDiff, +} from './helpers' const debug = require('debug')('postmark-cli:templates:push') @@ -119,7 +124,7 @@ async function push( } try { - const templateList = await client.getTemplates({ count: 300 }) + const templateList = await fetchAllTemplates(client) const newList = templateList.TotalCount === 0 ? []