/** * 平台换票所用的 Logto API Resource Indicator 约定值。 * * 对应 access_token 的 `aud`;平台 API 验签、平台 Web / api-only 业务前端的 * `LogtoConfig.resources` 与 `getAccessToken(resource)` 须与此字符串完全一致 *(含尾斜杠)。稳定标识符,不必真实可达。 * * 平台侧可用环境变量 `LOGTO_API_RESOURCE` 覆盖;业务前端直接 import 本常量即可, * 无需各应用单独配置。 * * @see docs/auth-token-exchange-proposal.md */ declare const STAGO_LOGTO_API_RESOURCE = "https://stago.platform/api"; /** * @stago/sdk/client — 业务前端运行时(api-only 模块的前端)。 * * 统一收口:logto access_token → 换 Stago JWT → 存储 → 带 Bearer 请求 → 401 懒刷新。 * 纯 Web API(fetch/localStorage),不依赖 Elysia/React,浏览器 bundle 零后端代码。 */ interface SessionUser { id: number; username: string; displayName: string | null; roles: string[]; } interface StorageLike { getItem(key: string): string | null; setItem(key: string, value: string): void; removeItem(key: string): void; } interface StagoClientOptions { /** * API 基址(可含部署前缀),如 `https://api.zhangchuwang.cn/stago`。 * login → `{apiBaseUrl}{loginPath}`;业务请求 → `{apiBaseUrl}{prefix}{path}`。 */ apiBaseUrl: string; /** * 模块 api.prefix(与 stago.config.ts 一致),如 `/tasks`。 * fetch('/list') → `{apiBaseUrl}/tasks/list`。 */ prefix?: string; /** * 换票路径(相对 apiBaseUrl)。默认 `/auth/logto-callback`。 * 若网关仍挂在平台 `/api` 下,可显式设为 `/api/auth/logto-callback`。 */ loginPath?: string; /** * 取 logto access_token(须针对平台 API Resource)。 * 推荐 `logto.getAccessToken(STAGO_LOGTO_API_RESOURCE)`,并与 * `LogtoConfig.resources: [STAGO_LOGTO_API_RESOURCE]` 一致。 */ getAccessToken: () => Promise; /** 默认 localStorage,可注入(SSR/测试) */ storage?: StorageLike; /** 默认全局 fetch,可注入测试 */ fetch?: typeof globalThis.fetch; /** 请求超时,默认 30s */ timeout?: number; } /** 公开换票路径默认值(相对 apiBaseUrl,不含平台内部的 /api 前缀) */ declare const DEFAULT_LOGIN_PATH = "/auth/logto-callback"; interface StagoFetchInit extends Omit { /** query 参数,自动序列化拼到 URL */ query?: Record; /** json body,自动 stringify + 设 content-type */ json?: unknown; body?: BodyInit; } interface StagoClient { login(): Promise; getToken(): string | null; getUser(): SessionUser | null; fetch(path: string, init?: StagoFetchInit): Promise; logout(): Promise; } declare const TOKEN_KEY = "stago.token"; declare const USER_KEY = "stago.user"; declare class StagoClientError extends Error { constructor(message: string); } declare class AuthError extends StagoClientError { readonly code?: string | undefined; constructor(message: string, code?: string | undefined); } declare class ApiError extends StagoClientError { readonly status: number; readonly body?: unknown | undefined; constructor(message: string, status: number, body?: unknown | undefined); } declare function createStagoClient(options: StagoClientOptions): StagoClient; export { ApiError, AuthError, DEFAULT_LOGIN_PATH, STAGO_LOGTO_API_RESOURCE, type SessionUser, type StagoClient, StagoClientError, type StagoClientOptions, type StagoFetchInit, type StorageLike, TOKEN_KEY, USER_KEY, createStagoClient };