diff --git a/CHANGELOG.md b/CHANGELOG.md index aaaa5e7..c4e4dfd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## Unreleased + +- Fix: the project dropdown reads the next page of projects when you click "Load more". Before, it showed the first 100 projects again, so a project after the first 100 never showed. +- Fix: the AI agent, project template and project member dropdowns show more than the first page of rows. +- Fix: Find Project searches all of your projects, not only the first 100. +- Each list reads at most 20 pages, so a bad API response cannot make it ask for pages forever. + ## 1.3.0 - New triggers (instant, Taskade Pro plan or higher): New Comment, Task Assigned, New Project, Project Assigned, Member Joined Project. diff --git a/packages/n8n-nodes-taskade/CHANGELOG.md b/packages/n8n-nodes-taskade/CHANGELOG.md index c60881c..e51516b 100644 --- a/packages/n8n-nodes-taskade/CHANGELOG.md +++ b/packages/n8n-nodes-taskade/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## Unreleased + +- Fix: a list call reads at most 100 pages (10,000 rows). Before, a response that ignored `page` or `after` made Return All and the member and template dropdowns ask for pages forever. + ## 1.0.1 - No change to the node. This release is the first one from GitHub Actions through npm trusted publishing (OIDC), with no npm token. diff --git a/packages/n8n-nodes-taskade/nodes/Taskade/GenericFunctions.ts b/packages/n8n-nodes-taskade/nodes/Taskade/GenericFunctions.ts index 4068f27..28e6a03 100644 --- a/packages/n8n-nodes-taskade/nodes/Taskade/GenericFunctions.ts +++ b/packages/n8n-nodes-taskade/nodes/Taskade/GenericFunctions.ts @@ -72,8 +72,15 @@ export async function taskadeApiRequest( } /** - * Collects every item of a cursor-paginated v2 list (`listTasks`), or stops at `limit`. - * The cursor is the ID of the last item on the previous page. + * The most pages that one list call reads (10,000 rows at 100 rows a page). + * A response that ignores `page` or `after` returns the same full page every + * time, and this bound stops the loop. + */ +export const MAX_PAGES = 100; + +/** + * Collects every item of a cursor-paginated v2 list (`listTasks`), or stops at `limit` + * or after MAX_PAGES pages. The cursor is the ID of the last item on the previous page. */ export async function taskadeApiRequestCursorItems( this: IExecuteFunctions | ILoadOptionsFunctions, @@ -85,25 +92,29 @@ export async function taskadeApiRequestCursorItems( const items: IDataObject[] = []; let after: string | undefined; - for (;;) { + for (let page = 1; page <= MAX_PAGES; page++) { const response = await taskadeApiRequest.call(this, endpoint, { ...body, limit: pageSize, ...(after === undefined ? {} : { after }), }); - const page = (response.items as IDataObject[] | undefined) ?? []; - items.push(...page); + const pageItems = (response.items as IDataObject[] | undefined) ?? []; + items.push(...pageItems); if (limit !== undefined && items.length >= limit) { return items.slice(0, limit); } - if (page.length < pageSize) { + if (pageItems.length < pageSize) { return items; } - after = String(page[page.length - 1].id); + after = String(pageItems[pageItems.length - 1].id); } + return items; } -/** Collects every item of a page-numbered v2 list (`listMyProjects`, `listTemplates`), or stops at `limit`. */ +/** + * Collects every item of a page-numbered v2 list (`listMyProjects`, `listTemplates`), + * or stops at `limit` or after MAX_PAGES pages. + */ export async function taskadeApiRequestPagedItems( this: IExecuteFunctions | ILoadOptionsFunctions, endpoint: string, @@ -113,7 +124,7 @@ export async function taskadeApiRequestPagedItems( const pageSize = 100; const items: IDataObject[] = []; - for (let page = 1; ; page++) { + for (let page = 1; page <= MAX_PAGES; page++) { const response = await taskadeApiRequest.call(this, endpoint, { ...body, limit: pageSize, @@ -128,6 +139,7 @@ export async function taskadeApiRequestPagedItems( return items; } } + return items; } /** Sends a numeric string as a number, because number fields reject a string value. */ diff --git a/packages/n8n-nodes-taskade/test/Taskade.node.test.ts b/packages/n8n-nodes-taskade/test/Taskade.node.test.ts index bad5ec0..138031d 100644 --- a/packages/n8n-nodes-taskade/test/Taskade.node.test.ts +++ b/packages/n8n-nodes-taskade/test/Taskade.node.test.ts @@ -1,6 +1,7 @@ import type { IDataObject, IExecuteFunctions, ILoadOptionsFunctions } from 'n8n-workflow'; import { describe, expect, it } from 'vitest'; +import { MAX_PAGES } from '../nodes/Taskade/GenericFunctions'; import { Taskade, toTaskadeDate } from '../nodes/Taskade/Taskade.node'; import { createContext } from './helpers'; @@ -299,6 +300,38 @@ describe('list search', () => { }); }); +describe('page bound', () => { + // An API that ignores `page` and `after` returns the same full page every + // time. The fake API throws after 1,000 calls, so a loop without a bound + // fails this test instead of running forever. + const fullPageForever = () => { + let count = 0; + return () => { + count += 1; + if (count > 1000) { + throw new Error('runaway loop: more than 1,000 requests'); + } + return { ok: true, items: Array.from({ length: 100 }, (_, n) => ({ id: `r${n}`, handle: `u${n}` })) }; + }; + }; + + it('stops Get Many tasks with Return All at MAX_PAGES when the API ignores the cursor', async () => { + const { output, calls } = await run( + { resource: 'task', operation: 'getMany', projectId: 'p1', returnAll: true }, + fullPageForever(), + ); + expect(calls).toHaveLength(MAX_PAGES); + expect(output).toHaveLength(MAX_PAGES * 100); + }); + + it('stops the project member list at MAX_PAGES when the API ignores the page', async () => { + const context = createContext({ authentication: 'accessToken', projectId: 'p1' }, fullPageForever()); + await node.methods.loadOptions.getProjectMembers.call(context as unknown as ILoadOptionsFunctions); + expect(context.calls).toHaveLength(MAX_PAGES); + expect(context.calls.at(-1)?.body?.page).toBe(MAX_PAGES); + }); +}); + describe('toTaskadeDate', () => { it('keeps only the date for all-day values', () => { expect(toTaskadeDate('2026-10-09T15:30:00', true, 'UTC')).toEqual({ date: '2026-10-09' }); diff --git a/src/client.ts b/src/client.ts index 6fb36b7..721bd1f 100644 --- a/src/client.ts +++ b/src/client.ts @@ -3,6 +3,16 @@ import { Bundle, ZObject } from 'zapier-platform-core'; export const BASE_V1 = 'https://www.taskade.com/api/v1'; export const BASE_V2 = 'https://www.taskade.com/api/v2'; +/** Rows to ask for on each page of a page-numbered list. It is the default of `GET /me/projects`. */ +export const PAGE_SIZE = 100; + +/** + * The most pages that the app reads from one page-numbered list (2,000 rows at + * PAGE_SIZE). A response that ignores the `page` parameter returns the same + * full page every time, and this bound stops the app from asking forever. + */ +export const MAX_PAGES = 20; + export interface ApiResponse { ok?: boolean; message?: string; @@ -40,6 +50,41 @@ export const request = async (z: ZObject, bundle: Bundle, opts: RequestOptions) }); }; +/** + * Turns the dropdown page that Zapier sends (`bundle.meta.page`, 0 for the + * first page) into the `page` (1 for the first page) and `limit` parameters of + * a page-numbered Taskade list. Returns null at MAX_PAGES and after it, so the + * caller returns no rows and Zapier stops asking for more. + */ +export const dropdownPageParams = (bundle: Bundle): { limit: string; page: string } | null => { + const page = Number(bundle.meta?.page ?? 0); + if (!Number.isInteger(page) || page < 0 || page >= MAX_PAGES) { + return null; + } + return { limit: String(PAGE_SIZE), page: String(page + 1) }; +}; + +/** + * Reads every page of a page-numbered Taskade list. It stops at the first page + * with fewer than PAGE_SIZE rows, or after MAX_PAGES pages. + */ +export const listAllPages = async (z: ZObject, bundle: Bundle, path: string) => { + const items: Array> = []; + for (let page = 1; page <= MAX_PAGES; page++) { + const response = await request(z, bundle, { + method: 'GET', + path, + params: { limit: String(PAGE_SIZE), page: String(page) }, + }); + const pageItems = parseOk(z, response.json).items ?? []; + items.push(...pageItems); + if (pageItems.length < PAGE_SIZE) { + break; + } + } + return items; +}; + /** Throws the Taskade error message when the API returns `{ ok: false }`, else returns the body. */ export const parseOk = (z: ZObject, data: ApiResponse): ApiResponse => { if (data && data.ok === false) { diff --git a/src/searches/findProject.ts b/src/searches/findProject.ts index c135f42..2071b4b 100644 --- a/src/searches/findProject.ts +++ b/src/searches/findProject.ts @@ -1,6 +1,6 @@ import { Bundle, ZObject } from 'zapier-platform-core'; -import { ApiResponse, parseOk, request } from '../client'; +import { ApiResponse, listAllPages, parseOk, request } from '../client'; import { spaceField } from '../fields'; const perform = async (z: ZObject, bundle: Bundle) => { @@ -9,13 +9,20 @@ const perform = async (z: ZObject, bundle: Bundle) => { ? `/folders/${bundle.inputData.space_id}/projects` : '/me/projects'; - // Best-effort: widen the page size; matching is client-side over the result set. - const response = await request(z, bundle, { method: 'GET', path, params: { limit: '100' } }); + // The match runs here, over the projects that the API returns. GET /me/projects + // is page-numbered, so read each page. GET /folders/{folderId}/projects takes no page parameter. + let projects: Array>; + if (bundle.inputData.space_id != null) { + const response = await request(z, bundle, { method: 'GET', path, params: { limit: '100' } }); + const data: ApiResponse = parseOk(z, response.json); + projects = data.items ?? []; + } else { + projects = await listAllPages(z, bundle, path); + } - const data: ApiResponse = parseOk(z, response.json); const query = String(bundle.inputData.query ?? '').toLowerCase(); - return (data.items ?? []) + return projects .filter((project) => String(project.name ?? project.title ?? '') .toLowerCase() diff --git a/src/test/pagination.test.ts b/src/test/pagination.test.ts new file mode 100644 index 0000000..b60c678 --- /dev/null +++ b/src/test/pagination.test.ts @@ -0,0 +1,153 @@ +import { Bundle, ZObject } from 'zapier-platform-core'; + +import { MAX_PAGES } from '../client'; +import FindProject from '../searches/findProject'; +import GetAllAgents from '../triggers/getAllAgents'; +import GetAllAssignableMembers from '../triggers/getAllAssignableMembers'; +import GetAllProjects from '../triggers/getAllProjects'; +import GetAllProjectTemplates from '../triggers/getAllProjectTemplates'; + +// A stand-in for the Taskade API. It pages a list of `total` rows with the +// `page` and `limit` query parameters, the same way that the public API does. +// `defaultLimit` is the page size that the API uses when no `limit` is sent. +// With `ignorePage`, it returns the first page for every request, which is the +// bad response that the page bound protects against. + +interface FakeRequest { + url: string; + method: string; + params?: Record; +} + +const pagedZ = (total: number, options: { defaultLimit?: number; ignorePage?: boolean } = {}) => { + const rows = Array.from({ length: total }, (_, index) => ({ + id: `id-${index + 1}`, + name: `Row ${index + 1}`, + handle: `user${index + 1}`, + displayName: `User ${index + 1}`, + })); + const calls: FakeRequest[] = []; + const z = { + request: async (request: FakeRequest) => { + calls.push(request); + const limit = Number(request.params?.limit ?? options.defaultLimit ?? 100); + const page = options.ignorePage ? 1 : Number(request.params?.page ?? 1); + const items = rows.slice((page - 1) * limit, page * limit); + return { status: 200, json: { ok: true, items } }; + }, + errors: { Error: class extends Error {} }, + } as unknown as ZObject; + return { z, calls }; +}; + +const bundleFor = (inputData: Record, page?: number) => + ({ + authData: { access_token: 'token' }, + inputData, + meta: page === undefined ? {} : { page }, + }) as unknown as Bundle; + +const ids = (rows: Array<{ id: string }>) => rows.map((row) => row.id); + +describe('dropdown: get_all_projects without a folder', () => { + it('shows the projects after the first 100 when Zapier asks for the next page', async () => { + const { z } = pagedZ(150); + const first = await GetAllProjects.operation.perform(z, bundleFor({}, 0)); + const second = await GetAllProjects.operation.perform(z, bundleFor({}, 1)); + expect(first).toHaveLength(100); + expect(ids(second)).toEqual(Array.from({ length: 50 }, (_, index) => `id-${index + 101}`)); + }); + + it('sends the page and limit parameters of GET /me/projects', async () => { + const { z, calls } = pagedZ(150); + await GetAllProjects.operation.perform(z, bundleFor({}, 1)); + expect(calls[0].url).toBe('https://www.taskade.com/api/v1/me/projects'); + expect(calls[0].params).toEqual({ limit: '100', page: '2' }); + }); + + it('returns no rows after the last page', async () => { + const { z } = pagedZ(150); + expect(await GetAllProjects.operation.perform(z, bundleFor({}, 2))).toEqual([]); + }); + + it('stops at the page bound when the API ignores the page parameter', async () => { + const { z, calls } = pagedZ(150, { ignorePage: true }); + expect(await GetAllProjects.operation.perform(z, bundleFor({}, MAX_PAGES))).toEqual([]); + expect(calls).toHaveLength(0); + }); +}); + +describe('dropdown: get_all_projects in a folder', () => { + it('lists the folder projects on the first page', async () => { + const { z, calls } = pagedZ(3); + const result = await GetAllProjects.operation.perform(z, bundleFor({ space_id: 'folder-1' }, 0)); + expect(ids(result)).toEqual(['id-1', 'id-2', 'id-3']); + expect(calls[0].url).toBe('https://www.taskade.com/api/v1/folders/folder-1/projects'); + }); + + it('does not repeat the folder projects when Zapier asks for the next page', async () => { + // GET /folders/{folderId}/projects takes no page parameter, so one request returns every row. + const { z, calls } = pagedZ(3); + expect(await GetAllProjects.operation.perform(z, bundleFor({ space_id: 'folder-1' }, 1))).toEqual([]); + expect(calls).toHaveLength(0); + }); +}); + +describe('search: find_project without a folder', () => { + it('finds a project that is after the first 100', async () => { + const { z } = pagedZ(150); + const result = await FindProject.operation.perform(z, bundleFor({ query: 'Row 130' })); + expect(result).toEqual([{ id: 'id-130', name: 'Row 130' }]); + }); + + it('reads at most the page bound when the API ignores the page parameter', async () => { + const { z, calls } = pagedZ(150, { ignorePage: true }); + expect(await FindProject.operation.perform(z, bundleFor({ query: 'no match' }))).toEqual([]); + expect(calls).toHaveLength(MAX_PAGES); + }); +}); + +describe('dropdowns on other page-numbered lists', () => { + it('get_all_agents shows the second page', async () => { + const { z, calls } = pagedZ(130, { defaultLimit: 20 }); + const result = await GetAllAgents.operation.perform(z, bundleFor({ space_id: 'folder-1' }, 1)); + expect(ids(result)).toEqual(Array.from({ length: 30 }, (_, index) => `id-${index + 101}`)); + expect(calls[0].params).toEqual({ limit: '100', page: '2' }); + }); + + it('get_all_project_templates shows more than the 20 rows of the default page', async () => { + const { z } = pagedZ(30, { defaultLimit: 20 }); + const result = await GetAllProjectTemplates.operation.perform(z, bundleFor({ space_id: 'folder-1' }, 0)); + expect(result).toHaveLength(30); + }); + + it('get_all_assignable_members shows more than the 20 rows of the default page', async () => { + const { z, calls } = pagedZ(30, { defaultLimit: 20 }); + const result = await GetAllAssignableMembers.operation.perform(z, bundleFor({ project_id: 'project-1' }, 0)); + expect(result).toHaveLength(30); + expect(calls[0].url).toBe('https://www.taskade.com/api/v1/projects/project-1/members'); + }); + + it('every page-numbered dropdown tells Zapier that it can load more rows', () => { + for (const trigger of [GetAllProjects, GetAllAgents, GetAllProjectTemplates, GetAllAssignableMembers]) { + const { canPaginate } = trigger.operation as { canPaginate?: boolean }; + expect({ key: trigger.key, canPaginate }).toEqual({ + key: trigger.key, + canPaginate: true, + }); + } + }); + + it('every page-numbered dropdown stops at the page bound', async () => { + const triggers = [ + { trigger: GetAllAgents, inputData: { space_id: 'folder-1' } }, + { trigger: GetAllProjectTemplates, inputData: { space_id: 'folder-1' } }, + { trigger: GetAllAssignableMembers, inputData: { project_id: 'project-1' } }, + ]; + for (const { trigger, inputData } of triggers) { + const { z, calls } = pagedZ(150, { ignorePage: true }); + expect(await trigger.operation.perform(z, bundleFor(inputData, MAX_PAGES))).toEqual([]); + expect(calls).toHaveLength(0); + } + }); +}); diff --git a/src/triggers/getAllAgents.ts b/src/triggers/getAllAgents.ts index 23f2a57..8afd082 100644 --- a/src/triggers/getAllAgents.ts +++ b/src/triggers/getAllAgents.ts @@ -1,14 +1,20 @@ import { Bundle, ZObject } from 'zapier-platform-core'; -import { ApiResponse, parseOk, request } from '../client'; +import { ApiResponse, dropdownPageParams, parseOk, request } from '../client'; import { spaceField } from '../fields'; const perform = async (z: ZObject, bundle: Bundle) => { + // The route returns 20 agents by default. Ask for 100, and read the page that + // Zapier asks for when the user clicks "Load more". + const params = dropdownPageParams(bundle); + if (params == null) { + return []; + } + const response = await request(z, bundle, { method: 'GET', path: `/folders/${bundle.inputData.space_id}/agents`, - // The route returns 20 agents by default. Ask for more so older agents show too. - params: { limit: '100' }, + params, }); const data: ApiResponse = parseOk(z, response.json); @@ -28,6 +34,7 @@ export default { { key: 'id', label: 'ID' }, { key: 'title', label: 'Name' }, ], + canPaginate: true, }, key: 'get_all_agents', noun: 'Agents', diff --git a/src/triggers/getAllAssignableMembers.ts b/src/triggers/getAllAssignableMembers.ts index dbc0b6a..ec0a514 100644 --- a/src/triggers/getAllAssignableMembers.ts +++ b/src/triggers/getAllAssignableMembers.ts @@ -1,5 +1,7 @@ import { Bundle, ZObject } from 'zapier-platform-core'; +import { dropdownPageParams } from '../client'; + interface ProjectMember { handle: string; displayName: string; @@ -18,6 +20,13 @@ interface ProjectMembersResponseError { type ProjectMembersResponse = ProjectMembersResponseOk | ProjectMembersResponseError; const perform = async (z: ZObject, bundle: Bundle) => { + // The route returns 20 members by default. Ask for 100, and read the page + // that Zapier asks for when the user clicks "Load more". + const params = dropdownPageParams(bundle); + if (params == null) { + return []; + } + const response = await z.request({ url: `https://www.taskade.com/api/v1/projects/${bundle.inputData.project_id}/members`, method: 'GET', @@ -26,6 +35,7 @@ const perform = async (z: ZObject, bundle: Bundle) => { 'content-type': 'application/json', authorization: `Bearer ${bundle.authData.access_token}`, }, + params, }); const data: ProjectMembersResponse = response.json; @@ -59,6 +69,7 @@ export default { { key: 'id', label: 'Handle' }, { key: 'displayName', label: 'Display Name' }, ], + canPaginate: true, }, key: 'get_all_assignable_members', noun: 'Assignable Members', diff --git a/src/triggers/getAllProjectTemplates.ts b/src/triggers/getAllProjectTemplates.ts index f2fa092..74bbc4d 100644 --- a/src/triggers/getAllProjectTemplates.ts +++ b/src/triggers/getAllProjectTemplates.ts @@ -1,12 +1,20 @@ import { Bundle, ZObject } from 'zapier-platform-core'; -import { ApiResponse, parseOk, request } from '../client'; +import { ApiResponse, dropdownPageParams, parseOk, request } from '../client'; import { spaceField } from '../fields'; const perform = async (z: ZObject, bundle: Bundle) => { + // The route returns 20 templates by default. Ask for 100, and read the page + // that Zapier asks for when the user clicks "Load more". + const params = dropdownPageParams(bundle); + if (params == null) { + return []; + } + const response = await request(z, bundle, { method: 'GET', path: `/folders/${bundle.inputData.space_id}/project-templates`, + params, }); const data: ApiResponse = parseOk(z, response.json); @@ -26,6 +34,7 @@ export default { { key: 'id', label: 'ID' }, { key: 'title', label: 'Title' }, ], + canPaginate: true, }, key: 'get_all_project_templates', noun: 'Project Templates', diff --git a/src/triggers/getAllProjects.ts b/src/triggers/getAllProjects.ts index b5f29ad..6660a63 100644 --- a/src/triggers/getAllProjects.ts +++ b/src/triggers/getAllProjects.ts @@ -1,5 +1,7 @@ import { Bundle, ZObject } from 'zapier-platform-core'; +import { dropdownPageParams } from '../client'; + interface Project { id: string; name: string; @@ -19,6 +21,13 @@ type ProjectsResponse = ProjectsResponseOk | ProjectsResponseError; const perform = async (z: ZObject, bundle: Bundle) => { if (bundle.inputData.space_id == null) { + // GET /me/projects is page-numbered. Zapier asks for the next page when the + // user clicks "Load more", so read the page that Zapier asks for. + const params = dropdownPageParams(bundle); + if (params == null) { + return []; + } + const response = await z.request({ url: `https://www.taskade.com/api/v1/me/projects`, method: 'GET', @@ -27,6 +36,7 @@ const perform = async (z: ZObject, bundle: Bundle) => { 'content-type': 'application/json', authorization: `Bearer ${bundle.authData.access_token}`, }, + params, }); const data: ProjectsResponse = response.json; @@ -38,6 +48,13 @@ const perform = async (z: ZObject, bundle: Bundle) => { return data.items.map((i) => ({ id: i.id, title: i.name })); } + // GET /folders/{folderId}/projects takes no page parameter, so a later page + // has no new projects. Return no rows for it, so "Load more" does not show + // the same projects again. + if ((bundle.meta?.page ?? 0) > 0) { + return []; + } + const response = await z.request({ url: `https://www.taskade.com/api/v1/folders/${bundle.inputData.space_id}/projects`, method: 'GET',