@notionhq/workers 0.2.0 → 0.3.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.
Files changed (50) hide show
  1. package/dist/capabilities/ai_connector.d.ts +97 -0
  2. package/dist/capabilities/ai_connector.d.ts.map +1 -0
  3. package/dist/capabilities/ai_connector.js +54 -0
  4. package/dist/capabilities/ai_connector.test.d.ts +2 -0
  5. package/dist/capabilities/ai_connector.test.d.ts.map +1 -0
  6. package/dist/capabilities/automation.d.ts.map +1 -1
  7. package/dist/capabilities/automation.js +10 -10
  8. package/dist/capabilities/output.d.ts +5 -0
  9. package/dist/capabilities/output.d.ts.map +1 -0
  10. package/dist/capabilities/output.js +10 -0
  11. package/dist/capabilities/stateful-capability.d.ts +30 -0
  12. package/dist/capabilities/stateful-capability.d.ts.map +1 -0
  13. package/dist/capabilities/stateful-capability.js +50 -0
  14. package/dist/capabilities/stateful-capability.test.d.ts +2 -0
  15. package/dist/capabilities/stateful-capability.test.d.ts.map +1 -0
  16. package/dist/capabilities/sync.d.ts +9 -19
  17. package/dist/capabilities/sync.d.ts.map +1 -1
  18. package/dist/capabilities/sync.js +13 -60
  19. package/dist/capabilities/tool.d.ts.map +1 -1
  20. package/dist/capabilities/tool.js +5 -20
  21. package/dist/capabilities/webhook.d.ts +101 -0
  22. package/dist/capabilities/webhook.d.ts.map +1 -0
  23. package/dist/capabilities/webhook.js +47 -0
  24. package/dist/index.d.ts +3 -0
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/index.js +2 -0
  27. package/dist/pacer_internal.d.ts +7 -0
  28. package/dist/pacer_internal.d.ts.map +1 -1
  29. package/dist/pacer_internal.js +17 -0
  30. package/dist/schema.d.ts +28 -11
  31. package/dist/schema.d.ts.map +1 -1
  32. package/dist/schema.js +2 -2
  33. package/dist/worker.d.ts +91 -9
  34. package/dist/worker.d.ts.map +1 -1
  35. package/dist/worker.js +111 -1
  36. package/package.json +1 -1
  37. package/src/capabilities/ai_connector.test.ts +341 -0
  38. package/src/capabilities/ai_connector.ts +194 -0
  39. package/src/capabilities/automation.ts +10 -6
  40. package/src/capabilities/output.ts +8 -0
  41. package/src/capabilities/stateful-capability.test.ts +25 -0
  42. package/src/capabilities/stateful-capability.ts +87 -0
  43. package/src/capabilities/sync.test.ts +89 -3
  44. package/src/capabilities/sync.ts +22 -86
  45. package/src/capabilities/tool.ts +5 -12
  46. package/src/capabilities/webhook.ts +148 -0
  47. package/src/index.ts +22 -0
  48. package/src/pacer_internal.ts +34 -0
  49. package/src/schema.ts +29 -12
  50. package/src/worker.ts +137 -10
@@ -0,0 +1,101 @@
1
+ import { ExecutionError } from "../error.js";
2
+ import type { HandlerOptions } from "../types.js";
3
+ import type { CapabilityContext } from "./context.js";
4
+ /**
5
+ * A single incoming webhook request.
6
+ */
7
+ export interface WebhookEvent {
8
+ /**
9
+ * Unique ID for this webhook delivery, stable across retries.
10
+ * Best-effort idempotency key — prefer using the webhook provider's
11
+ * own delivery/event ID when available, since providers may redeliver
12
+ * the same event with a new deliveryId.
13
+ */
14
+ deliveryId: string;
15
+ /**
16
+ * The parsed JSON body of the incoming request, or an empty object if
17
+ * the body is not valid JSON.
18
+ */
19
+ body: Record<string, unknown>;
20
+ /**
21
+ * The raw request body as a string, useful for signature verification.
22
+ */
23
+ rawBody: string;
24
+ /**
25
+ * The HTTP headers from the incoming request
26
+ */
27
+ headers: Record<string, string>;
28
+ /**
29
+ * The HTTP method of the incoming request (e.g. "POST")
30
+ */
31
+ method: string;
32
+ }
33
+ /**
34
+ * Throw this error from your webhook execute handler to signal that
35
+ * signature verification failed. After 5 consecutive verification
36
+ * failures, the platform will short-circuit and reject all incoming
37
+ * requests for this webhook without executing the handler.
38
+ *
39
+ * @example
40
+ * ```ts
41
+ * worker.webhook("onGithubPush", {
42
+ * title: "GitHub Push",
43
+ * description: "Handles GitHub push events",
44
+ * execute: async (events) => {
45
+ * const secret = process.env.GITHUB_WEBHOOK_SECRET;
46
+ * if (!secret) {
47
+ * throw new WebhookVerificationError("GITHUB_WEBHOOK_SECRET not set");
48
+ * }
49
+ * for (const event of events) {
50
+ * if (!verifyGitHubSignature(event.rawBody, event.headers, secret)) {
51
+ * throw new WebhookVerificationError("Invalid GitHub signature");
52
+ * }
53
+ * // ... handle verified event
54
+ * }
55
+ * },
56
+ * });
57
+ * ```
58
+ */
59
+ export declare class WebhookVerificationError extends ExecutionError {
60
+ constructor(cause?: unknown);
61
+ }
62
+ /**
63
+ * Configuration for a webhook capability
64
+ */
65
+ export interface WebhookConfiguration {
66
+ /**
67
+ * Title of the webhook - shown in the UI when viewing webhooks
68
+ */
69
+ title: string;
70
+ /**
71
+ * Description of what this webhook does - shown in the UI
72
+ */
73
+ description: string;
74
+ /**
75
+ * The function that executes when the webhook is triggered.
76
+ * Receives an array of events (currently always a single event,
77
+ * but may be batched in the future).
78
+ *
79
+ * To signal a verification failure, throw a `WebhookVerificationError`.
80
+ * After 5 consecutive verification failures, the platform will
81
+ * short-circuit and stop executing the handler.
82
+ *
83
+ * @param events - The incoming webhook events
84
+ * @param context - The capability execution context (Notion client, etc.)
85
+ * @returns A promise that resolves when the webhook processing completes
86
+ */
87
+ execute: (events: WebhookEvent[], context: CapabilityContext) => Promise<void> | void;
88
+ }
89
+ export type WebhookCapability = ReturnType<typeof createWebhookCapability>;
90
+ export declare function createWebhookCapability(key: string, config: WebhookConfiguration): {
91
+ _tag: "webhook";
92
+ key: string;
93
+ config: {
94
+ title: string;
95
+ description: string;
96
+ };
97
+ handler(events: WebhookEvent[], options?: HandlerOptions): Promise<{
98
+ status: "success";
99
+ } | undefined>;
100
+ };
101
+ //# sourceMappingURL=webhook.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"webhook.d.ts","sourceRoot":"","sources":["../../src/capabilities/webhook.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAClD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAItD;;GAEG;AACH,MAAM,WAAW,YAAY;IAC5B;;;;;OAKG;IACH,UAAU,EAAE,MAAM,CAAC;IACnB;;;OAGG;IACH,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC9B;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;IAChB;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAChC;;OAEG;IACH,MAAM,EAAE,MAAM,CAAC;CACf;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,qBAAa,wBAAyB,SAAQ,cAAc;gBAC/C,KAAK,CAAC,EAAE,OAAO;CAI3B;AAED;;GAEG;AACH,MAAM,WAAW,oBAAoB;IACpC;;OAEG;IACH,KAAK,EAAE,MAAM,CAAC;IAEd;;OAEG;IACH,WAAW,EAAE,MAAM,CAAC;IAEpB;;;;;;;;;;;;OAYG;IACH,OAAO,EAAE,CACR,MAAM,EAAE,YAAY,EAAE,EACtB,OAAO,EAAE,iBAAiB,KACtB,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;CAC1B;AAED,MAAM,MAAM,iBAAiB,GAAG,UAAU,CAAC,OAAO,uBAAuB,CAAC,CAAC;AAE3E,wBAAgB,uBAAuB,CACtC,GAAG,EAAE,MAAM,EACX,MAAM,EAAE,oBAAoB;;;;;;;oBAUlB,YAAY,EAAE,YACZ,cAAc,GACtB,OAAO,CAAC;QAAE,MAAM,EAAE,SAAS,CAAA;KAAE,GAAG,SAAS,CAAC;EA6B9C"}
@@ -0,0 +1,47 @@
1
+ import { ExecutionError } from "../error.js";
2
+ import { createCapabilityContext } from "./context.js";
3
+ import { writeOutput } from "./output.js";
4
+ class WebhookVerificationError extends ExecutionError {
5
+ constructor(cause) {
6
+ super(cause ?? "Webhook signature verification failed");
7
+ this.name = "WebhookVerificationError";
8
+ }
9
+ }
10
+ function createWebhookCapability(key, config) {
11
+ return {
12
+ _tag: "webhook",
13
+ key,
14
+ config: {
15
+ title: config.title,
16
+ description: config.description
17
+ },
18
+ async handler(events, options) {
19
+ try {
20
+ const capabilityContext = createCapabilityContext();
21
+ await config.execute(events, capabilityContext);
22
+ if (options?.concreteOutput) {
23
+ return { status: "success" };
24
+ } else {
25
+ writeOutput({ _tag: "success", value: { status: "success" } });
26
+ }
27
+ } catch (err) {
28
+ const error = err instanceof ExecutionError ? err : new ExecutionError(err);
29
+ if (!options?.concreteOutput) {
30
+ writeOutput({
31
+ _tag: "error",
32
+ error: {
33
+ name: error.name,
34
+ message: error.message,
35
+ trace: error.stack
36
+ }
37
+ });
38
+ }
39
+ throw error;
40
+ }
41
+ }
42
+ };
43
+ }
44
+ export {
45
+ WebhookVerificationError,
46
+ createWebhookCapability
47
+ };
package/dist/index.d.ts CHANGED
@@ -1,9 +1,12 @@
1
1
  export { emojiIcon, imageIcon, notionIcon, place } from "./builder.js";
2
+ export type { AiConnectorArchetype, AiConnectorCapability, AiConnectorChange, AiConnectorChangeUpsert, AiConnectorChatAuthor, AiConnectorChatChannel, AiConnectorChatMessage, AiConnectorChatRecord, AiConnectorConfiguration, AiConnectorExecutionResult, AiConnectorMode, AiConnectorProjectBlock, AiConnectorProjectRecord, AiConnectorRecordByArchetype, } from "./capabilities/ai_connector.js";
2
3
  export type { AutomationCapability, AutomationConfiguration, AutomationEvent, PageObjectResponse, } from "./capabilities/automation.js";
3
4
  export type { CapabilityContext } from "./capabilities/context.js";
4
5
  export type { NotionManagedOAuthConfiguration, OAuthCapability, OAuthConfiguration, UserManagedOAuthConfiguration, } from "./capabilities/oauth.js";
5
6
  export type { SyncCapability, SyncChange, SyncChangeDelete, SyncChangeUpsert, SyncConfiguration, SyncExecutionResult, SyncMode, } from "./capabilities/sync.js";
6
7
  export type { ToolCapability, ToolConfiguration } from "./capabilities/tool.js";
8
+ export type { WebhookCapability, WebhookConfiguration, WebhookEvent, } from "./capabilities/webhook.js";
9
+ export { WebhookVerificationError } from "./capabilities/webhook.js";
7
10
  export type { AnyJSONSchema, JSONSchema } from "./json-schema.js";
8
11
  export type { Infer, SchemaBuilder } from "./schema-builder.js";
9
12
  export { getSchema, j } from "./schema-builder.js";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AACvE,YAAY,EACX,oBAAoB,EACpB,uBAAuB,EACvB,eAAe,EACf,kBAAkB,GAClB,MAAM,8BAA8B,CAAC;AACtC,YAAY,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AACnE,YAAY,EACX,+BAA+B,EAC/B,eAAe,EACf,kBAAkB,EAClB,6BAA6B,GAC7B,MAAM,yBAAyB,CAAC;AACjC,YAAY,EACX,cAAc,EACd,UAAU,EACV,gBAAgB,EAChB,gBAAgB,EAChB,iBAAiB,EACjB,mBAAmB,EACnB,QAAQ,GACR,MAAM,wBAAwB,CAAC;AAChC,YAAY,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAChF,YAAY,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAClE,YAAY,EAAE,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAChE,OAAO,EAAE,SAAS,EAAE,CAAC,EAAE,MAAM,qBAAqB,CAAC;AACnD,YAAY,EACX,IAAI,EACJ,SAAS,EACT,YAAY,EACZ,WAAW,EACX,UAAU,EACV,QAAQ,GACR,MAAM,YAAY,CAAC;AACpB,YAAY,EACX,cAAc,EACd,cAAc,EACd,qBAAqB,EACrB,WAAW,EACX,gBAAgB,EAChB,WAAW,EACX,cAAc,GACd,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC"}
1
+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AAC;AACpB,YAAY,EACX,cAAc,EACd,cAAc,EACd,qBAAqB,EACrB,WAAW,EACX,gBAAgB,EAChB,WAAW,EACX,cAAc,GACd,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC"}
package/dist/index.js CHANGED
@@ -1,7 +1,9 @@
1
1
  import { emojiIcon, imageIcon, notionIcon, place } from "./builder.js";
2
+ import { WebhookVerificationError } from "./capabilities/webhook.js";
2
3
  import { getSchema, j } from "./schema-builder.js";
3
4
  import { Worker } from "./worker.js";
4
5
  export {
6
+ WebhookVerificationError,
5
7
  Worker,
6
8
  emojiIcon,
7
9
  getSchema,
@@ -1,6 +1,13 @@
1
1
  export type PacerState = {
2
2
  pacers: Record<string, PacerEntry>;
3
3
  };
4
+ export type PacerDeclaration = {
5
+ readonly key: string;
6
+ readonly config: {
7
+ allowedRequests: number;
8
+ intervalMs: number;
9
+ };
10
+ };
4
11
  export declare function setPacerState(state: PacerState): void;
5
12
  export declare function getPacerState(): PacerState;
6
13
  //# sourceMappingURL=pacer_internal.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"pacer_internal.d.ts","sourceRoot":"","sources":["../src/pacer_internal.ts"],"names":[],"mappings":"AAYA,MAAM,MAAM,UAAU,GAAG;IACxB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;CACnC,CAAC;AAIF,wBAAgB,aAAa,CAAC,KAAK,EAAE,UAAU,GAAG,IAAI,CAErD;AAED,wBAAgB,aAAa,IAAI,UAAU,CAE1C"}
1
+ {"version":3,"file":"pacer_internal.d.ts","sourceRoot":"","sources":["../src/pacer_internal.ts"],"names":[],"mappings":"AAYA,MAAM,MAAM,UAAU,GAAG;IACxB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;CACnC,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC9B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,MAAM,EAAE;QAAE,eAAe,EAAE,MAAM,CAAC;QAAC,UAAU,EAAE,MAAM,CAAA;KAAE,CAAC;CACjE,CAAC;AAIF,wBAAgB,aAAa,CAAC,KAAK,EAAE,UAAU,GAAG,IAAI,CAErD;AAED,wBAAgB,aAAa,IAAI,UAAU,CAE1C"}
@@ -5,6 +5,22 @@ function setPacerState(state) {
5
5
  function getPacerState() {
6
6
  return pacerState;
7
7
  }
8
+ function initPacerState(runtimePacers, declarations) {
9
+ const pacers = runtimePacers ?? {};
10
+ if (Object.keys(pacers).length === 0 && declarations && declarations.length > 0) {
11
+ const localPacers = {};
12
+ for (const decl of declarations) {
13
+ localPacers[decl.key] = {
14
+ lastScheduledAtMs: 0,
15
+ allowedRequests: decl.config.allowedRequests,
16
+ intervalMs: decl.config.intervalMs
17
+ };
18
+ }
19
+ setPacerState({ pacers: localPacers });
20
+ } else {
21
+ setPacerState({ pacers });
22
+ }
23
+ }
8
24
  async function pacerWait(key) {
9
25
  const state = getPacerState();
10
26
  const entry = state.pacers[key];
@@ -27,6 +43,7 @@ async function pacerWait(key) {
27
43
  }
28
44
  export {
29
45
  getPacerState,
46
+ initPacerState,
30
47
  pacerWait,
31
48
  setPacerState
32
49
  };
package/dist/schema.d.ts CHANGED
@@ -48,10 +48,10 @@ export type PropertyConfiguration = {
48
48
  } | {
49
49
  type: "relation";
50
50
  /**
51
- * The export name of the sync capability that defines the related database.
52
- * This must match the export name used when defining the related sync capability.
51
+ * The key of the related database declaration.
52
+ * This must match the key passed to `worker.database()`.
53
53
  */
54
- relatedSyncKey: string;
54
+ relatedDatabaseKey: string;
55
55
  config: {
56
56
  twoWay: false;
57
57
  } | {
@@ -143,26 +143,43 @@ export declare function people(): PropertyConfiguration;
143
143
  */
144
144
  export declare function place(): PropertyConfiguration;
145
145
  /**
146
- * Creates a relation property definition that references another sync capability.
147
- * The related database must be defined by a sync capability in the same worker.
146
+ * Creates a relation property definition that references another database.
147
+ * The related database must be declared in the same worker.
148
148
  *
149
- * @param relatedSyncKey - The export name of the sync capability that defines the related database.
149
+ * @param relatedDatabaseKey - The key of the related database declaration.
150
150
  * @example
151
151
  * ```typescript
152
- * export const projectsSync = sync({...});
152
+ * const projects = worker.database("projects", {
153
+ * type: "managed",
154
+ * initialTitle: "Projects",
155
+ * primaryKeyProperty: "Project ID",
156
+ * schema: {
157
+ * properties: {
158
+ * "Project Name": Schema.title(),
159
+ * "Project ID": Schema.richText(),
160
+ * },
161
+ * },
162
+ * });
153
163
  *
154
- * export const tasksSync = sync({
164
+ * const tasks = worker.database("tasks", {
165
+ * type: "managed",
166
+ * initialTitle: "Tasks",
167
+ * primaryKeyProperty: "Task ID",
155
168
  * schema: {
156
169
  * properties: {
157
170
  * "Task Title": Schema.title(),
158
- * "Project": Schema.relation("projectsSync"),
171
+ * "Project": Schema.relation("projects"),
159
172
  * },
160
173
  * },
161
- * ...
174
+ * });
175
+ *
176
+ * export const tasksSync = worker.sync("tasksSync", {
177
+ * database: tasks,
178
+ * execute: async () => ({ changes: [], hasMore: false }),
162
179
  * });
163
180
  * ```
164
181
  */
165
- export declare function relation(relatedSyncKey: string, config?: {
182
+ export declare function relation(relatedDatabaseKey: string, config?: {
166
183
  twoWay: false;
167
184
  } | {
168
185
  twoWay: true;
@@ -1 +1 @@
1
- {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,UAAU,EACV,IAAI,EACJ,YAAY,EACZ,YAAY,EACZ,WAAW,EACX,MAAM,YAAY,CAAC;AAEpB;;GAEG;AACH,MAAM,MAAM,YAAY,GACrB,OAAO,GACP,WAAW,GACX,KAAK,GACL,OAAO,GACP,cAAc,GACd,UAAU,GACV,MAAM,GACN,QAAQ,GACR,MAAM,GACN,QAAQ,GACR,cAAc,GACd,QAAQ,GACR,QAAQ,GACR,OAAO,CAAC;AAEX;;GAEG;AACH,MAAM,MAAM,kBAAkB,GAAG;IAChC,IAA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qB,CAEhD;AAED;;GAEG;AACH,wBAAgB,IAAI,IAAI,qBAAqB,CAE5C;AAED;;GAEG;AACH,wBAAgB,MAAM,CAAC,MAAM,CAAC,EAAE,YAAY,GAAG,qBAAqB,CAEnE;AAED;;GAEG;AACH,wBAAgB,IAAI,CAAC,WAAW,CAAC,EAAE,UAAU,GAAG,qBAAqB,CAEpE;AAED;;GAEG;AACH,wBAAgB,MAAM,CAAC,OAAO,EAAE,YAAY,EAAE,GAAG,qBAAqB,CAErE;AAED;;GAEG;AACH,wBAAgB,WAAW,CAAC,OAAO,EAAE,YAAY,EAAE,GAAG,qBAAqB,CAE1E;AAED;;GAEG;AACH,wBAAgB,MAAM,CAAC,MAAM,EAAE;IAC9B,MAAM,EAAE,WAAW,EAAE,CAAC;CACtB,GAAG,qBAAqB,CAExB;AAED;;GAEG;AACH,wBAAgB,MAAM,IAAI,qBAAqB,CAE9C;AAED;;GAEG;AACH,wBAAgB,KAAK,IAAI,qBAAqB,CAE7C;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,wBAAgB,QAAQ,CACvB,kBAAkB,EAAE,MAAM,EAC1B,MAAM,CAAC,EAAE;IAAE,MAAM,EAAE,KAAK,CAAA;CAAE,GAAG;IAAE,MAAM,EAAE,IAAI,CAAC;IAAC,mBAAmB,EAAE,MAAM,CAAA;CAAE,GACxE,qBAAqB,CAMvB"}
package/dist/schema.js CHANGED
@@ -40,10 +40,10 @@ function people() {
40
40
  function place() {
41
41
  return { type: "place" };
42
42
  }
43
- function relation(relatedSyncKey, config) {
43
+ function relation(relatedDatabaseKey, config) {
44
44
  return {
45
45
  type: "relation",
46
- relatedSyncKey,
46
+ relatedDatabaseKey,
47
47
  config: config ?? { twoWay: false }
48
48
  };
49
49
  }
package/dist/worker.d.ts CHANGED
@@ -1,12 +1,17 @@
1
+ import type { AiConnectorArchetype, AiConnectorCapability, AiConnectorConfiguration } from "./capabilities/ai_connector.js";
1
2
  import type { AutomationCapability, AutomationConfiguration, AutomationEvent } from "./capabilities/automation.js";
2
3
  import type { CapabilityContext } from "./capabilities/context.js";
3
4
  import type { NotionManagedOAuthConfiguration, OAuthCapability, OAuthConfiguration, UserManagedOAuthConfiguration } from "./capabilities/oauth.js";
4
5
  import type { SyncCapability, SyncConfiguration } from "./capabilities/sync.js";
5
6
  import type { ToolCapability, ToolConfiguration } from "./capabilities/tool.js";
7
+ import type { WebhookCapability, WebhookConfiguration, WebhookEvent } from "./capabilities/webhook.js";
8
+ import { WebhookVerificationError } from "./capabilities/webhook.js";
9
+ import type { PacerDeclaration } from "./pacer_internal.js";
6
10
  import type { Schema } from "./schema.js";
7
11
  import type { HandlerOptions, JSONValue } from "./types.js";
8
- export type { AutomationConfiguration, AutomationEvent, CapabilityContext, OAuthConfiguration, NotionManagedOAuthConfiguration, UserManagedOAuthConfiguration, SyncConfiguration, ToolConfiguration, };
9
- type Capability = SyncCapability | ToolCapability<any, any> | AutomationCapability | OAuthCapability;
12
+ export type { AutomationConfiguration, AutomationEvent, AiConnectorConfiguration, CapabilityContext, OAuthConfiguration, NotionManagedOAuthConfiguration, UserManagedOAuthConfiguration, SyncConfiguration, ToolConfiguration, WebhookConfiguration, WebhookEvent, };
13
+ export { WebhookVerificationError };
14
+ type Capability = SyncCapability | AiConnectorCapability | ToolCapability<any, any> | AutomationCapability | OAuthCapability | WebhookCapability;
10
15
  /**
11
16
  * Configuration for a database that Notion creates and manages on behalf
12
17
  * of the worker. The database is created on first deploy and its schema
@@ -87,13 +92,7 @@ export type PacerHandle = {
87
92
  */
88
93
  wait(): Promise<void>;
89
94
  };
90
- /**
91
- * A pacer declaration as it appears in the manifest.
92
- */
93
- export type PacerDeclaration = {
94
- readonly key: string;
95
- readonly config: PacerConfig;
96
- };
95
+ export type { PacerDeclaration };
97
96
  /** A capability entry stripped of its handler, used in the manifest. */
98
97
  type CapabilityManifestEntry = Pick<Capability, "_tag" | "key" | "config">;
99
98
  /**
@@ -189,6 +188,60 @@ export declare class Worker {
189
188
  * @returns The capability object.
190
189
  */
191
190
  sync<PK extends string, S extends Schema<PK>, Context = unknown>(key: string, config: SyncConfiguration<PK, S, Context>): SyncCapability;
191
+ /**
192
+ * Register an AI connector capability.
193
+ *
194
+ * Example:
195
+ *
196
+ * ```ts
197
+ * const worker = new Worker();
198
+ * export default worker;
199
+ *
200
+ * worker.aiConnector("supportChats", {
201
+ * aiConnectorId: "connector-record-id",
202
+ * archetype: "chat",
203
+ * mode: "incremental",
204
+ * schedule: "15m",
205
+ * execute: async (state) => ({
206
+ * changes: [
207
+ * {
208
+ * type: "upsert",
209
+ * key: "thread-1",
210
+ * record: {
211
+ * createdAt: "2026-01-01T00:00:00Z",
212
+ * updatedAt: "2026-01-01T00:00:00Z",
213
+ * channel: {
214
+ * id: "channel-1",
215
+ * name: "Support",
216
+ * },
217
+ * threadId: "thread-1",
218
+ * messages: [
219
+ * {
220
+ * id: "message-1",
221
+ * content: "Hello from support",
222
+ * timestamp: "2026-01-01T00:00:00Z",
223
+ * author: {
224
+ * id: "user-1",
225
+ * username: "alice",
226
+ * },
227
+ * },
228
+ * ],
229
+ * },
230
+ * },
231
+ * ],
232
+ * hasMore: false,
233
+ * }),
234
+ * });
235
+ * ```
236
+ *
237
+ * `aiConnectorId` must be the Notion record ID of an existing custom
238
+ * connector in the workspace where you deploy this worker.
239
+ *
240
+ * @param key - The unique key for this capability.
241
+ * @param config - The AI connector configuration.
242
+ * @returns The capability object.
243
+ */
244
+ aiConnector<A extends AiConnectorArchetype, Context = unknown>(key: string, config: AiConnectorConfiguration<A, Context>): AiConnectorCapability;
192
245
  /**
193
246
  * Register a tool capability.
194
247
  *
@@ -245,6 +298,35 @@ export declare class Worker {
245
298
  * @returns The capability object.
246
299
  */
247
300
  automation(key: string, config: AutomationConfiguration): AutomationCapability;
301
+ /**
302
+ * Register a webhook capability.
303
+ *
304
+ * Webhooks expose an HTTP endpoint that external services can call.
305
+ * When hit, the request is processed by the worker's execute function.
306
+ *
307
+ * Example:
308
+ *
309
+ * ```ts
310
+ * const worker = new Worker();
311
+ * export default worker;
312
+ *
313
+ * worker.webhook("onGithubPush", {
314
+ * title: "GitHub Push Webhook",
315
+ * description: "Handles push events from GitHub",
316
+ * execute: async (events, { notion }) => {
317
+ * for (const event of events) {
318
+ * console.log("Received webhook:", event.body);
319
+ * console.log("Headers:", event.headers);
320
+ * }
321
+ * },
322
+ * })
323
+ * ```
324
+ *
325
+ * @param key - The unique key for this capability. Used as the webhook name in the URL.
326
+ * @param config - The webhook configuration.
327
+ * @returns The capability object.
328
+ */
329
+ webhook(key: string, config: WebhookConfiguration): WebhookCapability;
248
330
  /**
249
331
  * Register an OAuth capability.
250
332
  *
@@ -1 +1 @@
1
- {"version":3,"file":"worker.d.ts","sourceRoot":"","sources":["../src/worker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,oBAAoB,EACpB,uBAAuB,EACvB,eAAe,EACf,MAAM,8BAA8B,CAAC;AAEtC,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AACnE,OAAO,KAAK,EACX,+BAA+B,EAC/B,eAAe,EACf,kBAAkB,EAClB,6BAA6B,EAC7B,MAAM,yBAAyB,CAAC;AAEjC,OAAO,KAAK,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAEhF,OAAO,KAAK,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAGhF,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,KAAK,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAG5D,YAAY,EACX,uBAAuB,EACvB,eAAe,EACf,iBAAiB,EACjB,kBAAkB,EAClB,+BAA+B,EAC/B,6BAA6B,EAC7B,iBAAiB,EACjB,iBAAiB,GACjB,CAAC;AAMF,KAAK,UAAU,GACZ,cAAc,GAEd,cAAc,CAAC,GAAG,EAAE,GAAG,CAAC,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OAEG;IACH,IAAI,YAAY,IAAI,IAAI,CAAC,UAAU,EAAE,MAAM,GAAG,KAAK,GAAG,QAAQ,CAAC,EAAE,CAMhE;IAED,IAAI,QAAQ,IAAI,cAAc,CAO7B;IAED;;;;;;;OAOG;IACG,GAAG,CACR,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,OAAO,EAChB,OAAO,GAAE,cAAmB,GAC1B,OAAO,CAAC,OAAO,CAAC;CAiCnB"}
package/dist/worker.js CHANGED
@@ -1,7 +1,12 @@
1
+ import { createAiConnectorCapability } from "./capabilities/ai_connector.js";
1
2
  import { createAutomationCapability } from "./capabilities/automation.js";
2
3
  import { createOAuthCapability } from "./capabilities/oauth.js";
3
4
  import { createSyncCapability } from "./capabilities/sync.js";
4
5
  import { createToolCapability } from "./capabilities/tool.js";
6
+ import {
7
+ createWebhookCapability,
8
+ WebhookVerificationError
9
+ } from "./capabilities/webhook.js";
5
10
  import { pacerWait } from "./pacer_internal.js";
6
11
  import { createRequire } from "node:module";
7
12
  const SDK_VERSION = (() => {
@@ -111,7 +116,74 @@ class Worker {
111
116
  */
112
117
  sync(key, config) {
113
118
  this.#validateUniqueKey(key);
114
- const capability = createSyncCapability(key, config);
119
+ const capability = createSyncCapability(
120
+ key,
121
+ config,
122
+ Array.from(this.#pacers.values())
123
+ );
124
+ this.#capabilities.set(key, capability);
125
+ return capability;
126
+ }
127
+ /**
128
+ * Register an AI connector capability.
129
+ *
130
+ * Example:
131
+ *
132
+ * ```ts
133
+ * const worker = new Worker();
134
+ * export default worker;
135
+ *
136
+ * worker.aiConnector("supportChats", {
137
+ * aiConnectorId: "connector-record-id",
138
+ * archetype: "chat",
139
+ * mode: "incremental",
140
+ * schedule: "15m",
141
+ * execute: async (state) => ({
142
+ * changes: [
143
+ * {
144
+ * type: "upsert",
145
+ * key: "thread-1",
146
+ * record: {
147
+ * createdAt: "2026-01-01T00:00:00Z",
148
+ * updatedAt: "2026-01-01T00:00:00Z",
149
+ * channel: {
150
+ * id: "channel-1",
151
+ * name: "Support",
152
+ * },
153
+ * threadId: "thread-1",
154
+ * messages: [
155
+ * {
156
+ * id: "message-1",
157
+ * content: "Hello from support",
158
+ * timestamp: "2026-01-01T00:00:00Z",
159
+ * author: {
160
+ * id: "user-1",
161
+ * username: "alice",
162
+ * },
163
+ * },
164
+ * ],
165
+ * },
166
+ * },
167
+ * ],
168
+ * hasMore: false,
169
+ * }),
170
+ * });
171
+ * ```
172
+ *
173
+ * `aiConnectorId` must be the Notion record ID of an existing custom
174
+ * connector in the workspace where you deploy this worker.
175
+ *
176
+ * @param key - The unique key for this capability.
177
+ * @param config - The AI connector configuration.
178
+ * @returns The capability object.
179
+ */
180
+ aiConnector(key, config) {
181
+ this.#validateUniqueKey(key);
182
+ const capability = createAiConnectorCapability(
183
+ key,
184
+ config,
185
+ Array.from(this.#pacers.values())
186
+ );
115
187
  this.#capabilities.set(key, capability);
116
188
  return capability;
117
189
  }
@@ -181,6 +253,40 @@ class Worker {
181
253
  this.#capabilities.set(key, capability);
182
254
  return capability;
183
255
  }
256
+ /**
257
+ * Register a webhook capability.
258
+ *
259
+ * Webhooks expose an HTTP endpoint that external services can call.
260
+ * When hit, the request is processed by the worker's execute function.
261
+ *
262
+ * Example:
263
+ *
264
+ * ```ts
265
+ * const worker = new Worker();
266
+ * export default worker;
267
+ *
268
+ * worker.webhook("onGithubPush", {
269
+ * title: "GitHub Push Webhook",
270
+ * description: "Handles push events from GitHub",
271
+ * execute: async (events, { notion }) => {
272
+ * for (const event of events) {
273
+ * console.log("Received webhook:", event.body);
274
+ * console.log("Headers:", event.headers);
275
+ * }
276
+ * },
277
+ * })
278
+ * ```
279
+ *
280
+ * @param key - The unique key for this capability. Used as the webhook name in the URL.
281
+ * @param config - The webhook configuration.
282
+ * @returns The capability object.
283
+ */
284
+ webhook(key, config) {
285
+ this.#validateUniqueKey(key);
286
+ const capability = createWebhookCapability(key, config);
287
+ this.#capabilities.set(key, capability);
288
+ return capability;
289
+ }
184
290
  /**
185
291
  * Register an OAuth capability.
186
292
  *
@@ -264,6 +370,9 @@ class Worker {
264
370
  `Cannot run OAuth capability "${key}" - OAuth capabilities only provide configuration`
265
371
  );
266
372
  }
373
+ if (!capability.handler) {
374
+ throw new Error(`Capability "${key}" cannot be executed`);
375
+ }
267
376
  return capability.handler(context, options);
268
377
  }
269
378
  #validateUniqueKey(key) {
@@ -276,5 +385,6 @@ class Worker {
276
385
  }
277
386
  }
278
387
  export {
388
+ WebhookVerificationError,
279
389
  Worker
280
390
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@notionhq/workers",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "An SDK for building workers for the Notion Workers platform",
5
5
  "license": "MIT",
6
6
  "type": "module",