Skip to content
Merged
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
91 changes: 90 additions & 1 deletion src/clients/channels-client.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -63,4 +69,87 @@ describe('ChannelsClient — wire serialization', () => {
})
},
)

it('createChannel sends iconName as icon_name and returns it as iconName', async () => {
let body: Record<string, unknown> | undefined
server.use(
http.post(`${BASE}/channels/add`, async ({ request }) => {
body = (await request.json()) as Record<string, unknown>
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<string, unknown> | undefined
server.use(
http.post(`${BASE}/channels/update`, async ({ request }) => {
body = (await request.json()) as Record<string, unknown>
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<string, unknown> | undefined
server.use(
http.post(`${BASE}/channels/update`, async ({ request }) => {
body = (await request.json()) as Record<string, unknown>
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<ChannelIconName>().toEqualTypeOf<(typeof CHANNEL_ICON_NAMES)[number]>()
expectTypeOf<Channel['iconName']>().toEqualTypeOf<ChannelIconName | null | undefined>()

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)
})
})
12 changes: 11 additions & 1 deletion src/clients/channels-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<Channel> {
return this.simple('POST', 'update', { ...args }, this.channelSchema)
Expand Down
9 changes: 8 additions & 1 deletion src/types/entities.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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(),
})
Expand Down
60 changes: 60 additions & 0 deletions src/types/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions src/types/requests.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -22,22 +22,28 @@ 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<typeof CreateChannelArgsSchema>

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(),
useDefaultRecipients: z.boolean().nullable().optional(),
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<typeof UpdateChannelArgsSchema>
Expand Down
Loading