@01.software/sdk 0.1.0-dev.260210.4ecca43 → 0.1.1
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 +151 -74
- package/dist/auth-BipHkgbu.d.cts +267 -0
- package/dist/auth-Brdn6voY.d.ts +267 -0
- package/dist/auth.cjs +129 -0
- package/dist/auth.cjs.map +1 -0
- package/dist/auth.d.cts +3 -0
- package/dist/auth.d.ts +3 -0
- package/dist/auth.js +107 -0
- package/dist/auth.js.map +1 -0
- package/dist/components.cjs +76 -0
- package/dist/components.cjs.map +1 -0
- package/dist/components.d.cts +28 -0
- package/dist/components.d.ts +28 -0
- package/dist/components.js +50 -0
- package/dist/components.js.map +1 -0
- package/dist/index.cjs +204 -88
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +71 -2838
- package/dist/index.d.ts +71 -2838
- package/dist/index.js +203 -86
- package/dist/index.js.map +1 -1
- package/dist/payload-types-BE-5_Y2d.d.cts +2675 -0
- package/dist/payload-types-BE-5_Y2d.d.ts +2675 -0
- package/dist/webhook-D13_sbzU.d.ts +40 -0
- package/dist/webhook-Dg4AkcDQ.d.cts +40 -0
- package/dist/webhook.cjs +116 -0
- package/dist/webhook.cjs.map +1 -0
- package/dist/webhook.d.cts +2 -0
- package/dist/webhook.d.ts +2 -0
- package/dist/webhook.js +94 -0
- package/dist/webhook.js.map +1 -0
- package/package.json +35 -4
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { C as Config } from './payload-types-BE-5_Y2d.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Collection type derived from Payload Config.
|
|
5
|
+
* This ensures type safety and automatic synchronization with payload-types.ts
|
|
6
|
+
*/
|
|
7
|
+
type Collection = keyof Config['collections'];
|
|
8
|
+
/**
|
|
9
|
+
* Internal Payload collections that should not be exposed via SDK.
|
|
10
|
+
*/
|
|
11
|
+
declare const INTERNAL_COLLECTIONS: readonly ["users", "payload-kv", "payload-locked-documents", "payload-preferences", "payload-migrations"];
|
|
12
|
+
type InternalCollection = (typeof INTERNAL_COLLECTIONS)[number];
|
|
13
|
+
/**
|
|
14
|
+
* Public collections available for SDK access.
|
|
15
|
+
* Excludes internal Payload system collections.
|
|
16
|
+
*/
|
|
17
|
+
type PublicCollection = Exclude<Collection, InternalCollection>;
|
|
18
|
+
/**
|
|
19
|
+
* Array of all public collection names for runtime use (e.g., Zod enum validation).
|
|
20
|
+
* This is derived from Config to ensure type safety.
|
|
21
|
+
*/
|
|
22
|
+
declare const COLLECTIONS: readonly ["tenants", "tenant-metadata", "tenant-logos", "tenant-og-images", "products", "product-variants", "product-options", "product-categories", "product-tags", "product-images", "brands", "brand-logos", "orders", "order-products", "returns", "return-products", "transactions", "documents", "document-categories", "document-images", "posts", "post-categories", "post-tags", "post-images", "playlists", "playlist-images", "musics", "galleries", "gallery-images", "forms", "form-submissions", "media"];
|
|
23
|
+
|
|
24
|
+
type CollectionType<T extends Collection> = T extends keyof Config['collections'] ? Config['collections'][T] : never;
|
|
25
|
+
|
|
26
|
+
type WebhookOperation = 'create' | 'update';
|
|
27
|
+
interface WebhookEvent<T extends Collection = Collection> {
|
|
28
|
+
collection: T;
|
|
29
|
+
operation: WebhookOperation;
|
|
30
|
+
data: CollectionType<T>;
|
|
31
|
+
}
|
|
32
|
+
type WebhookHandler<T extends Collection = Collection> = (event: WebhookEvent<T>) => Promise<void> | void;
|
|
33
|
+
interface WebhookOptions {
|
|
34
|
+
secret?: string;
|
|
35
|
+
}
|
|
36
|
+
declare function isValidWebhookEvent(data: unknown): data is WebhookEvent;
|
|
37
|
+
declare function handleWebhook<T extends Collection = Collection>(request: Request, handler: WebhookHandler<T>, options?: WebhookOptions): Promise<Response>;
|
|
38
|
+
declare function createTypedWebhookHandler<T extends Collection>(collection: T, handler: (event: WebhookEvent<T>) => Promise<void> | void): WebhookHandler<T>;
|
|
39
|
+
|
|
40
|
+
export { type Collection as C, type PublicCollection as P, type WebhookOperation as W, type CollectionType as a, COLLECTIONS as b, type WebhookEvent as c, type WebhookHandler as d, type WebhookOptions as e, createTypedWebhookHandler as f, handleWebhook as h, isValidWebhookEvent as i };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { C as Config } from './payload-types-BE-5_Y2d.cjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Collection type derived from Payload Config.
|
|
5
|
+
* This ensures type safety and automatic synchronization with payload-types.ts
|
|
6
|
+
*/
|
|
7
|
+
type Collection = keyof Config['collections'];
|
|
8
|
+
/**
|
|
9
|
+
* Internal Payload collections that should not be exposed via SDK.
|
|
10
|
+
*/
|
|
11
|
+
declare const INTERNAL_COLLECTIONS: readonly ["users", "payload-kv", "payload-locked-documents", "payload-preferences", "payload-migrations"];
|
|
12
|
+
type InternalCollection = (typeof INTERNAL_COLLECTIONS)[number];
|
|
13
|
+
/**
|
|
14
|
+
* Public collections available for SDK access.
|
|
15
|
+
* Excludes internal Payload system collections.
|
|
16
|
+
*/
|
|
17
|
+
type PublicCollection = Exclude<Collection, InternalCollection>;
|
|
18
|
+
/**
|
|
19
|
+
* Array of all public collection names for runtime use (e.g., Zod enum validation).
|
|
20
|
+
* This is derived from Config to ensure type safety.
|
|
21
|
+
*/
|
|
22
|
+
declare const COLLECTIONS: readonly ["tenants", "tenant-metadata", "tenant-logos", "tenant-og-images", "products", "product-variants", "product-options", "product-categories", "product-tags", "product-images", "brands", "brand-logos", "orders", "order-products", "returns", "return-products", "transactions", "documents", "document-categories", "document-images", "posts", "post-categories", "post-tags", "post-images", "playlists", "playlist-images", "musics", "galleries", "gallery-images", "forms", "form-submissions", "media"];
|
|
23
|
+
|
|
24
|
+
type CollectionType<T extends Collection> = T extends keyof Config['collections'] ? Config['collections'][T] : never;
|
|
25
|
+
|
|
26
|
+
type WebhookOperation = 'create' | 'update';
|
|
27
|
+
interface WebhookEvent<T extends Collection = Collection> {
|
|
28
|
+
collection: T;
|
|
29
|
+
operation: WebhookOperation;
|
|
30
|
+
data: CollectionType<T>;
|
|
31
|
+
}
|
|
32
|
+
type WebhookHandler<T extends Collection = Collection> = (event: WebhookEvent<T>) => Promise<void> | void;
|
|
33
|
+
interface WebhookOptions {
|
|
34
|
+
secret?: string;
|
|
35
|
+
}
|
|
36
|
+
declare function isValidWebhookEvent(data: unknown): data is WebhookEvent;
|
|
37
|
+
declare function handleWebhook<T extends Collection = Collection>(request: Request, handler: WebhookHandler<T>, options?: WebhookOptions): Promise<Response>;
|
|
38
|
+
declare function createTypedWebhookHandler<T extends Collection>(collection: T, handler: (event: WebhookEvent<T>) => Promise<void> | void): WebhookHandler<T>;
|
|
39
|
+
|
|
40
|
+
export { type Collection as C, type PublicCollection as P, type WebhookOperation as W, type CollectionType as a, COLLECTIONS as b, type WebhookEvent as c, type WebhookHandler as d, type WebhookOptions as e, createTypedWebhookHandler as f, handleWebhook as h, isValidWebhookEvent as i };
|
package/dist/webhook.cjs
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var __async = (__this, __arguments, generator) => {
|
|
20
|
+
return new Promise((resolve, reject) => {
|
|
21
|
+
var fulfilled = (value) => {
|
|
22
|
+
try {
|
|
23
|
+
step(generator.next(value));
|
|
24
|
+
} catch (e) {
|
|
25
|
+
reject(e);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
var rejected = (value) => {
|
|
29
|
+
try {
|
|
30
|
+
step(generator.throw(value));
|
|
31
|
+
} catch (e) {
|
|
32
|
+
reject(e);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
36
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// src/webhook.ts
|
|
41
|
+
var webhook_exports = {};
|
|
42
|
+
__export(webhook_exports, {
|
|
43
|
+
createTypedWebhookHandler: () => createTypedWebhookHandler,
|
|
44
|
+
handleWebhook: () => handleWebhook,
|
|
45
|
+
isValidWebhookEvent: () => isValidWebhookEvent
|
|
46
|
+
});
|
|
47
|
+
module.exports = __toCommonJS(webhook_exports);
|
|
48
|
+
|
|
49
|
+
// src/core/webhook/index.tsx
|
|
50
|
+
function isValidWebhookEvent(data) {
|
|
51
|
+
if (typeof data !== "object" || data === null) return false;
|
|
52
|
+
const obj = data;
|
|
53
|
+
return typeof obj.collection === "string" && (obj.operation === "create" || obj.operation === "update") && typeof obj.data === "object" && obj.data !== null;
|
|
54
|
+
}
|
|
55
|
+
function verifySignature(payload, secret, signature) {
|
|
56
|
+
return __async(this, null, function* () {
|
|
57
|
+
const encoder = new TextEncoder();
|
|
58
|
+
const key = yield crypto.subtle.importKey(
|
|
59
|
+
"raw",
|
|
60
|
+
encoder.encode(secret),
|
|
61
|
+
{ name: "HMAC", hash: "SHA-256" },
|
|
62
|
+
false,
|
|
63
|
+
["sign"]
|
|
64
|
+
);
|
|
65
|
+
const sig = yield crypto.subtle.sign("HMAC", key, encoder.encode(payload));
|
|
66
|
+
const expected = Array.from(new Uint8Array(sig)).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
67
|
+
return expected === signature;
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
function handleWebhook(request, handler, options) {
|
|
71
|
+
return __async(this, null, function* () {
|
|
72
|
+
try {
|
|
73
|
+
const rawBody = yield request.text();
|
|
74
|
+
if (options == null ? void 0 : options.secret) {
|
|
75
|
+
const signature = request.headers.get("x-webhook-signature") || "";
|
|
76
|
+
const valid = yield verifySignature(rawBody, options.secret, signature);
|
|
77
|
+
if (!valid) {
|
|
78
|
+
return new Response(
|
|
79
|
+
JSON.stringify({ error: "Invalid webhook signature" }),
|
|
80
|
+
{ status: 401, headers: { "Content-Type": "application/json" } }
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
const body = JSON.parse(rawBody);
|
|
85
|
+
if (!isValidWebhookEvent(body)) {
|
|
86
|
+
return new Response(
|
|
87
|
+
JSON.stringify({ error: "Invalid webhook event format" }),
|
|
88
|
+
{ status: 400, headers: { "Content-Type": "application/json" } }
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
yield handler(body);
|
|
92
|
+
return new Response(
|
|
93
|
+
JSON.stringify({ success: true, message: "Webhook processed" }),
|
|
94
|
+
{ status: 200, headers: { "Content-Type": "application/json" } }
|
|
95
|
+
);
|
|
96
|
+
} catch (error) {
|
|
97
|
+
console.error("Webhook processing error:", error);
|
|
98
|
+
return new Response(
|
|
99
|
+
JSON.stringify({
|
|
100
|
+
error: "Internal server error",
|
|
101
|
+
message: error instanceof Error ? error.message : "Unknown error"
|
|
102
|
+
}),
|
|
103
|
+
{ status: 500, headers: { "Content-Type": "application/json" } }
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
function createTypedWebhookHandler(collection, handler) {
|
|
109
|
+
return (event) => __async(null, null, function* () {
|
|
110
|
+
if (event.collection !== collection) {
|
|
111
|
+
throw new Error(`Expected collection "${collection}", got "${event.collection}"`);
|
|
112
|
+
}
|
|
113
|
+
return handler(event);
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
//# sourceMappingURL=webhook.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/webhook.ts","../src/core/webhook/index.tsx"],"sourcesContent":["export {\n handleWebhook,\n createTypedWebhookHandler,\n isValidWebhookEvent,\n} from './core/webhook'\nexport type {\n WebhookEvent,\n WebhookHandler,\n WebhookOperation,\n WebhookOptions,\n} from './core/webhook'\n","import type { Collection } from '../client/types'\nimport type { CollectionType } from '../collection/types'\n\nexport type WebhookOperation = 'create' | 'update'\n\nexport interface WebhookEvent<T extends Collection = Collection> {\n collection: T\n operation: WebhookOperation\n data: CollectionType<T>\n}\n\nexport type WebhookHandler<T extends Collection = Collection> = (\n event: WebhookEvent<T>,\n) => Promise<void> | void\n\nexport interface WebhookOptions {\n secret?: string\n}\n\nexport function isValidWebhookEvent(data: unknown): data is WebhookEvent {\n if (typeof data !== 'object' || data === null) return false\n const obj = data as Record<string, unknown>\n return (\n typeof obj.collection === 'string' &&\n (obj.operation === 'create' || obj.operation === 'update') &&\n typeof obj.data === 'object' &&\n obj.data !== null\n )\n}\n\nasync function verifySignature(\n payload: string,\n secret: string,\n signature: string,\n): Promise<boolean> {\n const encoder = new TextEncoder()\n const key = await crypto.subtle.importKey(\n 'raw',\n encoder.encode(secret),\n { name: 'HMAC', hash: 'SHA-256' },\n false,\n ['sign'],\n )\n const sig = await crypto.subtle.sign('HMAC', key, encoder.encode(payload))\n const expected = Array.from(new Uint8Array(sig))\n .map((b) => b.toString(16).padStart(2, '0'))\n .join('')\n return expected === signature\n}\n\nexport async function handleWebhook<T extends Collection = Collection>(\n request: Request,\n handler: WebhookHandler<T>,\n options?: WebhookOptions,\n): Promise<Response> {\n try {\n const rawBody = await request.text()\n\n if (options?.secret) {\n const signature = request.headers.get('x-webhook-signature') || ''\n const valid = await verifySignature(rawBody, options.secret, signature)\n if (!valid) {\n return new Response(\n JSON.stringify({ error: 'Invalid webhook signature' }),\n { status: 401, headers: { 'Content-Type': 'application/json' } },\n )\n }\n }\n\n const body = JSON.parse(rawBody)\n\n if (!isValidWebhookEvent(body)) {\n return new Response(\n JSON.stringify({ error: 'Invalid webhook event format' }),\n { status: 400, headers: { 'Content-Type': 'application/json' } },\n )\n }\n\n await handler(body as WebhookEvent<T>)\n\n return new Response(\n JSON.stringify({ success: true, message: 'Webhook processed' }),\n { status: 200, headers: { 'Content-Type': 'application/json' } },\n )\n } catch (error) {\n console.error('Webhook processing error:', error)\n\n return new Response(\n JSON.stringify({\n error: 'Internal server error',\n message: error instanceof Error ? error.message : 'Unknown error',\n }),\n { status: 500, headers: { 'Content-Type': 'application/json' } },\n )\n }\n}\n\nexport function createTypedWebhookHandler<T extends Collection>(\n collection: T,\n handler: (event: WebhookEvent<T>) => Promise<void> | void,\n): WebhookHandler<T> {\n return async (event: WebhookEvent<T>) => {\n if (event.collection !== collection) {\n throw new Error(`Expected collection \"${collection}\", got \"${event.collection}\"`)\n }\n return handler(event)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACmBO,SAAS,oBAAoB,MAAqC;AACvE,MAAI,OAAO,SAAS,YAAY,SAAS,KAAM,QAAO;AACtD,QAAM,MAAM;AACZ,SACE,OAAO,IAAI,eAAe,aACzB,IAAI,cAAc,YAAY,IAAI,cAAc,aACjD,OAAO,IAAI,SAAS,YACpB,IAAI,SAAS;AAEjB;AAEA,SAAe,gBACb,SACA,QACA,WACkB;AAAA;AAClB,UAAM,UAAU,IAAI,YAAY;AAChC,UAAM,MAAM,MAAM,OAAO,OAAO;AAAA,MAC9B;AAAA,MACA,QAAQ,OAAO,MAAM;AAAA,MACrB,EAAE,MAAM,QAAQ,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;AAAA,QACT,KAAK,UAAU;AAAA,UACb,OAAO;AAAA,UACP,SAAS,iBAAiB,QAAQ,MAAM,UAAU;AAAA,QACpD,CAAC;AAAA,QACD,EAAE,QAAQ,KAAK,SAAS,EAAE,gBAAgB,mBAAmB,EAAE;AAAA,MACjE;AAAA,IACF;AAAA,EACF;AAAA;AAEO,SAAS,0BACd,YACA,SACmB;AACnB,SAAO,CAAO,UAA2B;AACvC,QAAI,MAAM,eAAe,YAAY;AACnC,YAAM,IAAI,MAAM,wBAAwB,UAAU,WAAW,MAAM,UAAU,GAAG;AAAA,IAClF;AACA,WAAO,QAAQ,KAAK;AAAA,EACtB;AACF;","names":[]}
|
package/dist/webhook.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// src/core/webhook/index.tsx
|
|
23
|
+
function isValidWebhookEvent(data) {
|
|
24
|
+
if (typeof data !== "object" || data === null) return false;
|
|
25
|
+
const obj = data;
|
|
26
|
+
return typeof obj.collection === "string" && (obj.operation === "create" || obj.operation === "update") && typeof obj.data === "object" && obj.data !== null;
|
|
27
|
+
}
|
|
28
|
+
function verifySignature(payload, secret, signature) {
|
|
29
|
+
return __async(this, null, function* () {
|
|
30
|
+
const encoder = new TextEncoder();
|
|
31
|
+
const key = yield crypto.subtle.importKey(
|
|
32
|
+
"raw",
|
|
33
|
+
encoder.encode(secret),
|
|
34
|
+
{ name: "HMAC", hash: "SHA-256" },
|
|
35
|
+
false,
|
|
36
|
+
["sign"]
|
|
37
|
+
);
|
|
38
|
+
const sig = yield crypto.subtle.sign("HMAC", key, encoder.encode(payload));
|
|
39
|
+
const expected = Array.from(new Uint8Array(sig)).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
40
|
+
return expected === signature;
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
function handleWebhook(request, handler, options) {
|
|
44
|
+
return __async(this, null, function* () {
|
|
45
|
+
try {
|
|
46
|
+
const rawBody = yield request.text();
|
|
47
|
+
if (options == null ? void 0 : options.secret) {
|
|
48
|
+
const signature = request.headers.get("x-webhook-signature") || "";
|
|
49
|
+
const valid = yield verifySignature(rawBody, options.secret, signature);
|
|
50
|
+
if (!valid) {
|
|
51
|
+
return new Response(
|
|
52
|
+
JSON.stringify({ error: "Invalid webhook signature" }),
|
|
53
|
+
{ status: 401, headers: { "Content-Type": "application/json" } }
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const body = JSON.parse(rawBody);
|
|
58
|
+
if (!isValidWebhookEvent(body)) {
|
|
59
|
+
return new Response(
|
|
60
|
+
JSON.stringify({ error: "Invalid webhook event format" }),
|
|
61
|
+
{ status: 400, headers: { "Content-Type": "application/json" } }
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
yield handler(body);
|
|
65
|
+
return new Response(
|
|
66
|
+
JSON.stringify({ success: true, message: "Webhook processed" }),
|
|
67
|
+
{ status: 200, headers: { "Content-Type": "application/json" } }
|
|
68
|
+
);
|
|
69
|
+
} catch (error) {
|
|
70
|
+
console.error("Webhook processing error:", error);
|
|
71
|
+
return new Response(
|
|
72
|
+
JSON.stringify({
|
|
73
|
+
error: "Internal server error",
|
|
74
|
+
message: error instanceof Error ? error.message : "Unknown error"
|
|
75
|
+
}),
|
|
76
|
+
{ status: 500, headers: { "Content-Type": "application/json" } }
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
function createTypedWebhookHandler(collection, handler) {
|
|
82
|
+
return (event) => __async(null, null, function* () {
|
|
83
|
+
if (event.collection !== collection) {
|
|
84
|
+
throw new Error(`Expected collection "${collection}", got "${event.collection}"`);
|
|
85
|
+
}
|
|
86
|
+
return handler(event);
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
export {
|
|
90
|
+
createTypedWebhookHandler,
|
|
91
|
+
handleWebhook,
|
|
92
|
+
isValidWebhookEvent
|
|
93
|
+
};
|
|
94
|
+
//# sourceMappingURL=webhook.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/core/webhook/index.tsx"],"sourcesContent":["import type { Collection } from '../client/types'\nimport type { CollectionType } from '../collection/types'\n\nexport type WebhookOperation = 'create' | 'update'\n\nexport interface WebhookEvent<T extends Collection = Collection> {\n collection: T\n operation: WebhookOperation\n data: CollectionType<T>\n}\n\nexport type WebhookHandler<T extends Collection = Collection> = (\n event: WebhookEvent<T>,\n) => Promise<void> | void\n\nexport interface WebhookOptions {\n secret?: string\n}\n\nexport function isValidWebhookEvent(data: unknown): data is WebhookEvent {\n if (typeof data !== 'object' || data === null) return false\n const obj = data as Record<string, unknown>\n return (\n typeof obj.collection === 'string' &&\n (obj.operation === 'create' || obj.operation === 'update') &&\n typeof obj.data === 'object' &&\n obj.data !== null\n )\n}\n\nasync function verifySignature(\n payload: string,\n secret: string,\n signature: string,\n): Promise<boolean> {\n const encoder = new TextEncoder()\n const key = await crypto.subtle.importKey(\n 'raw',\n encoder.encode(secret),\n { name: 'HMAC', hash: 'SHA-256' },\n false,\n ['sign'],\n )\n const sig = await crypto.subtle.sign('HMAC', key, encoder.encode(payload))\n const expected = Array.from(new Uint8Array(sig))\n .map((b) => b.toString(16).padStart(2, '0'))\n .join('')\n return expected === signature\n}\n\nexport async function handleWebhook<T extends Collection = Collection>(\n request: Request,\n handler: WebhookHandler<T>,\n options?: WebhookOptions,\n): Promise<Response> {\n try {\n const rawBody = await request.text()\n\n if (options?.secret) {\n const signature = request.headers.get('x-webhook-signature') || ''\n const valid = await verifySignature(rawBody, options.secret, signature)\n if (!valid) {\n return new Response(\n JSON.stringify({ error: 'Invalid webhook signature' }),\n { status: 401, headers: { 'Content-Type': 'application/json' } },\n )\n }\n }\n\n const body = JSON.parse(rawBody)\n\n if (!isValidWebhookEvent(body)) {\n return new Response(\n JSON.stringify({ error: 'Invalid webhook event format' }),\n { status: 400, headers: { 'Content-Type': 'application/json' } },\n )\n }\n\n await handler(body as WebhookEvent<T>)\n\n return new Response(\n JSON.stringify({ success: true, message: 'Webhook processed' }),\n { status: 200, headers: { 'Content-Type': 'application/json' } },\n )\n } catch (error) {\n console.error('Webhook processing error:', error)\n\n return new Response(\n JSON.stringify({\n error: 'Internal server error',\n message: error instanceof Error ? error.message : 'Unknown error',\n }),\n { status: 500, headers: { 'Content-Type': 'application/json' } },\n )\n }\n}\n\nexport function createTypedWebhookHandler<T extends Collection>(\n collection: T,\n handler: (event: WebhookEvent<T>) => Promise<void> | void,\n): WebhookHandler<T> {\n return async (event: WebhookEvent<T>) => {\n if (event.collection !== collection) {\n throw new Error(`Expected collection \"${collection}\", got \"${event.collection}\"`)\n }\n return handler(event)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAmBO,SAAS,oBAAoB,MAAqC;AACvE,MAAI,OAAO,SAAS,YAAY,SAAS,KAAM,QAAO;AACtD,QAAM,MAAM;AACZ,SACE,OAAO,IAAI,eAAe,aACzB,IAAI,cAAc,YAAY,IAAI,cAAc,aACjD,OAAO,IAAI,SAAS,YACpB,IAAI,SAAS;AAEjB;AAEA,SAAe,gBACb,SACA,QACA,WACkB;AAAA;AAClB,UAAM,UAAU,IAAI,YAAY;AAChC,UAAM,MA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AmB,EAAE;AAAA,MACjE;AAAA,IACF,SAAS,OAAO;AACd,cAAQ,MAAM,6BAA6B,KAAK;AAEhD,aAAO,IAAI;AAAA,QACT,KAAK,UAAU;AAAA,UACb,OAAO;AAAA,UACP,SAAS,iBAAiB,QAAQ,MAAM,UAAU;AAAA,QACpD,CAAC;AAAA,QACD,EAAE,QAAQ,KAAK,SAAS,EAAE,gBAAgB,mBAAmB,EAAE;AAAA,MACjE;AAAA,IACF;AAAA,EACF;AAAA;AAEO,SAAS,0BACd,YACA,SACmB;AACnB,SAAO,CAAO,UAA2B;AACvC,QAAI,MAAM,eAAe,YAAY;AACnC,YAAM,IAAI,MAAM,wBAAwB,UAAU,WAAW,MAAM,UAAU,GAAG;AAAA,IAClF;AACA,WAAO,QAAQ,KAAK;AAAA,EACtB;AACF;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@01.software/sdk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "01.software SDK",
|
|
5
5
|
"author": "<office@01.works>",
|
|
6
6
|
"keywords": [],
|
|
@@ -19,6 +19,36 @@
|
|
|
19
19
|
"default": "./dist/index.cjs"
|
|
20
20
|
}
|
|
21
21
|
},
|
|
22
|
+
"./auth": {
|
|
23
|
+
"import": {
|
|
24
|
+
"types": "./dist/auth.d.ts",
|
|
25
|
+
"default": "./dist/auth.js"
|
|
26
|
+
},
|
|
27
|
+
"require": {
|
|
28
|
+
"types": "./dist/auth.d.cts",
|
|
29
|
+
"default": "./dist/auth.cjs"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"./webhook": {
|
|
33
|
+
"import": {
|
|
34
|
+
"types": "./dist/webhook.d.ts",
|
|
35
|
+
"default": "./dist/webhook.js"
|
|
36
|
+
},
|
|
37
|
+
"require": {
|
|
38
|
+
"types": "./dist/webhook.d.cts",
|
|
39
|
+
"default": "./dist/webhook.cjs"
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"./components": {
|
|
43
|
+
"import": {
|
|
44
|
+
"types": "./dist/components.d.ts",
|
|
45
|
+
"default": "./dist/components.js"
|
|
46
|
+
},
|
|
47
|
+
"require": {
|
|
48
|
+
"types": "./dist/components.d.cts",
|
|
49
|
+
"default": "./dist/components.cjs"
|
|
50
|
+
}
|
|
51
|
+
},
|
|
22
52
|
"./package.json": "./package.json"
|
|
23
53
|
},
|
|
24
54
|
"sideEffects": false,
|
|
@@ -40,8 +70,8 @@
|
|
|
40
70
|
"shadcn": "^3.6.3",
|
|
41
71
|
"tsup": "^8.3.7",
|
|
42
72
|
"vitest": "^3.2.3",
|
|
43
|
-
"@repo/
|
|
44
|
-
"@repo/
|
|
73
|
+
"@repo/typescript-config": "0.0.0",
|
|
74
|
+
"@repo/eslint-config": "0.0.0"
|
|
45
75
|
},
|
|
46
76
|
"dependencies": {
|
|
47
77
|
"@payloadcms/richtext-lexical": "3.72.0",
|
|
@@ -56,11 +86,12 @@
|
|
|
56
86
|
"react-dom": ">=18"
|
|
57
87
|
},
|
|
58
88
|
"scripts": {
|
|
89
|
+
"dev": "tsup --watch",
|
|
59
90
|
"clean": "rimraf ./dist ./coverage",
|
|
60
91
|
"lint": "eslint ./src",
|
|
61
92
|
"test": "vitest",
|
|
62
93
|
"test:watch": "vitest --watch",
|
|
63
|
-
"test:publish": "publint --strict && attw --pack",
|
|
94
|
+
"test:publish": "publint --strict && attw --pack --ignore-rules no-resolution",
|
|
64
95
|
"build": "tsup",
|
|
65
96
|
"version:dev": "node ../../scripts/update-version.js",
|
|
66
97
|
"publish:dev": "pnpm run version:dev && pnpm publish --no-git-checks --access public --tag dev",
|