@notionhq/workers 0.0.82

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 (73) hide show
  1. package/README.md +22 -0
  2. package/dist/block.d.ts +321 -0
  3. package/dist/block.d.ts.map +1 -0
  4. package/dist/block.js +0 -0
  5. package/dist/builder.d.ts +117 -0
  6. package/dist/builder.d.ts.map +1 -0
  7. package/dist/builder.js +168 -0
  8. package/dist/capabilities/automation.d.ts +91 -0
  9. package/dist/capabilities/automation.d.ts.map +1 -0
  10. package/dist/capabilities/automation.js +40 -0
  11. package/dist/capabilities/automation.test.d.ts +2 -0
  12. package/dist/capabilities/automation.test.d.ts.map +1 -0
  13. package/dist/capabilities/context.d.ts +7 -0
  14. package/dist/capabilities/context.d.ts.map +1 -0
  15. package/dist/capabilities/context.js +15 -0
  16. package/dist/capabilities/oauth.d.ts +120 -0
  17. package/dist/capabilities/oauth.d.ts.map +1 -0
  18. package/dist/capabilities/oauth.js +55 -0
  19. package/dist/capabilities/oauth.test.d.ts +2 -0
  20. package/dist/capabilities/oauth.test.d.ts.map +1 -0
  21. package/dist/capabilities/sync.d.ts +180 -0
  22. package/dist/capabilities/sync.d.ts.map +1 -0
  23. package/dist/capabilities/sync.js +84 -0
  24. package/dist/capabilities/sync.test.d.ts +2 -0
  25. package/dist/capabilities/sync.test.d.ts.map +1 -0
  26. package/dist/capabilities/tool.d.ts +68 -0
  27. package/dist/capabilities/tool.d.ts.map +1 -0
  28. package/dist/capabilities/tool.js +174 -0
  29. package/dist/capabilities/tool.test.d.ts +2 -0
  30. package/dist/capabilities/tool.test.d.ts.map +1 -0
  31. package/dist/error.d.ts +12 -0
  32. package/dist/error.d.ts.map +1 -0
  33. package/dist/error.js +15 -0
  34. package/dist/icon-names.d.ts +6 -0
  35. package/dist/icon-names.d.ts.map +1 -0
  36. package/dist/icon-names.js +0 -0
  37. package/dist/index.d.ts +10 -0
  38. package/dist/index.d.ts.map +1 -0
  39. package/dist/index.js +9 -0
  40. package/dist/json-schema.d.ts +117 -0
  41. package/dist/json-schema.d.ts.map +1 -0
  42. package/dist/json-schema.js +0 -0
  43. package/dist/json-schema.test.d.ts +2 -0
  44. package/dist/json-schema.test.d.ts.map +1 -0
  45. package/dist/schema.d.ts +178 -0
  46. package/dist/schema.d.ts.map +1 -0
  47. package/dist/schema.js +66 -0
  48. package/dist/types.d.ts +206 -0
  49. package/dist/types.d.ts.map +1 -0
  50. package/dist/types.js +0 -0
  51. package/dist/worker.d.ts +177 -0
  52. package/dist/worker.d.ts.map +1 -0
  53. package/dist/worker.js +219 -0
  54. package/package.json +68 -0
  55. package/src/block.ts +529 -0
  56. package/src/builder.ts +299 -0
  57. package/src/capabilities/automation.test.ts +152 -0
  58. package/src/capabilities/automation.ts +130 -0
  59. package/src/capabilities/context.ts +23 -0
  60. package/src/capabilities/oauth.test.ts +52 -0
  61. package/src/capabilities/oauth.ts +157 -0
  62. package/src/capabilities/sync.test.ts +104 -0
  63. package/src/capabilities/sync.ts +311 -0
  64. package/src/capabilities/tool.test.ts +351 -0
  65. package/src/capabilities/tool.ts +279 -0
  66. package/src/error.ts +19 -0
  67. package/src/icon-names.ts +890 -0
  68. package/src/index.ts +34 -0
  69. package/src/json-schema.test.ts +719 -0
  70. package/src/json-schema.ts +179 -0
  71. package/src/schema.ts +241 -0
  72. package/src/types.ts +272 -0
  73. package/src/worker.ts +285 -0
package/dist/worker.js ADDED
@@ -0,0 +1,219 @@
1
+ import { createAutomationCapability } from "./capabilities/automation.js";
2
+ import { createOAuthCapability } from "./capabilities/oauth.js";
3
+ import { createSyncCapability } from "./capabilities/sync.js";
4
+ import { createToolCapability } from "./capabilities/tool.js";
5
+ class Worker {
6
+ #capabilities = /* @__PURE__ */ new Map();
7
+ /**
8
+ * Register a sync capability.
9
+ *
10
+ * Example:
11
+ *
12
+ * ```ts
13
+ * import { Worker } from "@notionhq/workers-sdk";
14
+ * import * as Builder from "@notionhq/workers-sdk/builder";
15
+ * import * as Schema from "@notionhq/workers-sdk/schema";
16
+ *
17
+ * const worker = new Worker();
18
+ * export default worker;
19
+ *
20
+ * worker.sync("tasksSync", {
21
+ * primaryKeyProperty: "Task ID",
22
+ * schema: {
23
+ * defaultName: "Tasks",
24
+ * properties: {
25
+ * "Task Name": Schema.title(),
26
+ * "Task ID": Schema.richText(),
27
+ * Status: Schema.select([
28
+ * { name: "Open", color: "default" },
29
+ * { name: "Done", color: "green" },
30
+ * ]),
31
+ * },
32
+ * },
33
+ * execute: async () => {
34
+ * const changes = [
35
+ * {
36
+ * key: "task-1",
37
+ * properties: {
38
+ * "Task Name": Builder.title("Write docs"),
39
+ * "Task ID": Builder.richText("task-1"),
40
+ * Status: Builder.select("Open"),
41
+ * },
42
+ * },
43
+ * ];
44
+ *
45
+ * return { changes, hasMore: false };
46
+ * },
47
+ * });
48
+ * ```
49
+ *
50
+ * @param key - The unique key for this capability.
51
+ * @param config - The sync configuration.
52
+ * @returns The capability object.
53
+ */
54
+ sync(key, config) {
55
+ this.#validateUniqueKey(key);
56
+ const capability = createSyncCapability(key, config);
57
+ this.#capabilities.set(key, capability);
58
+ return capability;
59
+ }
60
+ /**
61
+ * Register a tool capability.
62
+ *
63
+ * Example:
64
+ *
65
+ * ```ts
66
+ * worker.tool<{ name: string }, string>("sayHello", {
67
+ * title: "Say Hello",
68
+ * description: "Say hello to the user",
69
+ * schema: {
70
+ * type: "object",
71
+ * properties: {
72
+ * name: { type: "string" },
73
+ * },
74
+ * required: ["name"],
75
+ * },
76
+ * execute: ({ name }, { notion }) => {
77
+ * return `Hello, ${name}!`;
78
+ * },
79
+ * })
80
+ * ```
81
+ *
82
+ *
83
+ * @param key - The unique key for this capability.
84
+ * @param config - The tool configuration.
85
+ * @returns The capability object.
86
+ */
87
+ tool(key, config) {
88
+ this.#validateUniqueKey(key);
89
+ const capability = createToolCapability(key, config);
90
+ this.#capabilities.set(key, capability);
91
+ return capability;
92
+ }
93
+ /**
94
+ * Register an automation capability.
95
+ *
96
+ * Example:
97
+ *
98
+ * ```ts
99
+ * const worker = new Worker();
100
+ * export default worker;
101
+ *
102
+ * worker.automation("sendWelcomeEmail", {
103
+ * title: "Send Welcome Email",
104
+ * description: "Sends a welcome email when a new user is added",
105
+ * execute: async (event, { notion }) => {
106
+ * const { pageId, pageData } = event;
107
+ *
108
+ * // Access page properties from the Public API format
109
+ * if (pageData) {
110
+ * const name = pageData.properties.Name; // Access any property
111
+ * const status = pageData.properties.Status;
112
+ * console.log(`Processing: ${name}`);
113
+ * }
114
+ *
115
+ * // Your automation logic here
116
+ * await sendEmail(pageId);
117
+ * },
118
+ * })
119
+ * ```
120
+ *
121
+ * @param key - The unique key for this capability.
122
+ * @param config - The automation configuration.
123
+ * @returns The capability object.
124
+ */
125
+ automation(key, config) {
126
+ this.#validateUniqueKey(key);
127
+ const capability = createAutomationCapability(key, config);
128
+ this.#capabilities.set(key, capability);
129
+ return capability;
130
+ }
131
+ /**
132
+ * Register an OAuth capability.
133
+ *
134
+ * There are two ways to configure OAuth:
135
+ *
136
+ * 1. Notion-managed providers:
137
+ * ```ts
138
+ * const worker = new Worker();
139
+ * export default worker;
140
+ *
141
+ * worker.oauth("googleAuth", {
142
+ * type: "notion_managed",
143
+ * name: "my-google-auth",
144
+ * provider: "google"
145
+ * })
146
+ * ```
147
+ *
148
+ * 2. User-managed OAuth configuration:
149
+ * ```ts
150
+ * const worker = new Worker();
151
+ * export default worker;
152
+ *
153
+ * worker.oauth("myCustomAuth", {
154
+ * type: "user_managed",
155
+ * name: "my-custom-oauth",
156
+ * authorizationEndpoint: "https://provider.com/oauth/authorize",
157
+ * tokenEndpoint: "https://provider.com/oauth/token",
158
+ * scope: "read write",
159
+ * clientId: process.env.CLIENT_ID,
160
+ * clientSecret: process.env.CLIENT_SECRET,
161
+ * authorizationParams: {
162
+ * access_type: "offline",
163
+ * prompt: "consent"
164
+ * }
165
+ * })
166
+ * ```
167
+ *
168
+ * @param key - The unique key used to register this OAuth capability.
169
+ * @param config - The OAuth configuration (Notion-managed or user-managed) for this capability.
170
+ * @returns The registered OAuth capability.
171
+ */
172
+ oauth(key, config) {
173
+ this.#validateUniqueKey(key);
174
+ const capability = createOAuthCapability(key, config);
175
+ this.#capabilities.set(key, capability);
176
+ return capability;
177
+ }
178
+ /**
179
+ * Get all registered capabilities (for discovery) without their handlers.
180
+ */
181
+ get capabilities() {
182
+ return Array.from(this.#capabilities.values()).map((c) => ({
183
+ _tag: c._tag,
184
+ key: c.key,
185
+ config: c.config
186
+ }));
187
+ }
188
+ /**
189
+ * Execute a capability by key.
190
+ *
191
+ * @param key - The key of the capability to execute.
192
+ * @param context - The context to pass to the capability.
193
+ * @param options - Additional options for execution (e.g. for testing).
194
+ * @returns The result of the capability execution.
195
+ */
196
+ async run(key, context, options = {}) {
197
+ const capability = this.#capabilities.get(key);
198
+ if (!capability) {
199
+ throw new Error(`Capability "${key}" not found`);
200
+ }
201
+ if (capability._tag === "oauth") {
202
+ throw new Error(
203
+ `Cannot run OAuth capability "${key}" - OAuth capabilities only provide configuration`
204
+ );
205
+ }
206
+ return capability.handler(context, options);
207
+ }
208
+ #validateUniqueKey(key) {
209
+ if (!key || typeof key !== "string") {
210
+ throw new Error("Capability key must be a non-empty string");
211
+ }
212
+ if (this.#capabilities.has(key)) {
213
+ throw new Error(`Capability with key "${key}" already registered`);
214
+ }
215
+ }
216
+ }
217
+ export {
218
+ Worker
219
+ };
package/package.json ADDED
@@ -0,0 +1,68 @@
1
+ {
2
+ "name": "@notionhq/workers",
3
+ "version": "0.0.82",
4
+ "description": "An SDK for building workers for the Notion Workers platform",
5
+ "license": "UNLICENSED",
6
+ "type": "module",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "default": "./dist/index.js"
11
+ },
12
+ "./block": {
13
+ "types": "./dist/block.d.ts",
14
+ "default": "./dist/block.js"
15
+ },
16
+ "./builder": {
17
+ "types": "./dist/builder.d.ts",
18
+ "default": "./dist/builder.js"
19
+ },
20
+ "./schema": {
21
+ "types": "./dist/schema.d.ts",
22
+ "default": "./dist/schema.js"
23
+ },
24
+ "./error": {
25
+ "types": "./dist/error.d.ts",
26
+ "default": "./dist/error.js"
27
+ },
28
+ "./types": {
29
+ "types": "./dist/types.d.ts",
30
+ "default": "./dist/types.js"
31
+ }
32
+ },
33
+ "engines": {
34
+ "node": ">=22.0.0",
35
+ "npm": ">=10.9.2"
36
+ },
37
+ "publishConfig": {
38
+ "access": "public"
39
+ },
40
+ "files": [
41
+ "dist/",
42
+ "src/"
43
+ ],
44
+ "scripts": {
45
+ "bootstrap": "mise install && mise exec -- lefthook install",
46
+ "build": "rm -rf dist && npm run build:types && npm run build:js",
47
+ "build:types": "tsc",
48
+ "build:js": "tsx scripts/build.ts",
49
+ "build:watch": "concurrently --group \"npm run build:watch:types\" \"npm run build:watch:js\"",
50
+ "build:watch:types": "tsc --watch",
51
+ "build:watch:js": "esbuild --watch --format=esm --outdir=dist --target=esnext --packages=external ./src/**.ts ./src/**/*.ts",
52
+ "test": "vitest run"
53
+ },
54
+ "devDependencies": {
55
+ "@types/node": "^24.10.0",
56
+ "concurrently": "^9.2.1",
57
+ "esbuild": "^0.27.3",
58
+ "tsx": "^4.20.6",
59
+ "typescript": "^5.9.3",
60
+ "vitest": "^4.0.8"
61
+ },
62
+ "peerDependencies": {
63
+ "@notionhq/client": "^2.2.15"
64
+ },
65
+ "dependencies": {
66
+ "ajv": "^8.17.1"
67
+ }
68
+ }