@c1m-public/channel-client 0.19.0 → 0.21.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 +16 -5
- package/dist/chunk-BSTXJIVL.js +334 -0
- package/dist/chunk-C44TGKRK.js +611 -0
- package/dist/chunk-FRUWMKQP.js +17 -0
- package/dist/chunk-RJQ4YEZV.js +180 -0
- package/dist/dash-next.d.ts +8 -5
- package/dist/dash-next.js +10 -11
- package/dist/dash.d.ts +7 -5
- package/dist/dash.js +3 -16
- package/dist/index.d.ts +1339 -27
- package/dist/index.js +2 -513
- package/dist/moving-lead-model/index.d.ts +417 -3
- package/dist/moving-lead-model/index.js +288 -2
- package/dist/next.d.ts +355 -103
- package/dist/next.js +3 -69
- package/dist/renderer.d.ts +11 -9
- package/dist/renderer.js +75 -95
- package/dist/webhooks.d.ts +28 -26
- package/dist/webhooks.js +1 -228
- package/package.json +5 -3
- package/dist/errors.d.ts +0 -12
- package/dist/errors.js +0 -19
- package/dist/moving-lead-model/address-model.d.ts +0 -12
- package/dist/moving-lead-model/address-model.js +0 -43
- package/dist/moving-lead-model/address-types.d.ts +0 -55
- package/dist/moving-lead-model/address-types.js +0 -12
- package/dist/moving-lead-model/quote-model.d.ts +0 -348
- package/dist/moving-lead-model/quote-model.js +0 -267
- package/dist/next-cart.d.ts +0 -76
- package/dist/next-cart.js +0 -87
- package/dist/next-session.d.ts +0 -121
- package/dist/next-session.js +0 -177
- package/dist/next-webhooks.d.ts +0 -58
- package/dist/next-webhooks.js +0 -109
- package/dist/types.d.ts +0 -1273
- package/dist/types.js +0 -1
|
@@ -0,0 +1,611 @@
|
|
|
1
|
+
import { ChannelConfigurationError } from './chunk-RJQ4YEZV.js';
|
|
2
|
+
import 'server-only';
|
|
3
|
+
|
|
4
|
+
// ../transport/src/index.ts
|
|
5
|
+
var RETRYABLE_STATUSES = /* @__PURE__ */ new Set([429, 502, 503, 504]);
|
|
6
|
+
var TransportError = class extends Error {
|
|
7
|
+
constructor(failure) {
|
|
8
|
+
super(failure.message || failure.outcome);
|
|
9
|
+
this.failure = failure;
|
|
10
|
+
this.name = "TransportError";
|
|
11
|
+
}
|
|
12
|
+
failure;
|
|
13
|
+
};
|
|
14
|
+
function isTransportError(error) {
|
|
15
|
+
return error instanceof TransportError || typeof error === "object" && error !== null && error.name === "TransportError";
|
|
16
|
+
}
|
|
17
|
+
function positiveNumber(value, label, fail) {
|
|
18
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value <= 0) throw fail(`${label} must be a positive finite number`);
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
function nonNegativeInteger(value, label, fail) {
|
|
22
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0) throw fail(`${label} must be a non-negative safe integer`);
|
|
23
|
+
return value;
|
|
24
|
+
}
|
|
25
|
+
function requiredString(value, label, fail) {
|
|
26
|
+
const normalized = typeof value === "string" ? value.trim() : "";
|
|
27
|
+
if (!normalized) throw fail(`${label} is required`);
|
|
28
|
+
return normalized;
|
|
29
|
+
}
|
|
30
|
+
function backoffDelay(attempt, maxDelayMs) {
|
|
31
|
+
const base = Math.min(250 * 2 ** Math.max(attempt, 0), maxDelayMs);
|
|
32
|
+
return Math.floor(Math.random() * base);
|
|
33
|
+
}
|
|
34
|
+
function retryDelay(response, attempt, maxDelayMs) {
|
|
35
|
+
const value = response.headers.get("Retry-After");
|
|
36
|
+
if (!value) return backoffDelay(attempt, maxDelayMs);
|
|
37
|
+
const seconds = Number(value);
|
|
38
|
+
if (Number.isFinite(seconds)) return Math.min(Math.max(seconds * 1e3, 0), maxDelayMs);
|
|
39
|
+
const date = Date.parse(value);
|
|
40
|
+
return Number.isNaN(date) ? backoffDelay(attempt, maxDelayMs) : Math.min(Math.max(date - Date.now(), 0), maxDelayMs);
|
|
41
|
+
}
|
|
42
|
+
function wait(ms) {
|
|
43
|
+
return ms <= 0 ? Promise.resolve() : new Promise((resolve) => setTimeout(resolve, ms));
|
|
44
|
+
}
|
|
45
|
+
function combineSignals(...signals) {
|
|
46
|
+
const active = signals.filter((signal) => Boolean(signal));
|
|
47
|
+
if (active.length === 0) return void 0;
|
|
48
|
+
if (active.length === 1) return active[0];
|
|
49
|
+
return AbortSignal.any(active);
|
|
50
|
+
}
|
|
51
|
+
function isRetryableRequest(method, headers, retry) {
|
|
52
|
+
if (method === "GET") return true;
|
|
53
|
+
return retry.idempotentWrites === true && headers.has("Idempotency-Key");
|
|
54
|
+
}
|
|
55
|
+
async function performRequest(transport, url, init, observe) {
|
|
56
|
+
const method = (init.method ?? "GET").toUpperCase();
|
|
57
|
+
const headers = new Headers(init.headers);
|
|
58
|
+
const startedAt = Date.now();
|
|
59
|
+
const retryable = isRetryableRequest(method, headers, transport.retry);
|
|
60
|
+
const maxAttempts = retryable ? transport.retry.attempts : 0;
|
|
61
|
+
let attempts = 0;
|
|
62
|
+
let status = 0;
|
|
63
|
+
let requestId = "";
|
|
64
|
+
let outcome = "network_error";
|
|
65
|
+
const fail = (failure) => {
|
|
66
|
+
outcome = failure.outcome;
|
|
67
|
+
throw new TransportError({ requestId, ...failure });
|
|
68
|
+
};
|
|
69
|
+
try {
|
|
70
|
+
let response;
|
|
71
|
+
for (let attempt = 0; attempt <= maxAttempts; attempt += 1) {
|
|
72
|
+
attempts = attempt + 1;
|
|
73
|
+
const controller = new AbortController();
|
|
74
|
+
const timeout = setTimeout(() => controller.abort(), transport.timeoutMs);
|
|
75
|
+
try {
|
|
76
|
+
response = await transport.fetch(url, {
|
|
77
|
+
...init,
|
|
78
|
+
method,
|
|
79
|
+
headers,
|
|
80
|
+
signal: combineSignals(transport.signal, init.signal, controller.signal)
|
|
81
|
+
});
|
|
82
|
+
} catch {
|
|
83
|
+
if (controller.signal.aborted) fail({ outcome: "timeout", status: 0, code: "", message: "" });
|
|
84
|
+
if (transport.signal?.aborted || init.signal?.aborted) fail({ outcome: "cancelled", status: 0, code: "", message: "" });
|
|
85
|
+
if (attempt < maxAttempts) {
|
|
86
|
+
await wait(backoffDelay(attempt, transport.retry.maxDelayMs));
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
fail({ outcome: "network_error", status: 0, code: "", message: "" });
|
|
90
|
+
} finally {
|
|
91
|
+
clearTimeout(timeout);
|
|
92
|
+
}
|
|
93
|
+
if (!RETRYABLE_STATUSES.has(response.status) || attempt === maxAttempts) break;
|
|
94
|
+
await wait(retryDelay(response, attempt, transport.retry.maxDelayMs));
|
|
95
|
+
}
|
|
96
|
+
if (!response) return fail({ outcome: "network_error", status: 0, code: "", message: "" });
|
|
97
|
+
status = response.status;
|
|
98
|
+
requestId = response.headers.get("X-Request-ID") ?? "";
|
|
99
|
+
if (!response.ok) {
|
|
100
|
+
const envelope = await response.json().catch(() => ({}));
|
|
101
|
+
if (typeof envelope.request_id === "string") requestId = envelope.request_id;
|
|
102
|
+
return fail({
|
|
103
|
+
outcome: "api_error",
|
|
104
|
+
status,
|
|
105
|
+
code: typeof envelope.code === "string" ? envelope.code : "",
|
|
106
|
+
message: typeof envelope.message === "string" ? envelope.message : "",
|
|
107
|
+
requestId
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
let payload;
|
|
111
|
+
try {
|
|
112
|
+
if (status === 204) {
|
|
113
|
+
payload = void 0;
|
|
114
|
+
} else {
|
|
115
|
+
const text = await response.text();
|
|
116
|
+
payload = text ? JSON.parse(text) : void 0;
|
|
117
|
+
}
|
|
118
|
+
} catch {
|
|
119
|
+
return fail({ outcome: "invalid_response", status, code: "", message: "", requestId });
|
|
120
|
+
}
|
|
121
|
+
outcome = "success";
|
|
122
|
+
return { payload, status, requestId };
|
|
123
|
+
} finally {
|
|
124
|
+
if (observe) {
|
|
125
|
+
try {
|
|
126
|
+
observe({ method, outcome, status, requestId, durationMs: Math.max(0, Date.now() - startedAt), attempts });
|
|
127
|
+
} catch {
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// src/index.ts
|
|
134
|
+
var CHANNEL_TOKEN_PREFIX = "sk_chan_";
|
|
135
|
+
var DEFAULT_CHANNEL_API_URL = "https://api.saasdash.app";
|
|
136
|
+
var ChannelAPIError = class extends Error {
|
|
137
|
+
constructor(message, status, code, requestId) {
|
|
138
|
+
super(message);
|
|
139
|
+
this.status = status;
|
|
140
|
+
this.code = code;
|
|
141
|
+
this.requestId = requestId;
|
|
142
|
+
this.name = "ChannelAPIError";
|
|
143
|
+
}
|
|
144
|
+
status;
|
|
145
|
+
code;
|
|
146
|
+
requestId;
|
|
147
|
+
};
|
|
148
|
+
function isChannelAPIError(error) {
|
|
149
|
+
return error instanceof ChannelAPIError || typeof error === "object" && error !== null && error.name === "ChannelAPIError" && typeof error.status === "number" && typeof error.code === "string" && typeof error.requestId === "string";
|
|
150
|
+
}
|
|
151
|
+
var configurationError = (message) => new ChannelConfigurationError(message);
|
|
152
|
+
function required(value, label) {
|
|
153
|
+
return requiredString(value, label, configurationError);
|
|
154
|
+
}
|
|
155
|
+
function encodePath(value) {
|
|
156
|
+
return encodeURIComponent(required(value, "path value"));
|
|
157
|
+
}
|
|
158
|
+
function appendQuery(path, values) {
|
|
159
|
+
const query = new URLSearchParams();
|
|
160
|
+
for (const [key, value] of Object.entries(values)) {
|
|
161
|
+
if (value !== void 0 && value !== "") query.set(key, String(value));
|
|
162
|
+
}
|
|
163
|
+
const encoded = query.toString();
|
|
164
|
+
return encoded ? `${path}?${encoded}` : path;
|
|
165
|
+
}
|
|
166
|
+
function isFormDataBody(body) {
|
|
167
|
+
return typeof FormData !== "undefined" && body instanceof FormData;
|
|
168
|
+
}
|
|
169
|
+
async function visitorHeaders(token, method, path, visitor) {
|
|
170
|
+
if (!visitor) return void 0;
|
|
171
|
+
const ip = visitor.ip?.trim() ?? "";
|
|
172
|
+
const country = visitor.country?.trim().toUpperCase() ?? "";
|
|
173
|
+
const userAgent = visitor.userAgent?.trim() ?? "";
|
|
174
|
+
if (!ip && !country && !userAgent) return void 0;
|
|
175
|
+
const timestamp = Math.floor(Date.now() / 1e3).toString();
|
|
176
|
+
const encoder = new TextEncoder();
|
|
177
|
+
const uaHash = await crypto.subtle.digest("SHA-256", encoder.encode(userAgent));
|
|
178
|
+
const hex = (bytes) => Array.from(new Uint8Array(bytes), (value) => value.toString(16).padStart(2, "0")).join("");
|
|
179
|
+
const canonical = ["v1", timestamp, method, `/api/v1/channel${path}`, ip, country, hex(uaHash)].join("\n");
|
|
180
|
+
const key = await crypto.subtle.importKey("raw", encoder.encode(token), { name: "HMAC", hash: "SHA-256" }, false, ["sign"]);
|
|
181
|
+
const signature = hex(await crypto.subtle.sign("HMAC", key, encoder.encode(canonical)));
|
|
182
|
+
return {
|
|
183
|
+
"X-C1M-Visitor-IP": ip,
|
|
184
|
+
"X-C1M-Visitor-Country": country,
|
|
185
|
+
"X-C1M-Visitor-User-Agent": userAgent,
|
|
186
|
+
"X-C1M-Visitor-Timestamp": timestamp,
|
|
187
|
+
"X-C1M-Visitor-Signature": signature
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
function createChannelClient(options) {
|
|
191
|
+
const baseUrl = (options.baseUrl === void 0 ? DEFAULT_CHANNEL_API_URL : required(options.baseUrl, "baseUrl")).replace(/\/+$/, "");
|
|
192
|
+
const token = required(options.token, "token");
|
|
193
|
+
if (!token.startsWith(CHANNEL_TOKEN_PREFIX)) throw new ChannelConfigurationError("token must start with sk_chan_");
|
|
194
|
+
const fetcher = options.fetch ?? globalThis.fetch;
|
|
195
|
+
if (!fetcher) throw new ChannelConfigurationError("fetch implementation is required");
|
|
196
|
+
const transport = {
|
|
197
|
+
fetch: fetcher,
|
|
198
|
+
timeoutMs: positiveNumber(options.timeoutMs ?? 1e4, "timeoutMs", configurationError),
|
|
199
|
+
retry: {
|
|
200
|
+
attempts: options.retry === false ? 0 : nonNegativeInteger(options.retry?.attempts ?? 1, "retry.attempts", configurationError),
|
|
201
|
+
maxDelayMs: positiveNumber(options.retry === false ? 2e3 : options.retry?.maxDelayMs ?? 2e3, "retry.maxDelayMs", configurationError),
|
|
202
|
+
idempotentWrites: options.retry === false ? false : options.retry?.idempotentWrites ?? true
|
|
203
|
+
},
|
|
204
|
+
signal: options.signal
|
|
205
|
+
};
|
|
206
|
+
const observe = options.observe;
|
|
207
|
+
async function request(operation, path, init = {}, session) {
|
|
208
|
+
const method = (init.method ?? "GET").toUpperCase();
|
|
209
|
+
const headers = new Headers(init.headers);
|
|
210
|
+
headers.set("Authorization", `Bearer ${token}`);
|
|
211
|
+
const signedVisitor = await visitorHeaders(token, method, path, options.visitor);
|
|
212
|
+
if (signedVisitor) {
|
|
213
|
+
for (const [key, value] of Object.entries(signedVisitor)) headers.set(key, value);
|
|
214
|
+
}
|
|
215
|
+
if (session) headers.set("X-End-User-Token", session);
|
|
216
|
+
if (init.body !== void 0 && !isFormDataBody(init.body) && !headers.has("Content-Type")) {
|
|
217
|
+
headers.set("Content-Type", "application/json");
|
|
218
|
+
}
|
|
219
|
+
const requestInit = { ...init, method, headers };
|
|
220
|
+
if (method !== "GET" || session) {
|
|
221
|
+
requestInit.cache = "no-store";
|
|
222
|
+
}
|
|
223
|
+
try {
|
|
224
|
+
const result = await performRequest(transport, `${baseUrl}/api/v1/channel${path}`, requestInit, observe && ((event) => observe({ operation, ...event })));
|
|
225
|
+
return result.payload;
|
|
226
|
+
} catch (error) {
|
|
227
|
+
if (!isTransportError(error)) throw error;
|
|
228
|
+
const { outcome, status, code, message, requestId } = error.failure;
|
|
229
|
+
switch (outcome) {
|
|
230
|
+
case "timeout":
|
|
231
|
+
throw new ChannelAPIError("Channel API request timed out", 0, "channel_request_timeout", "");
|
|
232
|
+
case "cancelled":
|
|
233
|
+
throw new ChannelAPIError("Channel API request was cancelled", 0, "channel_request_cancelled", "");
|
|
234
|
+
case "network_error":
|
|
235
|
+
throw new ChannelAPIError("Channel API request failed", 0, "channel_network_error", "");
|
|
236
|
+
case "invalid_response":
|
|
237
|
+
throw new ChannelAPIError("Channel API response was not valid JSON", status, "invalid_channel_response", requestId);
|
|
238
|
+
case "api_error":
|
|
239
|
+
throw new ChannelAPIError(message || "Channel API request failed", status, code || "channel_request_failed", requestId);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
function mutationHeaders(mutation) {
|
|
244
|
+
return mutation?.idempotencyKey ? { "Idempotency-Key": mutation.idempotencyKey } : void 0;
|
|
245
|
+
}
|
|
246
|
+
return {
|
|
247
|
+
/**
|
|
248
|
+
* Verify the channel token and see what it points at.
|
|
249
|
+
*
|
|
250
|
+
* The cheapest call on the door — it does no database work server-side. Use
|
|
251
|
+
* it as a deployment health check: a 200 means C1M_CHANNEL_TOKEN is right and
|
|
252
|
+
* aimed at the scope you expect. A ChannelAPIError with status 401 means
|
|
253
|
+
* the token is bad, revoked, or the channel is disabled (the door will not
|
|
254
|
+
* say which). A 503 means C1M is unavailable — NOT that your token is wrong.
|
|
255
|
+
*/
|
|
256
|
+
ping: (challenge) => request("channel.ping", appendQuery("/ping", { challenge })),
|
|
257
|
+
/** Return a request-scoped client that signs trusted edge visitor metadata. */
|
|
258
|
+
withVisitor: (visitor) => createChannelClient({ ...options, visitor }),
|
|
259
|
+
geo: {
|
|
260
|
+
detect: () => request("channel.geo.detect", "/geo/detect")
|
|
261
|
+
},
|
|
262
|
+
payments: {
|
|
263
|
+
/**
|
|
264
|
+
* Read the platform-wide collection cutoff before rendering a money
|
|
265
|
+
* action. The server returns `available: false` if its authority cannot
|
|
266
|
+
* be read, so callers never need to interpret a missing state as safe.
|
|
267
|
+
*/
|
|
268
|
+
availability: () => request(
|
|
269
|
+
"channel.payments.availability",
|
|
270
|
+
"/payments/availability",
|
|
271
|
+
{ cache: "no-store" }
|
|
272
|
+
)
|
|
273
|
+
},
|
|
274
|
+
/** Tenant-level data exposed only to a dash-scoped channel token. */
|
|
275
|
+
dash: {
|
|
276
|
+
profile: () => request("channel.dash.profile", "/dash/profile"),
|
|
277
|
+
init: () => request("channel.dash.init", "/dash/init"),
|
|
278
|
+
plans: () => request("channel.dash.plans", "/dash/plans"),
|
|
279
|
+
directory: {
|
|
280
|
+
regions: () => request("channel.dash.directory.regions", "/dash/directory/regions"),
|
|
281
|
+
list: (params = {}) => request("channel.dash.directory.browse", appendQuery("/dash/directory", params)),
|
|
282
|
+
get: (idOrSlug) => request("channel.dash.directory.get", `/dash/directory/${encodePath(idOrSlug)}`)
|
|
283
|
+
},
|
|
284
|
+
leads: {
|
|
285
|
+
create: (input, mutation) => request("channel.dash.leads.capture", "/dash/leads", {
|
|
286
|
+
method: "POST",
|
|
287
|
+
headers: mutationHeaders(mutation),
|
|
288
|
+
body: JSON.stringify(input)
|
|
289
|
+
})
|
|
290
|
+
},
|
|
291
|
+
// The Lead Match Portal. A requester asks for a signed link by email,
|
|
292
|
+
// then views their quote requests with the token that link carries. Not
|
|
293
|
+
// a customer session: the token is the only credential, and view() takes
|
|
294
|
+
// it explicitly so a site never has to hold it in a cookie.
|
|
295
|
+
leadPortal: {
|
|
296
|
+
requestLink: (input) => request(
|
|
297
|
+
"channel.dash.lead_portal.request_link",
|
|
298
|
+
"/dash/lead-portal/request-link",
|
|
299
|
+
{ method: "POST", body: JSON.stringify(input) }
|
|
300
|
+
),
|
|
301
|
+
view: (token2) => request(
|
|
302
|
+
"channel.dash.lead_portal.my_quotes",
|
|
303
|
+
appendQuery("/dash/lead-portal/my-quotes", { token: token2 })
|
|
304
|
+
)
|
|
305
|
+
}
|
|
306
|
+
},
|
|
307
|
+
assetUrl: (pathOrUrl) => new URL(required(pathOrUrl, "asset URL"), `${baseUrl}/`).toString(),
|
|
308
|
+
site: {
|
|
309
|
+
resolve: (params = {}) => request("channel.site.resolve", appendQuery("/site/resolve", params))
|
|
310
|
+
},
|
|
311
|
+
init: {
|
|
312
|
+
get: () => request("channel.init.get", "/init"),
|
|
313
|
+
light: () => request("channel.init.light", "/init/light")
|
|
314
|
+
},
|
|
315
|
+
pages: {
|
|
316
|
+
list: (params = {}) => request("content.pages.public.list.channel", appendQuery("/pages", params)),
|
|
317
|
+
get: (slug) => request("content.pages.public.get.channel", `/pages/${encodePath(slug)}`)
|
|
318
|
+
},
|
|
319
|
+
products: {
|
|
320
|
+
list: (params = {}) => request("shop.public.products.list.channel", appendQuery("/products", params)),
|
|
321
|
+
get: (slug) => request("shop.public.products.get.channel", `/products/${encodePath(slug)}`),
|
|
322
|
+
media: (slug) => request("shop.public.products.media.channel", `/products/${encodePath(slug)}/media`)
|
|
323
|
+
},
|
|
324
|
+
categories: {
|
|
325
|
+
list: (params = {}) => request("channel.catalog.categories.list", appendQuery("/categories", params)),
|
|
326
|
+
products: (slug) => request("channel.catalog.categories.products", `/categories/by-slug/${encodePath(slug)}/products`)
|
|
327
|
+
},
|
|
328
|
+
collections: {
|
|
329
|
+
list: (params = {}) => request("channel.catalog.collections.list", appendQuery("/collections", params)),
|
|
330
|
+
products: (slug) => request("channel.catalog.collections.products", `/collections/by-slug/${encodePath(slug)}/products`)
|
|
331
|
+
},
|
|
332
|
+
brands: {
|
|
333
|
+
list: (params = {}) => request("channel.catalog.brands.list", appendQuery("/brands", params))
|
|
334
|
+
},
|
|
335
|
+
cart: {
|
|
336
|
+
get: (cartToken) => request("channel.cart.get", appendQuery("/cart", { cart_token: required(cartToken, "cartToken") })),
|
|
337
|
+
addItem: (input, mutation) => request("channel.cart.items.add", "/cart/items", {
|
|
338
|
+
method: "POST",
|
|
339
|
+
headers: mutationHeaders(mutation),
|
|
340
|
+
body: JSON.stringify(input)
|
|
341
|
+
}),
|
|
342
|
+
updateItem: (itemId, input, mutation) => request("channel.cart.items.update", `/cart/items/${encodePath(itemId)}`, {
|
|
343
|
+
method: "PATCH",
|
|
344
|
+
headers: mutationHeaders(mutation),
|
|
345
|
+
body: JSON.stringify(input)
|
|
346
|
+
}),
|
|
347
|
+
removeItem: (itemId, cartToken, mutation) => request("channel.cart.items.remove", appendQuery(`/cart/items/${encodePath(itemId)}`, {
|
|
348
|
+
cart_token: required(cartToken, "cartToken")
|
|
349
|
+
}), {
|
|
350
|
+
method: "DELETE",
|
|
351
|
+
headers: mutationHeaders(mutation)
|
|
352
|
+
}),
|
|
353
|
+
clear: (cartToken, mutation) => request("channel.cart.clear", appendQuery("/cart", { cart_token: required(cartToken, "cartToken") }), {
|
|
354
|
+
method: "DELETE",
|
|
355
|
+
headers: mutationHeaders(mutation)
|
|
356
|
+
}),
|
|
357
|
+
merge: (input, mutation) => request("channel.cart.merge", "/cart/merge", {
|
|
358
|
+
method: "POST",
|
|
359
|
+
headers: mutationHeaders(mutation),
|
|
360
|
+
body: JSON.stringify(input)
|
|
361
|
+
})
|
|
362
|
+
},
|
|
363
|
+
// Checkout is addressed by the ORDER the cart became: the server mounts
|
|
364
|
+
// /checkout/{order_id}/... (the cart is an orders row, and every cart call
|
|
365
|
+
// returns its order.id). 0.5.0 shipped these without the order_id segment,
|
|
366
|
+
// so every checkout call 404'd — the signature change is safe because the
|
|
367
|
+
// old shape never worked.
|
|
368
|
+
checkout: {
|
|
369
|
+
options: (orderId, cartToken) => request("channel.checkout.options", appendQuery(`/checkout/${encodePath(orderId)}/options`, {
|
|
370
|
+
cart_token: required(cartToken, "cartToken")
|
|
371
|
+
})),
|
|
372
|
+
status: (orderId, params = {}) => request("channel.checkout.status", appendQuery(`/checkout/${encodePath(orderId)}/status`, {
|
|
373
|
+
cart_token: params.cartToken,
|
|
374
|
+
checkout_session_id: params.checkoutSessionId
|
|
375
|
+
})),
|
|
376
|
+
setCustomer: (orderId, input, mutation) => request("channel.checkout.customer.set", `/checkout/${encodePath(orderId)}/customer`, {
|
|
377
|
+
method: "PUT",
|
|
378
|
+
headers: mutationHeaders(mutation),
|
|
379
|
+
body: JSON.stringify(input)
|
|
380
|
+
}),
|
|
381
|
+
setAddresses: (orderId, input, mutation) => request("channel.checkout.addresses.set", `/checkout/${encodePath(orderId)}/addresses`, {
|
|
382
|
+
method: "PUT",
|
|
383
|
+
headers: mutationHeaders(mutation),
|
|
384
|
+
body: JSON.stringify(input)
|
|
385
|
+
}),
|
|
386
|
+
setFulfillment: (orderId, input, mutation) => request("channel.checkout.fulfillment.set", `/checkout/${encodePath(orderId)}/fulfillment`, {
|
|
387
|
+
method: "PUT",
|
|
388
|
+
headers: mutationHeaders(mutation),
|
|
389
|
+
body: JSON.stringify(input)
|
|
390
|
+
}),
|
|
391
|
+
pay: (orderId, input, mutation) => request("channel.checkout.pay", `/checkout/${encodePath(orderId)}/pay`, {
|
|
392
|
+
method: "POST",
|
|
393
|
+
headers: mutationHeaders(mutation),
|
|
394
|
+
body: JSON.stringify(input)
|
|
395
|
+
})
|
|
396
|
+
},
|
|
397
|
+
reviews: {
|
|
398
|
+
list: (params = {}) => request("reviews.public.list.channel", appendQuery("/reviews", params)),
|
|
399
|
+
summary: () => request("reviews.public.summary.channel", "/reviews/summary"),
|
|
400
|
+
submit: (input, mutation) => request("reviews.public.submit.channel", "/reviews", {
|
|
401
|
+
method: "POST",
|
|
402
|
+
headers: mutationHeaders(mutation),
|
|
403
|
+
body: JSON.stringify(input)
|
|
404
|
+
})
|
|
405
|
+
},
|
|
406
|
+
theme: {
|
|
407
|
+
get: () => request("channel.theme.get", "/theme")
|
|
408
|
+
},
|
|
409
|
+
menus: {
|
|
410
|
+
get: (handle) => request("channel.menus.get", `/menus/${encodePath(handle)}`)
|
|
411
|
+
},
|
|
412
|
+
blog: {
|
|
413
|
+
list: (params = {}) => request("channel.blog.list", appendQuery("/blog/posts", params)),
|
|
414
|
+
get: (slug) => request("channel.blog.get", `/blog/posts/${encodePath(slug)}`)
|
|
415
|
+
},
|
|
416
|
+
giving: {
|
|
417
|
+
entries: {
|
|
418
|
+
list: () => request("channel.giving.entries.list", "/giving/entries"),
|
|
419
|
+
get: (slug) => request("channel.giving.entries.get", `/giving/entries/${encodePath(slug)}`)
|
|
420
|
+
},
|
|
421
|
+
start: (input, mutation) => request("channel.giving.start", "/give/start", {
|
|
422
|
+
method: "POST",
|
|
423
|
+
headers: mutationHeaders(mutation),
|
|
424
|
+
body: JSON.stringify(input)
|
|
425
|
+
})
|
|
426
|
+
},
|
|
427
|
+
subscriptions: {
|
|
428
|
+
// Plan catalogue for rendering a membership picker. Managing an agreement
|
|
429
|
+
// a customer already holds lives under withCustomer(...).subscriptions.
|
|
430
|
+
plans: {
|
|
431
|
+
list: () => request("channel.subscriptions.plans.list", "/subscriptions/plans"),
|
|
432
|
+
get: (planId) => request(
|
|
433
|
+
"channel.subscriptions.plans.get",
|
|
434
|
+
`/subscriptions/plans/${encodePath(planId)}`
|
|
435
|
+
)
|
|
436
|
+
}
|
|
437
|
+
},
|
|
438
|
+
paymentLinks: {
|
|
439
|
+
get: (linkId) => request(
|
|
440
|
+
"monetization.payment_links.public_get.channel",
|
|
441
|
+
`/payment-links/${encodePath(linkId)}`
|
|
442
|
+
),
|
|
443
|
+
checkout: (linkId, input, mutation) => request(
|
|
444
|
+
"monetization.payment_links.public_checkout.channel",
|
|
445
|
+
`/payment-links/${encodePath(linkId)}/checkout`,
|
|
446
|
+
{ method: "POST", headers: mutationHeaders(mutation), body: JSON.stringify(input) }
|
|
447
|
+
),
|
|
448
|
+
redemptions: {
|
|
449
|
+
get: (linkId, redemptionId) => request(
|
|
450
|
+
"monetization.payment_links.public_redemption_get.channel",
|
|
451
|
+
`/payment-links/${encodePath(linkId)}/redemptions/${encodePath(redemptionId)}`
|
|
452
|
+
)
|
|
453
|
+
}
|
|
454
|
+
},
|
|
455
|
+
print: {
|
|
456
|
+
settings: () => request("channel.print.settings", "/print/settings"),
|
|
457
|
+
quote: (input, mutation) => request("channel.print.quote", "/print/quote", {
|
|
458
|
+
method: "POST",
|
|
459
|
+
headers: mutationHeaders(mutation),
|
|
460
|
+
body: JSON.stringify(input)
|
|
461
|
+
}),
|
|
462
|
+
intake: (input, mutation) => {
|
|
463
|
+
const form = new FormData();
|
|
464
|
+
form.set("name", input.name);
|
|
465
|
+
if (input.email) form.set("email", input.email);
|
|
466
|
+
if (input.phone) form.set("phone", input.phone);
|
|
467
|
+
if (input.notes) form.set("notes", input.notes);
|
|
468
|
+
form.set("options", JSON.stringify(input.options));
|
|
469
|
+
for (const file of input.files) form.append("files", file.blob, file.filename);
|
|
470
|
+
return request("channel.print.intake", "/print/intake", {
|
|
471
|
+
method: "POST",
|
|
472
|
+
headers: mutationHeaders(mutation),
|
|
473
|
+
body: form
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
},
|
|
477
|
+
dining: {
|
|
478
|
+
menu: (boardSlug) => request("dining.public.board_menu.channel", `/dining/boards/${encodePath(boardSlug)}/menu`)
|
|
479
|
+
},
|
|
480
|
+
// Customer sign-in. These are anonymous calls — they CREATE a session.
|
|
481
|
+
auth: {
|
|
482
|
+
check: (email) => request("channel.auth.check", "/auth/check", { method: "POST", body: JSON.stringify({ email }) }),
|
|
483
|
+
signup: (input) => request("channel.auth.signup", "/auth/signup", { method: "POST", body: JSON.stringify(input) }),
|
|
484
|
+
sendCode: (email) => request("channel.auth.send_code", "/auth/send-code", { method: "POST", body: JSON.stringify({ email }) }),
|
|
485
|
+
verifyCode: (input) => request("channel.auth.verify_code", "/auth/verify-code", { method: "POST", body: JSON.stringify(input) }),
|
|
486
|
+
refresh: (refreshToken) => request("channel.auth.refresh", "/auth/refresh", { method: "POST", body: JSON.stringify({ refresh_token: refreshToken }) })
|
|
487
|
+
},
|
|
488
|
+
/**
|
|
489
|
+
* Scope calls to a signed-in customer.
|
|
490
|
+
*
|
|
491
|
+
* The session is passed here rather than stored on the client, so it cannot
|
|
492
|
+
* leak between requests: on a server rendering for many visitors at once, a
|
|
493
|
+
* mutable "current customer" is a cross-customer data leak waiting to
|
|
494
|
+
* happen. Each request carries the session it was given, or none.
|
|
495
|
+
*
|
|
496
|
+
* Orders are scoped server-side to this customer's own email within the
|
|
497
|
+
* channel's workspace — another customer's order id returns 404.
|
|
498
|
+
*/
|
|
499
|
+
withCustomer(session) {
|
|
500
|
+
const customerSession = required(session, "session");
|
|
501
|
+
return {
|
|
502
|
+
me: () => request("channel.auth.me", "/auth/me", {}, customerSession),
|
|
503
|
+
updateProfile: (input) => request("channel.auth.me.update", "/auth/me", { method: "PUT", body: JSON.stringify(input) }, customerSession),
|
|
504
|
+
logout: () => request("channel.auth.logout", "/auth/logout", { method: "POST" }, customerSession),
|
|
505
|
+
sessions: {
|
|
506
|
+
list: () => request("channel.auth.sessions.list", "/auth/me/sessions", {}, customerSession),
|
|
507
|
+
revoke: (sessionId) => request("channel.auth.sessions.revoke", `/auth/me/sessions/${encodePath(sessionId)}`, { method: "DELETE" }, customerSession),
|
|
508
|
+
revokeOthers: () => request("channel.auth.sessions.revoke_others", "/auth/me/sessions/revoke-others", { method: "POST" }, customerSession),
|
|
509
|
+
revokeAll: () => request("channel.auth.sessions.revoke_all", "/auth/me/sessions/revoke-all", { method: "POST" }, customerSession)
|
|
510
|
+
},
|
|
511
|
+
securityActivity: (params = {}) => request("channel.auth.security_activity.list", appendQuery("/auth/me/security-activity", params), {}, customerSession),
|
|
512
|
+
orders: {
|
|
513
|
+
list: (params = {}) => request("channel.account.orders.list", appendQuery("/account/orders", params), {}, customerSession),
|
|
514
|
+
summary: () => request("channel.account.orders.summary", "/account/orders/summary", {}, customerSession),
|
|
515
|
+
get: (orderId) => request("channel.account.orders.get", `/account/orders/${encodePath(orderId)}`, {}, customerSession)
|
|
516
|
+
},
|
|
517
|
+
addresses: {
|
|
518
|
+
list: () => request("channel.account.addresses.list", "/account/addresses", {}, customerSession),
|
|
519
|
+
get: (addressId) => request("channel.account.addresses.get", `/account/addresses/${encodePath(addressId)}`, {}, customerSession),
|
|
520
|
+
create: (input, mutation) => request("channel.account.addresses.create", "/account/addresses", {
|
|
521
|
+
method: "POST",
|
|
522
|
+
headers: mutationHeaders(mutation),
|
|
523
|
+
body: JSON.stringify(input)
|
|
524
|
+
}, customerSession),
|
|
525
|
+
update: (addressId, input, mutation) => request("channel.account.addresses.update", `/account/addresses/${encodePath(addressId)}`, {
|
|
526
|
+
method: "PUT",
|
|
527
|
+
headers: mutationHeaders(mutation),
|
|
528
|
+
body: JSON.stringify(input)
|
|
529
|
+
}, customerSession),
|
|
530
|
+
delete: (addressId, mutation) => request("channel.account.addresses.delete", `/account/addresses/${encodePath(addressId)}`, {
|
|
531
|
+
method: "DELETE",
|
|
532
|
+
headers: mutationHeaders(mutation)
|
|
533
|
+
}, customerSession),
|
|
534
|
+
setDefault: (addressId, kind, mutation) => request("channel.account.addresses.set_default", `/account/addresses/${encodePath(addressId)}/default`, {
|
|
535
|
+
method: "POST",
|
|
536
|
+
headers: mutationHeaders(mutation),
|
|
537
|
+
body: JSON.stringify({ kind })
|
|
538
|
+
}, customerSession)
|
|
539
|
+
},
|
|
540
|
+
paymentMethods: {
|
|
541
|
+
list: () => request("channel.account.payment_methods.list", "/account/payment-methods", {}, customerSession),
|
|
542
|
+
setup: (input, mutation) => request("channel.account.payment_methods.setup", "/account/payment-methods/setup", {
|
|
543
|
+
method: "POST",
|
|
544
|
+
headers: mutationHeaders(mutation),
|
|
545
|
+
body: JSON.stringify(input)
|
|
546
|
+
}, customerSession),
|
|
547
|
+
confirm: (input, mutation) => request("channel.account.payment_methods.confirm", "/account/payment-methods/confirm", {
|
|
548
|
+
method: "POST",
|
|
549
|
+
headers: mutationHeaders(mutation),
|
|
550
|
+
body: JSON.stringify(input)
|
|
551
|
+
}, customerSession),
|
|
552
|
+
setDefault: (methodId, mutation) => request("channel.account.payment_methods.default", `/account/payment-methods/${encodePath(methodId)}/default`, {
|
|
553
|
+
method: "POST",
|
|
554
|
+
headers: mutationHeaders(mutation)
|
|
555
|
+
}, customerSession),
|
|
556
|
+
delete: (methodId, mutation) => request("channel.account.payment_methods.delete", `/account/payment-methods/${encodePath(methodId)}`, {
|
|
557
|
+
method: "DELETE",
|
|
558
|
+
headers: mutationHeaders(mutation)
|
|
559
|
+
}, customerSession)
|
|
560
|
+
},
|
|
561
|
+
subscriptions: {
|
|
562
|
+
start: (input, mutation) => request("channel.subscriptions.start", "/subscriptions/start", {
|
|
563
|
+
method: "POST",
|
|
564
|
+
headers: mutationHeaders(mutation),
|
|
565
|
+
body: JSON.stringify(input)
|
|
566
|
+
}, customerSession),
|
|
567
|
+
list: (params = {}) => request("channel.account.subscriptions.list", appendQuery("/account/subscriptions", params), {}, customerSession),
|
|
568
|
+
get: (agreementId) => request("channel.account.subscriptions.get", `/account/subscriptions/${encodePath(agreementId)}`, {}, customerSession),
|
|
569
|
+
source: (agreementId) => request("channel.account.subscriptions.source", `/account/subscriptions/${encodePath(agreementId)}/source`, {}, customerSession),
|
|
570
|
+
cancel: (agreementId, params = {}, mutation) => request("channel.account.subscriptions.cancel", appendQuery(`/account/subscriptions/${encodePath(agreementId)}/cancel`, params), {
|
|
571
|
+
method: "POST",
|
|
572
|
+
headers: mutationHeaders(mutation)
|
|
573
|
+
}, customerSession),
|
|
574
|
+
pause: (agreementId, mutation) => request("channel.account.subscriptions.pause", `/account/subscriptions/${encodePath(agreementId)}/pause`, {
|
|
575
|
+
method: "POST",
|
|
576
|
+
headers: mutationHeaders(mutation)
|
|
577
|
+
}, customerSession),
|
|
578
|
+
resume: (agreementId, mutation) => request("channel.account.subscriptions.resume", `/account/subscriptions/${encodePath(agreementId)}/resume`, {
|
|
579
|
+
method: "POST",
|
|
580
|
+
headers: mutationHeaders(mutation)
|
|
581
|
+
}, customerSession),
|
|
582
|
+
invoices: {
|
|
583
|
+
list: (agreementId, params = {}) => request("channel.account.subscriptions.invoices.list", appendQuery(`/account/subscriptions/${encodePath(agreementId)}/invoices`, params), {}, customerSession),
|
|
584
|
+
get: (invoiceId) => request("channel.account.subscriptions.invoices.get", `/account/subscriptions/invoices/${encodePath(invoiceId)}`, {}, customerSession),
|
|
585
|
+
payNow: (invoiceId, mutation) => request("channel.account.subscriptions.invoices.pay_now", `/account/subscriptions/invoices/${encodePath(invoiceId)}/pay-now`, {
|
|
586
|
+
method: "POST",
|
|
587
|
+
headers: mutationHeaders(mutation)
|
|
588
|
+
}, customerSession)
|
|
589
|
+
}
|
|
590
|
+
},
|
|
591
|
+
giving: {
|
|
592
|
+
list: () => request("channel.account.giving.list", "/account/giving", {}, customerSession),
|
|
593
|
+
start: (input, mutation) => request("channel.giving.start", "/give/start", {
|
|
594
|
+
method: "POST",
|
|
595
|
+
headers: mutationHeaders(mutation),
|
|
596
|
+
body: JSON.stringify(input)
|
|
597
|
+
}, customerSession)
|
|
598
|
+
}
|
|
599
|
+
};
|
|
600
|
+
},
|
|
601
|
+
leads: {
|
|
602
|
+
create: (input, mutation) => request("growth.public_workspace.leads.capture.channel", "/leads", {
|
|
603
|
+
method: "POST",
|
|
604
|
+
headers: mutationHeaders(mutation),
|
|
605
|
+
body: JSON.stringify(input)
|
|
606
|
+
})
|
|
607
|
+
}
|
|
608
|
+
};
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
export { ChannelAPIError, DEFAULT_CHANNEL_API_URL, createChannelClient, isChannelAPIError };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { createChannelClient } from './chunk-C44TGKRK.js';
|
|
2
|
+
import 'server-only';
|
|
3
|
+
|
|
4
|
+
function narrow(client) {
|
|
5
|
+
return {
|
|
6
|
+
ping: client.ping,
|
|
7
|
+
geo: client.geo,
|
|
8
|
+
dash: client.dash,
|
|
9
|
+
assetUrl: client.assetUrl,
|
|
10
|
+
withVisitor: (visitor) => narrow(client.withVisitor(visitor))
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
function createDashChannelClient(options) {
|
|
14
|
+
return narrow(createChannelClient(options));
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { createDashChannelClient, narrow };
|