@cimplify/sdk 0.38.5 → 0.39.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,92 @@
1
+ import { createCimplifyClient } from './chunk-3WD6U5RD.mjs';
2
+ import './chunk-EZK2FXTM.mjs';
3
+ export { CimplifyError } from './chunk-6TVQQYQ5.mjs';
4
+ import { cache } from 'react';
5
+
6
+ var DEFAULT_DEV_URL = "http://127.0.0.1:8787";
7
+ function readEnv(...keys) {
8
+ const env = globalThis.process?.env;
9
+ if (!env) return void 0;
10
+ for (const k of keys) {
11
+ const v = env[k]?.trim();
12
+ if (v) return v;
13
+ }
14
+ return void 0;
15
+ }
16
+ var getServerClient = cache((opts = {}) => {
17
+ const baseUrl = opts.apiUrl ?? readEnv("CIMPLIFY_API_URL", "NEXT_PUBLIC_CIMPLIFY_API_URL") ?? DEFAULT_DEV_URL;
18
+ const publicKey = opts.secretKey ?? readEnv("CIMPLIFY_SECRET_KEY", "NEXT_PUBLIC_CIMPLIFY_PUBLIC_KEY") ?? "mock-dev";
19
+ const client = createCimplifyClient({
20
+ baseUrl,
21
+ publicKey,
22
+ suppressPublicKeyWarning: true
23
+ });
24
+ if (opts.locationId) {
25
+ client.setLocationId(opts.locationId);
26
+ }
27
+ return client;
28
+ });
29
+
30
+ // src/server/tags.ts
31
+ var tags = {
32
+ products: () => "cimplify:products",
33
+ product: (id) => `cimplify:product:${id}`,
34
+ categories: () => "cimplify:categories",
35
+ category: (id) => `cimplify:category:${id}`,
36
+ categoryProducts: (id) => `cimplify:category:${id}:products`,
37
+ collections: () => "cimplify:collections",
38
+ collection: (id) => `cimplify:collection:${id}`,
39
+ collectionProducts: (id) => `cimplify:collection:${id}:products`,
40
+ business: () => "cimplify:business",
41
+ locations: () => "cimplify:locations",
42
+ orders: (customerId) => `cimplify:orders:${customerId}`,
43
+ order: (id) => `cimplify:order:${id}`
44
+ };
45
+
46
+ // src/server/revalidate.ts
47
+ var _revalidateTag = null;
48
+ async function getRevalidateTag() {
49
+ if (_revalidateTag) return _revalidateTag;
50
+ const specifier = "next/cache";
51
+ const mod = await import(
52
+ /* webpackIgnore: true */
53
+ /* @vite-ignore */
54
+ specifier
55
+ );
56
+ _revalidateTag = mod.revalidateTag;
57
+ return _revalidateTag;
58
+ }
59
+ async function revalidate(...tagList) {
60
+ const fn = await getRevalidateTag();
61
+ for (const t of tagList) fn(t);
62
+ }
63
+ async function revalidateProducts() {
64
+ return revalidate(tags.products());
65
+ }
66
+ async function revalidateProduct(id) {
67
+ return revalidate(tags.product(id), tags.products());
68
+ }
69
+ async function revalidateCategories() {
70
+ return revalidate(tags.categories());
71
+ }
72
+ async function revalidateCategory(id) {
73
+ return revalidate(tags.category(id), tags.categoryProducts(id), tags.categories());
74
+ }
75
+ async function revalidateCollections() {
76
+ return revalidate(tags.collections());
77
+ }
78
+ async function revalidateCollection(id) {
79
+ return revalidate(
80
+ tags.collection(id),
81
+ tags.collectionProducts(id),
82
+ tags.collections()
83
+ );
84
+ }
85
+ async function revalidateBusiness() {
86
+ return revalidate(tags.business());
87
+ }
88
+ async function revalidateByTag(tag) {
89
+ return revalidate(tag);
90
+ }
91
+
92
+ export { getServerClient, revalidateBusiness, revalidateByTag, revalidateCategories, revalidateCategory, revalidateCollection, revalidateCollections, revalidateProduct, revalidateProducts, tags };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cimplify/sdk",
3
- "version": "0.38.5",
3
+ "version": "0.39.0",
4
4
  "description": "Cimplify Commerce SDK for storefronts",
5
5
  "keywords": [
6
6
  "cimplify",
@@ -45,6 +45,12 @@
45
45
  "import": "./dist/advanced.mjs",
46
46
  "require": "./dist/advanced.js"
47
47
  },
48
+ "./server": {
49
+ "types": "./dist/server.d.ts",
50
+ "react-server": "./dist/server.mjs",
51
+ "import": "./dist/server.mjs",
52
+ "require": "./dist/server.js"
53
+ },
48
54
  "./mock": {
49
55
  "types": "./dist/mock/library.d.ts",
50
56
  "import": "./dist/mock/library.mjs",
@@ -1,59 +1,55 @@
1
- "use client";
2
-
3
- import { useEffect, useState, use } from "react";
1
+ import type { Metadata } from "next";
4
2
  import Link from "next/link";
5
- import { ProductGrid, useCimplifyClient } from "@cimplify/sdk/react";
6
- import type { Category, Product } from "@cimplify/sdk";
3
+ import { notFound } from "next/navigation";
4
+ import { cacheTag, cacheLife } from "next/cache";
5
+ import { ProductGrid } from "@cimplify/sdk/react";
6
+ import { getServerClient, tags, type Category, type Product } from "@cimplify/sdk/server";
7
7
  import { StoreProductCard } from "@/components/store-product-card";
8
8
 
9
- /**
10
- * Full grid of products within a single category. Reads `slug` from the
11
- * route, resolves it against the catalogue API, and renders the SDK
12
- * <ProductGrid/> with our URL-modal-aware <StoreProductCard/>.
13
- */
14
- export default function CategoryPage({ params }: { params: Promise<{ slug: string }> }) {
15
- const { slug } = use(params);
16
- const { client } = useCimplifyClient();
17
- const [category, setCategory] = useState<Category>();
18
- const [products, setProducts] = useState<Product[]>([]);
19
- const [loading, setLoading] = useState(true);
9
+ async function getCategory(slug: string): Promise<{
10
+ category: Category;
11
+ products: Product[];
12
+ } | null> {
13
+ "use cache";
14
+ cacheTag(tags.categories());
15
+ cacheLife("hours");
16
+
17
+ const client = getServerClient();
18
+ const catRes = await client.catalogue.getCategoryBySlug(slug);
19
+ if (!catRes.ok) return null;
20
+
21
+ cacheTag(tags.category(catRes.value.id), tags.categoryProducts(catRes.value.id));
22
+ const r = await client.catalogue.getCategoryProducts(catRes.value.id);
23
+ const products = r.ok
24
+ ? ((r.value as { items?: Product[] }).items ?? (r.value as Product[]))
25
+ : [];
26
+ return { category: catRes.value, products };
27
+ }
20
28
 
21
- useEffect(() => {
22
- let cancelled = false;
23
- void (async () => {
24
- try {
25
- const catRes = await client.catalogue.getCategoryBySlug(slug);
26
- if (cancelled || !catRes.ok) return;
27
- setCategory(catRes.value);
28
- const r = await client.catalogue.getCategoryProducts(catRes.value.id);
29
- if (cancelled) return;
30
- if (r.ok) {
31
- const items = (r.value as { items?: Product[] }).items ?? (r.value as Product[]);
32
- setProducts(items);
33
- }
34
- } finally {
35
- if (!cancelled) setLoading(false);
36
- }
37
- })();
38
- return () => {
39
- cancelled = true;
40
- };
41
- }, [client, slug]);
29
+ export async function generateMetadata({
30
+ params,
31
+ }: {
32
+ params: Promise<{ slug: string }>;
33
+ }): Promise<Metadata> {
34
+ const { slug } = await params;
35
+ const data = await getCategory(slug);
36
+ if (!data) return {};
37
+ return {
38
+ title: `${data.category.name} — Akua's Bakery`,
39
+ description: data.category.description ?? undefined,
40
+ };
41
+ }
42
42
 
43
- if (loading) {
44
- return <p className="text-center py-20 text-muted-foreground">Loading category…</p>;
45
- }
46
- if (!category) {
47
- return (
48
- <div className="text-center py-20 text-muted-foreground">
49
- <p>Category not found.</p>
50
- <Link href="/" className="text-primary font-semibold">
51
- ← Back home
52
- </Link>
53
- </div>
54
- );
55
- }
43
+ export default async function CategoryPage({
44
+ params,
45
+ }: {
46
+ params: Promise<{ slug: string }>;
47
+ }) {
48
+ const { slug } = await params;
49
+ const data = await getCategory(slug);
50
+ if (!data) notFound();
56
51
 
52
+ const { category, products } = data;
57
53
  return (
58
54
  <section className="max-w-7xl mx-auto px-8 pt-12">
59
55
  <header className="mb-8 text-center">
@@ -64,7 +60,9 @@ export default function CategoryPage({ params }: { params: Promise<{ slug: strin
64
60
  {category.name}
65
61
  </h1>
66
62
  {category.description && (
67
- <p className="mx-auto mb-2 max-w-xl text-muted-foreground">{category.description}</p>
63
+ <p className="mx-auto mb-2 max-w-xl text-muted-foreground">
64
+ {category.description}
65
+ </p>
68
66
  )}
69
67
  <p className="text-sm text-muted-foreground">
70
68
  {products.length} item{products.length === 1 ? "" : "s"}
@@ -75,6 +73,13 @@ export default function CategoryPage({ params }: { params: Promise<{ slug: strin
75
73
  emptyMessage="No products in this category yet."
76
74
  renderCard={(p) => <StoreProductCard product={p} />}
77
75
  />
76
+ {products.length === 0 && (
77
+ <p className="text-center mt-8">
78
+ <Link href="/" className="text-primary font-semibold">
79
+ ← Back home
80
+ </Link>
81
+ </p>
82
+ )}
78
83
  </section>
79
84
  );
80
85
  }
@@ -1,59 +1,55 @@
1
- "use client";
2
-
3
- import { useEffect, useState, use } from "react";
1
+ import type { Metadata } from "next";
4
2
  import Link from "next/link";
5
- import { ProductGrid, useCimplifyClient } from "@cimplify/sdk/react";
6
- import type { Collection, Product } from "@cimplify/sdk";
3
+ import { notFound } from "next/navigation";
4
+ import { cacheTag, cacheLife } from "next/cache";
5
+ import { ProductGrid } from "@cimplify/sdk/react";
6
+ import { getServerClient, tags, type Collection, type Product } from "@cimplify/sdk/server";
7
7
  import { StoreProductCard } from "@/components/store-product-card";
8
8
 
9
- /**
10
- * Full grid of products within a single collection. Reads `slug` from the
11
- * route, resolves it against the catalogue API, and renders the SDK
12
- * <ProductGrid/> with our URL-modal-aware <StoreProductCard/>.
13
- */
14
- export default function CollectionPage({ params }: { params: Promise<{ slug: string }> }) {
15
- const { slug } = use(params);
16
- const { client } = useCimplifyClient();
17
- const [collection, setCollection] = useState<Collection>();
18
- const [products, setProducts] = useState<Product[]>([]);
19
- const [loading, setLoading] = useState(true);
9
+ async function getCollection(slug: string): Promise<{
10
+ collection: Collection;
11
+ products: Product[];
12
+ } | null> {
13
+ "use cache";
14
+ cacheTag(tags.collections());
15
+ cacheLife("hours");
16
+
17
+ const client = getServerClient();
18
+ const colRes = await client.catalogue.getCollectionBySlug(slug);
19
+ if (!colRes.ok) return null;
20
+
21
+ cacheTag(tags.collection(colRes.value.id), tags.collectionProducts(colRes.value.id));
22
+ const r = await client.catalogue.getCollectionProducts(colRes.value.id);
23
+ const products = r.ok
24
+ ? ((r.value as { items?: Product[] }).items ?? (r.value as Product[]))
25
+ : [];
26
+ return { collection: colRes.value, products };
27
+ }
20
28
 
21
- useEffect(() => {
22
- let cancelled = false;
23
- void (async () => {
24
- try {
25
- const colRes = await client.catalogue.getCollectionBySlug(slug);
26
- if (cancelled || !colRes.ok) return;
27
- setCollection(colRes.value);
28
- const r = await client.catalogue.getCollectionProducts(colRes.value.id);
29
- if (cancelled) return;
30
- if (r.ok) {
31
- const items = (r.value as { items?: Product[] }).items ?? (r.value as Product[]);
32
- setProducts(items);
33
- }
34
- } finally {
35
- if (!cancelled) setLoading(false);
36
- }
37
- })();
38
- return () => {
39
- cancelled = true;
40
- };
41
- }, [client, slug]);
29
+ export async function generateMetadata({
30
+ params,
31
+ }: {
32
+ params: Promise<{ slug: string }>;
33
+ }): Promise<Metadata> {
34
+ const { slug } = await params;
35
+ const data = await getCollection(slug);
36
+ if (!data) return {};
37
+ return {
38
+ title: `${data.collection.name} — Akua's Bakery`,
39
+ description: data.collection.description ?? undefined,
40
+ };
41
+ }
42
42
 
43
- if (loading) {
44
- return <p className="text-center py-20 text-muted-foreground">Loading collection…</p>;
45
- }
46
- if (!collection) {
47
- return (
48
- <div className="text-center py-20 text-muted-foreground">
49
- <p>Collection not found.</p>
50
- <Link href="/" className="text-primary font-semibold">
51
- ← Back home
52
- </Link>
53
- </div>
54
- );
55
- }
43
+ export default async function CollectionPage({
44
+ params,
45
+ }: {
46
+ params: Promise<{ slug: string }>;
47
+ }) {
48
+ const { slug } = await params;
49
+ const data = await getCollection(slug);
50
+ if (!data) notFound();
56
51
 
52
+ const { collection, products } = data;
57
53
  return (
58
54
  <section className="max-w-7xl mx-auto px-8 pt-12">
59
55
  <header className="mb-8 text-center">
@@ -64,7 +60,9 @@ export default function CollectionPage({ params }: { params: Promise<{ slug: str
64
60
  {collection.name}
65
61
  </h1>
66
62
  {collection.description && (
67
- <p className="mx-auto mb-2 max-w-xl text-muted-foreground">{collection.description}</p>
63
+ <p className="mx-auto mb-2 max-w-xl text-muted-foreground">
64
+ {collection.description}
65
+ </p>
68
66
  )}
69
67
  <p className="text-sm text-muted-foreground">
70
68
  {products.length} item{products.length === 1 ? "" : "s"}
@@ -75,6 +73,13 @@ export default function CollectionPage({ params }: { params: Promise<{ slug: str
75
73
  emptyMessage="No products in this collection yet."
76
74
  renderCard={(p) => <StoreProductCard product={p} />}
77
75
  />
76
+ {products.length === 0 && (
77
+ <p className="text-center mt-8">
78
+ <Link href="/" className="text-primary font-semibold">
79
+ ← Back home
80
+ </Link>
81
+ </p>
82
+ )}
78
83
  </section>
79
84
  );
80
85
  }
@@ -1,48 +1,47 @@
1
- "use client";
2
-
3
- import { useEffect, useState } from "react";
4
- import { useCimplifyClient } from "@cimplify/sdk/react";
5
- import type { Category, Collection, Product } from "@cimplify/sdk";
1
+ import type { Metadata } from "next";
2
+ import { cacheTag, cacheLife } from "next/cache";
3
+ import { getServerClient, tags, type Product } from "@cimplify/sdk/server";
6
4
  import { Hero } from "@/components/hero";
7
5
  import { CollectionStrip } from "@/components/collection-strip";
8
6
  import { CategoryGrid } from "@/components/category-grid";
9
7
 
10
- interface CollectionWithProducts {
11
- collection: Collection;
12
- products: Product[];
13
- }
8
+ export const metadata: Metadata = {
9
+ title: "Akua's Bakery — Fresh from the oven, every day",
10
+ description:
11
+ "Sourdough, croissants, cakes, and more — handcrafted in East Legon, delivered across Accra.",
12
+ };
13
+
14
+ async function getHomeData() {
15
+ "use cache";
16
+ cacheTag(tags.collections(), tags.categories(), tags.products());
17
+ cacheLife("hours");
18
+
19
+ const client = getServerClient();
20
+ const [colRes, catRes] = await Promise.all([
21
+ client.catalogue.getCollections(),
22
+ client.catalogue.getCategories(),
23
+ ]);
24
+ const collections = colRes.ok ? colRes.value : [];
25
+ const categories = catRes.ok ? catRes.value : [];
14
26
 
15
- export default function HomePage() {
16
- const { client } = useCimplifyClient();
17
- const [collections, setCollections] = useState<CollectionWithProducts[]>([]);
18
- const [categories, setCategories] = useState<Category[]>([]);
27
+ const collectionsWithProducts = await Promise.all(
28
+ collections.map(async (col) => {
29
+ const r = await client.catalogue.getCollectionProducts(col.id);
30
+ const items = r.ok
31
+ ? ((r.value as { items?: Product[] }).items ?? (r.value as Product[]))
32
+ : [];
33
+ return { collection: col, products: items };
34
+ }),
35
+ );
36
+
37
+ return {
38
+ collections: collectionsWithProducts.filter((x) => x.products.length > 0),
39
+ categories,
40
+ };
41
+ }
19
42
 
20
- useEffect(() => {
21
- let cancelled = false;
22
- void (async () => {
23
- const [colRes, catRes] = await Promise.all([
24
- client.catalogue.getCollections(),
25
- client.catalogue.getCategories(),
26
- ]);
27
- if (cancelled) return;
28
- if (catRes.ok) setCategories(catRes.value);
29
- if (colRes.ok) {
30
- const withProducts = await Promise.all(
31
- colRes.value.map(async (col) => {
32
- const r = await client.catalogue.getCollectionProducts(col.id);
33
- const items = r.ok
34
- ? ((r.value as { items?: Product[] }).items ?? (r.value as Product[]))
35
- : [];
36
- return { collection: col, products: items };
37
- }),
38
- );
39
- if (!cancelled) setCollections(withProducts.filter((x) => x.products.length > 0));
40
- }
41
- })();
42
- return () => {
43
- cancelled = true;
44
- };
45
- }, [client]);
43
+ export default async function HomePage() {
44
+ const { collections, categories } = await getHomeData();
46
45
 
47
46
  return (
48
47
  <>
@@ -1,46 +1,30 @@
1
- "use client";
1
+ import type { Metadata } from "next";
2
+ import { cacheTag, cacheLife } from "next/cache";
3
+ import { getServerClient, tags } from "@cimplify/sdk/server";
4
+ import { ShopClient } from "./shop-client";
2
5
 
3
- import { useEffect, useState } from "react";
4
- import { CataloguePage, useCimplifyClient } from "@cimplify/sdk/react";
5
- import type { Category, Product } from "@cimplify/sdk";
6
- import { StoreProductCard } from "@/components/store-product-card";
6
+ export const metadata: Metadata = {
7
+ title: "Shop — Akua's Bakery",
8
+ description: "Browse the full catalogue: breads, pastries, cakes, drinks, merch, and more.",
9
+ };
7
10
 
8
- /** Full catalogue page — every product, with the SDK's filterable layout. */
9
- export default function ShopPage() {
10
- const { client } = useCimplifyClient();
11
- const [products, setProducts] = useState<Product[]>();
12
- const [categories, setCategories] = useState<Category[]>();
13
- const [loading, setLoading] = useState(true);
11
+ async function getShopData() {
12
+ "use cache";
13
+ cacheTag(tags.products(), tags.categories());
14
+ cacheLife("hours");
14
15
 
15
- useEffect(() => {
16
- let cancelled = false;
17
- void (async () => {
18
- try {
19
- const [p, c] = await Promise.all([
20
- client.catalogue.getProducts({ limit: 50 }),
21
- client.catalogue.getCategories(),
22
- ]);
23
- if (cancelled) return;
24
- if (p.ok) setProducts(p.value.items);
25
- if (c.ok) setCategories(c.value);
26
- } finally {
27
- if (!cancelled) setLoading(false);
28
- }
29
- })();
30
- return () => {
31
- cancelled = true;
32
- };
33
- }, [client]);
16
+ const client = getServerClient();
17
+ const [p, c] = await Promise.all([
18
+ client.catalogue.getProducts({ limit: 50 }),
19
+ client.catalogue.getCategories(),
20
+ ]);
21
+ return {
22
+ products: p.ok ? p.value.items : [],
23
+ categories: c.ok ? c.value : [],
24
+ };
25
+ }
34
26
 
35
- if (loading) {
36
- return <p className="text-center py-20 text-muted-foreground">Loading shop…</p>;
37
- }
38
- return (
39
- <CataloguePage
40
- title="The Menu"
41
- products={products}
42
- categories={categories}
43
- renderCard={(p) => <StoreProductCard product={p} />}
44
- />
45
- );
27
+ export default async function ShopPage() {
28
+ const { products, categories } = await getShopData();
29
+ return <ShopClient products={products} categories={categories} />;
46
30
  }
@@ -0,0 +1,27 @@
1
+ "use client";
2
+
3
+ import { CataloguePage } from "@cimplify/sdk/react";
4
+ import type { Category, Product } from "@cimplify/sdk";
5
+ import { StoreProductCard } from "@/components/store-product-card";
6
+
7
+ /**
8
+ * Client island for the shop page. Server-side fetches all products and
9
+ * categories (cached via `'use cache'` in `app/shop/page.tsx`), then hands
10
+ * them to `<CataloguePage>` which owns the interactive filter / sort state.
11
+ */
12
+ export function ShopClient({
13
+ products,
14
+ categories,
15
+ }: {
16
+ products: Product[];
17
+ categories: Category[];
18
+ }) {
19
+ return (
20
+ <CataloguePage
21
+ title="The Menu"
22
+ products={products}
23
+ categories={categories}
24
+ renderCard={(p) => <StoreProductCard product={p} />}
25
+ />
26
+ );
27
+ }
@@ -0,0 +1,27 @@
1
+ import { createCimplifyClient, type CimplifyClient } from "@cimplify/sdk";
2
+
3
+ /**
4
+ * Server-side Cimplify client. Used by Server Components to fetch catalogue
5
+ * data during SSR/RSC for SEO-friendly product/category/collection pages.
6
+ *
7
+ * Cached per-request via React's cache mechanism — calling this multiple
8
+ * times within the same request returns the same client instance.
9
+ *
10
+ * Base-URL resolution:
11
+ * 1) NEXT_PUBLIC_CIMPLIFY_API_URL (matches the client-side default)
12
+ * 2) http://127.0.0.1:8787 — the local mock during dev
13
+ */
14
+ let cached: CimplifyClient | undefined;
15
+
16
+ export function getServerClient(): CimplifyClient {
17
+ if (cached) return cached;
18
+ const baseUrl =
19
+ process.env.NEXT_PUBLIC_CIMPLIFY_API_URL?.trim() || "http://127.0.0.1:8787";
20
+ const publicKey = process.env.NEXT_PUBLIC_CIMPLIFY_PUBLIC_KEY ?? "mock-dev";
21
+ cached = createCimplifyClient({
22
+ baseUrl,
23
+ publicKey,
24
+ suppressPublicKeyWarning: true,
25
+ });
26
+ return cached;
27
+ }
@@ -14,6 +14,11 @@ const MOCK_URL = process.env.NEXT_PUBLIC_CIMPLIFY_API_URL?.trim() || "http://127
14
14
  * pass the absolute URL through `<CimplifyProvider/>`).
15
15
  */
16
16
  const nextConfig: NextConfig = {
17
+ // Enable Next 16's `cacheComponents` mode so we can use the `'use cache'`
18
+ // directive + `cacheTag` / `cacheLife` from `next/cache` for SSR caching.
19
+ // The Cimplify storefront's RSC pages depend on this — disable only if
20
+ // you've migrated every server fetch to a different caching strategy.
21
+ cacheComponents: true,
17
22
  async rewrites() {
18
23
  return [
19
24
  { source: "/api/v1/:path*", destination: `${MOCK_URL}/api/v1/:path*` },