@project-ajax/sdk 0.0.67 → 0.0.69

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 (39) hide show
  1. package/dist/builder.d.ts +6 -1
  2. package/dist/builder.d.ts.map +1 -1
  3. package/dist/builder.js +7 -0
  4. package/dist/capabilities/automation.d.ts +8 -28
  5. package/dist/capabilities/automation.d.ts.map +1 -1
  6. package/dist/capabilities/automation.js +3 -2
  7. package/dist/capabilities/oauth.d.ts +6 -31
  8. package/dist/capabilities/oauth.d.ts.map +1 -1
  9. package/dist/capabilities/oauth.js +4 -2
  10. package/dist/capabilities/sync.d.ts +8 -4
  11. package/dist/capabilities/sync.d.ts.map +1 -1
  12. package/dist/capabilities/sync.js +3 -2
  13. package/dist/capabilities/tool.d.ts +5 -25
  14. package/dist/capabilities/tool.d.ts.map +1 -1
  15. package/dist/capabilities/tool.js +3 -2
  16. package/dist/index.d.ts +7 -9
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js +4 -9
  19. package/dist/schema.d.ts +14 -2
  20. package/dist/schema.d.ts.map +1 -1
  21. package/dist/schema.js +4 -0
  22. package/dist/types.d.ts +32 -0
  23. package/dist/types.d.ts.map +1 -1
  24. package/dist/worker.d.ts +175 -0
  25. package/dist/worker.d.ts.map +1 -0
  26. package/dist/worker.js +218 -0
  27. package/package.json +1 -5
  28. package/src/builder.ts +12 -0
  29. package/src/capabilities/automation.test.ts +17 -23
  30. package/src/capabilities/automation.ts +14 -30
  31. package/src/capabilities/oauth.test.ts +16 -15
  32. package/src/capabilities/oauth.ts +7 -31
  33. package/src/capabilities/sync.ts +10 -4
  34. package/src/capabilities/tool.test.ts +105 -89
  35. package/src/capabilities/tool.ts +12 -31
  36. package/src/index.ts +7 -5
  37. package/src/schema.ts +18 -3
  38. package/src/types.ts +38 -0
  39. package/src/worker.ts +278 -0
package/src/worker.ts ADDED
@@ -0,0 +1,278 @@
1
+ import type {
2
+ AutomationCapability,
3
+ AutomationConfiguration,
4
+ AutomationContext,
5
+ } from "./capabilities/automation.js";
6
+ import { createAutomationCapability } from "./capabilities/automation.js";
7
+ import type {
8
+ NotionManagedOAuthConfiguration,
9
+ OAuthCapability,
10
+ OAuthConfiguration,
11
+ UserManagedOAuthConfiguration,
12
+ } from "./capabilities/oauth.js";
13
+ import { createOAuthCapability } from "./capabilities/oauth.js";
14
+ import type { SyncCapability, SyncConfiguration } from "./capabilities/sync.js";
15
+ import { createSyncCapability } from "./capabilities/sync.js";
16
+ import type { ToolCapability, ToolConfiguration } from "./capabilities/tool.js";
17
+ import { createToolCapability } from "./capabilities/tool.js";
18
+ import type { Schema } from "./schema.js";
19
+ import type { JSONValue } from "./types.js";
20
+
21
+ // Re-export types for convenience
22
+ export type {
23
+ AutomationConfiguration,
24
+ AutomationContext,
25
+ OAuthConfiguration,
26
+ NotionManagedOAuthConfiguration,
27
+ UserManagedOAuthConfiguration,
28
+ SyncConfiguration,
29
+ ToolConfiguration,
30
+ };
31
+
32
+ // ============================================================================
33
+ // Capability types
34
+ // ============================================================================
35
+
36
+ type Capability =
37
+ | SyncCapability
38
+ // biome-ignore lint/suspicious/noExplicitAny: any is used to allow any input and output types
39
+ | ToolCapability<any, any>
40
+ | AutomationCapability
41
+ | OAuthCapability;
42
+
43
+ // ============================================================================
44
+ // Worker class
45
+ // ============================================================================
46
+
47
+ export class Worker {
48
+ #capabilities: Map<string, Capability> = new Map();
49
+
50
+ /**
51
+ * Register a sync capability.
52
+ *
53
+ * Example:
54
+ *
55
+ * ```ts
56
+ * import { Worker } from "@project-ajax/sdk";
57
+ * import * as Builder from "@project-ajax/sdk/builder";
58
+ * import * as Schema from "@project-ajax/sdk/schema";
59
+ *
60
+ * const worker = new Worker();
61
+ * export default worker;
62
+ *
63
+ * worker.sync("tasksSync", {
64
+ * primaryKeyProperty: "Task ID",
65
+ * schema: {
66
+ * defaultName: "Tasks",
67
+ * properties: {
68
+ * "Task Name": Schema.title(),
69
+ * "Task ID": Schema.richText(),
70
+ * Status: Schema.select([
71
+ * { name: "Open", color: "default" },
72
+ * { name: "Done", color: "green" },
73
+ * ]),
74
+ * },
75
+ * },
76
+ * execute: async () => {
77
+ * const objects = [
78
+ * {
79
+ * key: "task-1",
80
+ * properties: {
81
+ * "Task Name": Builder.title("Write docs"),
82
+ * "Task ID": Builder.richText("task-1"),
83
+ * Status: Builder.select("Open"),
84
+ * },
85
+ * },
86
+ * ];
87
+ *
88
+ * return { objects, done: true };
89
+ * },
90
+ * });
91
+ * ```
92
+ *
93
+ * @param key - The unique key for this capability.
94
+ * @param config - The sync configuration.
95
+ * @returns The capability object.
96
+ */
97
+ sync<PK extends string, S extends Schema<PK>, Context = unknown>(
98
+ key: string,
99
+ config: SyncConfiguration<PK, S, Context>,
100
+ ): SyncCapability {
101
+ this.#validateUniqueKey(key);
102
+ const capability = createSyncCapability(key, config);
103
+ this.#capabilities.set(key, capability);
104
+ return capability;
105
+ }
106
+
107
+ /**
108
+ * Register a tool capability.
109
+ *
110
+ * Example:
111
+ *
112
+ * ```ts
113
+ * worker.tool<{ name: string }, string>("sayHello", {
114
+ * title: "Say Hello",
115
+ * description: "Say hello to the user",
116
+ * schema: {
117
+ * type: "object",
118
+ * properties: {
119
+ * name: { type: "string" },
120
+ * },
121
+ * required: ["name"],
122
+ * },
123
+ * execute: ({ name }) => {
124
+ * return `Hello, ${name}!`;
125
+ * },
126
+ * })
127
+ * ```
128
+ *
129
+ *
130
+ * @param key - The unique key for this capability.
131
+ * @param config - The tool configuration.
132
+ * @returns The capability object.
133
+ */
134
+ tool<I extends JSONValue, O extends JSONValue = JSONValue>(
135
+ key: string,
136
+ config: ToolConfiguration<I, O>,
137
+ ): ToolCapability<I, O> {
138
+ this.#validateUniqueKey(key);
139
+ const capability = createToolCapability(key, config);
140
+ // biome-ignore lint/suspicious/noExplicitAny: any is used to allow any input and output types
141
+ this.#capabilities.set(key, capability as ToolCapability<any, any>);
142
+ return capability;
143
+ }
144
+
145
+ /**
146
+ * Register an automation capability.
147
+ *
148
+ * Example:
149
+ *
150
+ * ```ts
151
+ * const worker = new Worker();
152
+ * export default worker;
153
+ *
154
+ * worker.automation("sendWelcomeEmail", {
155
+ * title: "Send Welcome Email",
156
+ * description: "Sends a welcome email when a new user is added",
157
+ * execute: async (context) => {
158
+ * const { pageId, pageData } = context;
159
+ *
160
+ * // Access page properties from the Public API format
161
+ * if (pageData) {
162
+ * const name = pageData.properties.Name; // Access any property
163
+ * const status = pageData.properties.Status;
164
+ * console.log(`Processing: ${name}`);
165
+ * }
166
+ *
167
+ * // Your automation logic here
168
+ * await sendEmail(pageId);
169
+ * },
170
+ * })
171
+ * ```
172
+ *
173
+ * @param key - The unique key for this capability.
174
+ * @param config - The automation configuration.
175
+ * @returns The capability object.
176
+ */
177
+ automation(
178
+ key: string,
179
+ config: AutomationConfiguration,
180
+ ): AutomationCapability {
181
+ this.#validateUniqueKey(key);
182
+ const capability = createAutomationCapability(key, config);
183
+ this.#capabilities.set(key, capability);
184
+ return capability;
185
+ }
186
+
187
+ /**
188
+ * Register an OAuth capability.
189
+ *
190
+ * There are two ways to configure OAuth:
191
+ *
192
+ * 1. Notion-managed providers:
193
+ * ```ts
194
+ * const worker = new Worker();
195
+ * export default worker;
196
+ *
197
+ * worker.oauth("googleAuth", {
198
+ * type: "notion_managed",
199
+ * name: "my-google-auth",
200
+ * provider: "google"
201
+ * })
202
+ * ```
203
+ *
204
+ * 2. User-managed OAuth configuration:
205
+ * ```ts
206
+ * const worker = new Worker();
207
+ * export default worker;
208
+ *
209
+ * worker.oauth("myCustomAuth", {
210
+ * type: "user_managed",
211
+ * name: "my-custom-oauth",
212
+ * authorizationEndpoint: "https://provider.com/oauth/authorize",
213
+ * tokenEndpoint: "https://provider.com/oauth/token",
214
+ * scope: "read write",
215
+ * clientId: process.env.CLIENT_ID,
216
+ * clientSecret: process.env.CLIENT_SECRET,
217
+ * authorizationParams: {
218
+ * access_type: "offline",
219
+ * prompt: "consent"
220
+ * }
221
+ * })
222
+ * ```
223
+ *
224
+ * @param key - The unique key used to register this OAuth capability.
225
+ * @param config - The OAuth configuration (Notion-managed or user-managed) for this capability.
226
+ * @returns The registered OAuth capability.
227
+ */
228
+ oauth(key: string, config: OAuthConfiguration): OAuthCapability {
229
+ this.#validateUniqueKey(key);
230
+ const capability = createOAuthCapability(key, config);
231
+ this.#capabilities.set(key, capability);
232
+ return capability;
233
+ }
234
+
235
+ /**
236
+ * Get all registered capabilities (for discovery) without their handlers.
237
+ */
238
+ get capabilities(): Pick<Capability, "_tag" | "key" | "config">[] {
239
+ return Array.from(this.#capabilities.values()).map((c) => ({
240
+ _tag: c._tag,
241
+ key: c.key,
242
+ config: c.config,
243
+ }));
244
+ }
245
+
246
+ /**
247
+ * Execute a capability by key.
248
+ *
249
+ * @param key - The key of the capability to execute.
250
+ * @param context - The context to pass to the capability.
251
+ * @returns The result of the capability execution.
252
+ */
253
+ async run(key: string, context: unknown): Promise<unknown> {
254
+ const capability = this.#capabilities.get(key);
255
+
256
+ if (!capability) {
257
+ throw new Error(`Capability "${key}" not found`);
258
+ }
259
+
260
+ if (capability._tag === "oauth") {
261
+ throw new Error(
262
+ `Cannot run OAuth capability "${key}" - OAuth capabilities only provide configuration`,
263
+ );
264
+ }
265
+
266
+ // biome-ignore lint/suspicious/noExplicitAny: context is unknown, passed by external non-typed code.
267
+ return capability.handler(context as any);
268
+ }
269
+
270
+ #validateUniqueKey(key: string): void {
271
+ if (!key || typeof key !== "string") {
272
+ throw new Error("Capability key must be a non-empty string");
273
+ }
274
+ if (this.#capabilities.has(key)) {
275
+ throw new Error(`Capability with key "${key}" already registered`);
276
+ }
277
+ }
278
+ }