Ana içeriğe geç

TypeScript

Platform API belirteci oluşturmak için REST kılavuzuyla başlayın. Bu örnek, Node.js 18 veya üstü gibi fetch kullanılabilen bir sunucuda çalışır. Belirteci tarayıcı kodunda değil sunucuda tutun.

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);

Sonraki sayfayı almak için nextPageToken değerini URL kodlamalı pageToken sorgu parametresi olarak geçirin.