Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paginate over template results #100

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/commands/templates/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }
}
3 changes: 2 additions & 1 deletion src/commands/templates/pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
logError,
fatalError,
} from '../../utils'
import { fetchAllTemplates } from './helpers'

export const command = 'pull <output directory> [options]'
export const desc = 'Pull templates from a server to <output directory>'
Expand Down Expand Up @@ -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()
Expand Down
9 changes: 7 additions & 2 deletions src/commands/templates/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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
? []
Expand Down