@abraham/remotedata

中文文档
2.0.0发布于 2026/11/02MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 2558

Lightweight TypeScript RemoteData implementation

原始使用文档
文档目录3 节
  1. Install
  2. Usage
  3. Helpers

Version Status Build Status npm bundle size (minified + gzip)

RemoteData

Lightweight TypeScript RemoteData implementation

Install

npm install @abraham/remotedata

Usage

Read Slaying a UI Antipattern with Web Components (and TypeScript) for a more thorough guide.

This is an example class that gets a post (type Post) from a remote HTTP API.

import {
  Failure,
  fold,
  Initialized,
  Pending,
  Success,
  type RemoteData,
} from '@abraham/remotedata';

interface Post {
  title: string;
}

// Define what the RemoteData Failure and Success types will be.
type State = RemoteData<number, Post>;

class Thing {
  // Set the initial state as Initialized.
  private state: State = new Initialized();

  // Use `fold` to easily define logic based on the current state.
  // Fold takes four methods as parameters in the order of Initialized, Pending, Failure, and
  // Success and returns a method that takes a `RemoteData` state instance as a parameter. When
  // the returned method gets called with a `RemoteData` state, the method matching the correct
  // state gets called.
  private view = fold<string, number, Post>(
    // What to do if the state is Initialized.
    () => 'Initialized',
    // What to do if the request is Pending.
    () => 'Loading...',
    // What to do if the request fails.
    (error) => `Error: ${error}`,
    // What to do if the request succeeds.
    (post) => `Got post ${post.title}`,
  );

  constructor() {
    // On instantiation start the remote call and render the current state.
    void this.getPost();
    this.render();
  }

  private async getPost(): Promise<void> {
    // Before starting the remote call set the current state to Pending.
    this.state = new Pending();
    // Perform the actual remote request.
    const response = await fetch('https://api.example.com/post');
    if (response.ok) {
      // If the request succeeds set the state to Success with the returned data.
      const post = (await response.json()) as Post;
      this.state = new Success(post);
    } else {
      // If the request does not succeed set the state to Failure with the reason.
      this.state = new Failure(response.status);
    }
    // Render the new state.
    this.render();
  }

  private render(): void {
    // Render the current state in your UI.
    // `this.view` is executed with the current state as a parameter.
    const text = this.view(this.state);
    // Maybe put it in a template.
  }
}

Using included type guards.

import { isSuccess, type RemoteData } from '@abraham/remotedata';
import { getState, type Post } from './state';

// Define what the RemoteData Failure and Success types will be.
type State = RemoteData<number, Post>;

const state: State = getState();
if (isSuccess(state)) {
  // state is known to be of type Success<Post>
  console.log(state.data.title);
}

Helpers

import {
  andThen,
  combine,
  fromPromise,
  map,
  mapError,
  withDefault,
} from '@abraham/remotedata';

// Success or Failure from a promise.
const post = await fromPromise(fetchPost());
// Transform Success data or the Failure error; other states pass through.
const title = map(post, (p) => p.title);
const message = mapError(post, (error) => String(error));
// Chain into another RemoteData when Success.
const author = andThen(post, (p) => getAuthor(p.authorId));
// Success data or a fallback.
const text = withDefault(title, 'Untitled');
// First Failure, else Pending, else Initialized, else Success with all values.
const both = combine([post, author]);

References

版本与发布记录

@abraham/remotedata 共发布 20 个版本,首次发布于 2018/07/14;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.0.0latest2026/11/025 天前
1.4.02024/09/222 年前
1.3.02022/10/304 年前
1.2.02022/10/054 年前
1.1.02022/03/224 年前
1.0.02022/02/204 年前

关于 @abraham/remotedata 的常见问题

@abraham/remotedata 是什么?

Lightweight TypeScript RemoteData implementation

如何安装 @abraham/remotedata?

在项目目录运行 npm install @abraham/remotedata,或使用 pnpm add / yarn add / bun add。

@abraham/remotedata 支持 TypeScript 吗?

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

@abraham/remotedata 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@abraham/remotedata 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@abraham/remotedata 可以商用吗?

@abraham/remotedata 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@abraham/remotedata 的下载量有多少?

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

@abraham/remotedata 还在维护吗?

最新版本 2.0.0 发布于 2026/11/02(5 天前);至今共发布 20 个版本,其中过去 12 个月发布了 1 个;首次发布于 2018/07/14。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。