@c1m-public/channel-client 0.3.0 → 0.4.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.
package/README.md CHANGED
@@ -1,13 +1,27 @@
1
1
  # `@c1m-public/channel-client`
2
2
 
3
- Server-only TypeScript client for the C1M Channel API. Channel credentials resolve
4
- the tenant and workspace at the API door, so site URLs never carry either
5
- identifier.
3
+ Typed server-side client for the C1M Channel API. One npm package exposes
4
+ capability-based entry points so framework behavior does not leak into the
5
+ transport core.
6
+
7
+ ## Entry points
8
+
9
+ - `@c1m-public/channel-client` — framework-neutral transport, contracts,
10
+ authentication, retries, errors and request observations.
11
+ - `@c1m-public/channel-client/next` — Next.js server-only guard and Data Cache
12
+ policy layered over the same client.
13
+ - `@c1m-public/channel-client/webhooks` — framework-neutral webhook events and
14
+ signature verification.
15
+
16
+ Channel credentials resolve the tenant and workspace at the API door, so site
17
+ URLs never carry either identifier.
18
+
19
+ ### Next.js
6
20
 
7
21
  ```ts
8
- import { createChannelClient } from "@c1m-public/channel-client";
22
+ import { createNextChannelClient } from "@c1m-public/channel-client/next";
9
23
 
10
- const channel = createChannelClient({
24
+ const channel = createNextChannelClient({
11
25
  baseUrl: process.env.C1M_API_URL!,
12
26
  token: process.env.C1M_CHANNEL_TOKEN!,
13
27
  cache: { revalidate: 60 },
@@ -16,13 +30,27 @@ const channel = createChannelClient({
16
30
  const { page } = await channel.pages.get("home");
17
31
  ```
18
32
 
33
+ ### Other server runtimes
34
+
35
+ ```ts
36
+ import { createChannelClient } from "@c1m-public/channel-client";
37
+
38
+ const channel = createChannelClient({
39
+ baseUrl: process.env.C1M_API_URL!,
40
+ token: process.env.C1M_CHANNEL_TOKEN!,
41
+ });
42
+ ```
43
+
19
44
  The package must only be imported from server modules. Never expose the token in
20
45
  `NEXT_PUBLIC_*`, Client Components, browser storage, HTML, source maps, logs, or
21
- asset requests.
46
+ asset requests. The `/next` entry point enforces Next's `server-only` boundary;
47
+ other runtimes must enforce their own server-module boundary.
22
48
 
23
49
  ## Behavior
24
50
 
25
- - Reads may use explicit Next.js revalidation and retry one transient response.
51
+ - The core retries transient reads once and leaves anonymous-read caching to the
52
+ runtime adapter.
53
+ - The Next adapter supports explicit revalidation and tags.
26
54
  - Writes are always `no-store`, are never automatically retried, and accept an
27
55
  explicit idempotency key.
28
56
  - Requests time out after 10 seconds by default.
@@ -47,7 +75,7 @@ const channel = createChannelClient({
47
75
  Verify webhook requests before parsing or acting on their payload:
48
76
 
49
77
  ```ts
50
- import { parseChannelWebhookEvent } from "@c1m-public/channel-client";
78
+ import { parseChannelWebhookEvent } from "@c1m-public/channel-client/webhooks";
51
79
 
52
80
  const event = await parseChannelWebhookEvent(request, {
53
81
  secret: process.env.C1M_CHANNEL_WEBHOOK_SECRET!,
@@ -72,7 +100,7 @@ pnpm --filter @c1m-public/channel-client pack
72
100
  npm publish --access public
73
101
  ```
74
102
 
75
- The first release is published interactively with account 2FA. After the package
76
- exists, configure npm trusted publishing or staged publishing for GitHub Actions
77
- instead of storing a long-lived bypass-2FA write token. Increment the package
78
- version for every release; sites consume exact or controlled semver ranges.
103
+ Publishing runs through npm Trusted Publishing and GitHub OIDC. Push a protected
104
+ `channel-client-vX.Y.Z` tag matching `package.json`; the workflow verifies,
105
+ packs, and publishes without a stored npm token. Published versions are
106
+ immutable, so increment the package version for every release.
package/dist/index.d.ts CHANGED
@@ -1,11 +1,6 @@
1
- import "server-only";
2
1
  export * from "./webhooks.js";
3
2
  export type { BlogPost, BlogPostResponse, ChannelPing, Customer, CustomerResponse, CustomerSession, CustomerSignupResponse, LeadResponse, Order, OrderSummary, SendCodeResponse, LightInitResponse, ListResponse, Product, ProductAsset, PublicPage, PublicPageSummary, PublicSubmitReviewInput, PublicWorkspaceInit, Menu, MenuItem, MenuResponse, Review, ReviewSummaryResponse, SiteDocument, SiteGatedDocument, SiteResolvedDocument, ThemeResponse, } from "./types";
4
3
  import type { BlogPostResponse, ChannelPing, CustomerResponse, CustomerSession, CustomerSignupResponse, LeadResponse, LightInitResponse, ListResponse, Order, OrderSummary, SendCodeResponse, Product, ProductAsset, PublicPage, PublicPageSummary, PublicSubmitReviewInput, PublicWorkspaceInit, MenuResponse, Review, ReviewSummaryResponse, SiteDocument, ThemeResponse } from "./types";
5
- export type ChannelCachePolicy = "no-store" | {
6
- revalidate: number;
7
- tags?: string[];
8
- };
9
4
  export interface ChannelRetryPolicy {
10
5
  attempts?: number;
11
6
  maxDelayMs?: number;
@@ -14,7 +9,6 @@ export interface CreateChannelClientOptions {
14
9
  baseUrl: string;
15
10
  token: string;
16
11
  fetch?: typeof globalThis.fetch;
17
- cache?: ChannelCachePolicy;
18
12
  timeoutMs?: number;
19
13
  retry?: ChannelRetryPolicy | false;
20
14
  observe?: ChannelRequestObserver;
package/dist/index.js CHANGED
@@ -1,4 +1,3 @@
1
- import "server-only";
2
1
  export * from "./webhooks.js";
3
2
  const CHANNEL_TOKEN_PREFIX = "sk_chan_";
4
3
  const RETRYABLE_STATUSES = new Set([429, 502, 503, 504]);
@@ -58,10 +57,6 @@ export function createChannelClient(options) {
58
57
  const fetcher = options.fetch ?? globalThis.fetch;
59
58
  if (!fetcher)
60
59
  throw new Error("fetch implementation is required");
61
- const cachePolicy = options.cache ?? "no-store";
62
- if (cachePolicy !== "no-store" && (!Number.isFinite(cachePolicy.revalidate) || cachePolicy.revalidate < 0)) {
63
- throw new Error("cache.revalidate must be a non-negative finite number");
64
- }
65
60
  const timeoutMs = positiveNumber(options.timeoutMs ?? 10_000, "timeoutMs");
66
61
  const configuredRetryAttempts = options.retry === false ? 0 : options.retry?.attempts ?? 1;
67
62
  if (!Number.isSafeInteger(configuredRetryAttempts) || configuredRetryAttempts < 0) {
@@ -87,15 +82,9 @@ export function createChannelClient(options) {
87
82
  if (init.body !== undefined && !headers.has("Content-Type"))
88
83
  headers.set("Content-Type", "application/json");
89
84
  const requestInit = { ...init, method, headers };
90
- // A customer-scoped read is NEVER cached, whatever cache policy the client
91
- // was built with. These responses belong to one signed-in person: caching
92
- // `/account/orders` under a shared revalidate+tag would serve one customer's
93
- // orders from another's render. The guarantee lives here, in the library, so
94
- // an app cannot lose it by constructing the client the obvious way.
95
- if (method === "GET" && !session && cachePolicy !== "no-store") {
96
- requestInit.next = { revalidate: cachePolicy.revalidate, ...(cachePolicy.tags ? { tags: cachePolicy.tags } : {}) };
97
- }
98
- else {
85
+ // Private reads and every mutation are non-cacheable in every runtime. An
86
+ // adapter may add framework caching only to anonymous GET requests.
87
+ if (method !== "GET" || session) {
99
88
  requestInit.cache = "no-store";
100
89
  }
101
90
  try {
package/dist/next.d.ts ADDED
@@ -0,0 +1,96 @@
1
+ import "server-only";
2
+ import { type CreateChannelClientOptions } from "./index.js";
3
+ export type NextChannelCachePolicy = "no-store" | {
4
+ revalidate: number;
5
+ tags?: string[];
6
+ };
7
+ export interface CreateNextChannelClientOptions extends Omit<CreateChannelClientOptions, "fetch"> {
8
+ fetch?: typeof globalThis.fetch;
9
+ cache?: NextChannelCachePolicy;
10
+ }
11
+ /**
12
+ * Build the Channel API client with Next.js Data Cache semantics.
13
+ *
14
+ * Only anonymous GETs may receive `next.revalidate`; customer-scoped reads and
15
+ * mutations are forced to `no-store` by both this adapter and the core client.
16
+ */
17
+ export declare function createNextChannelClient(options: CreateNextChannelClientOptions): {
18
+ readonly ping: () => Promise<import("./types.js").ChannelPing>;
19
+ readonly assetUrl: (pathOrUrl: string) => string;
20
+ readonly site: {
21
+ readonly resolve: (params?: import("./index.js").ChannelSiteResolveParams) => Promise<import("./types.js").SiteDocument>;
22
+ };
23
+ readonly init: {
24
+ readonly get: () => Promise<import("./types.js").PublicWorkspaceInit>;
25
+ readonly light: () => Promise<import("./types.js").LightInitResponse>;
26
+ };
27
+ readonly pages: {
28
+ readonly list: (params?: import("./index.js").ChannelListParams) => Promise<import("./types.js").ListResponse<import("./types.js").PublicPageSummary>>;
29
+ readonly get: (slug: string) => Promise<{
30
+ page: import("./types.js").PublicPage;
31
+ }>;
32
+ };
33
+ readonly products: {
34
+ readonly list: (params?: import("./index.js").ChannelProductListParams) => Promise<import("./types.js").ListResponse<import("./types.js").Product>>;
35
+ readonly get: (slug: string) => Promise<{
36
+ product: import("./types.js").Product;
37
+ }>;
38
+ readonly media: (slug: string) => Promise<import("./types.js").ListResponse<import("./types.js").ProductAsset>>;
39
+ };
40
+ readonly reviews: {
41
+ readonly list: (params?: import("./index.js").ChannelListParams) => Promise<import("./types.js").ListResponse<import("./types.js").Review>>;
42
+ readonly summary: () => Promise<import("./types.js").ReviewSummaryResponse>;
43
+ readonly submit: (input: import("./types.js").PublicSubmitReviewInput, mutation?: import("./index.js").ChannelMutationOptions) => Promise<{
44
+ message: string;
45
+ }>;
46
+ };
47
+ readonly theme: {
48
+ readonly get: () => Promise<import("./types.js").ThemeResponse>;
49
+ };
50
+ readonly menus: {
51
+ readonly get: (handle: string) => Promise<import("./types.js").MenuResponse>;
52
+ };
53
+ readonly blog: {
54
+ readonly get: (slug: string) => Promise<import("./types.js").BlogPostResponse>;
55
+ };
56
+ readonly auth: {
57
+ readonly check: (email: string) => Promise<{
58
+ exists: boolean;
59
+ }>;
60
+ readonly signup: (input: {
61
+ email: string;
62
+ name: string;
63
+ phone?: string;
64
+ }) => Promise<import("./types.js").CustomerSignupResponse>;
65
+ readonly sendCode: (email: string) => Promise<import("./types.js").SendCodeResponse>;
66
+ readonly verifyCode: (input: {
67
+ email: string;
68
+ code: string;
69
+ }) => Promise<import("./types.js").CustomerSession>;
70
+ readonly refresh: (refreshToken: string) => Promise<import("./types.js").CustomerSession>;
71
+ };
72
+ readonly withCustomer: (session: string) => {
73
+ readonly me: () => Promise<import("./types.js").CustomerResponse>;
74
+ readonly updateProfile: (input: {
75
+ name?: string;
76
+ phone?: string;
77
+ }) => Promise<import("./types.js").CustomerResponse>;
78
+ readonly logout: () => Promise<{
79
+ status?: string;
80
+ }>;
81
+ readonly orders: {
82
+ readonly list: (params?: import("./index.js").ChannelListParams) => Promise<import("./types.js").ListResponse<import("./types.js").Order>>;
83
+ readonly summary: () => Promise<{
84
+ summary: import("./types.js").OrderSummary;
85
+ }>;
86
+ readonly get: (orderId: string) => Promise<{
87
+ order: import("./types.js").Order;
88
+ }>;
89
+ };
90
+ };
91
+ readonly leads: {
92
+ readonly create: (input: import("./index.js").ChannelLeadInput, mutation?: import("./index.js").ChannelMutationOptions) => Promise<{
93
+ lead: import("./types.js").LeadResponse;
94
+ }>;
95
+ };
96
+ };
package/dist/next.js ADDED
@@ -0,0 +1,40 @@
1
+ import "server-only";
2
+ import { createChannelClient } from "./index.js";
3
+ function validateCachePolicy(policy) {
4
+ if (policy === "no-store")
5
+ return;
6
+ if (!Number.isFinite(policy.revalidate) || policy.revalidate < 0) {
7
+ throw new Error("cache.revalidate must be a non-negative finite number");
8
+ }
9
+ }
10
+ /**
11
+ * Build the Channel API client with Next.js Data Cache semantics.
12
+ *
13
+ * Only anonymous GETs may receive `next.revalidate`; customer-scoped reads and
14
+ * mutations are forced to `no-store` by both this adapter and the core client.
15
+ */
16
+ export function createNextChannelClient(options) {
17
+ const { cache = "no-store", fetch: suppliedFetch, ...coreOptions } = options;
18
+ validateCachePolicy(cache);
19
+ const fetcher = suppliedFetch ?? globalThis.fetch;
20
+ if (!fetcher)
21
+ throw new Error("fetch implementation is required");
22
+ const nextFetch = (input, init = {}) => {
23
+ const request = { ...init };
24
+ const method = (request.method ?? "GET").toUpperCase();
25
+ const customerScoped = new Headers(request.headers).has("X-End-User-Token");
26
+ if (method === "GET" && !customerScoped && cache !== "no-store") {
27
+ delete request.cache;
28
+ request.next = {
29
+ revalidate: cache.revalidate,
30
+ ...(cache.tags ? { tags: cache.tags } : {}),
31
+ };
32
+ }
33
+ else {
34
+ delete request.next;
35
+ request.cache = "no-store";
36
+ }
37
+ return fetcher(input, request);
38
+ };
39
+ return createChannelClient({ ...coreOptions, fetch: nextFetch });
40
+ }
@@ -1,4 +1,3 @@
1
- import "server-only";
2
1
  /**
3
2
  * Channel webhooks — the inbound half of the channel contract.
4
3
  *
package/dist/webhooks.js CHANGED
@@ -1,4 +1,3 @@
1
- import "server-only";
2
1
  /**
3
2
  * Channel webhooks — the inbound half of the channel contract.
4
3
  *
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@c1m-public/channel-client",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
- "description": "Server-only TypeScript client for the MML headless Channel API",
6
+ "description": "Typed server-side client for the MML headless Channel API",
7
7
  "repository": {
8
8
  "type": "git",
9
9
  "url": "https://github.com/cloud1mediaLtd/mml-js.git",
@@ -19,6 +19,14 @@
19
19
  ".": {
20
20
  "types": "./dist/index.d.ts",
21
21
  "default": "./dist/index.js"
22
+ },
23
+ "./next": {
24
+ "types": "./dist/next.d.ts",
25
+ "default": "./dist/next.js"
26
+ },
27
+ "./webhooks": {
28
+ "types": "./dist/webhooks.d.ts",
29
+ "default": "./dist/webhooks.js"
22
30
  }
23
31
  },
24
32
  "files": [