Skip to content

Commit 93e6063

Browse files
authored
feat!: update event-iterator options (#199)
* update event-source ping options * wip * make LinkFetchClientOptions extends ToFetchRequestOptions * sync
1 parent d16a1b6 commit 93e6063

File tree

39 files changed

+141
-127
lines changed

39 files changed

+141
-127
lines changed

apps/content/docs/client/event-iterator.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ for await (const event of iterator) {
4949
If an error occurs during streaming, oRPC will attempt retries based on your configuration. It can retry multiple times until the specified limit is reached, after which the error will be thrown.
5050

5151
::: info
52-
If you're using [RPCLink](/docs/client/rpc-link), you can customize the retry behavior [here](/docs/client/rpc-link#event-source-configuration).
52+
If you're using [RPCLink](/docs/client/rpc-link), you can customize the retry behavior [here](/docs/client/rpc-link#event-iterator-configuration).
5353
:::
5454

5555
```ts

apps/content/docs/client/rpc-link.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,22 +118,22 @@ const link = new RPCLink<ClientContext>({
118118
You should disable event iterator retries when streaming results from a chatbot AI.
119119
:::
120120

121-
## Event-Source Ping Interval
121+
## Event Iterator Keep Alive
122122

123-
To keep EventSource connections alive (the mechanism behind [Event Iterator](/docs/event-iterator)), `RPCLink` periodically sends a ping comment to the server. You can configure this behavior using the following options:
123+
To keep [Event Iterator](/docs/event-iterator) connections alive, `RPCLink` periodically sends a ping comment to the server. You can configure this behavior using the following options:
124124

125-
- `eventSourcePingEnabled` (default: `true`) – Enables or disables pings.
126-
- `eventSourcePingInterval` (default: `5000`) – Time between pings (in milliseconds).
127-
- `eventSourcePingContent` (default: `''`) – Custom content for ping messages.
125+
- `eventIteratorKeepAliveEnabled` (default: `true`) – Enables or disables pings.
126+
- `eventIteratorKeepAliveInterval` (default: `5000`) – Time between pings (in milliseconds).
127+
- `eventIteratorKeepAliveComment` (default: `''`) – Custom content for ping messages.
128128

129129
```ts
130130
const link = new RPCLink({
131-
eventSourcePingEnabled: true,
132-
eventSourcePingInterval: 5000, // 5 seconds
133-
eventSourcePingContent: '',
131+
eventIteratorKeepAliveEnabled: true,
132+
eventIteratorKeepAliveInterval: 5000, // 5 seconds
133+
eventIteratorKeepAliveComment: '',
134134
})
135135
```
136136

137137
:::warning
138-
These options for sending [Event Iterator](/docs/event-iterator) from client to the server, not from the server to client as used in [RPCHandler](/docs/rpc-handler#event-source-ping-interval) or [OpenAPIHandler](/docs/openapi/openapi-handler#event-source-ping-interval).
138+
These options for sending [Event Iterator](/docs/event-iterator) from client to the server, not from the server to client as used in [RPCHandler](/docs/rpc-handler#event-iterator-keep-alive) or [OpenAPIHandler](/docs/openapi/openapi-handler#event-iterator-keep-alive).
139139
:::

apps/content/docs/openapi/openapi-handler.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,18 @@ export default async function fetch(request: Request) {
8787
}
8888
```
8989

90-
## Event-Source Ping Interval
90+
## Event Iterator Keep Alive
9191

92-
To keep EventSource connections alive (the mechanism behind [Event Iterator](/docs/event-iterator)), `OpenAPIHandler` periodically sends a ping comment to the client. You can configure this behavior using the following options:
92+
To keep [Event Iterator](/docs/event-iterator) connections alive, `OpenAPIHandler` periodically sends a ping comment to the client. You can configure this behavior using the following options:
9393

94-
- `eventSourcePingEnabled` (default: `true`) – Enables or disables pings.
95-
- `eventSourcePingInterval` (default: `5000`) – Time between pings (in milliseconds).
96-
- `eventSourcePingContent` (default: `''`) – Custom content for ping messages.
94+
- `eventIteratorKeepAliveEnabled` (default: `true`) – Enables or disables pings.
95+
- `eventIteratorKeepAliveInterval` (default: `5000`) – Time between pings (in milliseconds).
96+
- `eventIteratorKeepAliveComment` (default: `''`) – Custom content for ping messages.
9797

9898
```ts
9999
const result = await handler.handle(request, {
100-
eventSourcePingEnabled: true,
101-
eventSourcePingInterval: 5000, // 5 seconds
102-
eventSourcePingContent: '',
100+
eventIteratorKeepAliveEnabled: true,
101+
eventIteratorKeepAliveInterval: 5000, // 5 seconds
102+
eventIteratorKeepAliveComment: '',
103103
})
104104
```

apps/content/docs/rpc-handler.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,18 @@ export default async function fetch(request: Request) {
6666
}
6767
```
6868

69-
## Event-Source Ping Interval
69+
## Event-Iterator Keep Alive
7070

71-
To keep EventSource connections alive (the mechanism behind [Event Iterator](/docs/event-iterator)), `RPCHandler` periodically sends a ping comment to the client. You can configure this behavior using the following options:
71+
To keep [Event Iterator](/docs/event-iterator) connections alive, `RPCHandler` periodically sends a ping comment to the client. You can configure this behavior using the following options:
7272

73-
- `eventSourcePingEnabled` (default: `true`) – Enables or disables pings.
74-
- `eventSourcePingInterval` (default: `5000`) – Time between pings (in milliseconds).
75-
- `eventSourcePingContent` (default: `''`) – Custom content for ping messages.
73+
- `eventIteratorKeepAliveEnabled` (default: `true`) – Enables or disables pings.
74+
- `eventIteratorKeepAliveInterval` (default: `5000`) – Time between pings (in milliseconds).
75+
- `eventIteratorKeepAliveComment` (default: `''`) – Custom content for ping comments.
7676

7777
```ts
7878
const result = await handler.handle(request, {
79-
eventSourcePingEnabled: true,
80-
eventSourcePingInterval: 5000, // 5 seconds
81-
eventSourcePingContent: '',
79+
eventIteratorKeepAliveEnabled: true,
80+
eventIteratorKeepAliveInterval: 5000, // 5 seconds
81+
eventIteratorKeepAliveComment: '',
8282
})
8383
```

packages/client/src/adapters/fetch/link-fetch-client.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('linkFetchClient', () => {
3535
expect(toStandardLazyResponseSpy).toBeCalledWith(await fetch.mock.results[0]!.value)
3636

3737
expect(toFetchRequestSpy).toBeCalledTimes(1)
38-
expect(toFetchRequestSpy).toBeCalledWith(standardRequest)
38+
expect(toFetchRequestSpy).toBeCalledWith(standardRequest, { fetch })
3939

4040
expect(fetch).toBeCalledTimes(1)
4141
expect(fetch).toBeCalledWith(

packages/client/src/adapters/fetch/link-fetch-client.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import type { StandardLazyResponse, StandardRequest } from '@orpc/standard-server'
2+
import type { ToFetchRequestOptions } from '@orpc/standard-server-fetch'
23
import type { ClientContext, ClientOptionsOut } from '../../types'
34
import type { StandardLinkClient } from '../standard'
45
import { toFetchRequest, toStandardLazyResponse } from '@orpc/standard-server-fetch'
56

6-
export interface LinkFetchClientOptions<T extends ClientContext> {
7+
export interface LinkFetchClientOptions<T extends ClientContext> extends ToFetchRequestOptions {
78
fetch?: (
89
request: Request,
910
init: Record<never, never>,
@@ -15,13 +16,15 @@ export interface LinkFetchClientOptions<T extends ClientContext> {
1516

1617
export class LinkFetchClient<T extends ClientContext> implements StandardLinkClient<T> {
1718
private readonly fetch: Exclude<LinkFetchClientOptions<T>['fetch'], undefined>
19+
private readonly toFetchRequestOptions: ToFetchRequestOptions
1820

1921
constructor(options: LinkFetchClientOptions<T>) {
2022
this.fetch = options?.fetch ?? globalThis.fetch.bind(globalThis)
23+
this.toFetchRequestOptions = options
2124
}
2225

2326
async call(request: StandardRequest, options: ClientOptionsOut<T>, path: readonly string[], input: unknown): Promise<StandardLazyResponse> {
24-
const fetchRequest = toFetchRequest(request)
27+
const fetchRequest = toFetchRequest(request, this.toFetchRequestOptions)
2528

2629
const fetchResponse = await this.fetch(fetchRequest, {}, options, path, input)
2730

packages/client/src/adapters/standard/link.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export class StandardLink<T extends ClientContext> implements ClientLink<T> {
9898
const maybeIterator = await this.#call(path, input, updatedOptions)
9999

100100
if (!isAsyncIteratorObject(maybeIterator)) {
101-
throw new InvalidEventIteratorRetryResponse('Invalid EventSource retry response')
101+
throw new InvalidEventIteratorRetryResponse('Invalid Event Iterator retry response')
102102
}
103103

104104
return maybeIterator

packages/client/src/adapters/standard/rpc-serializer.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ describe.each(supportedDataTypes)('rpcSerializer: $name', ({ value, expected })
6060
})
6161
})
6262

63-
describe('rpcSerializer: event-source iterator', async () => {
63+
describe('rpcSerializer: event iterator', async () => {
6464
const serializer = new RPCSerializer()
6565

6666
function serializeAndDeserialize(value: unknown): unknown {

packages/openapi-client/src/adapters/standard/openapi-serializer.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ describe('openAPISerializer', () => {
6666
expect(serialize).toHaveBeenCalledWith(data)
6767
})
6868

69-
describe('with event-source iterator', async () => {
69+
describe('with event iterator', async () => {
7070
it('on success', async () => {
7171
const date = new Date()
7272

@@ -235,7 +235,7 @@ describe('openAPISerializer', () => {
235235
expect(deserialized).toEqual(data)
236236
})
237237

238-
describe('with event-source iterator', async () => {
238+
describe('with event iterator', async () => {
239239
it('on success', async () => {
240240
const date = new Date()
241241

packages/openapi/src/adapters/fetch/openapi-handler.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ export class OpenAPIHandler<T extends Context> implements FetchHandler<T> {
2020

2121
async handle(
2222
request: Request,
23-
...[options]: MaybeOptionalOptions<StandardHandleOptions<T> & ToFetchResponseOptions>
23+
...[
24+
options = {} as StandardHandleOptions<T> & ToFetchResponseOptions,
25+
]: MaybeOptionalOptions<StandardHandleOptions<T> & ToFetchResponseOptions>
2426
): Promise<FetchHandleResult> {
2527
const standardRequest = toStandardLazyRequest(request)
2628

27-
const result = await this.standardHandler.handle(standardRequest, options as any)
29+
const result = await this.standardHandler.handle(standardRequest, options)
2830

2931
if (!result.matched) {
3032
return result

0 commit comments

Comments
 (0)