跳到主要內容

型別定義

Blocklet SDK 是強型別的,可幫助您編寫更健壯且易於維護的程式碼。本節提供了在處理使用者會話、通知、事件和 blocklet 設定時會遇到的最常見 TypeScript 型別和介面的參考。了解這些型別將有助於確保型別安全,並有效地利用您 IDE 的自動完成功能。

Session 與使用者型別

這些型別是管理應用程式內使用者驗證和身份的基礎。

SessionUser

SessionUser 物件由 session 中介軟體附加到 request 物件上(作為 req.user)。它包含了當前登入使用者的基本資訊。

SessionUser Type Definition

typescript
export type SessionUser = {
  did: string;
  role: string | undefined;
  provider: string;
  fullName: string;
  walletOS: string;
  emailVerified?: boolean;
  phoneVerified?: boolean;
  method?: AuthMethod;
  kyc?: number;
  [key: string]: any;
};
  • did string (required) — 使用者的去中心化識別碼 (DID)。
  • role string | undefined — 分配給使用者的角色(例如,'admin'、'owner'、'guest')。
  • provider string (required) — 用於登入的身份驗證提供者(例如,'wallet')。
  • fullName string (required) — 使用者的全名。
  • walletOS string (required) — 使用者錢包的作業系統。
  • emailVerified boolean — 表示使用者的電子郵件是否已驗證。
  • phoneVerified boolean — 表示使用者的電話是否已驗證。
  • method AuthMethod — 使用的身份驗證方法。常見值為 'loginToken'、'componentCall'、'signedToken'、'accessKey'。
  • kyc number — 使用者 KYC 狀態的數字表示。

TUserInfo

TUserInfo 型別提供了使用者個人資料的全面視圖,包括其身份、聯絡資訊、登入歷史以及相關的安全憑證。

  • did string (required) — 使用者的去中心化識別碼 (DID)。
  • pk string (required) — 使用者的公鑰。
  • role string (required) — 使用者的主要角色。
  • avatar string (required) — 使用者頭像圖片的 URL。
  • fullName string (required) — 使用者的全名。
  • email string (required) — 使用者的電子郵件地址。
  • approved boolean (required) — 表示使用者帳戶是否已獲批准。
  • createdAt number (required) — 使用者建立時的時間戳。
  • lastLoginAt number (required) — 使用者上次登入的時間戳。
  • passports TPassport[] (required) — 發給使用者的護照陣列。
  • connectedAccounts TConnectedAccount[] (required) — 連結到使用者個人資料的外部帳戶列表。

通知型別

當使用通知服務時,您將使用這些型別來建構並向使用者傳送訊息。

TNotification

這是定義通知的主要介面。它包含了控制通知內容、外觀和行為所需的所有欄位。

  • id string — 通知的唯一識別碼。
  • title string — 通知的主標題。
  • body string — 通知的主要內容或訊息。
  • type 'notification' | 'connect' | 'feed' | 'hi' | 'passthrough' — 通知的型別,這會影響其處理和呈現方式。
  • severity 'normal' | 'success' | 'error' | 'warning' — 嚴重性等級,常用於為通知上色。
  • actions TNotificationAction[] — 包含在通知中的互動式操作按鈕陣列。
  • attachments TNotificationAttachment[] — 富內容附件的陣列,例如圖片、文字區塊或連結。
  • activity TNotificationActivity — 描述觸發通知的社交活動,如評論或追蹤。
  • url string — 點擊通知時要導航到的 URL。

TNotificationAttachment

附件允許您向通知中添加豐富的結構化內容。

  • type 'asset' | 'vc' | 'token' | 'text' | 'image' | 'divider' | 'transaction' | 'dapp' | 'link' | 'section' (required) — 要顯示的內容型別。
  • data any — 附件的資料負載,其內容因型別而異。例如,'image' 型別將會有一個包含 url 屬性的物件。
  • fields any — 在附件中顯示的附加欄位,常用於 'section' 型別。

範例:圖片附件

json
{
  "type": "image",
  "data": {
    "url": "https://path.to/your/image.png",
    "alt": "圖片的描述性文字"
  }
}

TNotificationAction

操作是可添加到通知中的互動式按鈕,允許使用者直接回應。

  • name string (required) — 操作的名稱,通常用作識別碼。
  • title string — 按鈕上顯示的文字。如果未提供,則預設為 name。
  • link string — 點擊按鈕時要導航到的 URL。
  • color string — 按鈕的文字顏色。
  • bgColor string — 按鈕的背景顏色。

事件型別

在使用事件匯流排時,事件使用 TEvent 介面進行結構化。

TEvent

此介面定義了在 Blocklet 生態系統中發出和消費的事件結構。

  • id string (required) — 事件實例的唯一識別碼。
  • type string (required) — 事件型別名稱(例如,'user'、'post')。
  • time Date (required) — 事件發生的時間戳。
  • source unknown (required) — 事件的來源或發起者。
  • spec_version string (required) — CloudEvents 規範版本。
  • object_id string — 事件相關物件的 ID。
  • object_type string — 事件相關物件的型別。
  • data object (required) — 事件的負載,包含有關所發生事件的詳細資訊。

設定與狀態型別

這些型別定義了您的 blocklet 的設定物件和狀態資訊的結構。

WindowBlocklet

在客戶端,window.blocklet 物件提供了有關正在執行的 blocklet 的基本上下文。此物件的型別為 WindowBlocklet

  • did string (required) — blocklet 實例的 DID。
  • appId string (required) — 應用程式 ID。
  • appName string (required) — 應用程式的顯示名稱。
  • appUrl string (required) — 應用程式的公開 URL。
  • webWalletUrl string (required) — 相關網頁錢包的 URL。
  • isComponent boolean (required) — 如果 blocklet 作為另一個 blocklet 的組件執行,則為 true。
  • prefix string (required) — blocklet 路由的 URL 前綴。
  • theme TTheme (required) — 目前 UI 的主題設定。
  • navigation TNavigationItem[] (required) — 應用程式選單的導航項目陣列。

TBlockletState

此型別表示伺服器上 blocklet 實例的完整狀態,包括其元資料、狀態、設定以及與其他組件的關係。

  • meta TBlockletMeta — blocklet 的元資料,來自其 blocklet.yml 檔案。
  • status enum_pb.BlockletStatusMap (required) — blocklet 的目前執行狀態(例如,'running'、'stopped')。
  • port number (required) — blocklet 正在執行的連接埠。
  • appDid string (required) — blocklet 實例的 DID。
  • children TComponentState[] (required) — 如果此 blocklet 有子組件,則為組件狀態的陣列。
  • settings TBlockletSettings — 使用者為 blocklet 配置的設定。
  • environments TConfigEntry[] (required) — 為 blocklet 配置的環境變數列表。