@@ -23,19 +23,40 @@ export interface SerializableExtensions extends DefaultSerializable {}
2323
2424export type Serializable = SerializableExtensions [ keyof SerializableExtensions ]
2525
26+ export type UnionizeSerializationAdaptersInput <
27+ TAdapters extends ReadonlyArray < AnySerializationAdapter > ,
28+ > = TAdapters [ number ] [ '~types' ] [ 'input' ]
29+
2630export function createSerializationAdapter <
2731 TInput = unknown ,
28- TOutput = unknown /* we need to check that this type is actually serializable taking into account all seroval native types and any custom plugin WE=router/start add!!! */ ,
32+ TOutput = unknown ,
33+ const TExtendsAdapters extends
34+ | ReadonlyArray < AnySerializationAdapter >
35+ | never = never ,
2936> (
30- opts : CreateSerializationAdapterOptions < TInput , TOutput > ,
31- ) : SerializationAdapter < TInput , TOutput > {
32- return opts as unknown as SerializationAdapter < TInput , TOutput >
37+ opts : CreateSerializationAdapterOptions < TInput , TOutput , TExtendsAdapters > ,
38+ ) : SerializationAdapter < TInput , TOutput , TExtendsAdapters > {
39+ return opts as unknown as SerializationAdapter <
40+ TInput ,
41+ TOutput ,
42+ TExtendsAdapters
43+ >
3344}
3445
35- export interface CreateSerializationAdapterOptions < TInput , TOutput > {
46+ export interface CreateSerializationAdapterOptions <
47+ TInput ,
48+ TOutput ,
49+ TExtendsAdapters extends ReadonlyArray < AnySerializationAdapter > | never ,
50+ > {
3651 key : string
52+ extends ?: TExtendsAdapters
3753 test : ( value : unknown ) => value is TInput
38- toSerializable : ( value : TInput ) => ValidateSerializable < TOutput , Serializable >
54+ toSerializable : (
55+ value : TInput ,
56+ ) => ValidateSerializable <
57+ TOutput ,
58+ Serializable | UnionizeSerializationAdaptersInput < TExtendsAdapters >
59+ >
3960 fromSerializable : ( value : TOutput ) => TInput
4061}
4162
@@ -90,28 +111,42 @@ export interface DefaultSerializerExtensions {
90111
91112export interface SerializerExtensions extends DefaultSerializerExtensions { }
92113
93- export interface SerializationAdapter < TInput , TOutput > {
94- '~types' : SerializationAdapterTypes < TInput , TOutput >
114+ export interface SerializationAdapter <
115+ TInput ,
116+ TOutput ,
117+ TExtendsAdapters extends ReadonlyArray < AnySerializationAdapter > ,
118+ > {
119+ '~types' : SerializationAdapterTypes < TInput , TOutput , TExtendsAdapters >
95120 key : string
121+ extends ?: TExtendsAdapters
96122 test : ( value : unknown ) => value is TInput
97123 toSerializable : ( value : TInput ) => TOutput
98124 fromSerializable : ( value : TOutput ) => TInput
99- makePlugin : ( options : { didRun : boolean } ) => Plugin < TInput , SerovalNode >
100125}
101126
102- export interface SerializationAdapterTypes < TInput , TOutput > {
103- input : TInput
127+ export interface SerializationAdapterTypes <
128+ TInput ,
129+ TOutput ,
130+ TExtendsAdapters extends ReadonlyArray < AnySerializationAdapter > ,
131+ > {
132+ input : TInput | UnionizeSerializationAdaptersInput < TExtendsAdapters >
104133 output : TOutput
134+ extends : TExtendsAdapters
105135}
106136
107- export type AnySerializationAdapter = SerializationAdapter < any , any >
137+ export type AnySerializationAdapter = SerializationAdapter < any , any , any >
108138
109- export function makeSsrSerovalPlugin < TInput , TOutput > (
110- serializationAdapter : SerializationAdapter < TInput , TOutput > ,
139+ export function makeSsrSerovalPlugin (
140+ serializationAdapter : AnySerializationAdapter ,
111141 options : { didRun : boolean } ,
112- ) {
113- return createPlugin < TInput , SerovalNode > ( {
142+ ) : Plugin < any , SerovalNode > {
143+ return createPlugin < any , SerovalNode > ( {
114144 tag : '$TSR/t/' + serializationAdapter . key ,
145+ extends : serializationAdapter . extends
146+ ? ( serializationAdapter . extends as Array < AnySerializationAdapter > ) . map (
147+ ( ext ) => makeSsrSerovalPlugin ( ext , options ) ,
148+ )
149+ : undefined ,
115150 test : serializationAdapter . test ,
116151 parse : {
117152 stream ( value , ctx ) {
@@ -134,11 +169,16 @@ export function makeSsrSerovalPlugin<TInput, TOutput>(
134169 } )
135170}
136171
137- export function makeSerovalPlugin < TInput , TOutput > (
138- serializationAdapter : SerializationAdapter < TInput , TOutput > ,
139- ) {
140- return createPlugin < TInput , SerovalNode > ( {
172+ export function makeSerovalPlugin (
173+ serializationAdapter : AnySerializationAdapter ,
174+ ) : Plugin < any , SerovalNode > {
175+ return createPlugin < any , SerovalNode > ( {
141176 tag : '$TSR/t/' + serializationAdapter . key ,
177+ extends : serializationAdapter . extends
178+ ? ( serializationAdapter . extends as Array < AnySerializationAdapter > ) . map (
179+ makeSerovalPlugin ,
180+ )
181+ : undefined ,
142182 test : serializationAdapter . test ,
143183 parse : {
144184 sync ( value , ctx ) {
@@ -154,9 +194,7 @@ export function makeSerovalPlugin<TInput, TOutput>(
154194 // we don't generate JS code outside of SSR (for now)
155195 serialize : undefined as never ,
156196 deserialize ( node , ctx ) {
157- return serializationAdapter . fromSerializable (
158- ctx . deserialize ( node ) as TOutput ,
159- )
197+ return serializationAdapter . fromSerializable ( ctx . deserialize ( node ) )
160198 } ,
161199 } )
162200}
0 commit comments