-
Notifications
You must be signed in to change notification settings - Fork 83
feat(client): support custom_set and custom_unset in batch channel update #1856
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
952c8d8
feat(client): support custom_set and custom_unset in batch channel up…
kanat 814a1db
feat(client): add channelBatchUpdater().updateCustom
kanat 6da2bb6
refactor(client): use updateData for custom batch patches
kanat 049259b
refactor(client): simplify batch update signature
kanat e8a082c
fix(client): reject mixed batch update arguments
kanat 5512b21
fix(client): whitelist batch update option fields
kanat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import { StreamChat } from '../../src/client'; | ||
| import type { | ||
| APIResponse, | ||
| UpdateChannelsBatchOptions, | ||
| UpdateChannelsBatchResponse, | ||
| } from '../../src/types'; | ||
|
|
||
| describe('updateChannelsBatch', () => { | ||
| let client: StreamChat; | ||
| let putSpy: ReturnType<typeof vi.spyOn>; | ||
|
|
||
| const mockResponse: APIResponse & UpdateChannelsBatchResponse = { | ||
| duration: '0.01s', | ||
| result: {}, | ||
| task_id: 'task-id', | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| client = new StreamChat('api_key', 'api_secret'); | ||
| putSpy = vi.spyOn(client, 'put').mockResolvedValue(mockResponse); | ||
| }); | ||
|
|
||
| it('sends custom_set and custom_unset at the request root', async () => { | ||
| const options: UpdateChannelsBatchOptions = { | ||
| operation: 'updateData', | ||
| filter: { cids: { $in: ['messaging:a', 'messaging:b'] } }, | ||
| custom_set: { group: 'old', 'expiration.value': 3 }, | ||
| custom_unset: ['location_id'], | ||
| }; | ||
|
|
||
| await client.updateChannelsBatch(options); | ||
|
|
||
| expect(putSpy).toHaveBeenCalledWith(`${client.baseURL}/channels/batch`, { | ||
| operation: 'updateData', | ||
| filter: { cids: { $in: ['messaging:a', 'messaging:b'] } }, | ||
| custom_set: { group: 'old', 'expiration.value': 3 }, | ||
| custom_unset: ['location_id'], | ||
| }); | ||
|
|
||
| // The two fields are siblings of `operation` and `filter`. `data` is an | ||
| // extra-fields sink on the v1 routes, so a `custom_set` key sent inside it | ||
| // would mean "replace custom with a key named custom_set" instead. | ||
| const body = putSpy.mock.calls[0][1] as UpdateChannelsBatchOptions; | ||
| expect(Object.keys(body)).toContain('custom_set'); | ||
| expect(Object.keys(body)).toContain('custom_unset'); | ||
| expect(body.data).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('omits custom_set and custom_unset when they are not provided', async () => { | ||
| const options: UpdateChannelsBatchOptions = { | ||
| operation: 'updateData', | ||
| filter: { types: { $eq: 'messaging' } }, | ||
| data: { frozen: true }, | ||
| }; | ||
|
|
||
| await client.updateChannelsBatch(options); | ||
|
|
||
| const body = putSpy.mock.calls[0][1] as UpdateChannelsBatchOptions; | ||
| expect(body).not.toHaveProperty('custom_set'); | ||
| expect(body).not.toHaveProperty('custom_unset'); | ||
| }); | ||
|
|
||
| it('keeps the existing channelBatchUpdater().updateData data argument', async () => { | ||
| await client | ||
| .channelBatchUpdater() | ||
| .updateData({ cids: { $eq: 'messaging:a' } }, { frozen: true }); | ||
|
|
||
| expect(putSpy).toHaveBeenCalledWith(`${client.baseURL}/channels/batch`, { | ||
| operation: 'updateData', | ||
| filter: { cids: { $eq: 'messaging:a' } }, | ||
| data: { frozen: true }, | ||
| }); | ||
| }); | ||
|
|
||
| it('forwards a custom patch from channelBatchUpdater().updateData to the root', async () => { | ||
| await client.channelBatchUpdater().updateData( | ||
| { cids: { $eq: 'messaging:a' } }, | ||
| { | ||
| custom_set: { group: 'new' }, | ||
| custom_unset: ['location_id'], | ||
| }, | ||
| ); | ||
|
|
||
| const body = putSpy.mock.calls[0][1] as UpdateChannelsBatchOptions; | ||
| expect(body.operation).toBe('updateData'); | ||
| expect(body.custom_set).toEqual({ group: 'new' }); | ||
| expect(body.custom_unset).toEqual(['location_id']); | ||
| expect(body).not.toHaveProperty('data'); | ||
| }); | ||
|
|
||
| it('updates channel data and custom fields in one updateData call', async () => { | ||
| await client.channelBatchUpdater().updateData( | ||
| { cids: { $in: ['messaging:a', 'messaging:b'] } }, | ||
| { | ||
| data: { frozen: true }, | ||
| custom_set: { group: 'new', 'expiration.value': 3 }, | ||
| custom_unset: ['location_id'], | ||
| }, | ||
| ); | ||
|
|
||
| expect(putSpy).toHaveBeenCalledWith(`${client.baseURL}/channels/batch`, { | ||
| operation: 'updateData', | ||
| filter: { cids: { $in: ['messaging:a', 'messaging:b'] } }, | ||
| data: { frozen: true }, | ||
| custom_set: { group: 'new', 'expiration.value': 3 }, | ||
| custom_unset: ['location_id'], | ||
| }); | ||
| }); | ||
|
|
||
| it('does not let update options override the operation or filter', async () => { | ||
| const filter = { cids: { $eq: 'messaging:a' } } as const; | ||
| const options = { | ||
| operation: 'hide' as const, | ||
| filter: { cids: { $eq: 'messaging:b' } } as const, | ||
| data: { frozen: true }, | ||
| }; | ||
|
|
||
| await client.channelBatchUpdater().updateData(filter, options); | ||
|
|
||
| expect(putSpy).toHaveBeenCalledWith(`${client.baseURL}/channels/batch`, { | ||
| operation: 'updateData', | ||
| filter, | ||
| data: { frozen: true }, | ||
| }); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The union type lets a mixed literal compile, and the mixed call then silently drops the channel-data fields.
TypeScript relaxes the excess-property check across a union:
frozenis known toBatchChannelDataUpdate,custom_settoChannelBatchDataUpdateOptions, so the literal passes (checked withtsc --strict). At runtimeisChannelBatchDataUpdateOptionsseescustom_setand returns true, so the whole object is spread at the request root andfrozengoes out as a root-level key. The request root has no extra-fields sink, so the server ignores it: the caller gets a task_id back andfrozennever applies.Two real overloads on
updateData, one per argument shape, reject that literal at compile time (TS2769) while every valid call in the tests still type-checks.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in e8a082cb — restored the two public overloads and added a declaration test that rejects the mixed literal.