Skip to content

Commit cec5200

Browse files
committed
docs: Add serialization strategy
1 parent cf2943f commit cec5200

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,32 @@ See this test for the [serialization behavior](./test/serialization.test.ts).
1515

1616
[URLSearchParams]: https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams
1717

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+
1844
## Motivation
1945

2046
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.
3157
### Why not [qs](https://github.com/ljharb/qs)?
3258

3359
- Not a zero-dependency module. Has quite a few dependency layers.
34-
- Impractile as a reference implementation.
60+
- Impractical as a reference implementation.
3561
qs enables complex, non-standard parsing and serialization,
3662
which makes ensuing SDK parity much harder.
3763
Similarly, this puts an unreasonable burden on user's of the HTTP API or those implementing their own client.

0 commit comments

Comments
 (0)