@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/dist/webhooks.js
CHANGED
|
@@ -12,13 +12,23 @@
|
|
|
12
12
|
export const CHANNEL_WEBHOOK_EVENT_TYPES = [
|
|
13
13
|
"order.created",
|
|
14
14
|
"order.paid",
|
|
15
|
+
"order.updated",
|
|
16
|
+
"order.cancelled",
|
|
17
|
+
"order.refunded",
|
|
15
18
|
"lead.created",
|
|
16
19
|
"review.submitted",
|
|
20
|
+
"review.published",
|
|
17
21
|
"page.published",
|
|
22
|
+
"product.created",
|
|
23
|
+
"product.updated",
|
|
24
|
+
"product.deleted",
|
|
18
25
|
];
|
|
19
26
|
export function isChannelWebhookEventType(value) {
|
|
20
27
|
return CHANNEL_WEBHOOK_EVENT_TYPES.includes(value);
|
|
21
28
|
}
|
|
29
|
+
export function isKnownChannelWebhookEvent(event) {
|
|
30
|
+
return isChannelWebhookEventType(event.type);
|
|
31
|
+
}
|
|
22
32
|
export class ChannelWebhookVerificationError extends Error {
|
|
23
33
|
code;
|
|
24
34
|
constructor(message, code) {
|
|
@@ -50,7 +60,7 @@ function timingSafeEqual(a, b) {
|
|
|
50
60
|
return diff === 0;
|
|
51
61
|
}
|
|
52
62
|
function decodeHex(value) {
|
|
53
|
-
if (value.length === 0 || value.length % 2 !== 0)
|
|
63
|
+
if (value.length === 0 || value.length % 2 !== 0 || !/^[0-9a-f]+$/i.test(value))
|
|
54
64
|
return null;
|
|
55
65
|
const out = new Uint8Array(value.length / 2);
|
|
56
66
|
for (let i = 0; i < out.length; i += 1) {
|
|
@@ -150,8 +160,57 @@ export async function parseChannelWebhookEvent(request, options) {
|
|
|
150
160
|
catch {
|
|
151
161
|
throw new ChannelWebhookVerificationError("body is not valid JSON", "invalid_payload");
|
|
152
162
|
}
|
|
153
|
-
if (!event
|
|
163
|
+
if (!isEventEnvelope(event)) {
|
|
154
164
|
throw new ChannelWebhookVerificationError("body is not a channel event", "invalid_payload");
|
|
155
165
|
}
|
|
166
|
+
if (!isChannelWebhookEventType(event.type))
|
|
167
|
+
return event;
|
|
168
|
+
if (!isKnownEventData(event.type, event.data)) {
|
|
169
|
+
throw new ChannelWebhookVerificationError("body contains invalid event data", "invalid_payload");
|
|
170
|
+
}
|
|
156
171
|
return event;
|
|
157
172
|
}
|
|
173
|
+
function isRecord(value) {
|
|
174
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
175
|
+
}
|
|
176
|
+
function isEventEnvelope(value) {
|
|
177
|
+
if (!isRecord(value) || !isRecord(value.data))
|
|
178
|
+
return false;
|
|
179
|
+
for (const key of ["id", "type", "api_version", "occurred_at", "workspace_id"]) {
|
|
180
|
+
if (typeof value[key] !== "string" || value[key].trim() === "")
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
return value.channel_id === undefined || typeof value.channel_id === "string";
|
|
184
|
+
}
|
|
185
|
+
function hasStrings(data, keys) {
|
|
186
|
+
return keys.every((key) => typeof data[key] === "string");
|
|
187
|
+
}
|
|
188
|
+
function isKnownEventData(type, value) {
|
|
189
|
+
if (!isRecord(value))
|
|
190
|
+
return false;
|
|
191
|
+
switch (type) {
|
|
192
|
+
case "order.created":
|
|
193
|
+
case "order.paid":
|
|
194
|
+
case "order.updated":
|
|
195
|
+
case "order.cancelled":
|
|
196
|
+
case "order.refunded":
|
|
197
|
+
return hasStrings(value, ["order_id", "order_number", "status", "payment_status", "currency", "created_at"]) &&
|
|
198
|
+
typeof value.total_cents === "number";
|
|
199
|
+
case "lead.created":
|
|
200
|
+
return hasStrings(value, ["lead_id", "source", "created_at"]);
|
|
201
|
+
case "review.submitted":
|
|
202
|
+
return hasStrings(value, ["source", "submitted_at"]) && typeof value.rating === "number";
|
|
203
|
+
case "review.published":
|
|
204
|
+
return hasStrings(value, ["review_id", "source"]) && typeof value.rating === "number" &&
|
|
205
|
+
(value.published_at === undefined || typeof value.published_at === "string");
|
|
206
|
+
case "page.published":
|
|
207
|
+
return hasStrings(value, ["page_id", "route_pattern", "title"]) &&
|
|
208
|
+
(value.published_at === undefined || typeof value.published_at === "string");
|
|
209
|
+
case "product.created":
|
|
210
|
+
case "product.updated":
|
|
211
|
+
case "product.deleted":
|
|
212
|
+
return typeof value.product_id === "string" &&
|
|
213
|
+
(value.slug === undefined || typeof value.slug === "string") &&
|
|
214
|
+
(value.status === undefined || typeof value.status === "string");
|
|
215
|
+
}
|
|
216
|
+
}
|