@devexperts/remote-data-ts

中文文档
2.1.1发布于 2021/09/09MPL-2.0TypeScriptESM + CJS零依赖近 30 天下载 3.3万

RemoteData type

原始使用文档
文档目录7 节
  1. Description
  2. Installation
  3. How to lift (wrap) your data in RemoteData:
  4. How to fold (unwrap) your data from RemoteData:
  5. Docs & Examples
  6. Contributions
  7. Publish

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

版本与发布记录

@devexperts/remote-data-ts 共发布 17 个版本,首次发布于 2018/03/13;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.1.1latest2021/09/095 年前
2.1.02021/09/075 年前
2.0.52021/02/085 年前
2.0.42020/08/116 年前
2.0.32020/01/246 年前
2.0.22020/01/176 年前

关于 @devexperts/remote-data-ts 的常见问题

@devexperts/remote-data-ts 是什么?

RemoteData type

如何安装 @devexperts/remote-data-ts?

在项目目录运行 npm install @devexperts/remote-data-ts,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:fp-ts(^2.0.0)、io-ts(^2.0.0)、io-ts-types(^0.5.7)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@devexperts/remote-data-ts 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@devexperts/remote-data-ts 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@devexperts/remote-data-ts 可以商用吗?

@devexperts/remote-data-ts 使用 MPL-2.0 许可。弱 Copyleft:可以商用,也可以和闭源代码一起使用;但如果分发时修改了 MPL 授权的文件,这些文件的修改需以 MPL 开源。以上为概括,不构成法律意见,请以许可原文为准。

@devexperts/remote-data-ts 的下载量有多少?

根据 npm 官方下载统计,@devexperts/remote-data-ts 在 2026/10/12 – 2026/11/10 这 30 天内被下载 32,744 次,日均约 1,091 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@devexperts/remote-data-ts 还在维护吗?

最新版本 2.1.1 发布于 2021/09/09(5 年前);至今共发布 17 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/03/13。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。