diff --git a/src/clients/inbox-client.test.ts b/src/clients/inbox-client.test.ts new file mode 100644 index 0000000..d074298 --- /dev/null +++ b/src/clients/inbox-client.test.ts @@ -0,0 +1,117 @@ +import { HttpResponse, http } from 'msw' +import { apiUrl } from '../testUtils/msw-handlers' +import { server } from '../testUtils/msw-setup' +import { TEST_API_TOKEN } from '../testUtils/test-defaults' +import { InboxClient } from './inbox-client' + +describe('InboxClient', () => { + let client: InboxClient + + beforeEach(() => { + client = new InboxClient({ apiToken: TEST_API_TOKEN }) + }) + + describe('getInbox', () => { + it('should send newerThan as newer_than_ts query parameter', async () => { + const date = new Date('2024-06-15T12:00:00Z') + const expectedTs = Math.floor(date.getTime() / 1000) + + server.use( + http.get(apiUrl('api/v3/inbox/get'), ({ request }) => { + const url = new URL(request.url) + expect(url.searchParams.get('newer_than_ts')).toBe(String(expectedTs)) + expect(url.searchParams.get('workspace_id')).toBe('123') + return HttpResponse.json([]) + }), + ) + + await client.getInbox({ workspaceId: 123, newerThan: date }) + }) + + it('should send olderThan as older_than_ts query parameter', async () => { + const date = new Date('2024-06-15T12:00:00Z') + const expectedTs = Math.floor(date.getTime() / 1000) + + server.use( + http.get(apiUrl('api/v3/inbox/get'), ({ request }) => { + const url = new URL(request.url) + expect(url.searchParams.get('older_than_ts')).toBe(String(expectedTs)) + expect(url.searchParams.get('workspace_id')).toBe('123') + return HttpResponse.json([]) + }), + ) + + await client.getInbox({ workspaceId: 123, olderThan: date }) + }) + + it('should support deprecated since param as newer_than_ts', async () => { + const date = new Date('2024-06-15T12:00:00Z') + const expectedTs = Math.floor(date.getTime() / 1000) + + server.use( + http.get(apiUrl('api/v3/inbox/get'), ({ request }) => { + const url = new URL(request.url) + expect(url.searchParams.get('newer_than_ts')).toBe(String(expectedTs)) + expect(url.searchParams.get('workspace_id')).toBe('123') + return HttpResponse.json([]) + }), + ) + + await client.getInbox({ workspaceId: 123, since: date }) + }) + + it('should support deprecated until param as older_than_ts', async () => { + const date = new Date('2024-06-15T12:00:00Z') + const expectedTs = Math.floor(date.getTime() / 1000) + + server.use( + http.get(apiUrl('api/v3/inbox/get'), ({ request }) => { + const url = new URL(request.url) + expect(url.searchParams.get('older_than_ts')).toBe(String(expectedTs)) + expect(url.searchParams.get('workspace_id')).toBe('123') + return HttpResponse.json([]) + }), + ) + + await client.getInbox({ workspaceId: 123, until: date }) + }) + }) + + describe('archiveAll', () => { + it('should send olderThan as older_than_ts parameter', async () => { + const date = new Date('2024-06-15T12:00:00Z') + const expectedTs = Math.floor(date.getTime() / 1000) + + server.use( + http.post(apiUrl('api/v3/inbox/archive_all'), async ({ request }) => { + const body = await request.json() + expect(body).toEqual({ + workspace_id: 123, + older_than_ts: expectedTs, + }) + return HttpResponse.json(null) + }), + ) + + await client.archiveAll({ workspaceId: 123, olderThan: date }) + }) + + it('should support deprecated until param', async () => { + const date = new Date('2024-06-15T12:00:00Z') + const expectedTs = Math.floor(date.getTime() / 1000) + + server.use( + http.post(apiUrl('api/v3/inbox/archive_all'), async ({ request }) => { + const body = await request.json() + expect(body).toEqual({ + workspace_id: 123, + older_than_ts: expectedTs, + }) + return HttpResponse.json(null) + }), + ) + + await client.archiveAll({ workspaceId: 123, until: date }) + }) + }) +}) diff --git a/src/clients/inbox-client.ts b/src/clients/inbox-client.ts index e920e88..82a7d0b 100644 --- a/src/clients/inbox-client.ts +++ b/src/clients/inbox-client.ts @@ -20,8 +20,10 @@ export class InboxClient extends BaseClient { * * @param args - The arguments for getting inbox. * @param args.workspaceId - The workspace ID. - * @param args.since - Optional date to get items since. - * @param args.until - Optional date to get items until. + * @param args.newerThan - Optional date to get items newer than. + * @param args.olderThan - Optional date to get items older than. + * @param args.since - @deprecated Use `newerThan` instead. + * @param args.until - @deprecated Use `olderThan` instead. * @param args.limit - Optional limit on number of items returned. * @param args.cursor - Optional cursor for pagination. * @param options - Optional configuration. Set `batch: true` to return a descriptor for batch requests. @@ -31,7 +33,7 @@ export class InboxClient extends BaseClient { * ```typescript * const inbox = await api.inbox.getInbox({ * workspaceId: 123, - * since: new Date('2024-01-01') + * newerThan: new Date('2024-01-01') * }) * ``` */ @@ -45,8 +47,10 @@ export class InboxClient extends BaseClient { workspace_id: args.workspaceId, } - if (args.since) params.newer_than_ts = Math.floor(args.since.getTime() / 1000) - if (args.until) params.older_than_ts = Math.floor(args.until.getTime() / 1000) + const newerThan = args.newerThan ?? args.since + if (newerThan) params.newer_than_ts = Math.floor(newerThan.getTime() / 1000) + const olderThan = args.olderThan ?? args.until + if (olderThan) params.older_than_ts = Math.floor(olderThan.getTime() / 1000) if (args.limit) params.limit = args.limit if (args.cursor) params.cursor = args.cursor @@ -215,15 +219,15 @@ export class InboxClient extends BaseClient { * @param args - The arguments for archiving all. * @param args.workspaceId - The workspace ID. * @param args.channelIds - Optional array of channel IDs to filter by. - * @param args.since - Optional date to filter items since. - * @param args.until - Optional date to filter items until. + * @param args.olderThan - Optional date to filter items older than. + * @param args.until - @deprecated Use `olderThan` instead. * @param options - Optional configuration. Set `batch: true` to return a descriptor for batch requests. * * @example * ```typescript * await api.inbox.archiveAll({ * workspaceId: 123, - * since: new Date('2024-01-01') + * olderThan: new Date('2024-01-01') * }) * ``` */ @@ -238,8 +242,8 @@ export class InboxClient extends BaseClient { } if (args.channelIds) params.channel_ids = args.channelIds - if (args.since) params.since_ts_or_obj_idx = Math.floor(args.since.getTime() / 1000) - if (args.until) params.until_ts_or_obj_idx = Math.floor(args.until.getTime() / 1000) + const olderThan = args.olderThan ?? args.until + if (olderThan) params.older_than_ts = Math.floor(olderThan.getTime() / 1000) const method = 'POST' const url = `${ENDPOINT_INBOX}/archive_all` diff --git a/src/clients/threads-client.test.ts b/src/clients/threads-client.test.ts index ada83fc..e89f816 100644 --- a/src/clients/threads-client.test.ts +++ b/src/clients/threads-client.test.ts @@ -29,6 +29,70 @@ describe('ThreadsClient', () => { client = new ThreadsClient({ apiToken: TEST_API_TOKEN }) }) + describe('getThreads', () => { + it('should send newerThan as newer_than_ts query parameter', async () => { + const date = new Date('2024-06-15T12:00:00Z') + const expectedTs = Math.floor(date.getTime() / 1000) + + server.use( + http.get(apiUrl('api/v3/threads/get'), ({ request }) => { + const url = new URL(request.url) + expect(url.searchParams.get('newer_than_ts')).toBe(String(expectedTs)) + expect(url.searchParams.get('workspace_id')).toBe('1') + return HttpResponse.json([]) + }), + ) + + await client.getThreads({ workspaceId: 1, newerThan: date }) + }) + + it('should send olderThan as older_than_ts query parameter', async () => { + const date = new Date('2024-06-15T12:00:00Z') + const expectedTs = Math.floor(date.getTime() / 1000) + + server.use( + http.get(apiUrl('api/v3/threads/get'), ({ request }) => { + const url = new URL(request.url) + expect(url.searchParams.get('older_than_ts')).toBe(String(expectedTs)) + expect(url.searchParams.get('workspace_id')).toBe('1') + return HttpResponse.json([]) + }), + ) + + await client.getThreads({ workspaceId: 1, olderThan: date }) + }) + + it('should support deprecated newer_than_ts as newer_than_ts query parameter', async () => { + const expectedTs = 1718452800 + + server.use( + http.get(apiUrl('api/v3/threads/get'), ({ request }) => { + const url = new URL(request.url) + expect(url.searchParams.get('newer_than_ts')).toBe(String(expectedTs)) + expect(url.searchParams.get('workspace_id')).toBe('1') + return HttpResponse.json([]) + }), + ) + + await client.getThreads({ workspaceId: 1, newer_than_ts: expectedTs }) + }) + + it('should support deprecated older_than_ts as older_than_ts query parameter', async () => { + const expectedTs = 1718452800 + + server.use( + http.get(apiUrl('api/v3/threads/get'), ({ request }) => { + const url = new URL(request.url) + expect(url.searchParams.get('older_than_ts')).toBe(String(expectedTs)) + expect(url.searchParams.get('workspace_id')).toBe('1') + return HttpResponse.json([]) + }), + ) + + await client.getThreads({ workspaceId: 1, older_than_ts: expectedTs }) + }) + }) + describe('pinThread', () => { it('should pin a thread', async () => { server.use( diff --git a/src/clients/threads-client.ts b/src/clients/threads-client.ts index 41ca117..08b17c9 100644 --- a/src/clients/threads-client.ts +++ b/src/clients/threads-client.ts @@ -36,8 +36,10 @@ export class ThreadsClient extends BaseClient { * @param args.channelId - The channel ID. * @param args.workspaceId - Optional workspace ID. * @param args.archived - Optional flag to include archived threads. - * @param args.newer_than_ts - Optional timestamp to get threads newer than. - * @param args.older_than_ts - Optional timestamp to get threads older than. + * @param args.newerThan - Optional date to get threads newer than. + * @param args.olderThan - Optional date to get threads older than. + * @param args.newer_than_ts - @deprecated Use `newerThan` instead. + * @param args.older_than_ts - @deprecated Use `olderThan` instead. * @param args.limit - Optional limit on number of threads returned. * @param options - Optional configuration. Set `batch: true` to return a descriptor for batch requests. * @returns An array of thread objects. @@ -56,7 +58,14 @@ export class ThreadsClient extends BaseClient { ): Promise | BatchRequestDescriptor { const method = 'GET' const url = `${ENDPOINT_THREADS}/get` - const params = args + const { newerThan, olderThan, newer_than_ts, older_than_ts, ...rest } = args + const resolvedNewerThan = newerThan ? Math.floor(newerThan.getTime() / 1000) : newer_than_ts + const resolvedOlderThan = olderThan ? Math.floor(olderThan.getTime() / 1000) : older_than_ts + const params = { + ...rest, + ...(resolvedNewerThan != null ? { newer_than_ts: resolvedNewerThan } : {}), + ...(resolvedOlderThan != null ? { older_than_ts: resolvedOlderThan } : {}), + } if (options?.batch) { return { method, url, params, schema: z.array(ThreadSchema) } diff --git a/src/types/requests.ts b/src/types/requests.ts index 8063f1c..695b9e4 100644 --- a/src/types/requests.ts +++ b/src/types/requests.ts @@ -106,9 +106,22 @@ export const GetThreadsArgsSchema = z.object({ workspaceId: z.number(), channelId: z.number().nullable().optional(), archived: z.boolean().nullable().optional(), + newerThan: z.date().nullable().optional(), + olderThan: z.date().nullable().optional(), + limit: z.number().nullable().optional(), }) -export type GetThreadsArgs = z.infer +export type GetThreadsArgs = Omit< + z.infer, + 'newerThan' | 'olderThan' +> & { + newerThan?: Date | null + olderThan?: Date | null + /** @deprecated Use `newerThan` instead. */ + newer_than_ts?: number | null + /** @deprecated Use `olderThan` instead. */ + older_than_ts?: number | null +} export const GetCommentsArgsSchema = z.object({ threadId: z.number(), @@ -214,7 +227,11 @@ export type UpdateConversationMessageArgs = { // Inbox export type GetInboxArgs = { workspaceId: number + newerThan?: Date + olderThan?: Date + /** @deprecated Use `newerThan` instead. */ since?: Date + /** @deprecated Use `olderThan` instead. */ until?: Date limit?: number cursor?: string @@ -223,7 +240,8 @@ export type GetInboxArgs = { export type ArchiveAllArgs = { workspaceId: number channelIds?: number[] - since?: Date + olderThan?: Date + /** @deprecated Use `olderThan` instead. */ until?: Date }