diff --git a/packages/client/src/crud/CRUD.test.tsx b/packages/client/src/crud/CRUD.test.tsx index b2a350d..d223a2e 100644 --- a/packages/client/src/crud/CRUD.test.tsx +++ b/packages/client/src/crud/CRUD.test.tsx @@ -58,8 +58,8 @@ describe('CRUD', () => { const http = new HTTPClientMock() const actions = createCRUDActions( http, - '/one/:oneId/two', '/one/:oneId/two/:twoId', + '/one/:oneId/two', 'TEST', ) const crudReducer = new CRUDReducer('TEST') @@ -249,7 +249,6 @@ describe('CRUD', () => { it('updates state', async () => { const action = dispatch(testCase.method, actions[method]({ - query: undefined, params: testCase.params, body: testCase.body, })) @@ -300,7 +299,6 @@ describe('CRUD', () => { expect(store.getState().Crud.ids).toEqual([entity.id]) const action2 = store.dispatch(actions.remove({ params: removeTestCase.params, - body: removeTestCase.body, })) await action2.payload expect(store.getState().Crud.ids).toEqual([]) diff --git a/packages/client/src/crud/CRUDActions.ts b/packages/client/src/crud/CRUDActions.ts index 9c46d9d..eda55fa 100644 --- a/packages/client/src/crud/CRUDActions.ts +++ b/packages/client/src/crud/CRUDActions.ts @@ -1,12 +1,10 @@ import {ICRUDAction} from './ICRUDAction' import {ICRUDMethod} from './ICRUDMethod' import {IHTTPClient, ITypedRequestParams} from '../http' -import {IRoutes} from '@rondo/common' +import {IRoutes, Filter, OnlyDefined} from '@rondo/common' export type Optional = T extends {} ? T : undefined -type Filter = T extends U ? T : never - type Action = Filter, {method: Method, status: 'pending'}> @@ -22,12 +20,13 @@ export class SaveActionCreator< readonly type: ActionType, ) {} - save = (params: { + save = (params: OnlyDefined<{ body: T[Route]['post']['body'], params: T[Route]['post']['params'], - }): Action => { + }>): Action => { + const p = params as any return { - payload: this.http.post(this.route, params.body, params.params), + payload: this.http.post(this.route, p.body, p.params), type: this.type, method: 'save', status: 'pending', @@ -47,12 +46,13 @@ export class FindOneActionCreator< readonly type: ActionType, ) {} - findOne = (params: { + findOne = (params: OnlyDefined<{ query: Optional, params: T[Route]['get']['params'], - }): Action => { + }>): Action => { + const p = params as any return { - payload: this.http.get(this.route, params.query, params.params), + payload: this.http.get(this.route, p.query, p.params), type: this.type, method: 'findOne', status: 'pending', @@ -73,12 +73,13 @@ export class UpdateActionCreator< readonly type: ActionType, ) {} - update = (params: { + update = (params: OnlyDefined<{ body: T[Route]['put']['body'], params: T[Route]['put']['params'], - }): Action => { + }>): Action => { + const p = params as any return { - payload: this.http.put(this.route, params.body, params.params), + payload: this.http.put(this.route, p.body, p.params), type: this.type, method: 'update', status: 'pending', @@ -99,12 +100,13 @@ export class RemoveActionCreator< readonly type: ActionType, ) {} - remove = (params: { - body: T[Route]['delete']['body'], + remove = (params: OnlyDefined<{ + body: Optional, params: T[Route]['delete']['params'], - }): Action => { + }>): Action => { + const p = params as any return { - payload: this.http.delete(this.route, params.body, params.params), + payload: this.http.delete(this.route, p.body, p.params), type: this.type, method: 'remove', status: 'pending', @@ -124,12 +126,18 @@ export class FindManyActionCreator< readonly type: ActionType, ) {} - findMany = (params: { + findMany = (params: OnlyDefined<{ query: Optional, params: T[Route]['get']['params'], - }): Action => { + }>): { + payload: Promise + type: ActionType + status: 'pending', + method: 'findMany', + } => { + const p = params as any return { - payload: this.http.get(this.route, params.query, params.params), + payload: this.http.get(this.route, p.query, p.params), type: this.type, method: 'findMany', status: 'pending', @@ -149,11 +157,11 @@ export function createCRUDActions< listRoute: ListRoute, actionType: ActionType, ) { - const {save} = new SaveActionCreator(http, entityRoute, actionType) - const {update} = new UpdateActionCreator(http, listRoute, actionType) - const {remove} = new RemoveActionCreator(http, listRoute, actionType) - const {findOne} = new FindOneActionCreator(http, listRoute, actionType) - const {findMany} = new FindManyActionCreator(http, entityRoute, actionType) + const {save} = new SaveActionCreator(http, listRoute, actionType) + const {update} = new UpdateActionCreator(http, entityRoute, actionType) + const {remove} = new RemoveActionCreator(http, entityRoute, actionType) + const {findOne} = new FindOneActionCreator(http, entityRoute, actionType) + const {findMany} = new FindManyActionCreator(http, listRoute, actionType) return {save, update, remove, findOne, findMany} } diff --git a/packages/client/src/crud/CRUDReducer.ts b/packages/client/src/crud/CRUDReducer.ts index f9feec0..2cf9f8c 100644 --- a/packages/client/src/crud/CRUDReducer.ts +++ b/packages/client/src/crud/CRUDReducer.ts @@ -1,9 +1,7 @@ import {IAction, IResolvedAction} from '../actions' import {ICRUDAction} from './ICRUDAction' import {ICRUDMethod} from './ICRUDMethod' -import {indexBy, without} from '@rondo/common' - -type Filter = T extends U ? T : never +import {indexBy, without, Filter} from '@rondo/common' export interface ICRUDEntity { readonly id: number diff --git a/packages/client/src/crud/jerko.ts b/packages/client/src/crud/jerko.ts new file mode 100644 index 0000000..f3ef6d0 --- /dev/null +++ b/packages/client/src/crud/jerko.ts @@ -0,0 +1,31 @@ +export type NonUndefinedPropertyNames = { + [K in keyof T]: T[K] extends undefined ? never: K +}[keyof T] + +export type OnlyRequired = Pick> + +type Args1 = OnlyRequired<{ + query: {a: number, b: string} + params: {c: number} +}> + +type Args2 = OnlyRequired<{ + query: {a: number, b: string} + params: undefined +}> + +type Args3 = OnlyRequired<{ + query: undefined + params: undefined +}> + +const a: Args1 = { + query: {a: 1, b: 'two'}, + params: {c: 3}, +} + +const b: Args2 = { + query: {a: 1, b: 'two'}, +} + +const c: Args3 = {}