Skip to content

feat: Serialize objects #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,18 @@ Serialization uses
[`URLSearchParams.toString()`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/toString#return_value)
which encodes most non-alphanumeric characters.

- The primitive types `string`, `number`, and `bigint` are serialized using `.toString()`.
- The primitive `boolean` type is serialized using `.toString()` to `'true'` or `'false'`.
Serialization is guaranteed to be well-defined within each type, i.e.,
if the type of value for a given key in the query string is fixed and known by
the consumer parsing the string, it can be unambigously parsed back to the original primitive value.

- The primitive type `string` is serialized using `.toString()`.
- The primitive `number` and `bigint` types are serialized using `.toString()`.
- The primitive `boolean` type is serialized using `.toString()`,
e.g., `{ foo: true, bar: false }` serializes to `foo=true&bar=false`.
- The primitive `null` and `undefined` values are removed,
e.g., `{ foo: null, bar: undefined, baz: 1 }` serializes to `baz=1`.
- `Date` objects are detected and serialized using `Date.toISOString()`.
- `Date` objects are detected and serialized using `Date.toISOString()`,
e.g., `{ foo: new Date(0) }` serializes to `foo=1970-01-01T00%3A00%3A00.000Z`.
- `Temporal.Instant` objects are detected and serialized by first converting them to `Date`
and then serializing the `Date` as above.
- Arrays are serialized using
Expand All @@ -34,12 +41,20 @@ which encodes most non-alphanumeric characters.
- The array `{ foo: [1, 2] }` serializes to `foo=1&foo=2`.
- The single element array `{ foo: [1] }` serializes to `foo=1`.
- The empty array `{ foo: [] }` serializes to `foo=`.
- Serialization of the single element array containing the empty string is not supported
and will throw an `UnserializableParamError`.
- Serialization of arrays containing `null` or `undefined` values
is not supported and will throw an `UnserializableParamError`.
- Serialization of the single element array containing the empty string
is not supported and will throw an `UnserializableParamError`.
Otherwise, the serialization of `{ foo: [''] }` would conflict with `{ foo: [] }`.
This serializer chooses to support the more common and more useful case of an empty array.
- Serialization of functions or other objects is not supported
and will throw an `UnserializableParamError`.
- Serialization of objects and nested objects first serializes the keys
to dot-path format and then serializes the values as above, e.g.,
`{ foo: 'a', bar: { baz: 'b', fizz: [1, 2] } }` serializes to
`foo=a&bar.baz=b&bar.fizz=1&bar.fizz=2`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you consider using PHP style here? That is:

?foo=a&bar[baz]=b&bar[fizz][]=1&bar[fizz][]=2

Copy link
Member Author

@razor-x razor-x Feb 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No I did not directly consider it, but thanks for the suggestion.

I chose dot notation because it's the simplest to read, write and parse (and does not need escaping in other contexts like the command line).

The suggested bracket notation would work, but it looks too similar to the competing array notation bar[]=. Since URLSearchParams standardizes on foo=a&foo=b and not bracket notation, I decided not to introduce brackets.

Note that we still need to write a standardized parser, and the parser can be more permissive with what it accepts. E.g., the serialize will set the base standard, but our parser can still choose to accept other formats within reason, and if they do not conflict with the serializer.

For example, we can unambiguously parse foo.bar=1 and foo[bar]=1 to { foo: { bar: 1 } } since the Seam API will not allow brackets or dots in param names.

- Serialization of nested arrays or objects nested inside arrays
is not supported and will throw an `UnserializableParamError`.
- Serialization of functions or other objects is
is not supported and will throw an `UnserializableParamError`.

## Installation

Expand Down
6 changes: 5 additions & 1 deletion examples/axios.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Temporal } from '@js-temporal/polyfill'
import axios from 'axios'
import type { Builder, Command, Describe, Handler } from 'landlubber'

Expand All @@ -17,11 +18,14 @@ export const handler: Handler<Options> = async ({ logger }) => {
paramsSerializer: serializeUrlSearchParams,
params: {
a: 'bar',
b: 2,
b: 2.3,
c: true,
d: null,
e: ['a', 2],
f: [],
g: new Date(),
h: Temporal.Now.instant(),
i: { foo: 1, bar: { baz: 2, fizz: [1, 'a'] } },
},
})
logger.info({ data }, 'Response')
Expand Down
Loading