@c1m-public/channel-client 0.3.0 → 0.5.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 +51 -12
- package/dist/index.d.ts +23 -9
- package/dist/index.js +44 -14
- package/dist/next.d.ts +112 -0
- package/dist/next.js +40 -0
- package/dist/types.d.ts +126 -0
- package/dist/webhooks.d.ts +0 -1
- package/dist/webhooks.js +0 -1
- package/package.json +10 -2
package/README.md
CHANGED
|
@@ -1,30 +1,69 @@
|
|
|
1
1
|
# `@c1m-public/channel-client`
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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 {
|
|
22
|
+
import { createNextChannelClient } from "@c1m-public/channel-client/next";
|
|
9
23
|
|
|
10
|
-
const channel =
|
|
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 },
|
|
14
28
|
});
|
|
15
29
|
|
|
16
30
|
const { page } = await channel.pages.get("home");
|
|
31
|
+
|
|
32
|
+
const { order: cart } = await channel.cart.addItem(
|
|
33
|
+
{ product_id: "product-id", quantity: 1 },
|
|
34
|
+
{ idempotencyKey: crypto.randomUUID() },
|
|
35
|
+
);
|
|
36
|
+
const checkout = await channel.checkout.pay(
|
|
37
|
+
{ cart_token: cart.cart_token!, return_url: "https://example.com/checkout/complete" },
|
|
38
|
+
{ idempotencyKey: crypto.randomUUID() },
|
|
39
|
+
);
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Other server runtimes
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
import { createChannelClient } from "@c1m-public/channel-client";
|
|
46
|
+
|
|
47
|
+
const channel = createChannelClient({
|
|
48
|
+
baseUrl: process.env.C1M_API_URL!,
|
|
49
|
+
token: process.env.C1M_CHANNEL_TOKEN!,
|
|
50
|
+
});
|
|
17
51
|
```
|
|
18
52
|
|
|
19
53
|
The package must only be imported from server modules. Never expose the token in
|
|
20
54
|
`NEXT_PUBLIC_*`, Client Components, browser storage, HTML, source maps, logs, or
|
|
21
|
-
asset requests.
|
|
55
|
+
asset requests. The `/next` entry point enforces Next's `server-only` boundary;
|
|
56
|
+
other runtimes must enforce their own server-module boundary.
|
|
22
57
|
|
|
23
58
|
## Behavior
|
|
24
59
|
|
|
25
|
-
-
|
|
60
|
+
- The core retries transient reads once and leaves anonymous-read caching to the
|
|
61
|
+
runtime adapter.
|
|
62
|
+
- The Next adapter supports explicit revalidation and tags.
|
|
26
63
|
- Writes are always `no-store`, are never automatically retried, and accept an
|
|
27
64
|
explicit idempotency key.
|
|
65
|
+
- Cart and checkout operations use an opaque `cart_token`; the channel token
|
|
66
|
+
continues to resolve tenant and workspace identity at the API door.
|
|
28
67
|
- Requests time out after 10 seconds by default.
|
|
29
68
|
- API failures throw `ChannelAPIError` with safe status, code, and request ID.
|
|
30
69
|
- Assets remain tokenless and are normalized with `assetUrl()`.
|
|
@@ -47,7 +86,7 @@ const channel = createChannelClient({
|
|
|
47
86
|
Verify webhook requests before parsing or acting on their payload:
|
|
48
87
|
|
|
49
88
|
```ts
|
|
50
|
-
import { parseChannelWebhookEvent } from "@c1m-public/channel-client";
|
|
89
|
+
import { parseChannelWebhookEvent } from "@c1m-public/channel-client/webhooks";
|
|
51
90
|
|
|
52
91
|
const event = await parseChannelWebhookEvent(request, {
|
|
53
92
|
secret: process.env.C1M_CHANNEL_WEBHOOK_SECRET!,
|
|
@@ -72,7 +111,7 @@ pnpm --filter @c1m-public/channel-client pack
|
|
|
72
111
|
npm publish --access public
|
|
73
112
|
```
|
|
74
113
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
114
|
+
Publishing runs through npm Trusted Publishing and GitHub OIDC. Push a protected
|
|
115
|
+
`channel-client-vX.Y.Z` tag matching `package.json`; the workflow verifies,
|
|
116
|
+
packs, and publishes without a stored npm token. Published versions are
|
|
117
|
+
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
|
-
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
|
-
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
|
-
};
|
|
2
|
+
export type { BlogPost, BlogPostResponse, AddCartItemInput, CartResponse, CheckoutAddress, CheckoutAddressesInput, CheckoutCustomerInput, CheckoutFulfillmentInput, CheckoutOptions, CheckoutPayInput, CheckoutPayResponse, CheckoutStatus, CheckoutStatusResponse, ChannelPing, Customer, CustomerResponse, CustomerSession, CustomerSignupResponse, LeadResponse, Order, OrderItem, OrderSummary, MergeCartInput, SendCodeResponse, LightInitResponse, ListResponse, Product, ProductAsset, PublicPage, PublicPageSummary, PublicSubmitReviewInput, PublicWorkspaceInit, Menu, MenuItem, MenuResponse, Review, ReviewSummaryResponse, PaymentMethodOption, PaymentReadiness, ShippingMethodOption, SiteDocument, SiteGatedDocument, SiteResolvedDocument, ThemeResponse, UpdateCartItemInput, } from "./types";
|
|
3
|
+
import type { BlogPostResponse, AddCartItemInput, CartResponse, CheckoutAddressesInput, CheckoutCustomerInput, CheckoutFulfillmentInput, CheckoutOptions, CheckoutPayInput, CheckoutPayResponse, CheckoutStatusResponse, ChannelPing, CustomerResponse, CustomerSession, CustomerSignupResponse, LeadResponse, LightInitResponse, ListResponse, Order, OrderSummary, MergeCartInput, SendCodeResponse, Product, ProductAsset, PublicPage, PublicPageSummary, PublicSubmitReviewInput, PublicWorkspaceInit, MenuResponse, Review, ReviewSummaryResponse, SiteDocument, ThemeResponse, UpdateCartItemInput } from "./types";
|
|
9
4
|
export interface ChannelRetryPolicy {
|
|
10
5
|
attempts?: number;
|
|
11
6
|
maxDelayMs?: number;
|
|
@@ -14,13 +9,12 @@ 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;
|
|
21
15
|
}
|
|
22
16
|
export type ChannelRequestOutcome = "success" | "api_error" | "network_error" | "timeout" | "invalid_response";
|
|
23
|
-
export type ChannelOperation = "channel.ping" | "channel.site.resolve" | "channel.init.get" | "channel.init.light" | "content.pages.public.list.channel" | "content.pages.public.get.channel" | "shop.public.products.list.channel" | "shop.public.products.get.channel" | "shop.public.products.media.channel" | "reviews.public.list.channel" | "reviews.public.summary.channel" | "reviews.public.submit.channel" | "channel.theme.get" | "channel.menus.get" | "channel.blog.get" | "channel.auth.check" | "channel.auth.signup" | "channel.auth.send_code" | "channel.auth.verify_code" | "channel.auth.refresh" | "channel.auth.me" | "channel.auth.me.update" | "channel.auth.logout" | "channel.account.orders.list" | "channel.account.orders.summary" | "channel.account.orders.get" | "growth.public_workspace.leads.capture.channel";
|
|
17
|
+
export type ChannelOperation = "channel.ping" | "channel.site.resolve" | "channel.init.get" | "channel.init.light" | "content.pages.public.list.channel" | "content.pages.public.get.channel" | "shop.public.products.list.channel" | "shop.public.products.get.channel" | "shop.public.products.media.channel" | "reviews.public.list.channel" | "reviews.public.summary.channel" | "reviews.public.submit.channel" | "channel.theme.get" | "channel.menus.get" | "channel.blog.get" | "channel.auth.check" | "channel.auth.signup" | "channel.auth.send_code" | "channel.auth.verify_code" | "channel.auth.refresh" | "channel.auth.me" | "channel.auth.me.update" | "channel.auth.logout" | "channel.account.orders.list" | "channel.account.orders.summary" | "channel.account.orders.get" | "channel.cart.get" | "channel.cart.items.add" | "channel.cart.items.update" | "channel.cart.items.remove" | "channel.cart.clear" | "channel.cart.merge" | "channel.checkout.options" | "channel.checkout.status" | "channel.checkout.customer" | "channel.checkout.addresses" | "channel.checkout.fulfillment" | "channel.checkout.pay" | "growth.public_workspace.leads.capture.channel";
|
|
24
18
|
export interface ChannelRequestObservation {
|
|
25
19
|
operation: ChannelOperation;
|
|
26
20
|
method: string;
|
|
@@ -56,6 +50,10 @@ export interface ChannelLeadInput {
|
|
|
56
50
|
export interface ChannelMutationOptions {
|
|
57
51
|
idempotencyKey?: string;
|
|
58
52
|
}
|
|
53
|
+
export interface ChannelCheckoutStatusParams {
|
|
54
|
+
cartToken?: string;
|
|
55
|
+
checkoutSessionId?: string;
|
|
56
|
+
}
|
|
59
57
|
export declare class ChannelAPIError extends Error {
|
|
60
58
|
readonly status: number;
|
|
61
59
|
readonly code: string;
|
|
@@ -94,6 +92,22 @@ export declare function createChannelClient(options: CreateChannelClientOptions)
|
|
|
94
92
|
}>;
|
|
95
93
|
readonly media: (slug: string) => Promise<ListResponse<ProductAsset>>;
|
|
96
94
|
};
|
|
95
|
+
readonly cart: {
|
|
96
|
+
readonly get: (cartToken: string) => Promise<CartResponse>;
|
|
97
|
+
readonly addItem: (input: AddCartItemInput, mutation?: ChannelMutationOptions) => Promise<CartResponse>;
|
|
98
|
+
readonly updateItem: (itemId: string, input: UpdateCartItemInput, mutation?: ChannelMutationOptions) => Promise<CartResponse>;
|
|
99
|
+
readonly removeItem: (itemId: string, cartToken: string, mutation?: ChannelMutationOptions) => Promise<CartResponse>;
|
|
100
|
+
readonly clear: (cartToken: string, mutation?: ChannelMutationOptions) => Promise<CartResponse>;
|
|
101
|
+
readonly merge: (input: MergeCartInput, mutation?: ChannelMutationOptions) => Promise<CartResponse>;
|
|
102
|
+
};
|
|
103
|
+
readonly checkout: {
|
|
104
|
+
readonly options: (cartToken: string) => Promise<CheckoutOptions>;
|
|
105
|
+
readonly status: (params?: ChannelCheckoutStatusParams) => Promise<CheckoutStatusResponse>;
|
|
106
|
+
readonly setCustomer: (input: CheckoutCustomerInput, mutation?: ChannelMutationOptions) => Promise<CartResponse>;
|
|
107
|
+
readonly setAddresses: (input: CheckoutAddressesInput, mutation?: ChannelMutationOptions) => Promise<CartResponse>;
|
|
108
|
+
readonly setFulfillment: (input: CheckoutFulfillmentInput, mutation?: ChannelMutationOptions) => Promise<CartResponse>;
|
|
109
|
+
readonly pay: (input: CheckoutPayInput, mutation?: ChannelMutationOptions) => Promise<CheckoutPayResponse>;
|
|
110
|
+
};
|
|
97
111
|
readonly reviews: {
|
|
98
112
|
readonly list: (params?: ChannelListParams) => Promise<ListResponse<Review>>;
|
|
99
113
|
readonly summary: () => Promise<ReviewSummaryResponse>;
|
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
|
-
//
|
|
91
|
-
//
|
|
92
|
-
|
|
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 {
|
|
@@ -184,6 +173,47 @@ export function createChannelClient(options) {
|
|
|
184
173
|
get: (slug) => request("shop.public.products.get.channel", `/products/${encodePath(slug)}`),
|
|
185
174
|
media: (slug) => request("shop.public.products.media.channel", `/products/${encodePath(slug)}/media`),
|
|
186
175
|
},
|
|
176
|
+
cart: {
|
|
177
|
+
get: (cartToken) => request("channel.cart.get", appendQuery("/cart", { cart_token: required(cartToken, "cartToken") })),
|
|
178
|
+
addItem: (input, mutation) => request("channel.cart.items.add", "/cart/items", {
|
|
179
|
+
method: "POST", headers: mutationHeaders(mutation), body: JSON.stringify(input),
|
|
180
|
+
}),
|
|
181
|
+
updateItem: (itemId, input, mutation) => request("channel.cart.items.update", `/cart/items/${encodePath(itemId)}`, {
|
|
182
|
+
method: "PATCH", headers: mutationHeaders(mutation), body: JSON.stringify(input),
|
|
183
|
+
}),
|
|
184
|
+
removeItem: (itemId, cartToken, mutation) => request("channel.cart.items.remove", appendQuery(`/cart/items/${encodePath(itemId)}`, {
|
|
185
|
+
cart_token: required(cartToken, "cartToken"),
|
|
186
|
+
}), {
|
|
187
|
+
method: "DELETE", headers: mutationHeaders(mutation),
|
|
188
|
+
}),
|
|
189
|
+
clear: (cartToken, mutation) => request("channel.cart.clear", appendQuery("/cart", { cart_token: required(cartToken, "cartToken") }), {
|
|
190
|
+
method: "DELETE", headers: mutationHeaders(mutation),
|
|
191
|
+
}),
|
|
192
|
+
merge: (input, mutation) => request("channel.cart.merge", "/cart/merge", {
|
|
193
|
+
method: "POST", headers: mutationHeaders(mutation), body: JSON.stringify(input),
|
|
194
|
+
}),
|
|
195
|
+
},
|
|
196
|
+
checkout: {
|
|
197
|
+
options: (cartToken) => request("channel.checkout.options", appendQuery("/checkout/options", {
|
|
198
|
+
cart_token: required(cartToken, "cartToken"),
|
|
199
|
+
})),
|
|
200
|
+
status: (params = {}) => request("channel.checkout.status", appendQuery("/checkout/status", {
|
|
201
|
+
cart_token: params.cartToken,
|
|
202
|
+
checkout_session_id: params.checkoutSessionId,
|
|
203
|
+
})),
|
|
204
|
+
setCustomer: (input, mutation) => request("channel.checkout.customer", "/checkout/customer", {
|
|
205
|
+
method: "PUT", headers: mutationHeaders(mutation), body: JSON.stringify(input),
|
|
206
|
+
}),
|
|
207
|
+
setAddresses: (input, mutation) => request("channel.checkout.addresses", "/checkout/addresses", {
|
|
208
|
+
method: "PUT", headers: mutationHeaders(mutation), body: JSON.stringify(input),
|
|
209
|
+
}),
|
|
210
|
+
setFulfillment: (input, mutation) => request("channel.checkout.fulfillment", "/checkout/fulfillment", {
|
|
211
|
+
method: "PUT", headers: mutationHeaders(mutation), body: JSON.stringify(input),
|
|
212
|
+
}),
|
|
213
|
+
pay: (input, mutation) => request("channel.checkout.pay", "/checkout/pay", {
|
|
214
|
+
method: "POST", headers: mutationHeaders(mutation), body: JSON.stringify(input),
|
|
215
|
+
}),
|
|
216
|
+
},
|
|
187
217
|
reviews: {
|
|
188
218
|
list: (params = {}) => request("reviews.public.list.channel", appendQuery("/reviews", params)),
|
|
189
219
|
summary: () => request("reviews.public.summary.channel", "/reviews/summary"),
|
package/dist/next.d.ts
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
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 cart: {
|
|
41
|
+
readonly get: (cartToken: string) => Promise<import("./types.js").CartResponse>;
|
|
42
|
+
readonly addItem: (input: import("./types.js").AddCartItemInput, mutation?: import("./index.js").ChannelMutationOptions) => Promise<import("./types.js").CartResponse>;
|
|
43
|
+
readonly updateItem: (itemId: string, input: import("./types.js").UpdateCartItemInput, mutation?: import("./index.js").ChannelMutationOptions) => Promise<import("./types.js").CartResponse>;
|
|
44
|
+
readonly removeItem: (itemId: string, cartToken: string, mutation?: import("./index.js").ChannelMutationOptions) => Promise<import("./types.js").CartResponse>;
|
|
45
|
+
readonly clear: (cartToken: string, mutation?: import("./index.js").ChannelMutationOptions) => Promise<import("./types.js").CartResponse>;
|
|
46
|
+
readonly merge: (input: import("./types.js").MergeCartInput, mutation?: import("./index.js").ChannelMutationOptions) => Promise<import("./types.js").CartResponse>;
|
|
47
|
+
};
|
|
48
|
+
readonly checkout: {
|
|
49
|
+
readonly options: (cartToken: string) => Promise<import("./types.js").CheckoutOptions>;
|
|
50
|
+
readonly status: (params?: import("./index.js").ChannelCheckoutStatusParams) => Promise<import("./types.js").CheckoutStatusResponse>;
|
|
51
|
+
readonly setCustomer: (input: import("./types.js").CheckoutCustomerInput, mutation?: import("./index.js").ChannelMutationOptions) => Promise<import("./types.js").CartResponse>;
|
|
52
|
+
readonly setAddresses: (input: import("./types.js").CheckoutAddressesInput, mutation?: import("./index.js").ChannelMutationOptions) => Promise<import("./types.js").CartResponse>;
|
|
53
|
+
readonly setFulfillment: (input: import("./types.js").CheckoutFulfillmentInput, mutation?: import("./index.js").ChannelMutationOptions) => Promise<import("./types.js").CartResponse>;
|
|
54
|
+
readonly pay: (input: import("./types.js").CheckoutPayInput, mutation?: import("./index.js").ChannelMutationOptions) => Promise<import("./types.js").CheckoutPayResponse>;
|
|
55
|
+
};
|
|
56
|
+
readonly reviews: {
|
|
57
|
+
readonly list: (params?: import("./index.js").ChannelListParams) => Promise<import("./types.js").ListResponse<import("./types.js").Review>>;
|
|
58
|
+
readonly summary: () => Promise<import("./types.js").ReviewSummaryResponse>;
|
|
59
|
+
readonly submit: (input: import("./types.js").PublicSubmitReviewInput, mutation?: import("./index.js").ChannelMutationOptions) => Promise<{
|
|
60
|
+
message: string;
|
|
61
|
+
}>;
|
|
62
|
+
};
|
|
63
|
+
readonly theme: {
|
|
64
|
+
readonly get: () => Promise<import("./types.js").ThemeResponse>;
|
|
65
|
+
};
|
|
66
|
+
readonly menus: {
|
|
67
|
+
readonly get: (handle: string) => Promise<import("./types.js").MenuResponse>;
|
|
68
|
+
};
|
|
69
|
+
readonly blog: {
|
|
70
|
+
readonly get: (slug: string) => Promise<import("./types.js").BlogPostResponse>;
|
|
71
|
+
};
|
|
72
|
+
readonly auth: {
|
|
73
|
+
readonly check: (email: string) => Promise<{
|
|
74
|
+
exists: boolean;
|
|
75
|
+
}>;
|
|
76
|
+
readonly signup: (input: {
|
|
77
|
+
email: string;
|
|
78
|
+
name: string;
|
|
79
|
+
phone?: string;
|
|
80
|
+
}) => Promise<import("./types.js").CustomerSignupResponse>;
|
|
81
|
+
readonly sendCode: (email: string) => Promise<import("./types.js").SendCodeResponse>;
|
|
82
|
+
readonly verifyCode: (input: {
|
|
83
|
+
email: string;
|
|
84
|
+
code: string;
|
|
85
|
+
}) => Promise<import("./types.js").CustomerSession>;
|
|
86
|
+
readonly refresh: (refreshToken: string) => Promise<import("./types.js").CustomerSession>;
|
|
87
|
+
};
|
|
88
|
+
readonly withCustomer: (session: string) => {
|
|
89
|
+
readonly me: () => Promise<import("./types.js").CustomerResponse>;
|
|
90
|
+
readonly updateProfile: (input: {
|
|
91
|
+
name?: string;
|
|
92
|
+
phone?: string;
|
|
93
|
+
}) => Promise<import("./types.js").CustomerResponse>;
|
|
94
|
+
readonly logout: () => Promise<{
|
|
95
|
+
status?: string;
|
|
96
|
+
}>;
|
|
97
|
+
readonly orders: {
|
|
98
|
+
readonly list: (params?: import("./index.js").ChannelListParams) => Promise<import("./types.js").ListResponse<import("./types.js").Order>>;
|
|
99
|
+
readonly summary: () => Promise<{
|
|
100
|
+
summary: import("./types.js").OrderSummary;
|
|
101
|
+
}>;
|
|
102
|
+
readonly get: (orderId: string) => Promise<{
|
|
103
|
+
order: import("./types.js").Order;
|
|
104
|
+
}>;
|
|
105
|
+
};
|
|
106
|
+
};
|
|
107
|
+
readonly leads: {
|
|
108
|
+
readonly create: (input: import("./index.js").ChannelLeadInput, mutation?: import("./index.js").ChannelMutationOptions) => Promise<{
|
|
109
|
+
lead: import("./types.js").LeadResponse;
|
|
110
|
+
}>;
|
|
111
|
+
};
|
|
112
|
+
};
|
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
|
+
}
|
package/dist/types.d.ts
CHANGED
|
@@ -267,12 +267,138 @@ export interface CustomerResponse {
|
|
|
267
267
|
}
|
|
268
268
|
export interface Order {
|
|
269
269
|
id: string;
|
|
270
|
+
workspace_id?: string;
|
|
270
271
|
order_number: string;
|
|
272
|
+
cart_token?: string;
|
|
273
|
+
channel_id?: string;
|
|
271
274
|
status: string;
|
|
272
275
|
payment_status: string;
|
|
273
276
|
currency: string;
|
|
277
|
+
source?: string;
|
|
278
|
+
customer_email?: string;
|
|
279
|
+
customer_name?: string;
|
|
280
|
+
subtotal_cents?: number;
|
|
281
|
+
shipping_amount_cents?: number;
|
|
282
|
+
tax_amount_cents?: number;
|
|
274
283
|
total_cents: number;
|
|
284
|
+
items?: OrderItem[];
|
|
285
|
+
version?: number;
|
|
275
286
|
created_at: string;
|
|
287
|
+
updated_at?: string;
|
|
288
|
+
[key: string]: unknown;
|
|
289
|
+
}
|
|
290
|
+
export interface OrderItem {
|
|
291
|
+
id: string;
|
|
292
|
+
order_id: string;
|
|
293
|
+
product_id?: string;
|
|
294
|
+
variant_id?: string;
|
|
295
|
+
product_slug?: string;
|
|
296
|
+
variant_title?: string;
|
|
297
|
+
thumbnail_url?: string;
|
|
298
|
+
title: string;
|
|
299
|
+
quantity: number;
|
|
300
|
+
unit_price_cents: number;
|
|
301
|
+
line_total_cents: number;
|
|
302
|
+
[key: string]: unknown;
|
|
303
|
+
}
|
|
304
|
+
export interface CartResponse {
|
|
305
|
+
order: Order;
|
|
306
|
+
}
|
|
307
|
+
export interface AddCartItemInput {
|
|
308
|
+
cart_token?: string;
|
|
309
|
+
currency?: string;
|
|
310
|
+
product_id: string;
|
|
311
|
+
variant_id?: string;
|
|
312
|
+
quantity: number;
|
|
313
|
+
}
|
|
314
|
+
export interface UpdateCartItemInput {
|
|
315
|
+
cart_token: string;
|
|
316
|
+
quantity: number;
|
|
317
|
+
}
|
|
318
|
+
export interface MergeCartInput {
|
|
319
|
+
source_cart_token: string;
|
|
320
|
+
target_cart_token: string;
|
|
321
|
+
currency?: string;
|
|
322
|
+
}
|
|
323
|
+
export interface CheckoutCustomerInput {
|
|
324
|
+
cart_token: string;
|
|
325
|
+
email: string;
|
|
326
|
+
name: string;
|
|
327
|
+
}
|
|
328
|
+
export interface CheckoutAddress {
|
|
329
|
+
name?: string;
|
|
330
|
+
phone?: string;
|
|
331
|
+
line1: string;
|
|
332
|
+
line2?: string;
|
|
333
|
+
city: string;
|
|
334
|
+
region?: string;
|
|
335
|
+
postal_code?: string;
|
|
336
|
+
country_code: string;
|
|
337
|
+
}
|
|
338
|
+
export interface CheckoutAddressesInput {
|
|
339
|
+
cart_token: string;
|
|
340
|
+
shipping: CheckoutAddress;
|
|
341
|
+
billing?: CheckoutAddress;
|
|
342
|
+
}
|
|
343
|
+
export interface CheckoutFulfillmentInput {
|
|
344
|
+
cart_token: string;
|
|
345
|
+
method: "shipping" | "pickup";
|
|
346
|
+
shipping_method_id?: string;
|
|
347
|
+
}
|
|
348
|
+
export interface CheckoutPayInput {
|
|
349
|
+
cart_token: string;
|
|
350
|
+
return_url?: string;
|
|
351
|
+
}
|
|
352
|
+
export interface PaymentMethodOption {
|
|
353
|
+
provider: string;
|
|
354
|
+
name: string;
|
|
355
|
+
type: string;
|
|
356
|
+
is_available: boolean;
|
|
357
|
+
[key: string]: unknown;
|
|
358
|
+
}
|
|
359
|
+
export interface PaymentReadiness {
|
|
360
|
+
ready: boolean;
|
|
361
|
+
mode: string;
|
|
362
|
+
reasons?: string[];
|
|
363
|
+
providers_available?: string[];
|
|
364
|
+
connect_required?: boolean;
|
|
365
|
+
connect_configured?: boolean;
|
|
366
|
+
}
|
|
367
|
+
export interface ShippingMethodOption {
|
|
368
|
+
id: string;
|
|
369
|
+
name?: string;
|
|
370
|
+
code?: string;
|
|
371
|
+
amount_cents?: number;
|
|
372
|
+
[key: string]: unknown;
|
|
373
|
+
}
|
|
374
|
+
export interface CheckoutOptions {
|
|
375
|
+
checkout_variant: string;
|
|
376
|
+
order: Order;
|
|
377
|
+
shipping_methods?: ShippingMethodOption[];
|
|
378
|
+
payment_methods?: PaymentMethodOption[];
|
|
379
|
+
payment_ready?: boolean;
|
|
380
|
+
payment_readiness?: PaymentReadiness;
|
|
381
|
+
[key: string]: unknown;
|
|
382
|
+
}
|
|
383
|
+
export type CheckoutStatus = "finalizing" | "completed" | "failed" | "not_found";
|
|
384
|
+
export interface CheckoutStatusResponse {
|
|
385
|
+
status: CheckoutStatus;
|
|
386
|
+
order_status?: string;
|
|
387
|
+
payment_status?: string;
|
|
388
|
+
payment_reference?: string;
|
|
389
|
+
updated_at?: string;
|
|
390
|
+
reasons?: string[];
|
|
391
|
+
[key: string]: unknown;
|
|
392
|
+
}
|
|
393
|
+
export interface CheckoutPayResponse {
|
|
394
|
+
order: Order;
|
|
395
|
+
session?: Record<string, unknown>;
|
|
396
|
+
checkout_variant?: string;
|
|
397
|
+
payment?: Record<string, unknown>;
|
|
398
|
+
status?: string;
|
|
399
|
+
provider_ref?: string;
|
|
400
|
+
checkout_session_id?: string;
|
|
401
|
+
redirect_url?: string;
|
|
276
402
|
[key: string]: unknown;
|
|
277
403
|
}
|
|
278
404
|
export interface OrderSummary {
|
package/dist/webhooks.d.ts
CHANGED
package/dist/webhooks.js
CHANGED
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@c1m-public/channel-client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"description": "
|
|
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": [
|