@atlas/client

Klien fetch bertipe untuk /v1/public dengan revalidasi ETag. Berjalan di Node 22 dan runtime apa pun dengan fetch.

Baca 1 menit

Instal

Keempat paket @atlas/* berbagi satu versi dan diterbitkan secara privat ke GitHub Packages sebagai @lifefuturedev/atlas-*. Tulis ke [email protected] untuk mendapat akses bagi tim Anda, lalu arahkan scope ke GitHub Packages dan beri alias pada paketnya.

.npmrcbash
@lifefuturedev:registry=https://npm.pkg.github.com
//npm.pkg.github.com/:_authToken=${NODE_AUTH_TOKEN}
package.jsonjson
{
  "dependencies": {
    "@atlas/client": "npm:@lifefuturedev/[email protected]"
  }
}

Buat klien

atlas.tstypescript
import { createAtlasClient } from '@atlas/client'

export const atlas = createAtlasClient({
  baseUrl: 'https://api.atlascms.id',
  token: process.env.ATLAS_TOKEN!,
  timeoutMs: 8000,
})
OpsiDeskripsi
baseUrlWajib. Alamat API, tanpa path di belakangnya.
tokenWajib. Token baca.
previewTokenOpsional. Menggantikan token, membuat draf terjangkau, dan mematikan cache.
fetchOpsional. fetch milik Anda sendiri.
cacheOpsional. Cache ETag untuk dibagikan. Defaultnya LRU dalam memori berisi 500 respons.
timeoutMsOpsional. Batalkan permintaan setelah selama ini.

Metode

MetodeMengembalikan
getPage({ path, locale })Halaman, atau null pada 404. Media sudah diselesaikan.
getRoute({ path, locale })Route dan tema dari path yang tidak dijawab endpoint halaman, atau null.
getEntries({ type, locale, cursor, limit, term, taxonomy, fields, count }){ data, next_cursor, count }.
getMenu(key, locale)Menu dengan item yang sudah disanitasi, atau null.
getTerms({ taxonomy, slug })Daftar term beserta path arsipnya.
getSettings()Pengaturan brand yang ditandai publik oleh registry, atau null.
getTheme({ locale }){ tokens, options }.
getStrings({ locale, namespace })String antarmuka, atau null.
getSitemapUrls(), getRedirects()Daftar untuk sitemap dan peta redirect.
getLlms(locale), getLlmsText(locale)Data atau teks untuk llms.txt.
getManifest(), getIndexNowKey()Web manifest dan key IndexNow.

Baca halaman dan daftar

read.tstypescript
const page = await atlas.getPage({ path: '/pricing', locale: 'en' })
if (!page) throw new Error('No such page')

let cursor: string | undefined
const posts = []
do {
  const batch = await atlas.getEntries({ type: 'post', locale: 'en', limit: 25, cursor })
  posts.push(...batch.data)
  cursor = batch.next_cursor ?? undefined
} while (cursor)

Error dan cache

  • 404 mengembalikan null. Status gagal lainnya melempar AtlasApiError dengan status dan url.
  • Klien menyimpan ETag dari setiap respons dan mengirim If-None-Match pada waktu berikutnya. 304 mengembalikan body yang tersimpan, sehingga pembacaan berulang murah.
  • Gunakan satu klien per brand dan pertahankan tetap hidup. Dengan previewToken tidak ada yang di-cache, karena API menjawab private, no-store.
errors.tstypescript
import { AtlasApiError } from '@atlas/client'

try {
  await atlas.getPage({ path: '/' })
} catch (error) {
  if (error instanceof AtlasApiError && error.status === 429) {
    // back off and try again
  }
  throw error
}

Apakah halaman ini membantu?

Usulkan perubahan