Skip to content
This repository has been archived by the owner on Apr 4, 2022. It is now read-only.

Expose client.endpoints attribute (fixes #641) #742

Merged
merged 7 commits into from
Apr 9, 2020

Conversation

leplatrem
Copy link
Contributor

For Kinto/kinto.js#1751, I was thinking of something like this. Usage would be:

// Replace record endpoint with custom «resource»
kintoClient.endpoints.record = (bucket: string, coll: string, id?: string) =>
   `/custom/url/records` + (id ? `/${id}` : "");
// Add new endpoint
kintoClient.endpoints.changeset = (bucket: string, coll: string) =>
   `${client.endpoints.collection(bucket, coll)}/changeset`

const coll = kintoClient.bucket("bid").collection("cid");
const path = coll.endpoints.changeset(coll.bucket, coll.collection);
const response = await coll.client.execute({ path });

What do you think?

@leplatrem leplatrem mentioned this pull request Apr 6, 2020
@dstaley
Copy link
Member

dstaley commented Apr 6, 2020

The first use case (overriding a default endpoint) will work just fine in TypeScript, but the second use case (custom endpoints) might get a bit tricky to represent both accurately and safely as a class property. What if we suggested users create their own endpoint object like this:

const endpoints = {
  ...client.endpoints,
  changeset: (bucket: string, coll: string) =>
    `${client.endpoints.collection(bucket, coll)}/changeset`
};

This would allow TypeScript to correctly type all the properties of endpoints without introducing some complexity into the classes that have a _endpoint property.

@leplatrem leplatrem force-pushed the 641-custom-endpoints branch from 0f36e49 to a18b9db Compare April 7, 2020 16:51
@leplatrem
Copy link
Contributor Author

Thanks!

Based on your feedback (and Ethan's in #743 ) I changed my strategy and I don't think we need the use case of adding of a new endpoint.

@leplatrem leplatrem marked this pull request as ready for review April 7, 2020 16:56
@leplatrem leplatrem requested review from glasserc and dstaley April 7, 2020 16:56
@github-actions
Copy link

github-actions bot commented Apr 7, 2020

Size Change: +135 B (0%)

Total Size: 38.4 kB

Filename Size Change
dist/kinto-http.min.js 8.61 kB +40 B (0%)
dist/kinto-http.node.js 14.3 kB +53 B (0%)
dist/moz-kinto-http-client.js 15.5 kB +42 B (0%)

compressed-size-action

Copy link
Member

@dstaley dstaley left a comment

Choose a reason for hiding this comment

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

Nice work! I really like how you figured out a nicer way to stub out the request method.

Copy link
Contributor

@glasserc glasserc left a comment

Choose a reason for hiding this comment

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

LGTM but Dylan's review should supersede mine :)

@leplatrem
Copy link
Contributor Author

Thanks for your help @dstaley! I think we're good here, I'll merge :)

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

Successfully merging this pull request may close these issues.

3 participants