Skip to content

Commit 854b646

Browse files
authored
perf(server): use empty body for undefined RPC input/output (#128)
* perf(server): use empty body for undefined RPC input/output * fix tests
1 parent 1aaba0f commit 854b646

3 files changed

Lines changed: 22 additions & 1 deletion

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,15 @@ export class RPCLink<TClientContext> implements ClientLink<TClientContext> {
145145

146146
const method = expectMethod === 'GET' ? this.fallbackMethod : expectMethod
147147

148+
if (input === undefined) {
149+
return {
150+
body: undefined,
151+
method,
152+
headers,
153+
url,
154+
}
155+
}
156+
148157
if (isPlainObject(serialized)) {
149158
if (!headers.has('content-type')) {
150159
headers.set('content-type', 'application/json')

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ describe.each(supportedDataTypes)('rpcSerializer: $name', ({ value, expected })
1111
return serializer.deserialize(serialized)
1212
}
1313

14+
if (serialized === undefined) {
15+
return serializer.deserialize(undefined)
16+
}
17+
1418
return serializer.deserialize(JSON.parse(JSON.stringify(serialized))) // like in the real world
1519
}
1620

packages/server/src/adapters/standard/rpc-serializer.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ import type { Segment } from '@orpc/shared'
22
import { findDeepMatches, isPlainObject, set } from '@orpc/shared'
33

44
export type RPCSerializedJsonMeta = ['bigint' | 'date' | 'nan' | 'undefined' | 'set' | 'map' | 'regexp' | 'url', Segment[]][]
5-
export type RPCSerialized = { json: unknown, meta: RPCSerializedJsonMeta } | FormData | Blob
5+
export type RPCSerialized = { json: unknown, meta: RPCSerializedJsonMeta } | FormData | Blob | undefined
66
export type RPCSerializedFormDataMaps = Segment[][]
77

88
export class RPCSerializer {
99
serialize(data: unknown): RPCSerialized {
10+
if (data === undefined) {
11+
return undefined
12+
}
13+
1014
if (data instanceof Blob) {
1115
return data
1216
}
@@ -31,6 +35,10 @@ export class RPCSerializer {
3135
}
3236

3337
deserialize(serialized: RPCSerialized): unknown {
38+
if (serialized === undefined) {
39+
return undefined
40+
}
41+
3442
if (serialized instanceof Blob) {
3543
return serialized
3644
}

0 commit comments

Comments
 (0)