Skip to content

Commit

Permalink
perf(server): use empty body for undefined RPC input/output (#128)
Browse files Browse the repository at this point in the history
* perf(server): use empty body for undefined RPC input/output

* fix tests
  • Loading branch information
unnoq authored Feb 8, 2025
1 parent 1aaba0f commit 854b646
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
9 changes: 9 additions & 0 deletions packages/client/src/adapters/fetch/orpc-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ export class RPCLink<TClientContext> implements ClientLink<TClientContext> {

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

if (input === undefined) {
return {
body: undefined,
method,
headers,
url,
}
}

if (isPlainObject(serialized)) {
if (!headers.has('content-type')) {
headers.set('content-type', 'application/json')
Expand Down
4 changes: 4 additions & 0 deletions packages/server/src/adapters/standard/rpc-serializer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ describe.each(supportedDataTypes)('rpcSerializer: $name', ({ value, expected })
return serializer.deserialize(serialized)
}

if (serialized === undefined) {
return serializer.deserialize(undefined)
}

return serializer.deserialize(JSON.parse(JSON.stringify(serialized))) // like in the real world
}

Expand Down
10 changes: 9 additions & 1 deletion packages/server/src/adapters/standard/rpc-serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ import type { Segment } from '@orpc/shared'
import { findDeepMatches, isPlainObject, set } from '@orpc/shared'

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

export class RPCSerializer {
serialize(data: unknown): RPCSerialized {
if (data === undefined) {
return undefined
}

if (data instanceof Blob) {
return data
}
Expand All @@ -31,6 +35,10 @@ export class RPCSerializer {
}

deserialize(serialized: RPCSerialized): unknown {
if (serialized === undefined) {
return undefined
}

if (serialized instanceof Blob) {
return serialized
}
Expand Down

0 comments on commit 854b646

Please sign in to comment.