1+ import type { PublicJSONSerializer } from '../json-serializer'
12import { ORPCError } from '@orpc/server'
2- import { findDeepMatches , isPlainObject } from '@orpc/shared'
3+ import { findDeepMatches } from '@orpc/shared'
34import cd from 'content-disposition'
45import { safeParse } from 'fast-content-type-parse'
56import wcmatch from 'wildcard-match'
67import * as BracketNotation from './bracket-notation'
78
89export class OpenAPIPayloadCodec {
10+ constructor ( private readonly jsonSerializer : PublicJSONSerializer ) { }
11+
912 encode ( payload : unknown , accept ?: string ) : { body : FormData | Blob | string | undefined , headers ?: Headers } {
1013 const typeMatchers = (
1114 accept ?. split ( ',' ) . map ( safeParse ) ?? [ { type : '*/*' } ]
@@ -30,7 +33,7 @@ export class OpenAPIPayloadCodec {
3033 }
3134 }
3235
33- const handledPayload = this . serialize ( payload )
36+ const handledPayload = this . jsonSerializer . serialize ( payload )
3437 const hasBlobs = findDeepMatches ( v => v instanceof Blob , handledPayload ) . values . length > 0
3538
3639 const isExpectedMultipartFormData = typeMatchers . some ( isMatch =>
@@ -141,37 +144,6 @@ export class OpenAPIPayloadCodec {
141144 }
142145 }
143146
144- serialize ( payload : unknown ) : unknown {
145- if ( payload instanceof Set )
146- return this . serialize ( [ ...payload ] )
147- if ( payload instanceof Map )
148- return this . serialize ( [ ...payload . entries ( ) ] )
149- if ( Array . isArray ( payload ) ) {
150- return payload . map ( v => ( v === undefined ? 'undefined' : this . serialize ( v ) ) )
151- }
152- if ( Number . isNaN ( payload ) )
153- return 'NaN'
154- if ( typeof payload === 'bigint' )
155- return payload . toString ( )
156- if ( payload instanceof Date && Number . isNaN ( payload . getTime ( ) ) ) {
157- return 'Invalid Date'
158- }
159- if ( payload instanceof RegExp )
160- return payload . toString ( )
161- if ( payload instanceof URL )
162- return payload . toString ( )
163- if ( ! isPlainObject ( payload ) )
164- return payload
165- return Object . keys ( payload ) . reduce (
166- ( carry , key ) => {
167- const val = payload [ key ]
168- carry [ key ] = this . serialize ( val )
169- return carry
170- } ,
171- { } as Record < string , unknown > ,
172- )
173- }
174-
175147 async decode ( re : Request | Response | Headers | URLSearchParams | FormData ) : Promise < unknown > {
176148 if (
177149 re instanceof Headers
0 commit comments