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.
This PR refactors the implementation of service methods. This makes life easier and also simplifies the implementation of tests.
Major changes include:
ServiceMethod
andServiceMethodHandler
into one as they were almost identical.Action
andActionNoData
bindParams(action, ...)
andbindData(action, ...)
as a replacment foraction.bind(null, ...)
. They are functions which take in an action as their first argument and bind the relevant argument.**withData
when creating service methods. This is inferred from whether or not a data validation is present..client('NEW')
has been changed to.newClient()
. The latter takes fives keystrokes (.
+n
+tab
+(
+)
) as opposed to eight (.
+n
+tab
+(
+'
+N
+tab
+)
).*Note on params validation: I have currently implemented param validation with bare zod schemas. I did not use Validations as two steps of validation is unnecessary for params I think (and also maybe for data?). I did not change the Validation as that would be out of the scope of this PR. We need to discuss how it should be implemented properly as having two ways of creating validation is messy.
**Note on binding: I did not manage to implement the binding functions with dot notation as server actions cannot be objects, nor can they be functions with custom properties, thus this was the only way to implement something close to what we wanted. Another thing: Since server actions will be "normal functions" I also think that we should only bind arguments wherever necessary, as opposed to binding them first and the calling them immediately as was done previously. I.e. we should avoid writing
bindParams(action, ...)()
(the new version ofaction.bind(null, ...)()
, and simply doaction(...)
instead.***Note on index files for service: I think ideally all services for a model should be defined in the same file (
index.ts
or maybeservice.ts
, we need to decide on a name. I think Johan doesn't like index files if I'm not mistaken), in stead of it importing all the functions fromcreate.ts
,read.ts
,update.ts
anddestroy.ts
. I have not had the time to do this so I have simply removed these files for now.Minor changes include:
Things that I think still need to be done:
create.ts
,read.ts
,update.ts
,destroy.ts
in services into one file. Where alle the definitions of the service methods live in a single object. Preferably with a naming convention likeuserService
,lockerService
,cmsImageService
, etc...create.ts
,read.ts
,update.ts
,destroy.ts
in actions into one file. Most files with the new system will be only a few lines, so fragmenting the definitions will be more disorganized than organized I think.