-
-
Notifications
You must be signed in to change notification settings - Fork 716
/
transformer.ts
147 lines (129 loc) · 3.56 KB
/
transformer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { isPlainObject } from './utils'
export interface RouterTransformer {
stringify: (obj: unknown) => string
parse: (str: string) => unknown
encode: <T>(value: T) => T
decode: <T>(value: T) => T
}
export const defaultTransformer: RouterTransformer = {
stringify: (value: any) =>
JSON.stringify(value, function replacer(key, val) {
const ogVal = this[key]
const transformer = transformers.find((t) => t.stringifyCondition(ogVal))
if (transformer) {
return transformer.stringify(ogVal)
}
return val
}),
parse: (value: string) =>
JSON.parse(value, function parser(key, val) {
const ogVal = this[key]
if (isPlainObject(ogVal)) {
const transformer = transformers.find((t) => t.parseCondition(ogVal))
if (transformer) {
return transformer.parse(ogVal)
}
}
return val
}),
encode: (value: any) => {
// When encodign, dive first
if (Array.isArray(value)) {
return value.map((v) => defaultTransformer.encode(v))
}
if (isPlainObject(value)) {
return Object.fromEntries(
Object.entries(value).map(([key, v]) => [
key,
defaultTransformer.encode(v),
]),
)
}
const transformer = transformers.find((t) => t.stringifyCondition(value))
if (transformer) {
return transformer.stringify(value)
}
return value
},
decode: (value: any) => {
// Attempt transform first
if (isPlainObject(value)) {
const transformer = transformers.find((t) => t.parseCondition(value))
if (transformer) {
return transformer.parse(value)
}
}
if (Array.isArray(value)) {
return value.map((v) => defaultTransformer.decode(v))
}
if (isPlainObject(value)) {
return Object.fromEntries(
Object.entries(value).map(([key, v]) => [
key,
defaultTransformer.decode(v),
]),
)
}
return value
},
}
const createTransformer = <T extends string>(
key: T,
check: (value: any) => boolean,
toValue: (value: any) => any = (v) => v,
fromValue: (value: any) => any = (v) => v,
) => ({
key,
stringifyCondition: check,
stringify: (value: any) => ({ [`$${key}`]: toValue(value) }),
parseCondition: (value: any) => Object.hasOwn(value, `$${key}`),
parse: (value: any) => fromValue(value[`$${key}`]),
})
// Keep these ordered by predicted frequency
const transformers = [
createTransformer(
// Key
'undefined',
// Check
(v) => v === undefined,
// To
() => 0,
// From
() => undefined,
),
createTransformer(
// Key
'date',
// Check
(v) => v instanceof Date,
// To
(v) => v.toISOString(),
// From
(v) => new Date(v),
),
createTransformer(
// Key
'error',
// Check
(v) => v instanceof Error,
// To
(v) => ({ ...v, message: v.message, stack: v.stack, cause: v.cause }),
// From
(v) => Object.assign(new Error(v.message), v),
),
] as const
export type TransformerStringify<T, TSerializable> = T extends TSerializable
? T
: T extends (...args: Array<any>) => any
? 'Function is not serializable'
: { [K in keyof T]: TransformerStringify<T[K], TSerializable> }
export type TransformerParse<T, TSerializable> = T extends TSerializable
? T
: T extends React.JSX.Element
? ReadableStream
: { [K in keyof T]: TransformerParse<T[K], TSerializable> }
export type DefaultTransformerStringify<T> = TransformerStringify<
T,
Date | undefined
>
export type DefaultTransformerParse<T> = TransformerParse<T, Date | undefined>