@aspida/swr

中文文档
1.14.0发布于 2023/08/16MITTypeScriptESM + CJS零依赖近 30 天下载 5.8万

SWR wrapper for aspida

原始使用文档
文档目录7 节
  1. Getting Started
  2. Installation
  3. Make HTTP request from application
  4. Get response body/status/headers
  5. Use with SWR options
  6. Conditional Fetching
  7. License

@aspida/swr


aspida


Getting Started

Installation

  • Using npm:

    $ npm install aspida @aspida/swr @aspida/axios swr axios
    # $ npm install aspida @aspida/swr @aspida/fetch swr
    # $ npm install aspida @aspida/swr @aspida/node-fetch swr node-fetch
    
  • Using Yarn:

    $ yarn add aspida @aspida/swr @aspida/axios swr axios
    # $ yarn add aspida @aspida/swr @aspida/fetch swr
    # $ yarn add aspida @aspida/swr @aspida/node-fetch swr node-fetch
    

Make HTTP request from application

src/index.ts

import aspida from "@aspida/axios"; // "@aspida/fetch", "@aspida/node-fetch"
import useAspidaSWR from "@aspida/swr";
import api from "../api/$api";

const client = api(aspida());

function Profile() {
  const { data, error } = useAspidaSWR(client.user._userId(123), { query: { name: "mario" } });

  if (error) return <div>failed to load</div>;
  if (!data) return <div>loading...</div>;
  return <div>hello {data.name}!</div>;
}

Get response body/status/headers

src/index.ts

import aspida from "@aspida/axios"; // "@aspida/fetch", "@aspida/node-fetch"
import useAspidaSWR from "@aspida/swr";
import api from "../api/$api";

const client = api(aspida());

function Profile() {
  const { data, error } = useAspidaSWR(client.user._userId(123), "get", {
    query: { name: "mario" },
  });

  if (error) return <div>failed to load</div>;
  if (!data) return <div>loading...</div>;
  return (
    <>
      <div>Status: {data.status}</div>
      <div>Headers: {JSON.stringify(data.headers)}</div>
      <div>Name: {data.body.name}</div>
    </>
  );
}

useAspidaSWR(client.user._userId(123), { query }) is an alias of useAspidaSWR(client.user._userId(123), "$get", { query })

Use with SWR options

src/index.ts

import aspida from "@aspida/axios"; // "@aspida/fetch", "@aspida/node-fetch"
import useAspidaSWR from "@aspida/swr";
import api from "../api/$api";

const client = api(aspida());

function Profile() {
  const { data, error } = useAspidaSWR(client.user._userId(123), {
    query: { name: "mario" },
    revalidateOnMount: true,
    initialData: { name: "anonymous" },
  });

  if (error) return <div>failed to load</div>;
  return <div>hello {data.name}!</div>;
}

Conditional Fetching

If you use the enabled = false option, AspidaSWR will not start the request.

const { data: user } = useAspidaSWR(client.user);
const { data } = useAspidaSWR(client.articles, {
  query: { userId: user?.id ?? 0 },
  enabled: !!user,
});
// is an alias of
const { data } = useSWR(
  user ? [client.articles.$path({ query: { userId: user.id } }), "$get"] : null,
  () => client.articles.$get({ query: { userId: user.id } })
);

License

@aspida/swr is licensed under a MIT License.

版本与发布记录

@aspida/swr 共发布 37 个版本,首次发布于 2020/09/02;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.14.0latest2023/08/163 年前
1.13.32023/08/043 年前
1.13.22023/08/043 年前
1.13.12023/08/043 年前
1.13.02023/08/033 年前
1.12.02023/04/093 年前

关于 @aspida/swr 的常见问题

@aspida/swr 是什么?

SWR wrapper for aspida

如何安装 @aspida/swr?

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

@aspida/swr 支持 TypeScript 吗?

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

@aspida/swr 支持 ESM 还是 CommonJS?

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

@aspida/swr 可以商用吗?

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

@aspida/swr 的下载量有多少?

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

@aspida/swr 还在维护吗?

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