diff --git a/src/clients/channels-client.test.ts b/src/clients/channels-client.test.ts index 02370c0..e7a48be 100644 --- a/src/clients/channels-client.test.ts +++ b/src/clients/channels-client.test.ts @@ -1,6 +1,12 @@ import { http, HttpResponse } from 'msw' -import { describe, expect, it } from 'vitest' +import { describe, expect, expectTypeOf, it } from 'vitest' import { CommsApi } from '../comms-api' +import { + CHANNEL_ICON_NAMES, + type Channel, + type ChannelIconName, + type CreateChannelArgs, +} from '../index' import { server } from '../testUtils/msw-setup' import { TEST_API_BASE_URL as BASE, @@ -63,4 +69,87 @@ describe('ChannelsClient — wire serialization', () => { }) }, ) + + it('createChannel sends iconName as icon_name and returns it as iconName', async () => { + let body: Record | undefined + server.use( + http.post(`${BASE}/channels/add`, async ({ request }) => { + body = (await request.json()) as Record + return HttpResponse.json({ ...CHANNEL_RESPONSE, icon_name: 'heart' }) + }), + ) + + const api = new CommsApi(TEST_API_TOKEN) + const channel = await api.channels.createChannel({ + workspaceId: 1, + name: 'General', + iconName: 'heart', + }) + + expect(body).toMatchObject({ icon_name: 'heart' }) + expect(channel.iconName).toBe('heart') + }) + + it('updateChannel sends only the fields given, without requiring name', async () => { + let body: Record | undefined + server.use( + http.post(`${BASE}/channels/update`, async ({ request }) => { + body = (await request.json()) as Record + return HttpResponse.json(CHANNEL_RESPONSE) + }), + ) + + const api = new CommsApi(TEST_API_TOKEN) + await api.channels.updateChannel({ id: TEST_CHANNEL_ID, iconName: 'airplane' }) + + expect(body).toEqual({ id: TEST_CHANNEL_ID, icon_name: 'airplane' }) + }) + + it('updateChannel sends icon_name: null to reset the icon to the default', async () => { + let body: Record | undefined + server.use( + http.post(`${BASE}/channels/update`, async ({ request }) => { + body = (await request.json()) as Record + return HttpResponse.json({ ...CHANNEL_RESPONSE, icon_name: null }) + }), + ) + + const api = new CommsApi(TEST_API_TOKEN) + const channel = await api.channels.updateChannel({ id: TEST_CHANNEL_ID, iconName: null }) + + expect(body).toEqual({ id: TEST_CHANNEL_ID, icon_name: null }) + expect(channel.iconName).toBeNull() + }) +}) + +describe('ChannelsClient — icon names in responses', () => { + it('reads an icon name this SDK does not list yet as the default icon', async () => { + server.use( + http.get(`${BASE}/channels/getone`, () => + HttpResponse.json({ ...CHANNEL_RESPONSE, icon_name: 'icon-from-a-newer-api' }), + ), + ) + + const api = new CommsApi(TEST_API_TOKEN) + const channel = await api.channels.getChannel(TEST_CHANNEL_ID) + + expect(channel.iconName).toBeNull() + }) + + it('types icon names as the listed names only', () => { + expectTypeOf().toEqualTypeOf<(typeof CHANNEL_ICON_NAMES)[number]>() + expectTypeOf().toEqualTypeOf() + + const args: CreateChannelArgs = { + workspaceId: 1, + name: 'General', + // @ts-expect-error Only the names in CHANNEL_ICON_NAMES are accepted. + iconName: 'icon-from-a-newer-api', + } + void args + }) + + it('lists each icon name once', () => { + expect(new Set(CHANNEL_ICON_NAMES).size).toBe(CHANNEL_ICON_NAMES.length) + }) }) diff --git a/src/clients/channels-client.ts b/src/clients/channels-client.ts index 2f6e093..14f915a 100644 --- a/src/clients/channels-client.ts +++ b/src/clients/channels-client.ts @@ -82,6 +82,7 @@ export class ChannelsClient extends BaseClient { * @param args.name - The channel name. * @param args.description - Optional channel description. * @param args.color - Optional channel color. + * @param args.iconName - Optional channel icon, one of `CHANNEL_ICON_NAMES`. Leave it out, or pass `null`, for the default `#`. * @param args.userIds - Optional array of user IDs to add to the channel. * @param args.public - Optional flag to make the channel public. * @returns The created channel object. @@ -105,15 +106,24 @@ export class ChannelsClient extends BaseClient { } /** - * Partial update of an existing channel. + * Partial update of an existing channel. Only the fields passed change. * * @param args - The arguments for updating a channel. * @param args.id - The channel ID. * @param args.name - Optional new channel name. * @param args.description - Optional new channel description. * @param args.color - Optional new channel color. + * @param args.iconName - Optional new channel icon, one of `CHANNEL_ICON_NAMES`, or `null` to reset it to the default `#`. * @param args.public - Optional flag to change channel visibility. * @returns The updated channel object. + * + * @example + * ```typescript + * const channel = await api.channels.updateChannel({ + * id: 'CQ7yksHFbYFrSPVTaHVCs', + * iconName: 'airplane', + * }) + * ``` */ updateChannel(args: UpdateChannelArgs): Promise { return this.simple('POST', 'update', { ...args }, this.channelSchema) diff --git a/src/types/entities.ts b/src/types/entities.ts index 33a38bd..3660932 100644 --- a/src/types/entities.ts +++ b/src/types/entities.ts @@ -1,6 +1,6 @@ import { z } from 'zod' import { getFullCommsURL } from '../utils/url-helpers' -import { USER_TYPES } from './enums' +import { CHANNEL_ICON_NAMES, USER_TYPES } from './enums' // EVERYONE / EVERYONE_IN_THREAD / GROUP_ID_MARKERS / GroupId / GroupIdMarker // are defined in `./enums` (re-exported through `./index`) to keep this @@ -169,7 +169,14 @@ export const ChannelObjectSchema = z.object({ defaultGroups: z.array(z.string()).nullable().optional(), defaultRecipients: z.array(z.number()).nullable().optional(), isFavorited: z.boolean().nullable().optional(), + /** @deprecated The API always returns `0` here. Read `iconName` instead. */ icon: z.number().nullable().optional(), + /** + * The channel's icon, one of `CHANNEL_ICON_NAMES`, or `null` for the + * default `#`. A name this SDK doesn't list yet, from a newer API, reads + * as `null` rather than failing the parse. + */ + iconName: z.enum(CHANNEL_ICON_NAMES).nullable().optional().catch(null), version: z.number(), filters: z.record(z.string(), z.string()).nullable().optional(), }) diff --git a/src/types/enums.ts b/src/types/enums.ts index 6d7bc52..55f34d0 100644 --- a/src/types/enums.ts +++ b/src/types/enums.ts @@ -46,6 +46,66 @@ export const WORKSPACE_PLANS = ['free', 'unlimited', 'business'] as const */ export type WorkspacePlan = (typeof WORKSPACE_PLANS)[number] | (string & {}) +// Channel icons, as `iconName` sends and returns them. `null` is the default +// `#` icon. The backend only ever appends to this list, so a newer server can +// return a name missing here: `Channel.iconName` reads one as `null`, the +// default, which is how the backend asks clients to show an icon they don't +// know. The names match Todoist's project icons. +export const CHANNEL_ICON_NAMES = [ + 'briefcase', + 'folder', + 'diagram', + 'headset', + 'microphone', + 'flag', + 'bullseye', + 'house', + 'heart', + 'airplane', + 'gift', + 'pin', + 'school_cap', + 'lightbulb', + 'star', + 'command_prompt', + 'laptop', + 'envelope', + 'calendar', + 'person', + 'sofa', + 'cup_and_heat_waves', + 'paintpalette', + 'broom', + 'sun_max', + 'moon', + 'beach_umbrella', + 'pizza', + 'checkmark_line_3', + 'basketball', + 'face_smiling', + 'dog', + 'dollarsign', + 'smartphone', + 'person_2', + 'chart_line_uptrend_xyaxis_in_square', + 'laboratory_flask', + 'telescope', + 'book', + 'document', + 'sparkles', + 'video', + 'paintbrush', + 'puzzlepiece_extension', + 'clock', + 'pills', + 'birthday_cake', + 'stethoscope', + 'apple', +] as const + +/** A channel icon name, one of {@link CHANNEL_ICON_NAMES}. */ +export type ChannelIconName = (typeof CHANNEL_ICON_NAMES)[number] + // Audiences that comment-creating endpoints can target alongside (or instead // of) individual `recipients` / custom `groups`. export const NOTIFY_AUDIENCES = ['channel', 'thread'] as const diff --git a/src/types/requests.ts b/src/types/requests.ts index fdded48..70ee96e 100644 --- a/src/types/requests.ts +++ b/src/types/requests.ts @@ -1,6 +1,6 @@ import { z } from 'zod' import { type Attachment, RequestAttachmentSchema } from './entities' -import { NOTIFY_AUDIENCES } from './enums' +import { CHANNEL_ICON_NAMES, NOTIFY_AUDIENCES } from './enums' /** * Create-side requests require an `id` on the wire. The SDK clients accept @@ -22,14 +22,17 @@ export const CreateChannelArgsSchema = z.object({ defaultGroups: z.array(z.string()).nullable().optional(), defaultRecipients: z.array(z.number()).nullable().optional(), isFavorited: z.boolean().nullable().optional(), + /** @deprecated The API ignores this. Set `iconName` instead. */ icon: z.number().nullable().optional(), + /** One of `CHANNEL_ICON_NAMES`, or `null` for the default `#`. */ + iconName: z.enum(CHANNEL_ICON_NAMES).nullable().optional(), }) export type CreateChannelArgs = z.infer export const UpdateChannelArgsSchema = z.object({ id: z.string(), - name: z.string(), + name: z.string().optional(), color: z.number().nullable().optional(), public: z.boolean().nullable().optional(), description: z.string().nullable().optional(), @@ -37,7 +40,10 @@ export const UpdateChannelArgsSchema = z.object({ defaultGroups: z.array(z.string()).nullable().optional(), defaultRecipients: z.array(z.number()).nullable().optional(), isFavorited: z.boolean().nullable().optional(), + /** @deprecated The API ignores this. Set `iconName` instead. */ icon: z.number().nullable().optional(), + /** One of `CHANNEL_ICON_NAMES`, or `null` to reset to the default `#`. */ + iconName: z.enum(CHANNEL_ICON_NAMES).nullable().optional(), }) export type UpdateChannelArgs = z.infer