Skip to content
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

Date objects vs plain JSON for API calls #155

Closed
mehtank opened this issue Mar 9, 2021 · 3 comments
Closed

Date objects vs plain JSON for API calls #155

mehtank opened this issue Mar 9, 2021 · 3 comments

Comments

@mehtank
Copy link
Contributor

mehtank commented Mar 9, 2021

start: Match.Optional Date
finish: Match.Optional Date

I tried calling the API with a timestamp (which is what we see if we parse /api/log/get as a regular JSON), an ISO 8601 string, and a couple others, but they all resulted in Match Errors.

I realize now that you mention Date objects explicitly in the docs for why to use EJSON instead of plain JSON, but it would be nice to be more universal (and allow GET calls too). Perhaps a custom match where we feed the values into a Date constructor, and if it is successful use that result? That'll allow all manner of reasonable inputs to the api call.

@mehtank mehtank changed the title Date objects are hard to serialize in API calls Date objects vs plain JSON for API calls Mar 9, 2021
@edemaine
Copy link
Owner

Please give the master branch a try. It should support most forms of ISO 8601, as given by the ECMAScript standard.

@mehtank
Copy link
Contributor Author

mehtank commented Mar 17, 2021

The check only happens on GET parameters [ which works fine ], but not on options passed in through a POST request body:

for [key, value] from url.searchParams

So a POST request with plain JSON still fails. That said, I now realize that a Date object is none other than a regular JSON object with key "$date" and value milliseconds-since-epoch. And that's easy enough to manually create in code, so I'm fine with it as it stands.

@edemaine
Copy link
Owner

Good point. I added support in the post body as well, and documented the date (and RegExp) format.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants