enhance: createResource() -> resource() #3158
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
Reduced verbosity.
Solution
Open questions
Why not
new Resource()
?Class works for RestEndpoint, because the 'project-wide' code sharing are all non-type specific - things that do not affect the type of RestEndpoint. RestEndpoint's typing comes from it's schema, and function signature which are expected to be different in each instance, with some small similarities being doable with .extend().
However, for resources, while there are a few non-typed overrides like optimisticPartial, getName, Endpoint, Collection, many cases we want to establish common method shapes. This means affecting the type of the
resource
. (Like paginationField default, or getList shape to handle pagination). Because of this, classes fundamentally do not work from a TypeScript perspective. TypeScript simply does not allow changes in types of descendants in any way but specialization (aka extends). This means it is impractical for many common cases of 'Base class' to use class inheritance, which means we would need to fallback to a function anyway. Having two dominant ways of doing things seems confusing, so we will go with 'lowest common denominator'.Is
resource()
lower case with no action confusing?Convention is to have instances use PascalCase, so having a variable named
resource
would be against convention. Sinceresource
is camelCase, it is clear it should not be called withnew