@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,180 @@
|
|
|
1
|
+
// src/errors.ts
|
|
2
|
+
var ChannelConfigurationError = class extends Error {
|
|
3
|
+
constructor(message) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.name = "ChannelConfigurationError";
|
|
6
|
+
}
|
|
7
|
+
};
|
|
8
|
+
function isChannelConfigurationError(error) {
|
|
9
|
+
return error instanceof ChannelConfigurationError || typeof error === "object" && error !== null && error.name === "ChannelConfigurationError";
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// src/webhooks.ts
|
|
13
|
+
var CHANNEL_WEBHOOK_EVENT_TYPES = [
|
|
14
|
+
"order.created",
|
|
15
|
+
"order.paid",
|
|
16
|
+
"order.updated",
|
|
17
|
+
"order.cancelled",
|
|
18
|
+
"order.refunded",
|
|
19
|
+
"lead.created",
|
|
20
|
+
"review.submitted",
|
|
21
|
+
"review.published",
|
|
22
|
+
"page.published",
|
|
23
|
+
"product.created",
|
|
24
|
+
"product.updated",
|
|
25
|
+
"product.deleted"
|
|
26
|
+
];
|
|
27
|
+
function isChannelWebhookEventType(value) {
|
|
28
|
+
return CHANNEL_WEBHOOK_EVENT_TYPES.includes(value);
|
|
29
|
+
}
|
|
30
|
+
function isKnownChannelWebhookEvent(event) {
|
|
31
|
+
return isChannelWebhookEventType(event.type);
|
|
32
|
+
}
|
|
33
|
+
var ChannelWebhookVerificationError = class extends Error {
|
|
34
|
+
constructor(message, code) {
|
|
35
|
+
super(message);
|
|
36
|
+
this.code = code;
|
|
37
|
+
this.name = "ChannelWebhookVerificationError";
|
|
38
|
+
}
|
|
39
|
+
code;
|
|
40
|
+
};
|
|
41
|
+
function isChannelWebhookVerificationError(error) {
|
|
42
|
+
return error instanceof ChannelWebhookVerificationError || typeof error === "object" && error !== null && error.name === "ChannelWebhookVerificationError" && typeof error.code === "string";
|
|
43
|
+
}
|
|
44
|
+
var CHANNEL_WEBHOOK_SIGNATURE_HEADER = "X-C1M-Signature";
|
|
45
|
+
var CHANNEL_WEBHOOK_TIMESTAMP_HEADER = "X-C1M-Timestamp";
|
|
46
|
+
var CHANNEL_WEBHOOK_EVENT_ID_HEADER = "X-C1M-Event-ID";
|
|
47
|
+
var DEFAULT_TOLERANCE_SECONDS = 300;
|
|
48
|
+
var encoder = new TextEncoder();
|
|
49
|
+
function toBytes(payload) {
|
|
50
|
+
return typeof payload === "string" ? encoder.encode(payload) : payload;
|
|
51
|
+
}
|
|
52
|
+
function timingSafeEqual(a, b) {
|
|
53
|
+
if (a.length !== b.length) return false;
|
|
54
|
+
let diff = 0;
|
|
55
|
+
for (let i = 0; i < a.length; i += 1) diff |= a[i] ^ b[i];
|
|
56
|
+
return diff === 0;
|
|
57
|
+
}
|
|
58
|
+
function decodeHex(value) {
|
|
59
|
+
if (value.length === 0 || value.length % 2 !== 0 || !/^[0-9a-f]+$/i.test(value)) return null;
|
|
60
|
+
const out = new Uint8Array(value.length / 2);
|
|
61
|
+
for (let i = 0; i < out.length; i += 1) {
|
|
62
|
+
const byte = Number.parseInt(value.slice(i * 2, i * 2 + 2), 16);
|
|
63
|
+
if (Number.isNaN(byte)) return null;
|
|
64
|
+
out[i] = byte;
|
|
65
|
+
}
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
68
|
+
async function verifyChannelWebhookSignature(options) {
|
|
69
|
+
const { payload, signature, timestamp, secret } = options;
|
|
70
|
+
const tolerance = options.toleranceSeconds ?? DEFAULT_TOLERANCE_SECONDS;
|
|
71
|
+
const now = options.now ?? Date.now;
|
|
72
|
+
if (!secret) throw new ChannelConfigurationError("secret is required");
|
|
73
|
+
if (!signature) {
|
|
74
|
+
throw new ChannelWebhookVerificationError(
|
|
75
|
+
`missing ${CHANNEL_WEBHOOK_SIGNATURE_HEADER} header`,
|
|
76
|
+
"missing_signature"
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
if (!timestamp) {
|
|
80
|
+
throw new ChannelWebhookVerificationError(
|
|
81
|
+
`missing ${CHANNEL_WEBHOOK_TIMESTAMP_HEADER} header`,
|
|
82
|
+
"missing_timestamp"
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
const timestampSeconds = Number(timestamp);
|
|
86
|
+
if (!Number.isFinite(timestampSeconds) || !/^\d+$/.test(timestamp.trim())) {
|
|
87
|
+
throw new ChannelWebhookVerificationError("timestamp is not a unix timestamp", "invalid_timestamp");
|
|
88
|
+
}
|
|
89
|
+
if (tolerance > 0) {
|
|
90
|
+
const ageSeconds = Math.abs(now() / 1e3 - timestampSeconds);
|
|
91
|
+
if (ageSeconds > tolerance) {
|
|
92
|
+
throw new ChannelWebhookVerificationError(
|
|
93
|
+
`timestamp is outside the ${tolerance}s tolerance`,
|
|
94
|
+
"timestamp_out_of_tolerance"
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
const provided = decodeHex(signature.startsWith("sha256=") ? signature.slice(7) : signature);
|
|
99
|
+
if (!provided) {
|
|
100
|
+
throw new ChannelWebhookVerificationError("signature is not sha256=<hex>", "invalid_signature");
|
|
101
|
+
}
|
|
102
|
+
const key = await crypto.subtle.importKey(
|
|
103
|
+
"raw",
|
|
104
|
+
encoder.encode(secret),
|
|
105
|
+
{ name: "HMAC", hash: "SHA-256" },
|
|
106
|
+
false,
|
|
107
|
+
["sign"]
|
|
108
|
+
);
|
|
109
|
+
const body = toBytes(payload);
|
|
110
|
+
const signed = new Uint8Array(timestamp.length + 1 + body.length);
|
|
111
|
+
signed.set(encoder.encode(`${timestamp}.`), 0);
|
|
112
|
+
signed.set(body, timestamp.length + 1);
|
|
113
|
+
const expected = new Uint8Array(await crypto.subtle.sign("HMAC", key, signed));
|
|
114
|
+
if (!timingSafeEqual(expected, provided)) {
|
|
115
|
+
throw new ChannelWebhookVerificationError("signature does not match", "invalid_signature");
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
async function parseChannelWebhookEvent(request, options) {
|
|
119
|
+
const payload = await request.text();
|
|
120
|
+
await verifyChannelWebhookSignature({
|
|
121
|
+
payload,
|
|
122
|
+
signature: request.headers.get(CHANNEL_WEBHOOK_SIGNATURE_HEADER),
|
|
123
|
+
timestamp: request.headers.get(CHANNEL_WEBHOOK_TIMESTAMP_HEADER),
|
|
124
|
+
secret: options.secret,
|
|
125
|
+
toleranceSeconds: options.toleranceSeconds,
|
|
126
|
+
now: options.now
|
|
127
|
+
});
|
|
128
|
+
let event;
|
|
129
|
+
try {
|
|
130
|
+
event = JSON.parse(payload);
|
|
131
|
+
} catch {
|
|
132
|
+
throw new ChannelWebhookVerificationError("body is not valid JSON", "invalid_payload");
|
|
133
|
+
}
|
|
134
|
+
if (!isEventEnvelope(event)) {
|
|
135
|
+
throw new ChannelWebhookVerificationError("body is not a channel event", "invalid_payload");
|
|
136
|
+
}
|
|
137
|
+
if (!isChannelWebhookEventType(event.type)) return event;
|
|
138
|
+
if (!isKnownEventData(event.type, event.data)) {
|
|
139
|
+
throw new ChannelWebhookVerificationError("body contains invalid event data", "invalid_payload");
|
|
140
|
+
}
|
|
141
|
+
return event;
|
|
142
|
+
}
|
|
143
|
+
function isRecord(value) {
|
|
144
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
145
|
+
}
|
|
146
|
+
function isEventEnvelope(value) {
|
|
147
|
+
if (!isRecord(value) || !isRecord(value.data)) return false;
|
|
148
|
+
for (const key of ["id", "type", "api_version", "occurred_at", "workspace_id"]) {
|
|
149
|
+
if (typeof value[key] !== "string" || value[key].trim() === "") return false;
|
|
150
|
+
}
|
|
151
|
+
return value.channel_id === void 0 || typeof value.channel_id === "string";
|
|
152
|
+
}
|
|
153
|
+
function hasStrings(data, keys) {
|
|
154
|
+
return keys.every((key) => typeof data[key] === "string");
|
|
155
|
+
}
|
|
156
|
+
function isKnownEventData(type, value) {
|
|
157
|
+
if (!isRecord(value)) return false;
|
|
158
|
+
switch (type) {
|
|
159
|
+
case "order.created":
|
|
160
|
+
case "order.paid":
|
|
161
|
+
case "order.updated":
|
|
162
|
+
case "order.cancelled":
|
|
163
|
+
case "order.refunded":
|
|
164
|
+
return hasStrings(value, ["order_id", "order_number", "status", "payment_status", "currency", "created_at"]) && typeof value.total_cents === "number";
|
|
165
|
+
case "lead.created":
|
|
166
|
+
return hasStrings(value, ["lead_id", "source", "created_at"]);
|
|
167
|
+
case "review.submitted":
|
|
168
|
+
return hasStrings(value, ["source", "submitted_at"]) && typeof value.rating === "number";
|
|
169
|
+
case "review.published":
|
|
170
|
+
return hasStrings(value, ["review_id", "source"]) && typeof value.rating === "number" && (value.published_at === void 0 || typeof value.published_at === "string");
|
|
171
|
+
case "page.published":
|
|
172
|
+
return hasStrings(value, ["page_id", "route_pattern", "title"]) && (value.published_at === void 0 || typeof value.published_at === "string");
|
|
173
|
+
case "product.created":
|
|
174
|
+
case "product.updated":
|
|
175
|
+
case "product.deleted":
|
|
176
|
+
return typeof value.product_id === "string" && (value.slug === void 0 || typeof value.slug === "string") && (value.status === void 0 || typeof value.status === "string");
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export { CHANNEL_WEBHOOK_EVENT_ID_HEADER, CHANNEL_WEBHOOK_EVENT_TYPES, CHANNEL_WEBHOOK_SIGNATURE_HEADER, CHANNEL_WEBHOOK_TIMESTAMP_HEADER, ChannelConfigurationError, ChannelWebhookVerificationError, DEFAULT_TOLERANCE_SECONDS, isChannelConfigurationError, isChannelWebhookEventType, isChannelWebhookVerificationError, isKnownChannelWebhookEvent, parseChannelWebhookEvent, verifyChannelWebhookSignature };
|
package/dist/dash-next.d.ts
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
1
|
+
import { DashChannelClient } from './dash.js';
|
|
2
|
+
import { CreateNextChannelClientOptions } from './next.js';
|
|
3
|
+
import './index.js';
|
|
4
|
+
import './webhooks.js';
|
|
5
|
+
|
|
4
6
|
/**
|
|
5
7
|
* Dash-scoped client with Next.js Data Cache semantics — the factory a
|
|
6
8
|
* dash-site base should use. Anonymous dash GETs may receive `next.revalidate`;
|
|
7
9
|
* visitor-signed calls (geo, lead capture) should go through a `no-store`
|
|
8
10
|
* client so per-visitor answers are never cached under a shared tag.
|
|
9
11
|
*/
|
|
10
|
-
|
|
11
|
-
|
|
12
|
+
declare function createNextDashChannelClient(options: CreateNextChannelClientOptions): DashChannelClient;
|
|
13
|
+
|
|
14
|
+
export { DashChannelClient, createNextDashChannelClient };
|
package/dist/dash-next.js
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
*/
|
|
10
|
-
export function createNextDashChannelClient(options) {
|
|
11
|
-
return narrowToDash(createNextChannelClient(options));
|
|
1
|
+
import { createNextChannelClient } from './chunk-BSTXJIVL.js';
|
|
2
|
+
import { narrow } from './chunk-FRUWMKQP.js';
|
|
3
|
+
import './chunk-C44TGKRK.js';
|
|
4
|
+
import './chunk-RJQ4YEZV.js';
|
|
5
|
+
import 'server-only';
|
|
6
|
+
|
|
7
|
+
function createNextDashChannelClient(options) {
|
|
8
|
+
return narrow(createNextChannelClient(options));
|
|
12
9
|
}
|
|
10
|
+
|
|
11
|
+
export { createNextDashChannelClient };
|
package/dist/dash.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import { createChannelClient, ChannelVisitor, CreateChannelClientOptions } from './index.js';
|
|
2
|
+
import './webhooks.js';
|
|
3
|
+
|
|
3
4
|
/**
|
|
4
5
|
* The dash-scoped surface of the Channel API: what a dash marketing site
|
|
5
6
|
* (localprint.app, greatremovals…) is allowed to call. It is a TYPE PROJECTION
|
|
@@ -13,11 +14,12 @@ import { createChannelClient, type ChannelVisitor, type CreateChannelClientOptio
|
|
|
13
14
|
* door (the credential self-check must answer for either scope, and geo lets
|
|
14
15
|
* a dash site localise pricing).
|
|
15
16
|
*/
|
|
16
|
-
|
|
17
|
+
type DashChannelClient = Pick<ReturnType<typeof createChannelClient>, "ping" | "geo" | "dash" | "assetUrl"> & {
|
|
17
18
|
/** Request-scoped clone carrying signed visitor metadata (see ChannelVisitor). */
|
|
18
19
|
withVisitor(visitor: ChannelVisitor): DashChannelClient;
|
|
19
20
|
};
|
|
20
21
|
declare function narrow(client: ReturnType<typeof createChannelClient>): DashChannelClient;
|
|
21
22
|
/** Dash-scoped client over plain fetch (no framework cache semantics). */
|
|
22
|
-
|
|
23
|
-
|
|
23
|
+
declare function createDashChannelClient(options: CreateChannelClientOptions): DashChannelClient;
|
|
24
|
+
|
|
25
|
+
export { type DashChannelClient, createDashChannelClient, narrow as narrowToDash };
|
package/dist/dash.js
CHANGED
|
@@ -1,16 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
return {
|
|
5
|
-
ping: client.ping,
|
|
6
|
-
geo: client.geo,
|
|
7
|
-
dash: client.dash,
|
|
8
|
-
assetUrl: client.assetUrl,
|
|
9
|
-
withVisitor: (visitor) => narrow(client.withVisitor(visitor)),
|
|
10
|
-
};
|
|
11
|
-
}
|
|
12
|
-
/** Dash-scoped client over plain fetch (no framework cache semantics). */
|
|
13
|
-
export function createDashChannelClient(options) {
|
|
14
|
-
return narrow(createChannelClient(options));
|
|
15
|
-
}
|
|
16
|
-
export { narrow as narrowToDash };
|
|
1
|
+
export { createDashChannelClient, narrow as narrowToDash } from './chunk-FRUWMKQP.js';
|
|
2
|
+
import './chunk-C44TGKRK.js';
|
|
3
|
+
import './chunk-RJQ4YEZV.js';
|