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

add create member to resource class #2838

Open
th3fallen opened this issue Sep 28, 2023 · 8 comments
Open

add create member to resource class #2838

th3fallen opened this issue Sep 28, 2023 · 8 comments
Labels
enhancement New feature or request

Comments

@th3fallen
Copy link

Is your feature request related to a problem? Please describe.
at the moment the documented way to create a new item is to leverage getList.push per https://dataclient.io/rest/api/createResource#push which is pretty unintuitive, i'd love to see a create method replace that to more closely follow the get, update, delete methods

@th3fallen th3fallen added the enhancement New feature or request label Sep 28, 2023
@ntucker
Copy link
Collaborator

ntucker commented Oct 9, 2023

Thanks for the report!

Is this mainly about discoverability? I.e., you couldn't figure out how to write code to create - or is it also about readability - i.e., Resource.getList.push doesn't make sense.

@th3fallen
Copy link
Author

the latter mostly

@ntucker
Copy link
Collaborator

ntucker commented Oct 9, 2023

Hmm, curious about the use case: Do you want to create a new item and not add it to a list? Do you have a list endpoint at all?

@ntucker
Copy link
Collaborator

ntucker commented Oct 9, 2023

For context, the API was modeled after Backbone collections

@ntucker
Copy link
Collaborator

ntucker commented Oct 19, 2023

Hmm, push is the standard terminology I've found:

Do you have a suggestion for a better way to distinguish adding to end, beginning, or inserted in sorted order?

@jacobcossman
Copy link

I am having a slightly different but related issue, which I can open a new issue for if necessary. I cannot determine how to extend getList.push in order to specify a new body type for the POST request. The following does not work:

const InternalUserBaseResource = createResource({
    urlPrefix,
    path: 'internal/users/:userId',
    schema: InternalUser,
    Endpoint: AuthdEndpoint
})

export const InternalUserResource = InternalUserBaseResource.extend({
    ...InternalUserBaseResource,
    getList: {
        ...InternalUserBaseResource.getList,
        push: {
            ...InternalUserBaseResource.getList.push,
            body: {} as InternalUser & { password: string }
        }
    }
})

I get the following error:

TypeError: Cannot set property push of [object Object] which has only a getter

I may have missed it but I don't see anywhere in the docs or migration guide how we're supposed to extend this new getList.push method of creating entities. Do I have to extend create instead and ignore the deprecation warnings?

@ntucker
Copy link
Collaborator

ntucker commented Dec 28, 2023

@jacobcossman Thanks for the report.

A few things of note:

  • Resource.extend() cannot be used with spread.
  • RestEndpoint.push inherits the body type from its parent. Since 'GET' RestEndpoints don't use the body, you can simply set the getList body and it will only be used with unshift and push.

This means you can achieve what you desire by using the override version of Resource.extend

export const InternalUserResource = createResource({
    urlPrefix,
    path: 'internal/users/:userId',
    schema: InternalUser,
    Endpoint: AuthdEndpoint
}).extend({
    getList: {
        body: {} as InternalUser & { password: string }
    }
});

In an effort to help others avoid this confusion - was this the upgrade guide you were looking at? If not, could you link to what you were looking at?

PS) To help others more easily find issues, I would prefer new issue opened and simply include a reference to related issues when mentioning. It can be difficult to navigate in a block of comments for things, but including links ensures people can still find slightly related things easily.

@jacobcossman
Copy link

@ntucker thanks so much! Yes, the upgrade guide you linked is what I was looking at. I think a note under the Resource.extend() section for this specific use case would be very helpful.

Also noted about opening new issues 👍

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

No branches or pull requests

4 participants
@th3fallen @ntucker @jacobcossman and others