Skip to content

fix: allow json literals in serializer #3849

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions packages/router-core/src/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@ export interface StartSerializer {
decode: <T>(value: T) => T
}

type JSONLiteral = string | boolean | null | number

export type SerializerStringifyBy<T, TSerializable> = T extends TSerializable
? T
: T extends (...args: Array<any>) => any
? 'Function is not serializable'
: { [K in keyof T]: SerializerStringifyBy<T[K], TSerializable> }
: T extends JSONLiteral
? T
: T extends (...args: Array<any>) => any
? 'Function is not serializable'
: { [K in keyof T]: SerializerStringifyBy<T[K], TSerializable> }

export type SerializerParseBy<T, TSerializable> = T extends TSerializable
? T
: T extends React.JSX.Element
? ReadableStream
: { [K in keyof T]: SerializerParseBy<T[K], TSerializable> }
: T extends JSONLiteral
? T
: T extends React.JSX.Element
? ReadableStream
: { [K in keyof T]: SerializerParseBy<T[K], TSerializable> }

export type Serializable = Date | undefined | Error | FormData | bigint

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ test('createMiddleware merges client context and sends to the server', () => {
expectTypeOf(result).toEqualTypeOf<{
'use functions must return the result of next()': true
context: { a: boolean; b: string; c: number }
sendContext: { a: boolean; b: string; c: number; d: number }
sendContext: { a: boolean; b: string; c: number; d: 5 }
headers: HeadersInit
}>()

Expand All @@ -215,7 +215,7 @@ test('createMiddleware merges client context and sends to the server', () => {
a: boolean
b: string
c: number
d: number
d: 5
}>()

const result = await options.next({
Expand All @@ -232,7 +232,7 @@ test('createMiddleware merges client context and sends to the server', () => {
}
sendContext: undefined
}
context: { a: boolean; b: string; c: number; d: number; e: string }
context: { a: boolean; b: string; c: number; d: 5; e: string }
sendContext: undefined
}>()

Expand Down