最小公开读取
使用同一业务客户端读取站点与文章,避免额外状态或数据库依赖。
浏览器调用
在已有同源API代理的应用中创建业务客户端,在交互或组件生命周期中调用读取函数。不要把localhost:8084写死进发布的浏览器包。
TypeScript
import type { Shell } from '@mosaic-base/shell';
import type { ArticleSDK, SiteSDK, I18nSDK } from '@mosaic-base/client-sdk';
import type { AuthClient, ServerAuthClient } from '@mosaic-base/auth-sdk';
// Declare once in your application's type contract, never import SDK implementations.
declare module '@mosaic-base/shell/contracts' {
interface SdkRegistry {
'site.config': Pick<SiteSDK, 'getConfig'>;
'content.articles': Pick<ArticleSDK, 'list' | 'getBySlug' | 'getPreview' | 'search'>;
'i18n.messages': Pick<I18nSDK, 'load' | 't' | 'clearCache' | 'refresh'>;
'auth.client': AuthClient | ServerAuthClient;
}
}
import { createModuleLoader, readPublicBootstrap, startShell } from '@mosaic-base/shell/bootstrap';
export async function createBrowserApplication(appId: string) {
const bootstrap = await readPublicBootstrap(appId);
return startShell({ bootstrap, loader: createModuleLoader({ target: 'browser' }),
host: { apiBaseUrl: new URL('/api', location.origin).href } });
}
export async function readPublishedArticles(shell: Shell) {
const articles = await shell.getSdk('content.articles');
return articles.list({ language: 'en-US', page: 1, pageSize: 10 });
}消费结果
使用items渲染列表;detail为null时显示不存在;请求抛错时显示可重试故障。正文只能来自可信的已清洗内容链路。