Stivo
Stivo2y ago

Is it possible to create a generic router to pass a type on?

Hi, I was wondering if it is possible to create a generic router to pass a type which is expected to return
1 Reply
Nick
Nick2y ago
Create a factory function. I've done this for CRUD entities which need all the same behaviour over a similar base type
function createCrudRouter<TEntity extends BaseEntity>(config: { entity: TEntity /* + inversion of control methods, etc */ }) {
return router({
create: /* etc */,
get: /* etc */,
list: /* etc */,
update: /* etc */,
deleted: /* etc */,
})
}
function createCrudRouter<TEntity extends BaseEntity>(config: { entity: TEntity /* + inversion of control methods, etc */ }) {
return router({
create: /* etc */,
get: /* etc */,
list: /* etc */,
update: /* etc */,
deleted: /* etc */,
})
}
In my case I'm doing this with a codegen'd client over a rest API, and so I had to come up with some selector methods to get the endpoint. For an ORM you can probably just pass the Entity class itself