@gravity-ui/sdk

中文文档
2.2.0发布于 2026/11/09MITTypeScriptESM + CJS近 30 天下载 3.3万

SDK implementing work with REST/GRPC APIs

原始使用文档
文档目录8 节
  1. Install
  2. Usage
  3. Streaming Responses
  4. Invoking External Methods
  5. CancellablePromise
  6. Additional methods
  7. sdk.setLang()
  8. sdk.setDefaultHeader()

@gravity-ui/sdk · npm package CI

SDK implementing work with REST/GRPC APIs

Install

npm install --save-dev @gravity-ui/sdk

Usage

You need to import sdkFactory and Schema, and create an instance of the class.

If the schema is not divided into scopes:

import sdkFactory from '@gravity-ui/sdk';
import type Schema from '<schemas package>';

const config = {
  csrfToken: 'secret-token',
  endpoint: '/gateway',
};

const sdk = sdkFactory<{root: typeof Schema}>(config);

If the schema is divided into scopes:

import sdkFactory from '@gravity-ui/sdk';
import type Schema from '<schemas package>';
import type LocalSchema from '../../shared/schemas';

const config = {
  csrfToken: 'secret-token',
  endpoint: '/gateway',
};

const sdk = sdkFactory<{root: typeof Schema; local: typeof LocalSchema}>(config);

Structure of config:

import {AxiosRequestConfig} from 'axios';

interface SdkConfig {
  // Custom Axios settings
  axiosConfig?: AxiosRequestConfig;
  // CSRF-token
  csrfToken?: string;
  // The endpoint to which the request from the client will be sent. By default, "/api" is used
  endpoint?: string;
  // Custom error handler. If the configuration is not specified, the default one is used
  handleRequestError?: (error: unknown) => any;
}

In the code, the invocation of the SDK method looks like this:

sdk.<scope>.<service>.<action>(data, options); // => returns a CancelablePromise

If the default endpoint value /api is specified, the full request path will look like this:

/api/:scope/:service/:action

There is a special scope called root. Its methods can be called without explicitly specifying the scope.

The following calls are equivalent:

sdk.root.<service>.<action>(data, options);

sdk.<service>.<action>(data, options);

But the full request path will always include the scope:

/api/root/:service/:action

Structure of options:

interface SdkOptions {
  // Request identifier. The previous request with the same identifier will be canceled
  concurrentId?: string;
  // Ability to specify a specific number of retries for certain endpoints. By default, the number of retries is 0
  retries?: number;
  // Ability to specify a specific timeout for certain endpoints.
  // By default, the configuration is taken from axiosConfig or set to 60 seconds
  timeout?: number;
  // Ability to specify specific headers when making a call
  headers?: Record<string, unknown>;
  // Abort the request, including a response stream that is being read
  signal?: AbortSignal;
}

Streaming Responses

With stream: true the request goes through the axios fetch adapter, and the promise resolves with the response body as a ReadableStream<Uint8Array> as soon as the headers arrive. The gateway action must stream its response, e.g. a REST action with responseType: 'stream'.

const stream = await sdk.<scope>.<service>.<action>(data, {stream: true, signal});
const reader = stream.pipeThrough(new TextDecoderStream()).getReader();

for (let chunk = await reader.read(); !chunk.done; chunk = await reader.read()) {
  console.log(chunk.value);
}

The timeout keeps running until the stream ends, so stream requests have no timeout unless timeout is passed. Once the response headers arrive, cancel() and concurrentId no longer affect the request: stop the stream with signal or reader.cancel(). Gateway errors are read from the body and rejected as usual; only signal aborts reading an error body.

Invoking External Methods

Through the sdk, you have the ability to make calls to external methods that are not defined in the schemas. Such a call looks like this:

sdk(config, options);

The argument config has the type AxiosRequestConfig, and the argument options has the type SdkOptions described above.

CancellablePromise

For the convenience of creating cancelable promises, the package exports a class called CancellablePromise(promise: Promise, cancel: () => void).

Additional methods

sdk.setLang()

Allows setting the user's language, which will be passed to all invoked endpoints through a special accept-language header.

sdk.setDefaultHeader()

Allows setting base headers that will be passed to all invoked endpoints.

版本与发布记录

@gravity-ui/sdk 共发布 15 个版本,首次发布于 2023/08/26;过去 12 个月发布了 3 个版本。

版本发布日期距今
2.2.0latest2026/11/097 天前
2.1.12026/11/0214 天前
2.1.02026/04/246 个月前
2.0.02025/05/061.5 年前
1.6.1-alpha.0untagged2025/05/061.5 年前
1.6.02025/02/211.7 年前

关于 @gravity-ui/sdk 的常见问题

@gravity-ui/sdk 是什么?

SDK implementing work with REST/GRPC APIs

如何安装 @gravity-ui/sdk?

在项目目录运行 npm install @gravity-ui/sdk,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@gravity-ui/gateway(>=1.0.4)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@gravity-ui/sdk 支持 TypeScript 吗?

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

@gravity-ui/sdk 支持 ESM 还是 CommonJS?

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

@gravity-ui/sdk 可以商用吗?

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

@gravity-ui/sdk 的下载量有多少?

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

@gravity-ui/sdk 还在维护吗?

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