TypeScript
首先阅读 REST 指南,创建平台 API 令牌。本示例在具备 fetch 的服务器上运行,例如 Node.js 18 或更新版本。将令牌保存在服务器上,不要放入浏览器代码。
type Catalog = {catalogId: string; name: string; kind: string};
type CatalogDetail = Catalog & {description: string};
type CatalogPage = {catalogs?: Catalog[]; nextPageToken?: string};
const baseUrl = process.env.CR_RECONSTRUCT_REST_URL ?? 'https://reconstruct.dev/api/v1/platform';
const token = process.env.CR_RECONSTRUCT_API_TOKEN;
if (!token) throw new Error('Set CR_RECONSTRUCT_API_TOKEN');
async function getJson<T>(path: string): Promise<T> {
const response = await fetch(new URL(path, `${baseUrl.replace(/\/$/, '')}/`), {
headers: {Authorization: `Bearer ${token}`},
});
if (!response.ok) throw new Error(`API returned HTTP ${response.status}`);
return response.json() as Promise<T>;
}
async function main() {
const page = await getJson<CatalogPage>('catalogs?pageSize=10');
for (const catalog of page.catalogs ?? []) {
const detail = await getJson<CatalogDetail>(
`catalogs/${encodeURIComponent(catalog.catalogId)}`,
);
console.log(detail.catalogId, detail.description);
}
if (page.nextPageToken) console.log('Next page token:', page.nextPageToken);
}
main().catch(console.error);
将 nextPageToken 作为 URL 编码的 pageToken 查询参数传入,以获取下一页。