Skip to content

@redux-requests/core v1.1.0

Compare
Choose a tag to compare
@klis87 klis87 released this 01 Oct 21:52
· 137 commits to master since this release

Massively improved Typescript types. Added generics to RequestAction type, so that you can define data structure there, for example:

import { RequestAction } from '@redux-requests/core';

const fetchBooks: () => RequestAction<
  { raw: boolean },
  { parsed: boolean }
> = () => {
  return {
    type: 'FETCH_BOOKS',
    request: {
      url: '/books',
    },
    meta: {
      getData: data => ({ parsed: data.raw }),
    },
  };
};

2nd generic defaults to 1st, it is useful when you transform data in getData.

Ok, but why do we even care? It would be easy to just add types to getData if we needed.

The answer is, that those data types are now automatically inferred in querySelector and getQuerySelector if you define request actions with libraries like redux-smart-actions, redux-act or redux-actions - that's it, when you don't write constants and you pass actions themselves as type.

So, if you do:

import { RequestAction } from '@redux-requests/core';
import { createSmartAction } from 'redux-smart-actions';

const fetchBooks: () => RequestAction<
  { raw: boolean },
  { parsed: boolean }
> = createSmartAction(() => {
  return {
    request: {
      url: '/books',
    },
    meta: {
      getData: data => ({ parsed: data.raw }),
    },
  };
});

then if you use getQuery:

const booksQuery = getQuery(state, { type: fetchBooks });

then booksQuery.data will be automatically typed! Even if you have only fetchBooks written in typescript, but for some reason getQuery is in js file, then still you will get this like autocomplete for free! So the point is, add type only in request actions and enjoy types automatically everywhere you read query state.