Skip to main content

Blocklet SDK (Browser)

The Blocklet JS SDK is the official frontend library provided to assist in the development of blocklets.

It mainly includes the following functions:

  1. Provide fetch and axios creation functions that can automatically update tokens.
  2. Provide user profile related interface requests

Initialize the instance

typescript
import { BlockletSDK } from '@blocklet/js-sdk';
const sdk = new BlockletSDK();

User Information Example

user.getUserPublicInfo

Get user's public information.

typescript
type UserPublicInfo = {
  avatar: string;
  did: string;
  fullName: string;
};
const publicInfo: UserPublicInfo = await sdk.user.getUserPublicInfo(userDid: string);

user.getProfileUrl

Get user personal center address

typescript
const profileUrl = await sdk.user.getProfileUrl({did: string; locale: string});

user.logout

Log out

javascript
await sdk.user.logout();

Create an axios instance

createAxios

This example will manage the update of sessionToken and refreshToken.

typescript
import { createAxios } from '@blocklet/js-sdk';
import type { AxiosRequestConfig, AxiosInstance } from 'axios';

type RequestParams = {
  lazy?: boolean; // 是否需要将请求延迟响应
  lazyTime?: number; // 延迟响应的时间,默认 300 ms
};

const api = createAxios(config?: AxiosRequestConfig, requestParams?:RequestParams)

createFetch

This instance will manage the updating of sessionToken and refreshToken.

javascript
import { createFetch } from '@blocklet/js-sdk';

type RequestParams = {
  lazy?: boolean; // 是否需要将请求延迟响应
  lazyTime?: number; // 延迟响应的时间,默认 300 ms
};

const api = createFetch(options?: RequestInit, requestParams?:RequestParams)