From 875ba92f6d3076170656b6a789b533fc6cd26e2b Mon Sep 17 00:00:00 2001 From: Scott Lovegrove Date: Thu, 3 Dec 2026 12:34:28 +0000 Subject: [PATCH 1/3] feat(channels): set and read channel icons by name The API stores a channel's icon as a name (icon_name) and ignores the old integer icon: writes to it are dropped and it always reads back 0. Add iconName to Channel, CreateChannelArgs and UpdateChannelArgs, export CHANNEL_ICON_NAMES and ChannelIconName as a hint list (the schemas accept any string, as the backend appends icons over time), and mark the integer icon as deprecated. Also make name optional on UpdateChannelArgs: the endpoint takes a partial update, so changing only the icon or colour no longer needs the current name sent back. --- src/clients/channels-client.test.ts | 75 +++++++++++++++++++++++++++++ src/clients/channels-client.ts | 12 ++++- src/types/entities.ts | 7 +++ src/types/enums.ts | 63 ++++++++++++++++++++++++ src/types/requests.ts | 8 ++- 5 files changed, 163 insertions(+), 2 deletions(-) diff --git a/src/clients/channels-client.test.ts b/src/clients/channels-client.test.ts index 02370c0..b5de39c 100644 --- a/src/clients/channels-client.test.ts +++ b/src/clients/channels-client.test.ts @@ -1,6 +1,7 @@ import { http, HttpResponse } from 'msw' import { describe, expect, it } from 'vitest' import { CommsApi } from '../comms-api' +import { CHANNEL_ICON_NAMES } from '../index' import { server } from '../testUtils/msw-setup' import { TEST_API_BASE_URL as BASE, @@ -63,4 +64,78 @@ 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('keeps an icon name this SDK does not list yet', async () => { + server.use( + http.get(`${BASE}/channels/getone`, () => + HttpResponse.json({ ...CHANNEL_RESPONSE, icon_name: 'rocket' }), + ), + ) + + const api = new CommsApi(TEST_API_TOKEN) + const channel = await api.channels.getChannel(TEST_CHANNEL_ID) + + expect(CHANNEL_ICON_NAMES).not.toContain('rocket') + expect(channel.iconName).toBe('rocket') + }) + + it('lists the icon names the API accepts', () => { + expect(CHANNEL_ICON_NAMES).toContain('heart') + expect(CHANNEL_ICON_NAMES).toContain('apple') + expect(CHANNEL_ICON_NAMES).toHaveLength(49) + 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..7c7eb66 100644 --- a/src/types/entities.ts +++ b/src/types/entities.ts @@ -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 newer API can return a name this SDK doesn't list yet; + * show the default for one you don't recognise. + */ + iconName: z.string().nullable().optional(), 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..51b9a28 100644 --- a/src/types/enums.ts +++ b/src/types/enums.ts @@ -46,6 +46,69 @@ 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. The backend only ever +// appends to this list, and a newer server can return a name this SDK doesn't +// list yet, so the schemas accept any string and the const array is a hint +// for autocomplete and for callers that validate input. `null` is the +// default `#` icon. 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. Any string accepted; the listed values are the ones + * the backend accepts today. + */ +export type ChannelIconName = (typeof CHANNEL_ICON_NAMES)[number] | (string & {}) + // 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..66ecc65 100644 --- a/src/types/requests.ts +++ b/src/types/requests.ts @@ -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.string().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.string().nullable().optional(), }) export type UpdateChannelArgs = z.infer From 4d5a6415394622d6c2bfe6d72724e91145c9699f Mon Sep 17 00:00:00 2001 From: Scott Lovegrove Date: Thu, 3 Dec 2026 12:40:15 +0000 Subject: [PATCH 2/3] test(channels): keep icon name tests stable as the backend adds icons Use a name that can never be a real icon for the unlisted-name case, and drop the containment and length checks on CHANNEL_ICON_NAMES, which would need editing each time the backend appends an icon. --- src/clients/channels-client.test.ts | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/clients/channels-client.test.ts b/src/clients/channels-client.test.ts index b5de39c..b6e4879 100644 --- a/src/clients/channels-client.test.ts +++ b/src/clients/channels-client.test.ts @@ -121,21 +121,18 @@ describe('ChannelsClient — icon names in responses', () => { it('keeps an icon name this SDK does not list yet', async () => { server.use( http.get(`${BASE}/channels/getone`, () => - HttpResponse.json({ ...CHANNEL_RESPONSE, icon_name: 'rocket' }), + 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_ICON_NAMES).not.toContain('rocket') - expect(channel.iconName).toBe('rocket') + expect(CHANNEL_ICON_NAMES).not.toContain('icon-from-a-newer-api') + expect(channel.iconName).toBe('icon-from-a-newer-api') }) - it('lists the icon names the API accepts', () => { - expect(CHANNEL_ICON_NAMES).toContain('heart') - expect(CHANNEL_ICON_NAMES).toContain('apple') - expect(CHANNEL_ICON_NAMES).toHaveLength(49) + it('lists each icon name once', () => { expect(new Set(CHANNEL_ICON_NAMES).size).toBe(CHANNEL_ICON_NAMES.length) }) }) From f22a321117dbf0e3ca961f324ce97efe617a0def Mon Sep 17 00:00:00 2001 From: Scott Lovegrove Date: Thu, 3 Dec 2026 12:45:32 +0000 Subject: [PATCH 3/3] feat(channels): type channel icon names as the listed names Build iconName from CHANNEL_ICON_NAMES in the request and Channel schemas, so an unlisted name fails type-check and ChannelIconName is the plain union. A name a newer API returns that this SDK doesn't list yet reads as null, the default icon, rather than failing the parse, which is how the backend asks clients to show an icon they don't know. --- src/clients/channels-client.test.ts | 27 ++++++++++++++++++++++----- src/types/entities.ts | 8 ++++---- src/types/enums.ts | 17 +++++++---------- src/types/requests.ts | 6 +++--- 4 files changed, 36 insertions(+), 22 deletions(-) diff --git a/src/clients/channels-client.test.ts b/src/clients/channels-client.test.ts index b6e4879..e7a48be 100644 --- a/src/clients/channels-client.test.ts +++ b/src/clients/channels-client.test.ts @@ -1,7 +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 } from '../index' +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, @@ -118,7 +123,7 @@ describe('ChannelsClient — wire serialization', () => { }) describe('ChannelsClient — icon names in responses', () => { - it('keeps an icon name this SDK does not list yet', async () => { + 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' }), @@ -128,8 +133,20 @@ describe('ChannelsClient — icon names in responses', () => { const api = new CommsApi(TEST_API_TOKEN) const channel = await api.channels.getChannel(TEST_CHANNEL_ID) - expect(CHANNEL_ICON_NAMES).not.toContain('icon-from-a-newer-api') - expect(channel.iconName).toBe('icon-from-a-newer-api') + 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', () => { diff --git a/src/types/entities.ts b/src/types/entities.ts index 7c7eb66..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 @@ -173,10 +173,10 @@ export const ChannelObjectSchema = z.object({ icon: z.number().nullable().optional(), /** * The channel's icon, one of `CHANNEL_ICON_NAMES`, or `null` for the - * default `#`. A newer API can return a name this SDK doesn't list yet; - * show the default for one you don't recognise. + * default `#`. A name this SDK doesn't list yet, from a newer API, reads + * as `null` rather than failing the parse. */ - iconName: z.string().nullable().optional(), + 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 51b9a28..55f34d0 100644 --- a/src/types/enums.ts +++ b/src/types/enums.ts @@ -46,11 +46,11 @@ 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. The backend only ever -// appends to this list, and a newer server can return a name this SDK doesn't -// list yet, so the schemas accept any string and the const array is a hint -// for autocomplete and for callers that validate input. `null` is the -// default `#` icon. The names match Todoist's project icons. +// 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', @@ -103,11 +103,8 @@ export const CHANNEL_ICON_NAMES = [ 'apple', ] as const -/** - * A channel icon name. Any string accepted; the listed values are the ones - * the backend accepts today. - */ -export type ChannelIconName = (typeof CHANNEL_ICON_NAMES)[number] | (string & {}) +/** 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`. diff --git a/src/types/requests.ts b/src/types/requests.ts index 66ecc65..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 @@ -25,7 +25,7 @@ export const CreateChannelArgsSchema = z.object({ /** @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.string().nullable().optional(), + iconName: z.enum(CHANNEL_ICON_NAMES).nullable().optional(), }) export type CreateChannelArgs = z.infer @@ -43,7 +43,7 @@ export const UpdateChannelArgsSchema = z.object({ /** @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.string().nullable().optional(), + iconName: z.enum(CHANNEL_ICON_NAMES).nullable().optional(), }) export type UpdateChannelArgs = z.infer