ReSift

ReSift

  • Docs
  • API
  • Help
  • GitHub

โ€บGuides

Introduction

  • What is ReSift?
  • Installation

Tutorial

  • ReSift Rentals

Main Concepts

  • What's a fetch?
  • How to define a fetch
  • Making state consistent
  • Making sense of statuses
  • What are data services?
  • Error handling
  • Custom hooks

Examples

  • ReSift Notes (CRUD)
  • Infinite scroll
  • Custom hooks and React Router

Guides

  • ReSift vs Apollo and Relay
  • HTTP proxies
  • Usage with TypeScript
  • Usage with Redux
  • Usage with classes

API

  • About these docs
  • useStatus
  • useError
  • useDispatch
  • useData
  • useClearFetch
  • isUnknown
  • isNormal
  • isLoading
  • isError
  • defineFetch
  • dataServiceReducer
  • createStoreKey
  • createHttpService
  • createHttpProxy
  • createDataService
  • createActionType
  • combineStatuses
  • ResiftProvider
  • Guard
  • CanceledError
  • UNKNOWN
  • NORMAL
  • LOADING
  • ERROR
Edit

Usage with TypeScript

We love TypeScript at Sift. We use it in our production apps and we care deeply about getting the developer experience of TypeScript right within ReSift.

Proper usage with TypeScript requires that you type your fetch factories and data services.

Getting the types of the data services

When creating your data services, use the typing helper ServicesFrom, to get the type of the services from your services object.

import { createDataService, createHttpService, ServicesFrom } from 'resift';

const http = createHttpService(/* ... */);
const services = { http };

export type Services = ServicesFrom<typeof services>;

const dataService = createDataService({
  services,
  onError: (e) => {
    throw e;
  },
});

export default dataService;

You can then import this where you define your fetch factories.

Setting types for fetch factories

In order to add the correct type to the fetch factories, you need to use the helper typedFetchFactory. This helper allows you to set a type for your fetch. This type will be used by the useData and the Guard components.

makeUpdatePerson.ts

import { defineFetch, typedFetchFactory } from 'resift';
import { Services } from './dataService'; // this was created from the above example

interface Person {
  id: string;
  name: string;
}

const makeUpdatePerson = defineFetch({
  displayName: 'Update Person',
  // add types like so ๐Ÿ‘‡
  make: (personId: string) => ({
    // add types like so       ๐Ÿ‘‡
    request: (updatedPerson: Person) => ({ http }: Services) =>
      http({
        method: 'GET',
        route: `/people/${personId}`,
      }),
  }),
});

// then export with `typedFetchFactory` with the type of the data
//                                 ๐Ÿ‘‡
export default typedFetchFactory<Person>()(makeUpdatePerson);

This fetch factory asserts that the shape of the data is the shape of the Person.

When you go to use this fetch factory, it should just work.

import React from 'react';

import makeGetPerson from './makeGetPerson';
import makeUpdatePerson from './makeUpdatePerson';
import getPersonFromEvent from './getPersonFromEvent';

interface Props {
  id: string;
}

function Component({ id }: Props) {
  // typescript will enforce that this arg is a string
  //                                    ๐Ÿ‘‡
  const updatePerson = makeUpdatePerson(id);
  const getPerson = makeUpdatePerson(id);

  const handleUpdate = (e: React.FormEvent<unknown>) => {
    const person = getPersonFromEvent(e);

    // typescript will enforce that this is the correct type
    //                      ๐Ÿ‘‡
    dispatch(updatePerson(person));
  };

  // typescript will type this as `Person | null`
  const person = useData(getPerson);

  return (
    <Guard fetch={getPerson}>
      {(person) => {
        // ๐Ÿ‘† typescript knows the shape of this
        return person.name;
      }}
    </Guard>
  );
}

export default Component;
Last updated on 4/1/2020
โ† HTTP proxiesUsage with Redux โ†’
  • Getting the types of the data services
  • Setting types for fetch factories
ReSift
Docs
What is ReSift?What's a fetch?API
Community
Ask a questionOpen an issue@ meLeave feedback
More
StarBuild StatusCoverage Status
Copyright ยฉ 2021 Sift
www.justsift.com