Skip to content

add create member to resource class #2838

Open

Description

@th3fallen

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

Activity

  1. added
    enhancement
    on Sep 28, 2023
  2. ntucker commented on Oct 9, 2023

    @ntucker
    Collaborator

    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.

  3. th3fallen commented on Oct 9, 2023

    @th3fallen
    Author

    the latter mostly

  4. ntucker commented on Oct 9, 2023

    @ntucker
    Collaborator

    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?

  5. ntucker commented on Oct 9, 2023

    @ntucker
    Collaborator

    For context, the API was modeled after Backbone collections

  6. ntucker commented on Oct 19, 2023

    @ntucker
    Collaborator

    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?

  7. jacobcossman commented on Dec 27, 2023

    @jacobcossman

    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?

  8. ntucker commented on Dec 28, 2023

    @ntucker
    Collaborator

    @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.

  9. jacobcossman commented on Dec 28, 2023

    @jacobcossman

    @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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions