@c1m-public/channel-client 0.6.0 → 0.9.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 +40 -6
- package/dist/index.d.ts +88 -4
- package/dist/index.js +148 -7
- package/dist/next-cart.d.ts +3 -1
- package/dist/next-cart.js +6 -4
- package/dist/next-session.js +5 -24
- package/dist/next-webhooks.d.ts +6 -0
- package/dist/next-webhooks.js +32 -12
- package/dist/next.d.ts +67 -0
- package/dist/types.d.ts +526 -0
- package/dist/webhooks.d.ts +42 -2
- package/dist/webhooks.js +61 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -32,13 +32,13 @@ const { page } = await channel.pages.get("home");
|
|
|
32
32
|
const { order: cart } = await channel.cart.addItem({ product_id: "product-id", quantity: 1 });
|
|
33
33
|
|
|
34
34
|
// Checkout is addressed by the ORDER the cart became (every cart call returns
|
|
35
|
-
// its order.id).
|
|
36
|
-
//
|
|
37
|
-
//
|
|
35
|
+
// its order.id). One key belongs to one logical payment attempt. Keep it stable
|
|
36
|
+
// while retrying that attempt; mint a new key after a correctable checkout
|
|
37
|
+
// failure so the server does not replay the old 4xx response.
|
|
38
38
|
const checkout = await channel.checkout.pay(
|
|
39
39
|
cart.id,
|
|
40
40
|
{ cart_token: cart.cart_token!, return_url: "https://example.com/checkout/complete" },
|
|
41
|
-
{ idempotencyKey:
|
|
41
|
+
{ idempotencyKey: paymentAttemptId },
|
|
42
42
|
);
|
|
43
43
|
```
|
|
44
44
|
|
|
@@ -51,6 +51,14 @@ const channel = createChannelClient({
|
|
|
51
51
|
baseUrl: process.env.C1M_API_URL!,
|
|
52
52
|
token: process.env.C1M_CHANNEL_TOKEN!,
|
|
53
53
|
});
|
|
54
|
+
|
|
55
|
+
const category = await channel.categories.products("electric-cars");
|
|
56
|
+
|
|
57
|
+
// Customer credentials are request-scoped and never stored on the shared
|
|
58
|
+
// channel client. Every account read is forced to no-store.
|
|
59
|
+
const account = channel.withCustomer(customerAccessToken);
|
|
60
|
+
const addresses = await account.addresses.list();
|
|
61
|
+
const subscriptions = await account.subscriptions.list({ status: "active" });
|
|
54
62
|
```
|
|
55
63
|
|
|
56
64
|
The package must only be imported from server modules. Never expose the token in
|
|
@@ -69,6 +77,8 @@ other runtimes must enforce their own server-module boundary.
|
|
|
69
77
|
continues to resolve tenant and workspace identity at the API door.
|
|
70
78
|
- Requests time out after 10 seconds by default.
|
|
71
79
|
- API failures throw `ChannelAPIError` with safe status, code, and request ID.
|
|
80
|
+
- Use `isChannelAPIError(error)` instead of `instanceof`; it remains correct
|
|
81
|
+
when a server bundle contains more than one package instance.
|
|
72
82
|
- Assets remain tokenless and are normalized with `assetUrl()`.
|
|
73
83
|
- `observe` can receive one bounded telemetry event per logical request. Events
|
|
74
84
|
contain operation, outcome, status, request ID, duration and attempt count;
|
|
@@ -111,7 +121,6 @@ Build and inspect before publishing:
|
|
|
111
121
|
pnpm --filter @c1m-public/channel-client test:run
|
|
112
122
|
pnpm --filter @c1m-public/channel-client build
|
|
113
123
|
pnpm --filter @c1m-public/channel-client pack
|
|
114
|
-
npm publish --access public
|
|
115
124
|
```
|
|
116
125
|
|
|
117
126
|
Publishing runs through npm Trusted Publishing and GitHub OIDC. Push a protected
|
|
@@ -136,6 +145,31 @@ export const POST = createChannelWebhookRoute({
|
|
|
136
145
|
});
|
|
137
146
|
```
|
|
138
147
|
|
|
148
|
+
Out of the box that route keeps your cache honest with no configuration:
|
|
149
|
+
`page.published` revalidates the published path plus `CHANNEL_SITE_CACHE_TAG`,
|
|
150
|
+
and every `product.*` event revalidates `CHANNEL_CATALOG_CACHE_TAG`. Tag your
|
|
151
|
+
catalog reads with it and a merchant's price edit reaches the storefront on the
|
|
152
|
+
next request instead of at the end of a TTL — the difference between a correct
|
|
153
|
+
price and a stale one at checkout.
|
|
154
|
+
|
|
155
|
+
```ts
|
|
156
|
+
// A client whose reads are filed under the catalog tag the webhook route busts.
|
|
157
|
+
const catalog = createNextChannelClient({
|
|
158
|
+
baseUrl: process.env.C1M_API_URL!,
|
|
159
|
+
token: process.env.C1M_CHANNEL_TOKEN!,
|
|
160
|
+
cache: { revalidate: 3600, tags: [CHANNEL_CATALOG_CACHE_TAG] },
|
|
161
|
+
});
|
|
162
|
+
const products = await catalog.products.list();
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
The core also exposes the complete current channel door: public category,
|
|
166
|
+
collection and brand navigation; published blog and giving reads; cart and
|
|
167
|
+
checkout; Quick Print settings, quotes and multipart intake; assembled dining
|
|
168
|
+
menus; and signed-in customer orders, addresses, giving history, saved payment
|
|
169
|
+
methods and subscriptions. `pnpm contract:check` compares both client
|
|
170
|
+
inventories with the canonical server OpenAPI document when the repositories
|
|
171
|
+
are checked out together.
|
|
172
|
+
|
|
139
173
|
```ts
|
|
140
174
|
// session: cookie codec + the contested-vs-expired refresh decision
|
|
141
175
|
const session = createCustomerSession({ client: getChannelClient });
|
|
@@ -151,5 +185,5 @@ await cart.addItem({ cart_token, product_id, quantity: 1 });
|
|
|
151
185
|
const { redirect_url } = await cart.checkout.pay(orderId, {
|
|
152
186
|
cart_token,
|
|
153
187
|
return_url: "https://your-site.example/thanks", // must be on the channel's registered site
|
|
154
|
-
});
|
|
188
|
+
}, { idempotencyKey: paymentAttemptId });
|
|
155
189
|
```
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export * from "./webhooks.js";
|
|
2
|
-
export type
|
|
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";
|
|
2
|
+
export type * from "./types.js";
|
|
3
|
+
import type { CatalogBrand, CatalogCategory, CatalogCollection, CatalogProductsResponse, BlogPostResponse, DiningMenu, GivingEntry, GivingIntentListResponse, AddCartItemInput, CartResponse, CheckoutAddressesInput, CheckoutCustomerInput, CheckoutFulfillmentInput, CheckoutOptions, CheckoutPayInput, CheckoutPayResponse, CheckoutStatusResponse, ChannelPing, CustomerResponse, CustomerSession, CustomerSignupResponse, LeadResponse, LightInitResponse, ListResponse, Order, OrderSummary, MergeCartInput, SendCodeResponse, Product, ProductAsset, PrintIntakeInput, PrintJob, PrintOptionsSelection, PrintQuoteResponse, PrintSettingsResponse, PublicPage, PublicPageSummary, PublicSubmitReviewInput, PublicWorkspaceInit, MenuResponse, Review, ReviewSummaryResponse, SavedAddress, SavedAddressDefaultKind, SavedAddressInput, SavedAddressResponse, SavedAddressUpdateInput, SavedPaymentMethod, SiteDocument, StartGivingInput, StartGivingResponse, PaymentMethodConfirmInput, PaymentMethodMutationResponse, PaymentMethodSetupInput, PaymentMethodSetupResult, SubscriptionAgreementResponse, SubscriptionInvoiceListResponse, SubscriptionInvoiceResponse, SubscriptionListResponse, SubscriptionSourceResponse, ThemeResponse, UpdateCartItemInput } from "./types.js";
|
|
4
4
|
export interface ChannelRetryPolicy {
|
|
5
5
|
attempts?: number;
|
|
6
6
|
maxDelayMs?: number;
|
|
@@ -12,9 +12,11 @@ export interface CreateChannelClientOptions {
|
|
|
12
12
|
timeoutMs?: number;
|
|
13
13
|
retry?: ChannelRetryPolicy | false;
|
|
14
14
|
observe?: ChannelRequestObserver;
|
|
15
|
+
/** Optional request-scope cancellation signal, combined with the timeout. */
|
|
16
|
+
signal?: AbortSignal;
|
|
15
17
|
}
|
|
16
|
-
export type ChannelRequestOutcome = "success" | "api_error" | "network_error" | "timeout" | "invalid_response";
|
|
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";
|
|
18
|
+
export type ChannelRequestOutcome = "success" | "api_error" | "network_error" | "timeout" | "invalid_response" | "cancelled";
|
|
19
|
+
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.blog.list" | "channel.giving.entries.list" | "channel.giving.entries.get" | "channel.giving.start" | "channel.print.settings" | "channel.print.quote" | "channel.print.intake" | "dining.public.board_menu.channel" | "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.addresses.list" | "channel.account.addresses.create" | "channel.account.addresses.get" | "channel.account.addresses.update" | "channel.account.addresses.delete" | "channel.account.addresses.set_default" | "channel.account.giving.list" | "channel.account.orders.list" | "channel.account.orders.summary" | "channel.account.orders.get" | "channel.account.payment_methods.list" | "channel.account.payment_methods.setup" | "channel.account.payment_methods.confirm" | "channel.account.payment_methods.default" | "channel.account.payment_methods.delete" | "channel.account.subscriptions.list" | "channel.account.subscriptions.get" | "channel.account.subscriptions.source" | "channel.account.subscriptions.cancel" | "channel.account.subscriptions.pause" | "channel.account.subscriptions.resume" | "channel.account.subscriptions.invoices.list" | "channel.account.subscriptions.invoices.get" | "channel.account.subscriptions.invoices.pay_now" | "channel.catalog.brands.list" | "channel.catalog.categories.list" | "channel.catalog.categories.products" | "channel.catalog.collections.list" | "channel.catalog.collections.products" | "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.set" | "channel.checkout.addresses.set" | "channel.checkout.fulfillment.set" | "channel.checkout.pay" | "growth.public_workspace.leads.capture.channel";
|
|
18
20
|
export interface ChannelRequestObservation {
|
|
19
21
|
operation: ChannelOperation;
|
|
20
22
|
method: string;
|
|
@@ -35,6 +37,10 @@ export interface ChannelListParams {
|
|
|
35
37
|
}
|
|
36
38
|
export interface ChannelProductListParams extends ChannelListParams {
|
|
37
39
|
currency?: string;
|
|
40
|
+
search?: string;
|
|
41
|
+
category_id?: string;
|
|
42
|
+
brand_id?: string;
|
|
43
|
+
type_id?: string;
|
|
38
44
|
}
|
|
39
45
|
export interface ChannelSiteResolveParams {
|
|
40
46
|
route?: string;
|
|
@@ -54,12 +60,23 @@ export interface ChannelCheckoutStatusParams {
|
|
|
54
60
|
cartToken?: string;
|
|
55
61
|
checkoutSessionId?: string;
|
|
56
62
|
}
|
|
63
|
+
export interface ChannelSubscriptionListParams extends ChannelListParams {
|
|
64
|
+
status?: string;
|
|
65
|
+
}
|
|
66
|
+
export interface ChannelSubscriptionCancelParams {
|
|
67
|
+
immediate?: boolean;
|
|
68
|
+
}
|
|
69
|
+
export interface ChannelInvoiceListParams {
|
|
70
|
+
limit?: number;
|
|
71
|
+
}
|
|
57
72
|
export declare class ChannelAPIError extends Error {
|
|
58
73
|
readonly status: number;
|
|
59
74
|
readonly code: string;
|
|
60
75
|
readonly requestId: string;
|
|
61
76
|
constructor(message: string, status: number, code: string, requestId: string);
|
|
62
77
|
}
|
|
78
|
+
/** Works across duplicate package instances and transformed test bundles. */
|
|
79
|
+
export declare function isChannelAPIError(error: unknown): error is ChannelAPIError;
|
|
63
80
|
export declare function createChannelClient(options: CreateChannelClientOptions): {
|
|
64
81
|
/**
|
|
65
82
|
* Verify the channel token and see what it points at.
|
|
@@ -92,6 +109,17 @@ export declare function createChannelClient(options: CreateChannelClientOptions)
|
|
|
92
109
|
}>;
|
|
93
110
|
readonly media: (slug: string) => Promise<ListResponse<ProductAsset>>;
|
|
94
111
|
};
|
|
112
|
+
readonly categories: {
|
|
113
|
+
readonly list: (params?: ChannelListParams) => Promise<ListResponse<CatalogCategory>>;
|
|
114
|
+
readonly products: (slug: string) => Promise<CatalogProductsResponse>;
|
|
115
|
+
};
|
|
116
|
+
readonly collections: {
|
|
117
|
+
readonly list: (params?: ChannelListParams) => Promise<ListResponse<CatalogCollection>>;
|
|
118
|
+
readonly products: (slug: string) => Promise<CatalogProductsResponse>;
|
|
119
|
+
};
|
|
120
|
+
readonly brands: {
|
|
121
|
+
readonly list: (params?: ChannelListParams) => Promise<ListResponse<CatalogBrand>>;
|
|
122
|
+
};
|
|
95
123
|
readonly cart: {
|
|
96
124
|
readonly get: (cartToken: string) => Promise<CartResponse>;
|
|
97
125
|
readonly addItem: (input: AddCartItemInput, mutation?: ChannelMutationOptions) => Promise<CartResponse>;
|
|
@@ -122,8 +150,30 @@ export declare function createChannelClient(options: CreateChannelClientOptions)
|
|
|
122
150
|
readonly get: (handle: string) => Promise<MenuResponse>;
|
|
123
151
|
};
|
|
124
152
|
readonly blog: {
|
|
153
|
+
readonly list: (params?: ChannelListParams) => Promise<ListResponse<import("./types.js").BlogPost>>;
|
|
125
154
|
readonly get: (slug: string) => Promise<BlogPostResponse>;
|
|
126
155
|
};
|
|
156
|
+
readonly giving: {
|
|
157
|
+
readonly entries: {
|
|
158
|
+
readonly list: () => Promise<{
|
|
159
|
+
items: GivingEntry[];
|
|
160
|
+
}>;
|
|
161
|
+
readonly get: (slug: string) => Promise<{
|
|
162
|
+
entry: GivingEntry;
|
|
163
|
+
}>;
|
|
164
|
+
};
|
|
165
|
+
readonly start: (input: StartGivingInput, mutation?: ChannelMutationOptions) => Promise<StartGivingResponse>;
|
|
166
|
+
};
|
|
167
|
+
readonly print: {
|
|
168
|
+
readonly settings: () => Promise<PrintSettingsResponse>;
|
|
169
|
+
readonly quote: (input: PrintOptionsSelection) => Promise<PrintQuoteResponse>;
|
|
170
|
+
readonly intake: (input: PrintIntakeInput, mutation?: ChannelMutationOptions) => Promise<{
|
|
171
|
+
job: PrintJob;
|
|
172
|
+
}>;
|
|
173
|
+
};
|
|
174
|
+
readonly dining: {
|
|
175
|
+
readonly menu: (boardSlug: string) => Promise<DiningMenu>;
|
|
176
|
+
};
|
|
127
177
|
readonly auth: {
|
|
128
178
|
readonly check: (email: string) => Promise<{
|
|
129
179
|
exists: boolean;
|
|
@@ -169,6 +219,40 @@ export declare function createChannelClient(options: CreateChannelClientOptions)
|
|
|
169
219
|
order: Order;
|
|
170
220
|
}>;
|
|
171
221
|
};
|
|
222
|
+
readonly addresses: {
|
|
223
|
+
readonly list: () => Promise<ListResponse<SavedAddress>>;
|
|
224
|
+
readonly get: (addressId: string) => Promise<SavedAddressResponse>;
|
|
225
|
+
readonly create: (input: SavedAddressInput, mutation?: ChannelMutationOptions) => Promise<SavedAddressResponse>;
|
|
226
|
+
readonly update: (addressId: string, input: SavedAddressUpdateInput, mutation?: ChannelMutationOptions) => Promise<SavedAddressResponse>;
|
|
227
|
+
readonly delete: (addressId: string, mutation?: ChannelMutationOptions) => Promise<{
|
|
228
|
+
status?: string;
|
|
229
|
+
}>;
|
|
230
|
+
readonly setDefault: (addressId: string, kind: SavedAddressDefaultKind, mutation?: ChannelMutationOptions) => Promise<SavedAddressResponse>;
|
|
231
|
+
};
|
|
232
|
+
readonly paymentMethods: {
|
|
233
|
+
readonly list: () => Promise<ListResponse<SavedPaymentMethod>>;
|
|
234
|
+
readonly setup: (input: PaymentMethodSetupInput, mutation?: ChannelMutationOptions) => Promise<PaymentMethodSetupResult>;
|
|
235
|
+
readonly confirm: (input: PaymentMethodConfirmInput, mutation?: ChannelMutationOptions) => Promise<SavedPaymentMethod>;
|
|
236
|
+
readonly setDefault: (methodId: string, mutation?: ChannelMutationOptions) => Promise<PaymentMethodMutationResponse>;
|
|
237
|
+
readonly delete: (methodId: string, mutation?: ChannelMutationOptions) => Promise<PaymentMethodMutationResponse>;
|
|
238
|
+
};
|
|
239
|
+
readonly subscriptions: {
|
|
240
|
+
readonly list: (params?: ChannelSubscriptionListParams) => Promise<SubscriptionListResponse>;
|
|
241
|
+
readonly get: (agreementId: string) => Promise<SubscriptionAgreementResponse>;
|
|
242
|
+
readonly source: (agreementId: string) => Promise<SubscriptionSourceResponse>;
|
|
243
|
+
readonly cancel: (agreementId: string, params?: ChannelSubscriptionCancelParams, mutation?: ChannelMutationOptions) => Promise<SubscriptionAgreementResponse>;
|
|
244
|
+
readonly pause: (agreementId: string, mutation?: ChannelMutationOptions) => Promise<SubscriptionAgreementResponse>;
|
|
245
|
+
readonly resume: (agreementId: string, mutation?: ChannelMutationOptions) => Promise<SubscriptionAgreementResponse>;
|
|
246
|
+
readonly invoices: {
|
|
247
|
+
readonly list: (agreementId: string, params?: ChannelInvoiceListParams) => Promise<SubscriptionInvoiceListResponse>;
|
|
248
|
+
readonly get: (invoiceId: string) => Promise<SubscriptionInvoiceResponse>;
|
|
249
|
+
readonly payNow: (invoiceId: string, mutation?: ChannelMutationOptions) => Promise<SubscriptionInvoiceResponse>;
|
|
250
|
+
};
|
|
251
|
+
};
|
|
252
|
+
readonly giving: {
|
|
253
|
+
readonly list: () => Promise<GivingIntentListResponse>;
|
|
254
|
+
readonly start: (input: StartGivingInput, mutation?: ChannelMutationOptions) => Promise<StartGivingResponse>;
|
|
255
|
+
};
|
|
172
256
|
};
|
|
173
257
|
readonly leads: {
|
|
174
258
|
readonly create: (input: ChannelLeadInput, mutation?: ChannelMutationOptions) => Promise<{
|
package/dist/index.js
CHANGED
|
@@ -13,6 +13,15 @@ export class ChannelAPIError extends Error {
|
|
|
13
13
|
this.name = "ChannelAPIError";
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
|
+
/** Works across duplicate package instances and transformed test bundles. */
|
|
17
|
+
export function isChannelAPIError(error) {
|
|
18
|
+
return (error instanceof ChannelAPIError ||
|
|
19
|
+
(typeof error === "object" && error !== null &&
|
|
20
|
+
error.name === "ChannelAPIError" &&
|
|
21
|
+
typeof error.status === "number" &&
|
|
22
|
+
typeof error.code === "string" &&
|
|
23
|
+
typeof error.requestId === "string"));
|
|
24
|
+
}
|
|
16
25
|
function required(value, label) {
|
|
17
26
|
const normalized = value.trim();
|
|
18
27
|
if (!normalized)
|
|
@@ -49,6 +58,17 @@ function retryDelay(response, maxDelayMs) {
|
|
|
49
58
|
function wait(ms) {
|
|
50
59
|
return ms <= 0 ? Promise.resolve() : new Promise((resolve) => setTimeout(resolve, ms));
|
|
51
60
|
}
|
|
61
|
+
function combineSignals(...signals) {
|
|
62
|
+
const active = signals.filter((signal) => Boolean(signal));
|
|
63
|
+
if (active.length === 0)
|
|
64
|
+
return undefined;
|
|
65
|
+
if (active.length === 1)
|
|
66
|
+
return active[0];
|
|
67
|
+
return AbortSignal.any(active);
|
|
68
|
+
}
|
|
69
|
+
function isFormDataBody(body) {
|
|
70
|
+
return typeof FormData !== "undefined" && body instanceof FormData;
|
|
71
|
+
}
|
|
52
72
|
export function createChannelClient(options) {
|
|
53
73
|
const baseUrl = required(options.baseUrl, "baseUrl").replace(/\/+$/, "");
|
|
54
74
|
const token = required(options.token, "token");
|
|
@@ -79,8 +99,9 @@ export function createChannelClient(options) {
|
|
|
79
99
|
headers.set("Authorization", `Bearer ${token}`);
|
|
80
100
|
if (session)
|
|
81
101
|
headers.set("X-End-User-Token", session);
|
|
82
|
-
if (init.body !== undefined && !headers.has("Content-Type"))
|
|
102
|
+
if (init.body !== undefined && !isFormDataBody(init.body) && !headers.has("Content-Type")) {
|
|
83
103
|
headers.set("Content-Type", "application/json");
|
|
104
|
+
}
|
|
84
105
|
const requestInit = { ...init, method, headers };
|
|
85
106
|
// Private reads and every mutation are non-cacheable in every runtime. An
|
|
86
107
|
// adapter may add framework caching only to anonymous GET requests.
|
|
@@ -94,14 +115,25 @@ export function createChannelClient(options) {
|
|
|
94
115
|
const controller = new AbortController();
|
|
95
116
|
const timeout = setTimeout(() => controller.abort(), timeoutMs);
|
|
96
117
|
try {
|
|
97
|
-
response = await fetcher(`${baseUrl}/api/v1/channel${path}`, {
|
|
118
|
+
response = await fetcher(`${baseUrl}/api/v1/channel${path}`, {
|
|
119
|
+
...requestInit,
|
|
120
|
+
signal: combineSignals(options.signal, requestInit.signal, controller.signal),
|
|
121
|
+
});
|
|
98
122
|
}
|
|
99
|
-
catch
|
|
123
|
+
catch {
|
|
100
124
|
if (controller.signal.aborted) {
|
|
101
125
|
outcome = "timeout";
|
|
102
126
|
throw new ChannelAPIError("Channel API request timed out", 0, "channel_request_timeout", "");
|
|
103
127
|
}
|
|
128
|
+
if (options.signal?.aborted || requestInit.signal?.aborted) {
|
|
129
|
+
outcome = "cancelled";
|
|
130
|
+
throw new ChannelAPIError("Channel API request was cancelled", 0, "channel_request_cancelled", "");
|
|
131
|
+
}
|
|
104
132
|
outcome = "network_error";
|
|
133
|
+
if (method === "GET" && attempt < retryAttempts) {
|
|
134
|
+
await wait(Math.min(250, maxRetryDelayMs));
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
105
137
|
throw new ChannelAPIError("Channel API request failed", 0, "channel_network_error", "");
|
|
106
138
|
}
|
|
107
139
|
finally {
|
|
@@ -122,8 +154,13 @@ export function createChannelClient(options) {
|
|
|
122
154
|
throw new ChannelAPIError(typeof payload.message === "string" ? payload.message : "Channel API request failed", response.status, typeof payload.code === "string" ? payload.code : "channel_request_failed", requestId);
|
|
123
155
|
}
|
|
124
156
|
try {
|
|
125
|
-
const payload = (await response.json());
|
|
126
157
|
outcome = "success";
|
|
158
|
+
if (response.status === 204)
|
|
159
|
+
return undefined;
|
|
160
|
+
const text = await response.text();
|
|
161
|
+
if (!text)
|
|
162
|
+
return undefined;
|
|
163
|
+
const payload = JSON.parse(text);
|
|
127
164
|
return payload;
|
|
128
165
|
}
|
|
129
166
|
catch {
|
|
@@ -173,6 +210,17 @@ export function createChannelClient(options) {
|
|
|
173
210
|
get: (slug) => request("shop.public.products.get.channel", `/products/${encodePath(slug)}`),
|
|
174
211
|
media: (slug) => request("shop.public.products.media.channel", `/products/${encodePath(slug)}/media`),
|
|
175
212
|
},
|
|
213
|
+
categories: {
|
|
214
|
+
list: (params = {}) => request("channel.catalog.categories.list", appendQuery("/categories", params)),
|
|
215
|
+
products: (slug) => request("channel.catalog.categories.products", `/categories/by-slug/${encodePath(slug)}/products`),
|
|
216
|
+
},
|
|
217
|
+
collections: {
|
|
218
|
+
list: (params = {}) => request("channel.catalog.collections.list", appendQuery("/collections", params)),
|
|
219
|
+
products: (slug) => request("channel.catalog.collections.products", `/collections/by-slug/${encodePath(slug)}/products`),
|
|
220
|
+
},
|
|
221
|
+
brands: {
|
|
222
|
+
list: (params = {}) => request("channel.catalog.brands.list", appendQuery("/brands", params)),
|
|
223
|
+
},
|
|
176
224
|
cart: {
|
|
177
225
|
get: (cartToken) => request("channel.cart.get", appendQuery("/cart", { cart_token: required(cartToken, "cartToken") })),
|
|
178
226
|
addItem: (input, mutation) => request("channel.cart.items.add", "/cart/items", {
|
|
@@ -206,13 +254,13 @@ export function createChannelClient(options) {
|
|
|
206
254
|
cart_token: params.cartToken,
|
|
207
255
|
checkout_session_id: params.checkoutSessionId,
|
|
208
256
|
})),
|
|
209
|
-
setCustomer: (orderId, input, mutation) => request("channel.checkout.customer", `/checkout/${encodePath(orderId)}/customer`, {
|
|
257
|
+
setCustomer: (orderId, input, mutation) => request("channel.checkout.customer.set", `/checkout/${encodePath(orderId)}/customer`, {
|
|
210
258
|
method: "PUT", headers: mutationHeaders(mutation), body: JSON.stringify(input),
|
|
211
259
|
}),
|
|
212
|
-
setAddresses: (orderId, input, mutation) => request("channel.checkout.addresses", `/checkout/${encodePath(orderId)}/addresses`, {
|
|
260
|
+
setAddresses: (orderId, input, mutation) => request("channel.checkout.addresses.set", `/checkout/${encodePath(orderId)}/addresses`, {
|
|
213
261
|
method: "PUT", headers: mutationHeaders(mutation), body: JSON.stringify(input),
|
|
214
262
|
}),
|
|
215
|
-
setFulfillment: (orderId, input, mutation) => request("channel.checkout.fulfillment", `/checkout/${encodePath(orderId)}/fulfillment`, {
|
|
263
|
+
setFulfillment: (orderId, input, mutation) => request("channel.checkout.fulfillment.set", `/checkout/${encodePath(orderId)}/fulfillment`, {
|
|
216
264
|
method: "PUT", headers: mutationHeaders(mutation), body: JSON.stringify(input),
|
|
217
265
|
}),
|
|
218
266
|
pay: (orderId, input, mutation) => request("channel.checkout.pay", `/checkout/${encodePath(orderId)}/pay`, {
|
|
@@ -233,8 +281,43 @@ export function createChannelClient(options) {
|
|
|
233
281
|
get: (handle) => request("channel.menus.get", `/menus/${encodePath(handle)}`),
|
|
234
282
|
},
|
|
235
283
|
blog: {
|
|
284
|
+
list: (params = {}) => request("channel.blog.list", appendQuery("/blog/posts", params)),
|
|
236
285
|
get: (slug) => request("channel.blog.get", `/blog/posts/${encodePath(slug)}`),
|
|
237
286
|
},
|
|
287
|
+
giving: {
|
|
288
|
+
entries: {
|
|
289
|
+
list: () => request("channel.giving.entries.list", "/giving/entries"),
|
|
290
|
+
get: (slug) => request("channel.giving.entries.get", `/giving/entries/${encodePath(slug)}`),
|
|
291
|
+
},
|
|
292
|
+
start: (input, mutation) => request("channel.giving.start", "/give/start", {
|
|
293
|
+
method: "POST", headers: mutationHeaders(mutation), body: JSON.stringify(input),
|
|
294
|
+
}),
|
|
295
|
+
},
|
|
296
|
+
print: {
|
|
297
|
+
settings: () => request("channel.print.settings", "/print/settings"),
|
|
298
|
+
quote: (input) => request("channel.print.quote", "/print/quote", {
|
|
299
|
+
method: "POST", body: JSON.stringify(input),
|
|
300
|
+
}),
|
|
301
|
+
intake: (input, mutation) => {
|
|
302
|
+
const form = new FormData();
|
|
303
|
+
form.set("name", input.name);
|
|
304
|
+
if (input.email)
|
|
305
|
+
form.set("email", input.email);
|
|
306
|
+
if (input.phone)
|
|
307
|
+
form.set("phone", input.phone);
|
|
308
|
+
if (input.notes)
|
|
309
|
+
form.set("notes", input.notes);
|
|
310
|
+
form.set("options", JSON.stringify(input.options));
|
|
311
|
+
for (const file of input.files)
|
|
312
|
+
form.append("files", file.blob, file.filename);
|
|
313
|
+
return request("channel.print.intake", "/print/intake", {
|
|
314
|
+
method: "POST", headers: mutationHeaders(mutation), body: form,
|
|
315
|
+
});
|
|
316
|
+
},
|
|
317
|
+
},
|
|
318
|
+
dining: {
|
|
319
|
+
menu: (boardSlug) => request("dining.public.board_menu.channel", `/dining/boards/${encodePath(boardSlug)}/menu`),
|
|
320
|
+
},
|
|
238
321
|
// Customer sign-in. These are anonymous calls — they CREATE a session.
|
|
239
322
|
auth: {
|
|
240
323
|
check: (email) => request("channel.auth.check", "/auth/check", { method: "POST", body: JSON.stringify({ email }) }),
|
|
@@ -265,6 +348,64 @@ export function createChannelClient(options) {
|
|
|
265
348
|
summary: () => request("channel.account.orders.summary", "/account/orders/summary", {}, customerSession),
|
|
266
349
|
get: (orderId) => request("channel.account.orders.get", `/account/orders/${encodePath(orderId)}`, {}, customerSession),
|
|
267
350
|
},
|
|
351
|
+
addresses: {
|
|
352
|
+
list: () => request("channel.account.addresses.list", "/account/addresses", {}, customerSession),
|
|
353
|
+
get: (addressId) => request("channel.account.addresses.get", `/account/addresses/${encodePath(addressId)}`, {}, customerSession),
|
|
354
|
+
create: (input, mutation) => request("channel.account.addresses.create", "/account/addresses", {
|
|
355
|
+
method: "POST", headers: mutationHeaders(mutation), body: JSON.stringify(input),
|
|
356
|
+
}, customerSession),
|
|
357
|
+
update: (addressId, input, mutation) => request("channel.account.addresses.update", `/account/addresses/${encodePath(addressId)}`, {
|
|
358
|
+
method: "PUT", headers: mutationHeaders(mutation), body: JSON.stringify(input),
|
|
359
|
+
}, customerSession),
|
|
360
|
+
delete: (addressId, mutation) => request("channel.account.addresses.delete", `/account/addresses/${encodePath(addressId)}`, {
|
|
361
|
+
method: "DELETE", headers: mutationHeaders(mutation),
|
|
362
|
+
}, customerSession),
|
|
363
|
+
setDefault: (addressId, kind, mutation) => request("channel.account.addresses.set_default", `/account/addresses/${encodePath(addressId)}/default`, {
|
|
364
|
+
method: "POST", headers: mutationHeaders(mutation), body: JSON.stringify({ kind }),
|
|
365
|
+
}, customerSession),
|
|
366
|
+
},
|
|
367
|
+
paymentMethods: {
|
|
368
|
+
list: () => request("channel.account.payment_methods.list", "/account/payment-methods", {}, customerSession),
|
|
369
|
+
setup: (input, mutation) => request("channel.account.payment_methods.setup", "/account/payment-methods/setup", {
|
|
370
|
+
method: "POST", headers: mutationHeaders(mutation), body: JSON.stringify(input),
|
|
371
|
+
}, customerSession),
|
|
372
|
+
confirm: (input, mutation) => request("channel.account.payment_methods.confirm", "/account/payment-methods/confirm", {
|
|
373
|
+
method: "POST", headers: mutationHeaders(mutation), body: JSON.stringify(input),
|
|
374
|
+
}, customerSession),
|
|
375
|
+
setDefault: (methodId, mutation) => request("channel.account.payment_methods.default", `/account/payment-methods/${encodePath(methodId)}/default`, {
|
|
376
|
+
method: "POST", headers: mutationHeaders(mutation),
|
|
377
|
+
}, customerSession),
|
|
378
|
+
delete: (methodId, mutation) => request("channel.account.payment_methods.delete", `/account/payment-methods/${encodePath(methodId)}`, {
|
|
379
|
+
method: "DELETE", headers: mutationHeaders(mutation),
|
|
380
|
+
}, customerSession),
|
|
381
|
+
},
|
|
382
|
+
subscriptions: {
|
|
383
|
+
list: (params = {}) => request("channel.account.subscriptions.list", appendQuery("/account/subscriptions", params), {}, customerSession),
|
|
384
|
+
get: (agreementId) => request("channel.account.subscriptions.get", `/account/subscriptions/${encodePath(agreementId)}`, {}, customerSession),
|
|
385
|
+
source: (agreementId) => request("channel.account.subscriptions.source", `/account/subscriptions/${encodePath(agreementId)}/source`, {}, customerSession),
|
|
386
|
+
cancel: (agreementId, params = {}, mutation) => request("channel.account.subscriptions.cancel", appendQuery(`/account/subscriptions/${encodePath(agreementId)}/cancel`, params), {
|
|
387
|
+
method: "POST", headers: mutationHeaders(mutation),
|
|
388
|
+
}, customerSession),
|
|
389
|
+
pause: (agreementId, mutation) => request("channel.account.subscriptions.pause", `/account/subscriptions/${encodePath(agreementId)}/pause`, {
|
|
390
|
+
method: "POST", headers: mutationHeaders(mutation),
|
|
391
|
+
}, customerSession),
|
|
392
|
+
resume: (agreementId, mutation) => request("channel.account.subscriptions.resume", `/account/subscriptions/${encodePath(agreementId)}/resume`, {
|
|
393
|
+
method: "POST", headers: mutationHeaders(mutation),
|
|
394
|
+
}, customerSession),
|
|
395
|
+
invoices: {
|
|
396
|
+
list: (agreementId, params = {}) => request("channel.account.subscriptions.invoices.list", appendQuery(`/account/subscriptions/${encodePath(agreementId)}/invoices`, params), {}, customerSession),
|
|
397
|
+
get: (invoiceId) => request("channel.account.subscriptions.invoices.get", `/account/subscriptions/invoices/${encodePath(invoiceId)}`, {}, customerSession),
|
|
398
|
+
payNow: (invoiceId, mutation) => request("channel.account.subscriptions.invoices.pay_now", `/account/subscriptions/invoices/${encodePath(invoiceId)}/pay-now`, {
|
|
399
|
+
method: "POST", headers: mutationHeaders(mutation),
|
|
400
|
+
}, customerSession),
|
|
401
|
+
},
|
|
402
|
+
},
|
|
403
|
+
giving: {
|
|
404
|
+
list: () => request("channel.account.giving.list", "/account/giving", {}, customerSession),
|
|
405
|
+
start: (input, mutation) => request("channel.giving.start", "/give/start", {
|
|
406
|
+
method: "POST", headers: mutationHeaders(mutation), body: JSON.stringify(input),
|
|
407
|
+
}, customerSession),
|
|
408
|
+
},
|
|
268
409
|
};
|
|
269
410
|
},
|
|
270
411
|
leads: {
|
package/dist/next-cart.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import "server-only";
|
|
2
|
-
import { createChannelClient, type ChannelCheckoutStatusParams } from "./index.js";
|
|
2
|
+
import { createChannelClient, type ChannelCheckoutStatusParams, type ChannelMutationOptions } from "./index.js";
|
|
3
3
|
import type { AddCartItemInput, CartResponse, CheckoutAddressesInput, CheckoutCustomerInput, CheckoutFulfillmentInput, CheckoutOptions, CheckoutPayInput, CheckoutPayResponse, CheckoutStatusResponse, Order } from "./types.js";
|
|
4
4
|
export interface CartCookieAttributes {
|
|
5
5
|
httpOnly: true;
|
|
@@ -67,6 +67,8 @@ export declare function createCart(options: CreateCartOptions): {
|
|
|
67
67
|
*/
|
|
68
68
|
readonly pay: (orderId: string, input: CheckoutPayInput & {
|
|
69
69
|
return_url: string;
|
|
70
|
+
}, attempt: ChannelMutationOptions & {
|
|
71
|
+
idempotencyKey: string;
|
|
70
72
|
}) => Promise<CheckoutPayResponse>;
|
|
71
73
|
};
|
|
72
74
|
};
|
package/dist/next-cart.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import "server-only";
|
|
2
|
-
import { createChannelClient } from "./index.js";
|
|
2
|
+
import { createChannelClient, isChannelAPIError, } from "./index.js";
|
|
3
3
|
function inProduction() {
|
|
4
4
|
return typeof process !== "undefined" && process?.env?.NODE_ENV === "production";
|
|
5
5
|
}
|
|
@@ -39,8 +39,10 @@ export function createCart(options) {
|
|
|
39
39
|
const { order } = await resolve().cart.get(cartToken);
|
|
40
40
|
return order ?? null;
|
|
41
41
|
}
|
|
42
|
-
catch {
|
|
43
|
-
|
|
42
|
+
catch (error) {
|
|
43
|
+
if (isChannelAPIError(error) && error.status === 404)
|
|
44
|
+
return null;
|
|
45
|
+
throw error;
|
|
44
46
|
}
|
|
45
47
|
},
|
|
46
48
|
async addItem(input) {
|
|
@@ -79,7 +81,7 @@ export function createCart(options) {
|
|
|
79
81
|
* must be on the channel's registered site_url — the server refuses
|
|
80
82
|
* anything else, because an unchecked return URL is an open redirect.
|
|
81
83
|
*/
|
|
82
|
-
pay: (orderId, input) => resolve().checkout.pay(orderId, input,
|
|
84
|
+
pay: (orderId, input, attempt) => resolve().checkout.pay(orderId, input, attempt),
|
|
83
85
|
},
|
|
84
86
|
};
|
|
85
87
|
}
|
package/dist/next-session.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import "server-only";
|
|
2
|
-
import {
|
|
2
|
+
import { createChannelClient, isChannelAPIError } from "./index.js";
|
|
3
3
|
function inProduction() {
|
|
4
4
|
return typeof process !== "undefined" && process?.env?.NODE_ENV === "production";
|
|
5
5
|
}
|
|
@@ -62,23 +62,6 @@ export const sessionCookie = {
|
|
|
62
62
|
};
|
|
63
63
|
/** The server's code for "another request rotated this token moments ago". */
|
|
64
64
|
const CONTESTED = "refresh_contested";
|
|
65
|
-
/**
|
|
66
|
-
* Structural, not instanceof: a published package can be instantiated twice in
|
|
67
|
-
* one process (pnpm strictness, bundler duplication, test transforms), and
|
|
68
|
-
* `instanceof` across instances silently fails — which here would misclassify a
|
|
69
|
-
* DEAD session as a transient wobble and keep a corpse cookie forever.
|
|
70
|
-
*/
|
|
71
|
-
function asChannelAPIError(error) {
|
|
72
|
-
if (error instanceof ChannelAPIError)
|
|
73
|
-
return error;
|
|
74
|
-
if (typeof error === "object" && error !== null &&
|
|
75
|
-
error.name === "ChannelAPIError" &&
|
|
76
|
-
typeof error.status === "number" &&
|
|
77
|
-
typeof error.code === "string") {
|
|
78
|
-
return error;
|
|
79
|
-
}
|
|
80
|
-
return null;
|
|
81
|
-
}
|
|
82
65
|
function normalizeCustomer(customer) {
|
|
83
66
|
return {
|
|
84
67
|
id: customer.id,
|
|
@@ -127,8 +110,7 @@ export function createCustomerSession(options) {
|
|
|
127
110
|
}
|
|
128
111
|
catch (error) {
|
|
129
112
|
// A wrong code is an ordinary thing a person does, not a failure.
|
|
130
|
-
|
|
131
|
-
if (api && (api.status === 401 || api.status === 400)) {
|
|
113
|
+
if (isChannelAPIError(error) && error.code === "invalid_code") {
|
|
132
114
|
return { state: "invalid-code" };
|
|
133
115
|
}
|
|
134
116
|
return { state: "failed" };
|
|
@@ -153,14 +135,13 @@ export function createCustomerSession(options) {
|
|
|
153
135
|
};
|
|
154
136
|
}
|
|
155
137
|
catch (error) {
|
|
156
|
-
|
|
157
|
-
if (api) {
|
|
138
|
+
if (isChannelAPIError(error)) {
|
|
158
139
|
// Lost a race with the customer's own other tab: the session is alive
|
|
159
140
|
// and that tab already stored a good token. KEEP the cookie.
|
|
160
|
-
if (
|
|
141
|
+
if (error.code === CONTESTED)
|
|
161
142
|
return { action: "keep" };
|
|
162
143
|
// Genuinely dead — logged out, revoked, or aged out. Clear it.
|
|
163
|
-
if (
|
|
144
|
+
if (error.status === 401)
|
|
164
145
|
return { action: "clear" };
|
|
165
146
|
}
|
|
166
147
|
// An upstream wobble is not evidence anyone's session is dead.
|
package/dist/next-webhooks.d.ts
CHANGED
|
@@ -16,6 +16,12 @@ import { type ChannelWebhookEvent, type ChannelWebhookEventDataMap } from "./web
|
|
|
16
16
|
*/
|
|
17
17
|
/** The cache tag every anonymous site read is filed under. */
|
|
18
18
|
export declare const CHANNEL_SITE_CACHE_TAG = "channel:site";
|
|
19
|
+
/**
|
|
20
|
+
* The cache tag catalog reads (product list, product detail) are filed under.
|
|
21
|
+
* `product.*` events bust this tag by default, so a merchant's price edit
|
|
22
|
+
* reaches the storefront on the next request instead of at the end of a TTL.
|
|
23
|
+
*/
|
|
24
|
+
export declare const CHANNEL_CATALOG_CACHE_TAG = "channel:catalog";
|
|
19
25
|
export interface ChannelCacheInvalidator {
|
|
20
26
|
revalidatePath(path: string): void;
|
|
21
27
|
revalidateTag(tag: string): void;
|