buildo / avenger

A CQRS-flavoured data fetching and caching layer in TypeScript. Batching, caching, data-dependencies and manual invalidations in a declarative fashion for Node and the browser
MIT License
59 stars 2 forks source link

Add Query mapLeft #205

Closed giogonzo closed 5 years ago

giogonzo commented 5 years ago

While doing some backporting, I came across some instances of commands that are:

E.g.

declare function addUser: () => TaskEither<'generic' | 'specific', string>
declare const users: ObservableQuery<void, unknown, Array<User>>

const doAddUser = command(addUser, { users })

Given how types are written at the moment, the resulting doAddUser will return a TaskEither<unknown, string> because the resulting L is computed as CmdL | InvaldiationsL, and 'generic' | 'specific' | unknown is just = unknown

This is desired in general (and you can always spot it downstream when you are not able e.g. to be exhaustive in matching the error). But at the same time, it is currently hard/impossible to make the invalidations L more specific without changing the original query logic.

I think adding a mapLeft on Query would allow to use it inline when needed from command declarations, e.g.:

const doAddUser = command(addUser, { users: mapLeft(users, () => 'generic') })