@@ -15,6 +15,32 @@ See this test for the [serialization behavior](./test/serialization.test.ts).
15
15
16
16
[ URLSearchParams ] : https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams
17
17
18
+ ### Serialization strategy
19
+
20
+ Serialization uses
21
+ [ ` URLSearchParams.toString() ` ] ( https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/toString#return_value )
22
+ which encodes most non-alphanumeric characters.
23
+
24
+ - The primitive types of ` strings ` , ` number ` , and ` bigint ` are serialized using ` .toString() ` .
25
+ - The primitive ` boolean ` type is serialized using ` .toString() ` to ` 'true' ` or ` 'false' ` .
26
+ - The primitive ` null ` and ` undefined ` values are removed,
27
+ e.g., ` { foo: null, bar: undefined, baz: 1 } ` serializes to ` baz=1 ` .
28
+ - ` Date ` objects are detected and serialized using ` Date.toISOString() ` .
29
+ - ` Temporal.Instant ` objects are detected and serialized by first converting them to ` Date `
30
+ and then serializing the ` Date ` as above.
31
+ - Arrays are serialized using
32
+ [ ` URLSearchParams.append() ` ] ( https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/append ) :
33
+ - Array values are serialized as above.
34
+ - The array ` { foo: [1, 2] } ` serializes to ` foo=1&foo=2 ` .
35
+ - The single element array ` { foo: [1] } ` serializes to ` foo=1 ` .
36
+ - The empty array ` { foo: [] } ` serializes to ` foo= ` .
37
+ - Serialization of the single element array containing the empty string is not supported
38
+ and will throw an ` UnserializableParamError ` .
39
+ Otherwise, the serialization of ` { foo: [''] } ` would conflict with ` { foo: [] } ` .
40
+ This serializer chooses to support the more common and more useful case of an empty array.
41
+ - Serialization of functions or other objects is not supported
42
+ and will throw an ` UnserializableParamError ` .
43
+
18
44
## Motivation
19
45
20
46
URL search parameters are strings, however the Seam API will parse parameters as complex types.
@@ -31,7 +57,7 @@ This module establishes the serialization standard adopted by the Seam API.
31
57
### Why not [ qs] ( https://github.com/ljharb/qs ) ?
32
58
33
59
- Not a zero-dependency module. Has quite a few dependency layers.
34
- - Impractile as a reference implementation.
60
+ - Impractical as a reference implementation.
35
61
qs enables complex, non-standard parsing and serialization,
36
62
which makes ensuing SDK parity much harder.
37
63
Similarly, this puts an unreasonable burden on user's of the HTTP API or those implementing their own client.
0 commit comments