@smoothcdn/api-accelerator-sdk
v1.0.4
Published
SDK for consuming Smooth API Accelerator JSON outputs from Smooth CDN.
Maintainers
Readme
@smoothcdn/api-accelerator-sdk
TypeScript SDK for consuming JSON outputs generated by the Smooth API Accelerator plugin and hosted on Smooth CDN.
Scope
core- API client with token support- framework adapters:
next,nuxt,astro,sveltekit,remix - adapters include framework-oriented helpers for caching, revalidation, runtime fetch, and loader integration
Installation
npm install @smoothcdn/api-accelerator-sdkCore
import { createApiClient } from "@smoothcdn/api-accelerator-sdk";
const client = createApiClient({
userSlug: "example-user",
projectSlug: "my-project",
token: process.env.SMOOTHCDN_TOKEN
});
const posts = await client.fetch("/wp/v2/posts", {
fields: ["id", "title"]
});Query API
fields-> appends_fields=...(for example_fields=id,title)- dot notation is not supported:
title.renderedis normalized totitle page-> fetches paginated JSON from/page-N.jsonfetchAll()-> fetches collection metadata first and then combines every page into one arrayjoin-> replaces selected fields with extra fetches based on route templateswhere-> appends filters as query params- when
pageis omitted,fetch()prefers/page-1.jsonif/listcontains that route /listis resolved lazily whenfetch()orfetchAll()runs, not duringcreateApiClient()
import { createApiClient } from "@smoothcdn/api-accelerator-sdk";
const client = createApiClient({
userSlug: "example-user",
projectSlug: "my-project",
token: process.env.SMOOTHCDN_TOKEN
});
const post = await client.fetch("/wp/v2/posts", {
page: 1,
fields: ["id", "title"],
where: { id: 123 }
});For example:
client.fetch("/wp/v2/posts", { page: 1 })
fetches:
https://cdn.smoothcdn.com/<user-slug>/<project-slug>/wp/v2/posts/page-1.json
const allPosts = await client.fetchAll("/wp/v2/posts", {
fields: ["id", "title"]
});const posts = await client.fetch("/wp/v2/posts", {
join: {
author: "/wp/v2/users/[author]"
}
});join works for both a single object and arrays. For every item, the placeholder value is taken from the current result item and the fetched response replaces that field.
Next.js
import { createApiClient } from "@smoothcdn/api-accelerator-sdk/next";
const next = createApiClient({
userSlug: "example-user",
projectSlug: "my-project",
token: process.env.SMOOTHCDN_TOKEN
});
export async function getStaticProps() {
return next.createStaticProps("/wp/v2/pages", { revalidate: 60 });
}
export async function getFeaturedPost() {
return next.getRouteData("/wp/v2/posts", {
fields: ["id", "title"],
where: { id: 123 },
cache: "force-cache",
next: {
revalidate: 300,
tags: ["posts", "featured-post"]
}
});
}createStaticProps() uses fetchAll() for paginated collections.
Nuxt
import { createApiClient } from "@smoothcdn/api-accelerator-sdk/nuxt";
const nuxt = createApiClient({
userSlug: "example-user",
projectSlug: "my-project",
token: process.env.SMOOTHCDN_TOKEN
});
export default defineComponent({
setup() {
const requestFetch = useRequestFetch();
return nuxt.useRouteDataWithAutoKey(useAsyncData, "/wp/v2/posts", {
fetch: requestFetch,
asyncData: {
dedupe: "defer",
lazy: true
}
});
}
});useRouteData() and useRouteDataWithAutoKey() use fetchAll() for paginated collections.
Astro
import { createApiClient } from "@smoothcdn/api-accelerator-sdk/astro";
const astro = createApiClient({
userSlug: "example-user",
projectSlug: "my-project"
});
const loadPosts = astro.createLoader("/wp/v2/posts", {
fields: ["id", "title"]
});
const posts = await loadPosts({ fetch });createLoader() uses fetchAll() for paginated collections.
SvelteKit
import { createApiClient } from "@smoothcdn/api-accelerator-sdk/sveltekit";
const sveltekit = createApiClient({
userSlug: "example-user",
projectSlug: "my-project"
});
export const load = sveltekit.createLoad("/wp/v2/posts", {
fields: ["id", "title"],
depends: ["smooth:posts"],
cacheControl: "public, max-age=60"
});createLoad() uses fetchAll() for paginated collections.
Remix
import { json } from "@remix-run/node";
import { createApiClient } from "@smoothcdn/api-accelerator-sdk/remix";
const remix = createApiClient({
userSlug: "example-user",
projectSlug: "my-project",
token: process.env.SMOOTHCDN_TOKEN
});
export const loader = remix.createJsonLoader(json, "/wp/v2/posts", {
fields: ["id", "title"],
where: { id: 123 },
useRequestSignal: true
});createLoader() and createJsonLoader() use fetchAll() for paginated collections.
