包详细信息

@devexperts/remote-data-ts

devexperts69.6kMPL-2.02.1.1

RemoteData type

typescript, algebraic-data-types, functional-programming

自述文件

RemoteData type Build Status

Description

RemoteData is an ADT (algebraic data type) described in this article. Heavily based on fp-ts lib.

Installation

npm i --save @devexperts/remote-data-ts

How to lift (wrap) your data in RemoteData:

As you remember RemoteData is an union of few types: RemoteInitial, RemotePending, RemoteFailure and RemoteSuccess.

While your data in initial or pending state just use initial or pending constant, because you don't have any real values in this case.

import { initial, pending } from '@devexperts/remote-data-ts';

const customers = initial;
// or
const customers = pending;

When you receive data from server, use failure or success function, it depends on what you received:

import { failure, success } from '@devexperts/remote-data-ts';
import { apiClient } from 'apiClient';
import { TCustomer } from './MyModel';

const getCustomers = (): RemoteData<TCustomer[]> => {
   const rawData: TCustomer[] = apiClient.get('/customers');

   try {
        const length = rawData.length;

        return success(rawData);
   }
   catch(err) {
        return failure(new Error('parse error'));
   }
}

How to fold (unwrap) your data from RemoteData:

Finally you pass data to the component and want to render values, so now it's time to get our values back from RemoteData wrapper:

import { NoData, Pending, Failure } from './MyPlaceholders';
import { TCustomer } from './MyModel';

type TCustomersList = {
    entities: RemoteData<TCustomer[]>;
};

const CustomersList: SFC<TCustomersList> = ({ entities }) => entities.foldL(
    () => <NoData />,
    () => <Pending />,
    err => <Failure error={err} />,
    data => <ul>{data.map(item => <li>{item.name}</li>)}</ul>
);

Docs & Examples

Coming soon (check the source)

Contributions

Publish

Don't forget to run npm run changelog and to commit the changes

更新日志

2.1.1 (2021-09-09)

Bug Fixes

  • invalid main and typings path in package.json (#68) (4ca54c3)

2.1.0 (2021-08-17)

Features

  • Update constructors to receive all generics (#61) (674a2ac), closes #61

Bug Fixes

  • Correct RemoteDataT3 constrant for M (URIS4 -> URIS3) (f81f3b8)

BREAKING CHANGES

  • generic parameters changed for success and failure constructors

2.0.4 (2020-08-11)

Bug Fixes

  • rewrite imports from lib to es6 in es6 output (c6899d8), closes #46 #49

2.0.3 (2020-01-24)

Bug Fixes

  • fix bimap, mapLeft, extend to forward progress correctly (#45) (be78635)

2.0.2 (2020-01-17)

2.0.0 (2019-08-23)

Code Refactoring

feature

BREAKING CHANGES

  • transformer was completely rewritten
  • removed classes
  • simplified io-ts codec
  • fp-ts and io-ts-types dependencies updated to latest stable version

0.6.0 (2019-05-26)

Bug Fixes

Features

0.5.0 (2019-01-31)

Bug Fixes

  • move peer dependencies to dependencies (#24) (0812d29), closes #23

Features

  • add io-ts type (#19) (7d6785f)
  • major update - Fail fast ap, TS/fp-ts/io-ts upgrade, Traversable2v, Bifunctor (#28) (3955c17), closes #26
  • provide progress parameter via fold pending parameter (#20) (1cf41ce)
  • relax getMonoid dependencies to Semigroup instances instead of Monoid (#21) (d7b060e)

BREAKING CHANGES

  • new status priority, new dependencies, see #28 for more info
  • add io-ts and io-ts-types to peer-dependencies

0.3.1 (2018-10-16)

Features

0.3.0 (2018-09-25)

Features

  • add "recover" method (27d5591), closes #12
  • Add progress to RemotePending (4c89823), closes #9
  • update to TS@2.8.1, fp-ts@1.2.0 + implement Monoidal (d9a4a09)

0.2.0 (2018-03-20)