Skip to content

Commit 232b708

Browse files
authored
fix(client): set last-event-id header when sending (#266)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Refactor** - Enhanced the handling of event identifier headers by improving the merging process for new and existing values. This update ensures that event identifiers are combined reliably without unintended modifications. - **Tests** - Introduced comprehensive validations to confirm the correct behavior of header merging under various scenarios, further bolstering system reliability. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 8900489 commit 232b708

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

packages/client/src/adapters/standard/rpc-link-codec.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,47 @@ describe('standardRPCLinkCodec', () => {
9797
expect(serializeSpy).toBeCalledTimes(1)
9898
expect(serializeSpy).toBeCalledWith(input)
9999
})
100+
101+
describe('last-event-id', async () => {
102+
it('should set', async () => {
103+
const codec = new StandardRPCLinkCodec(serializer, {
104+
url: 'http://localhost:3000',
105+
method,
106+
headers: () => ({ 'x-custom-header': 'custom-value' }),
107+
})
108+
109+
const request = await codec.encode(['test'], 'input', { context: {}, lastEventId: '1' })
110+
111+
expect(request.headers['x-custom-header']).toEqual('custom-value')
112+
expect(request.headers['last-event-id']).toEqual('1')
113+
})
114+
115+
it('should merged', async () => {
116+
const codec = new StandardRPCLinkCodec(serializer, {
117+
url: 'http://localhost:3000',
118+
method,
119+
headers: () => ({ 'x-custom-header': 'custom-value', 'last-event-id': '2' }),
120+
})
121+
122+
const request = await codec.encode(['test'], 'input', { context: {}, lastEventId: '1' })
123+
124+
expect(request.headers['x-custom-header']).toEqual('custom-value')
125+
expect(request.headers['last-event-id']).toEqual(['2', '1'])
126+
})
127+
128+
it('should merged 2', async () => {
129+
const codec = new StandardRPCLinkCodec(serializer, {
130+
url: 'http://localhost:3000',
131+
method,
132+
headers: () => ({ 'x-custom-header': 'custom-value', 'last-event-id': ['2', '3'] }),
133+
})
134+
135+
const request = await codec.encode(['test'], 'input', { context: {}, lastEventId: '1' })
136+
137+
expect(request.headers['x-custom-header']).toEqual('custom-value')
138+
expect(request.headers['last-event-id']).toEqual(['2', '3', '1'])
139+
})
140+
})
100141
})
101142

102143
describe('decode', () => {

packages/client/src/adapters/standard/rpc-link-codec.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,22 @@ export class StandardRPCLinkCodec<T extends ClientContext> implements StandardLi
7777

7878
async encode(path: readonly string[], input: unknown, options: ClientOptionsOut<any>): Promise<StandardRequest> {
7979
const expectedMethod = await value(this.expectedMethod, options, path, input)
80-
const headers = await value(this.headers, options, path, input)
80+
const headers = { ...await value(this.headers, options, path, input) }
8181
const baseUrl = await value(this.baseUrl, options, path, input)
8282
const url = new URL(`${trim(baseUrl.toString(), '/')}/${path.map(encodeURIComponent).join('/')}`)
8383

84+
if (options.lastEventId !== undefined) {
85+
if (Array.isArray(headers['last-event-id'])) {
86+
headers['last-event-id'] = [...headers['last-event-id'], options.lastEventId]
87+
}
88+
else if (headers['last-event-id'] !== undefined) {
89+
headers['last-event-id'] = [headers['last-event-id'], options.lastEventId]
90+
}
91+
else {
92+
headers['last-event-id'] = options.lastEventId
93+
}
94+
}
95+
8496
const serialized = this.serializer.serialize(input)
8597

8698
if (

0 commit comments

Comments
 (0)