/
is-serializable-props.ts
145 lines (128 loc) · 3.56 KB
/
is-serializable-props.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
const regexpPlainIdentifier = /^[A-Za-z_$][A-Za-z0-9_$]*$/
function isPlainObject(value: any): boolean {
if (Object.prototype.toString.call(value) !== '[object Object]') {
return false
}
const prototype = Object.getPrototypeOf(value)
return prototype === null || prototype === Object.prototype
}
export function isSerializableProps(
page: string,
method: string,
input: any
): true {
if (!isPlainObject(input)) {
throw new SerializableError(
page,
method,
'',
`Props must be returned as a plain object from ${method}: \`{ props: { ... } }\`.`
)
}
function visit(visited: Map<any, string>, value: any, path: string) {
if (visited.has(value)) {
throw new SerializableError(
page,
method,
path,
`Circular references cannot be expressed in JSON (references: \`${
visited.get(value) || '(self)'
}\`).`
)
}
visited.set(value, path)
}
function isSerializable(
refs: Map<any, string>,
value: any,
path: string
): true {
const type = typeof value
if (
// `null` can be serialized, but not `undefined`.
value === null ||
// n.b. `bigint`, `function`, `symbol`, and `undefined` cannot be
// serialized.
//
// `object` is special-cased below, as it may represent `null`, an Array,
// a plain object, a class, et al.
type === 'boolean' ||
type === 'number' ||
type === 'string'
) {
return true
}
if (type === 'undefined') {
throw new SerializableError(
page,
method,
path,
'`undefined` cannot be serialized as JSON. Please use `null` or omit this value all together.'
)
}
if (isPlainObject(value)) {
visit(refs, value, path)
if (
Object.entries(value).every(([key, value]) => {
const nextPath = regexpPlainIdentifier.test(key)
? `${path}.${key}`
: `${path}[${JSON.stringify(key)}]`
const newRefs = new Map(refs)
return (
isSerializable(newRefs, key, nextPath) &&
isSerializable(newRefs, value, nextPath)
)
})
) {
return true
}
throw new SerializableError(
page,
method,
path,
`invariant: Unknown error encountered in Object.`
)
}
if (Array.isArray(value)) {
visit(refs, value, path)
const newRefs = new Map(refs)
if (
value.every((value, index) =>
isSerializable(newRefs, value, `${path}[${index}]`)
)
) {
return true
}
throw new SerializableError(
page,
method,
path,
`invariant: Unknown error encountered in Array.`
)
}
// None of these can be expressed as JSON:
// const type: "bigint" | "symbol" | "object" | "function"
throw new SerializableError(
page,
method,
path,
'`' +
type +
'`' +
(type === 'object'
? ` ("${Object.prototype.toString.call(value)}")`
: '') +
' cannot be serialized as JSON. Please only return JSON serializable data types.'
)
}
return isSerializable(new Map(), input, '')
}
export class SerializableError extends Error {
constructor(page: string, method: string, path: string, message: string) {
super(
path
? `Error serializing \`${path}\` returned from \`${method}\` in "${page}".\nReason: ${message}`
: `Error serializing props returned from \`${method}\` in "${page}".\nReason: ${message}`
)
}
}