@definedfi/sdk

中文文档
0.5.6发布于 2024/07/04MITTypeScriptCommonJS近 30 天下载 3万

The Defined.fi SDK for JavaScript/Typescript. It provides generated types and convenient ways to access the graphql api.

原始使用文档
文档目录7 节
  1. Installation
  2. Usage
  3. Running the examples
  4. Simple
  5. Codegen
  6. Next
  7. Contributing

Defined SDK

This package exists to help you develop on top of the Defined API (https://docs.defined.fi).

It provides the public schema SDL for you to use. You can use graphql-codegen to generate types and queries for example.

Installation

packagercommand
npmnpm add @definedfi/sdk
yarnyarn add @definedfi/sdk
bunbun add @definedfi/sdk

Usage

Follow one of the examples in the examples directory, or simply run.

Fetch a token.

import { Defined } from "@definedfi/sdk";

const sdk = new Defined(MY_API_KEY);

sdk.queries
  .token({
    input: {
      address: "0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c",
      networkId: 56,
    },
  })
  .then(console.log);

Use your own GraphQL selections

import { Network } from "../../src/resources/graphql";
import { Defined } from "@definedfi/sdk/dist/sdk";

const sdk = new Defined(process.env.DEFINED_API_KEY || "");

sdk
  .send<{ getNetworks: Network[] }>(
    `
  query GetNetworks {
    getNetworks { id name }
  }
`,
    {}
  )
  .then((res) => {
    console.log("Networks: ", res.getNetworks);
  });

Running the examples

You'll need to have curl installed in order to build this locally, as it fetches the schema from the Defined API.

You need to provide an API key in order for the examples to work. We have bun in use for development here.

After installing bun, from the project root.

  • bun i
  • bun run build

Simple

This performs a simple inline graphql request, and uses a user-provided query and selection set.

  • cd examples/simple
  • bun i
  • DEFINED_API_KEY=xyz bun run index.ts

You can define your own GraphQL queries and use those with codegen (see next section). The pre-defined queries we provide in the examples do not include all of the fields for every query.

Codegen

This shows how to use graphql-codegen to generate query types and get a fully typed end-to-end experience.

  • cd examples/codegen
  • bun i
  • bun run codegen
  • DEFINED_API_KEY=xyz bun run src/index.ts

Next

This shows how you could use it in a NextJS project.

  • cd examples/next
  • bun i
  • NEXT_PUBLIC_DEFINED_API_KEY=xyz bun run dev

Contributing

Prs open!

版本与发布记录

@definedfi/sdk 共发布 14 个版本,首次发布于 2023/08/30;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.5.6latest2024/07/042 年前
0.5.52024/05/242 年前
0.5.42024/05/222 年前
0.5.32024/05/142 年前
0.5.22024/05/082 年前
0.5.12023/11/212 年前

关于 @definedfi/sdk 的常见问题

@definedfi/sdk 是什么?

The Defined.fi SDK for JavaScript/Typescript. It provides generated types and convenient ways to access the graphql api.

如何安装 @definedfi/sdk?

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

@definedfi/sdk 支持 TypeScript 吗?

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

@definedfi/sdk 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@definedfi/sdk 需要什么版本的 Node.js?

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

@definedfi/sdk 可以商用吗?

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

@definedfi/sdk 的下载量有多少?

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

@definedfi/sdk 还在维护吗?

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