Skip to content

Commit 49fbbe7

Browse files
authored
Merge pull request #10 from seamapi/nested-objects
feat: Serialize objects
2 parents 1f4dd04 + 2661288 commit 49fbbe7

File tree

6 files changed

+3143
-1725
lines changed

6 files changed

+3143
-1725
lines changed

README.md

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,18 @@ Serialization uses
2121
[`URLSearchParams.toString()`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/toString#return_value)
2222
which encodes most non-alphanumeric characters.
2323

24-
- The primitive types `string`, `number`, and `bigint` are serialized using `.toString()`.
25-
- The primitive `boolean` type is serialized using `.toString()` to `'true'` or `'false'`.
24+
Serialization is guaranteed to be well-defined within each type, i.e.,
25+
if the type of value for a given key in the query string is fixed and known by
26+
the consumer parsing the string, it can be unambigously parsed back to the original primitive value.
27+
28+
- The primitive type `string` is serialized using `.toString()`.
29+
- The primitive `number` and `bigint` types are serialized using `.toString()`.
30+
- The primitive `boolean` type is serialized using `.toString()`,
31+
e.g., `{ foo: true, bar: false }` serializes to `foo=true&bar=false`.
2632
- The primitive `null` and `undefined` values are removed,
2733
e.g., `{ foo: null, bar: undefined, baz: 1 }` serializes to `baz=1`.
28-
- `Date` objects are detected and serialized using `Date.toISOString()`.
34+
- `Date` objects are detected and serialized using `Date.toISOString()`,
35+
e.g., `{ foo: new Date(0) }` serializes to `foo=1970-01-01T00%3A00%3A00.000Z`.
2936
- `Temporal.Instant` objects are detected and serialized by first converting them to `Date`
3037
and then serializing the `Date` as above.
3138
- Arrays are serialized using
@@ -34,12 +41,20 @@ which encodes most non-alphanumeric characters.
3441
- The array `{ foo: [1, 2] }` serializes to `foo=1&foo=2`.
3542
- The single element array `{ foo: [1] }` serializes to `foo=1`.
3643
- 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`.
44+
- Serialization of arrays containing `null` or `undefined` values
45+
is not supported and will throw an `UnserializableParamError`.
46+
- Serialization of the single element array containing the empty string
47+
is not supported and will throw an `UnserializableParamError`.
3948
Otherwise, the serialization of `{ foo: [''] }` would conflict with `{ foo: [] }`.
4049
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`.
50+
- Serialization of objects and nested objects first serializes the keys
51+
to dot-path format and then serializes the values as above, e.g.,
52+
`{ foo: 'a', bar: { baz: 'b', fizz: [1, 2] } }` serializes to
53+
`foo=a&bar.baz=b&bar.fizz=1&bar.fizz=2`.
54+
- Serialization of nested arrays or objects nested inside arrays
55+
is not supported and will throw an `UnserializableParamError`.
56+
- Serialization of functions or other objects is
57+
is not supported and will throw an `UnserializableParamError`.
4358

4459
## Installation
4560

examples/axios.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Temporal } from '@js-temporal/polyfill'
12
import axios from 'axios'
23
import type { Builder, Command, Describe, Handler } from 'landlubber'
34

@@ -17,11 +18,14 @@ export const handler: Handler<Options> = async ({ logger }) => {
1718
paramsSerializer: serializeUrlSearchParams,
1819
params: {
1920
a: 'bar',
20-
b: 2,
21+
b: 2.3,
2122
c: true,
2223
d: null,
2324
e: ['a', 2],
2425
f: [],
26+
g: new Date(),
27+
h: Temporal.Now.instant(),
28+
i: { foo: 1, bar: { baz: 2, fizz: [1, 'a'] } },
2529
},
2630
})
2731
logger.info({ data }, 'Response')

0 commit comments

Comments
 (0)