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

Allow specifying document ID #142

Merged
merged 10 commits into from
Sep 25, 2023
Merged

Allow specifying document ID #142

merged 10 commits into from
Sep 25, 2023

Conversation

paulgb
Copy link
Member

@paulgb paulgb commented Sep 25, 2023

In some cases, a developer may already be managing unique IDs (e.g. sequential or UUIDs) for a set of objects, and would like to map these to y-sweet documents. They could maintain a mapping from their own IDs to y-sweet document IDs, but it's easier for us to just allow them to provide an ID when they create the document.

I think this was suggested by @colelawrence, and it also relates to (but does not close) #141.

@paulgb paulgb requested a review from rolyatmax September 25, 2023 14:59
@vercel
Copy link

vercel bot commented Sep 25, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
y-sweet-debugger ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 25, 2023 10:31pm
y-sweet-demos ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 25, 2023 10:31pm
y-sweet-gendocs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 25, 2023 10:31pm

@paulgb paulgb force-pushed the paulgb/allow-specifying-doc-id branch 2 times, most recently from be0d2c1 to 7292abe Compare September 25, 2023 19:54
@@ -45,3 +43,10 @@ jobs:
Y_SWEET_S3_BUCKET_PREFIX: testing
Y_SWEET_S3_BUCKET_NAME: ysweet-testing-y-sweet-data
working-directory: tests
timeout-minutes: 10

- uses: actions/upload-artifact@v3
Copy link
Member Author

Choose a reason for hiding this comment

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

this PR adds log redirection to files for debugging (only for the native server, currently). This uploads those to GitHub to make debugging easier.

@@ -214,8 +219,8 @@ export class DocumentManager {
*
* @returns A {@link DocCreationResult} object containing the ID of the created document.
*/
public async createDoc(): Promise<DocCreationResult> {
const result = await this.doFetch('doc/new', { method: 'POST' })
Copy link
Member Author

Choose a reason for hiding this comment

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

This only worked by mistake: doFetch sends a POST request if the body is defined, and this sets the body to {method: POST}. The server ignores the method field.

Copy link
Member

Choose a reason for hiding this comment

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

I think I'd vote for just having doFetch accept a method (like fetch() does) rather than having to pass an empty object as the body in order to "hack around" the faulty heuristic of setting the method based on the body. Thoughts?

Copy link
Member

Choose a reason for hiding this comment

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

Also, I think the getClientToken() method below is using doFetch() incorrectly (passing what looks like FetchOptions as the request body).

Copy link
Member Author

Choose a reason for hiding this comment

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

My general principle for the API is that every POST request should have a valid JSON body, even if it is an empty object. Since doFetch is not a general-purpose fetch function but one designed for this API, the GET/POST distinction restricts the degrees of freedom that the caller has to break things.

Copy link
Member Author

Choose a reason for hiding this comment

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

Also, I think the getClientToken() method below is using doFetch() incorrectly (passing what looks like FetchOptions as the request body).

Good point, here's a fix: #145

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure it restricts degrees of freedom for the caller. After all, the caller of doFetch() can still break things by forgetting to pass an empty object.

My main issue is that it's just easy for the caller of doFetch to make bad assumptions about what doFetch will do. At this point in the logic, all the dev cares about are the 3 main components of a REST endpoint: method, path, and request body. Seems to me that designing a doFetch interface that looks like that is more likely to result in a dev correctly predicting what the code will do.

Copy link
Member Author

Choose a reason for hiding this comment

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

Are you thinking that the signature should be doFetch(url, method, body?)? I guess I could use typescript overloads to ensure that a body is passed if and only if the method is POST.

Alternatively, I could use doGet(url) and doPost(url, body), although they'd still use a shared underlying doFetch, so we could still shoot ourselves in the foot by calling the underlying doFetch instead of doGet or doPost.

Copy link
Member

Choose a reason for hiding this comment

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

I like the update you pushed

@@ -1 +1,2 @@
node_modules
out
Copy link
Member Author

Choose a reason for hiding this comment

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

Tests now create some output here for debugging.

Copy link
Member

Choose a reason for hiding this comment

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

nit: can we call this test-output or something like that?

}),
).rejects.toThrow('400')
})

Copy link
Member

Choose a reason for hiding this comment

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

What should happen when creating a doc with a name that's already been used? Is that a 409 Conflict?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it should just return the same document. This way document creation is idempotent.

@paulgb paulgb merged commit ca6e49b into main Sep 25, 2023
@paulgb paulgb deleted the paulgb/allow-specifying-doc-id branch September 25, 2023 22:35
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

Successfully merging this pull request may close these issues.

2 participants