web-font/src/api.ts
崮生(子虚) ca1d2fc1e7 feat(webfont-sdk): 0.2.0 新增服务端 API 客户端子路径 webfont-sdk/api
- createWebFontApi 工厂封装全部 6 个 REST 接口(fonts/fontMeta/
  config/stats/upload/reportEvent)+ 全套类型,独立子路径导出不进主入口
- leafer demo 删除硬编码 FONTS 数组,改 api.fonts() 实时拉取
  (24 个字体 vs 原硬编码 6 个),初始字体带回退、失败显式报错
- 主站 src/api.ts 重写为 SDK 客户端薄适配层(dogfooding):
  旧类型名保留为别名组件零改动,顺带消灭 FontInfo 重复声明
- /docs 文档页新增 npm 安装、双模式用法、API 客户端示例(中英)
- 主站 package.json 添加 workspace 依赖 webfont-sdk

已发布 npm webfont-sdk@0.2.0,CDN 与 demo 端到端验证通过。
2026-08-15 20:35:17 +08:00

81 lines
2.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 主站 API 适配层 —— 基于官方 SDK 客户端(webfont-sdk/api)的薄封装
*
* 所有类型与请求逻辑统一走 webfont-sdk:接口变更只改 SDK 一处,
* 主站(dogfooding)、leafer 插件 demo、npm 用户共享同一实现。
*
* 保留原有的函数签名与旧类型别名(FontInfo / FontMeta / ...),
* 组件层零改动即可切换;新代码建议直接 import SDK 类型。
*
* baseUrl 说明:主站与后端同源(dev 走 vite proxy /api → 8087),
* 传空串 baseUrl 后客户端拼出相对路径 "/api/...",同源与代理都正确。
*/
import {
createWebFontApi,
type IApiFontInfo,
type IApiFontMeta,
type IApiServerConfig,
type IApiServerStats,
type IApiUploadResult,
} from "webfont-sdk/api";
const api = createWebFontApi({ baseUrl: "" });
/** 字体列表项(旧名兼容;新代码用 IApiFontInfo) */
export type FontInfo = IApiFontInfo & {
/** 旧字段(后端实际不返回,恒 undefined),dev 预览页模板引用,保留可选 */
dir?: string;
};
/** 字体元数据(旧名兼容;新代码用 IApiFontMeta) */
export type FontMeta = IApiFontMeta;
/** 服务公开配置(旧名兼容;新代码用 IApiServerConfig) */
export type ServerConfig = IApiServerConfig;
/** 上传结果(旧名兼容;新代码用 IApiUploadResult) */
export type UploadResult = IApiUploadResult;
/** 运行统计(旧名兼容;新代码用 IApiServerStats) */
export type ServerStats = IApiServerStats;
export { type IApiCharsetCoverage as CharsetCoverage, type IApiFontUserConfig as FontUserConfig } from "webfont-sdk/api";
export async function fetchFonts(): Promise<FontInfo[]> {
return api.fonts();
}
export async function fetchFontMeta(fontName: string): Promise<FontMeta> {
return api.fontMeta(fontName);
}
export async function fetchConfig(): Promise<ServerConfig> {
return api.config();
}
export async function fetchStats(): Promise<ServerStats> {
return api.stats();
}
/**
* 上传字体(临时 / 管理员)
*
* SDK 客户端用 FormData 上传,主站调用方传的是 File —— 直接透传,
* FormData.append(name, File) 会自动带上 filename。
*/
export async function uploadFont(
file: File,
mode: "temp" | "admin",
apiKey?: string,
): Promise<UploadResult> {
return api.upload({ data: file, filename: file.name, mode, apiKey });
}
/**
* 离线裁剪匿名事件上报 —— 透传 SDK 实现
* (sendBeacon 优先,页面卸载也能送达;只发事件类型,无内容数据)
*/
export function reportOfflineEvent(event: "offline_subset" | "offline_download"): void {
api.reportEvent(event);
}