@notionhq/workers 0.5.0 → 0.7.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 (182) hide show
  1. package/dist/alpha/ajv-formats.d.js +0 -0
  2. package/dist/alpha/builder.d.ts +149 -0
  3. package/dist/alpha/builder.d.ts.map +1 -0
  4. package/dist/alpha/builder.js +260 -0
  5. package/dist/alpha/builder.test.d.ts +2 -0
  6. package/dist/alpha/builder.test.d.ts.map +1 -0
  7. package/dist/alpha/cli/build.d.ts +13 -0
  8. package/dist/alpha/cli/build.d.ts.map +1 -0
  9. package/dist/alpha/cli/build.js +36 -0
  10. package/dist/alpha/cli/build.test.d.ts +2 -0
  11. package/dist/alpha/cli/build.test.d.ts.map +1 -0
  12. package/dist/alpha/cli/codegen.d.ts +10 -0
  13. package/dist/alpha/cli/codegen.d.ts.map +1 -0
  14. package/dist/alpha/cli/codegen.js +41 -0
  15. package/dist/alpha/cli/codegen.test.d.ts +2 -0
  16. package/dist/alpha/cli/codegen.test.d.ts.map +1 -0
  17. package/dist/alpha/cli/discover.d.ts +35 -0
  18. package/dist/alpha/cli/discover.d.ts.map +1 -0
  19. package/dist/alpha/cli/discover.js +39 -0
  20. package/dist/alpha/cli/discover.test.d.ts +2 -0
  21. package/dist/alpha/cli/discover.test.d.ts.map +1 -0
  22. package/dist/alpha/cli/emit-manifest.d.ts +33 -0
  23. package/dist/alpha/cli/emit-manifest.d.ts.map +1 -0
  24. package/dist/alpha/cli/emit-manifest.js +266 -0
  25. package/dist/alpha/cli/emit-manifest.test.d.ts +2 -0
  26. package/dist/alpha/cli/emit-manifest.test.d.ts.map +1 -0
  27. package/dist/alpha/cli/index.d.ts +3 -0
  28. package/dist/alpha/cli/index.d.ts.map +1 -0
  29. package/dist/alpha/cli/index.js +36 -0
  30. package/dist/alpha/context.d.ts +8 -0
  31. package/dist/alpha/context.d.ts.map +1 -0
  32. package/dist/alpha/context.js +33 -0
  33. package/dist/alpha/database.d.ts +90 -0
  34. package/dist/alpha/database.d.ts.map +1 -0
  35. package/dist/alpha/database.js +9 -0
  36. package/dist/alpha/error.d.ts +41 -0
  37. package/dist/alpha/error.d.ts.map +1 -0
  38. package/dist/alpha/error.js +25 -0
  39. package/dist/alpha/icon-names.d.ts +6 -0
  40. package/dist/alpha/icon-names.d.ts.map +1 -0
  41. package/dist/alpha/icon-names.js +0 -0
  42. package/dist/alpha/json-schema.d.ts +117 -0
  43. package/dist/alpha/json-schema.d.ts.map +1 -0
  44. package/dist/alpha/json-schema.js +0 -0
  45. package/dist/alpha/manifest.d.ts +44 -0
  46. package/dist/alpha/manifest.d.ts.map +1 -0
  47. package/dist/alpha/manifest.js +0 -0
  48. package/dist/alpha/output.d.ts +7 -0
  49. package/dist/alpha/output.d.ts.map +1 -0
  50. package/dist/alpha/output.js +8 -0
  51. package/dist/alpha/pacer.d.ts +74 -0
  52. package/dist/alpha/pacer.d.ts.map +1 -0
  53. package/dist/alpha/pacer.js +66 -0
  54. package/dist/alpha/pacer.test.d.ts +2 -0
  55. package/dist/alpha/pacer.test.d.ts.map +1 -0
  56. package/dist/alpha/schedule.d.ts +6 -0
  57. package/dist/alpha/schedule.d.ts.map +1 -0
  58. package/dist/alpha/schedule.js +43 -0
  59. package/dist/alpha/schedule.test.d.ts +2 -0
  60. package/dist/alpha/schedule.test.d.ts.map +1 -0
  61. package/dist/alpha/schema-builder.d.ts +79 -0
  62. package/dist/alpha/schema-builder.d.ts.map +1 -0
  63. package/dist/alpha/schema-builder.js +135 -0
  64. package/dist/alpha/schema.d.ts +185 -0
  65. package/dist/alpha/schema.d.ts.map +1 -0
  66. package/dist/alpha/schema.js +115 -0
  67. package/dist/alpha/sync.d.ts +233 -0
  68. package/dist/alpha/sync.d.ts.map +1 -0
  69. package/dist/alpha/sync.js +70 -0
  70. package/dist/alpha/sync.test.d.ts +2 -0
  71. package/dist/alpha/sync.test.d.ts.map +1 -0
  72. package/dist/alpha/tool.d.ts +132 -0
  73. package/dist/alpha/tool.d.ts.map +1 -0
  74. package/dist/alpha/tool.js +156 -0
  75. package/dist/alpha/tool.test.d.ts +2 -0
  76. package/dist/alpha/tool.test.d.ts.map +1 -0
  77. package/dist/alpha/triggers.d.ts +365 -0
  78. package/dist/alpha/triggers.d.ts.map +1 -0
  79. package/dist/alpha/triggers.generated.d.ts +246 -0
  80. package/dist/alpha/triggers.generated.d.ts.map +1 -0
  81. package/dist/alpha/triggers.generated.js +239 -0
  82. package/dist/alpha/triggers.js +0 -0
  83. package/dist/alpha/types.d.ts +228 -0
  84. package/dist/alpha/types.d.ts.map +1 -0
  85. package/dist/alpha/types.js +0 -0
  86. package/dist/alpha/workflow.d.ts +70 -0
  87. package/dist/alpha/workflow.d.ts.map +1 -0
  88. package/dist/alpha/workflow.js +39 -0
  89. package/dist/alpha/workflow.test.d.ts +2 -0
  90. package/dist/alpha/workflow.test.d.ts.map +1 -0
  91. package/dist/block.d.ts.map +1 -1
  92. package/dist/builder.d.ts.map +1 -1
  93. package/dist/builder.js +2 -6
  94. package/dist/capabilities/ai_connector.d.ts.map +1 -1
  95. package/dist/capabilities/automation.d.ts.map +1 -1
  96. package/dist/capabilities/context.d.ts.map +1 -1
  97. package/dist/capabilities/oauth.d.ts.map +1 -1
  98. package/dist/capabilities/output.d.ts.map +1 -1
  99. package/dist/capabilities/output.js +2 -4
  100. package/dist/capabilities/stateful-capability.d.ts.map +1 -1
  101. package/dist/capabilities/stateful-capability.js +2 -6
  102. package/dist/capabilities/sync.d.ts.map +1 -1
  103. package/dist/capabilities/sync.js +2 -8
  104. package/dist/capabilities/tool.d.ts.map +1 -1
  105. package/dist/capabilities/tool.js +6 -20
  106. package/dist/capabilities/webhook.d.ts.map +1 -1
  107. package/dist/capabilities/workflow.d.ts +2 -2
  108. package/dist/capabilities/workflow.d.ts.map +1 -1
  109. package/dist/index.d.ts +2 -2
  110. package/dist/index.d.ts.map +1 -1
  111. package/dist/index.js +1 -7
  112. package/dist/json-schema.d.ts.map +1 -1
  113. package/dist/schema-builder.d.ts.map +1 -1
  114. package/dist/schema.d.ts.map +1 -1
  115. package/dist/types.d.ts.map +1 -1
  116. package/dist/worker.d.ts.map +1 -1
  117. package/dist/worker.js +1 -4
  118. package/package.json +74 -14
  119. package/src/alpha/ajv-formats.d.ts +7 -0
  120. package/src/alpha/builder.test.ts +197 -0
  121. package/src/alpha/builder.ts +338 -0
  122. package/src/alpha/cli/build.test.ts +240 -0
  123. package/src/alpha/cli/build.ts +57 -0
  124. package/src/alpha/cli/codegen.test.ts +31 -0
  125. package/src/alpha/cli/codegen.ts +51 -0
  126. package/src/alpha/cli/discover.test.ts +43 -0
  127. package/src/alpha/cli/discover.ts +81 -0
  128. package/src/alpha/cli/emit-manifest.test.ts +45 -0
  129. package/src/alpha/cli/emit-manifest.ts +409 -0
  130. package/src/alpha/cli/index.ts +41 -0
  131. package/src/alpha/context.ts +61 -0
  132. package/src/alpha/database.ts +102 -0
  133. package/src/alpha/error.ts +49 -0
  134. package/src/alpha/icon-names.ts +890 -0
  135. package/src/alpha/json-schema.ts +180 -0
  136. package/src/alpha/manifest.ts +47 -0
  137. package/src/alpha/output.ts +8 -0
  138. package/src/alpha/pacer.test.ts +81 -0
  139. package/src/alpha/pacer.ts +181 -0
  140. package/src/alpha/schedule.test.ts +31 -0
  141. package/src/alpha/schedule.ts +53 -0
  142. package/src/alpha/schema-builder.ts +193 -0
  143. package/src/alpha/schema.ts +222 -0
  144. package/src/alpha/sync.test.ts +191 -0
  145. package/src/alpha/sync.ts +340 -0
  146. package/src/alpha/tool.test.ts +116 -0
  147. package/src/alpha/tool.ts +319 -0
  148. package/src/alpha/triggers.generated.ts +489 -0
  149. package/src/alpha/triggers.ts +500 -0
  150. package/src/alpha/types.ts +284 -0
  151. package/src/alpha/workflow.test.ts +84 -0
  152. package/src/alpha/workflow.ts +131 -0
  153. package/src/block.ts +1 -5
  154. package/src/builder.test.ts +28 -60
  155. package/src/builder.ts +3 -10
  156. package/src/capabilities/ai_connector.test.ts +9 -32
  157. package/src/capabilities/ai_connector.ts +5 -17
  158. package/src/capabilities/automation.test.ts +3 -12
  159. package/src/capabilities/automation.ts +3 -11
  160. package/src/capabilities/context.ts +3 -6
  161. package/src/capabilities/oauth.test.ts +1 -0
  162. package/src/capabilities/oauth.ts +1 -3
  163. package/src/capabilities/output.ts +1 -3
  164. package/src/capabilities/stateful-capability.test.ts +1 -0
  165. package/src/capabilities/stateful-capability.ts +2 -6
  166. package/src/capabilities/sync.test.ts +15 -45
  167. package/src/capabilities/sync.ts +10 -40
  168. package/src/capabilities/tool.test.ts +106 -146
  169. package/src/capabilities/tool.ts +17 -39
  170. package/src/capabilities/webhook.ts +3 -10
  171. package/src/capabilities/workflow.test.ts +10 -31
  172. package/src/capabilities/workflow.ts +5 -12
  173. package/src/index.ts +2 -12
  174. package/src/json-schema.test.ts +2 -4
  175. package/src/json-schema.ts +7 -6
  176. package/src/pacer.test.ts +1 -3
  177. package/src/pacer_internal.ts +1 -5
  178. package/src/schema-builder.test.ts +10 -35
  179. package/src/schema-builder.ts +5 -16
  180. package/src/schema.ts +2 -10
  181. package/src/types.ts +1 -7
  182. package/src/worker.ts +8 -34
@@ -0,0 +1,61 @@
1
+ import { Client } from "@notionhq/client";
2
+ import type { ClientOptions } from "@notionhq/client/build/src/Client.js";
3
+
4
+ import type { CapabilityType } from "./manifest.js";
5
+
6
+ export type CapabilityContext = {
7
+ /** Notion API SDK client for this execution. */
8
+ notion: Client;
9
+ };
10
+
11
+ const FIX_STEPS =
12
+ "\n\nTo fix this:\n" +
13
+ "1. Create a personal access token at https://app.notion.com/developers/tokens\n" +
14
+ "2. Add NOTION_API_TOKEN=<your-token> to your .env file\n" +
15
+ "3. For deployed workers, run: ntn workers env push";
16
+
17
+ function missingTokenMessage(capabilityType: CapabilityType): string {
18
+ if (capabilityType === "tool") {
19
+ return (
20
+ "NOTION_API_TOKEN is not set. " +
21
+ "When tools are called through a Custom Agent, the platform sets NOTION_API_TOKEN automatically, " +
22
+ "using the permissions of the Custom Agent. " +
23
+ "Outside of that context (e.g. ntn workers exec), you must set it yourself." +
24
+ FIX_STEPS
25
+ );
26
+ }
27
+
28
+ return (
29
+ "NOTION_API_TOKEN is not set. " +
30
+ `context.notion requires an API token to make requests in ${capabilityType} capabilities to Notion.` +
31
+ FIX_STEPS
32
+ );
33
+ }
34
+
35
+ function createUnauthenticatedNotionProxy(capabilityType: CapabilityType): Client {
36
+ return new Proxy({} as Client, {
37
+ get(_target, prop) {
38
+ if (prop === "then" || typeof prop === "symbol") {
39
+ return undefined;
40
+ }
41
+ throw new Error(missingTokenMessage(capabilityType));
42
+ },
43
+ });
44
+ }
45
+
46
+ export function createCapabilityContext(capabilityType: CapabilityType): CapabilityContext {
47
+ const options: ClientOptions = {};
48
+
49
+ if (process.env.NOTION_API_BASE_URL) {
50
+ options.baseUrl = process.env.NOTION_API_BASE_URL;
51
+ }
52
+
53
+ if (!process.env.NOTION_API_TOKEN) {
54
+ return { notion: createUnauthenticatedNotionProxy(capabilityType) };
55
+ }
56
+
57
+ options.auth = process.env.NOTION_API_TOKEN;
58
+ const notion = new Client(options);
59
+
60
+ return { notion };
61
+ }
@@ -0,0 +1,102 @@
1
+ import type { PropertySchema, Schema } from "./schema.js";
2
+
3
+ /**
4
+ * Configuration for a database that Notion creates and manages on behalf
5
+ * of the worker. The database is created on first deploy and its schema
6
+ * is migrated on subsequent deploys.
7
+ */
8
+ export type ManagedDatabaseConfig<S extends PropertySchema = PropertySchema> = {
9
+ type: "managed";
10
+ /**
11
+ * The title for the database when it is first created in Notion.
12
+ * Defaults to the database's name.
13
+ *
14
+ * Updating this after the database has been created will not change the
15
+ * title of the database — users may rename it freely.
16
+ */
17
+ initialTitle?: string;
18
+ /**
19
+ * The property that serves as the primary key for matching records.
20
+ * Must be a property defined in the schema. The value of this property
21
+ * should match the `key` field on each sync change.
22
+ */
23
+ primaryKey: Extract<keyof S, string>;
24
+ /**
25
+ * The database schema. Notion will migrate the database to match
26
+ * the given schema on worker deploy.
27
+ */
28
+ schema: Schema<S>;
29
+ };
30
+
31
+ /**
32
+ * Configuration union for all database types.
33
+ * Currently only `"managed"` is supported.
34
+ */
35
+ export type DatabaseConfig<S extends PropertySchema = PropertySchema> = ManagedDatabaseConfig<S>;
36
+
37
+ /**
38
+ * A database declaration as returned by {@link createDatabase}.
39
+ */
40
+ export type Database<S extends PropertySchema = PropertySchema> = {
41
+ _tag: "database";
42
+ name: string;
43
+ config: DatabaseConfig<S>;
44
+ };
45
+
46
+ /**
47
+ * Declare a database.
48
+ *
49
+ * A database can be declared anywhere — inline in a sync's config, in the
50
+ * sync's file, or in a shared module that several syncs import. The build
51
+ * discovers databases by walking capability configs: any database attached
52
+ * to a sync (or referenced by a `Schema.relation(...)`) is included in the
53
+ * manifest and created on deploy.
54
+ *
55
+ * The `name` is the database's stable identity across deploys — it is how
56
+ * the platform matches the declaration to the actual Notion database it
57
+ * created previously. Renaming it orphans that database, so treat it like a
58
+ * primary key: pick it once and leave it alone. Two distinct
59
+ * `createDatabase(...)` calls sharing a name fail the build; share one
60
+ * instance via import instead.
61
+ *
62
+ * @example
63
+ * ```ts
64
+ * // src/lib/databases.ts — shared by several syncs
65
+ * import { createDatabase } from "@notionhq/workers/alpha/database";
66
+ * import { Schema } from "@notionhq/workers/alpha/schema";
67
+ *
68
+ * export const projects = createDatabase("projects", {
69
+ * type: "managed",
70
+ * primaryKey: "Project ID",
71
+ * schema: {
72
+ * properties: {
73
+ * Name: Schema.title(),
74
+ * "Project ID": Schema.richText(),
75
+ * },
76
+ * },
77
+ * });
78
+ *
79
+ * export const tasks = createDatabase("tasks", {
80
+ * type: "managed",
81
+ * initialTitle: "Tasks",
82
+ * primaryKey: "Task ID",
83
+ * schema: {
84
+ * properties: {
85
+ * Title: Schema.title(),
86
+ * "Task ID": Schema.richText(),
87
+ * Project: Schema.relation(projects),
88
+ * },
89
+ * },
90
+ * });
91
+ * ```
92
+ */
93
+ export function createDatabase<S extends PropertySchema>(
94
+ name: string,
95
+ config: DatabaseConfig<S>,
96
+ ): Database<S> {
97
+ if (!name || typeof name !== "string") {
98
+ throw new Error("Database name must be a non-empty string");
99
+ }
100
+
101
+ return { _tag: "database", name, config };
102
+ }
@@ -0,0 +1,49 @@
1
+ /**
2
+ * An error that occurred during the execution of a worker capability.
3
+ */
4
+ export class ExecutionError extends Error {
5
+ override readonly cause: unknown;
6
+
7
+ constructor(cause: unknown) {
8
+ super(`Error during worker execution: ${cause}`);
9
+ this.name = "ExecutionError";
10
+ this.cause = cause;
11
+ }
12
+ }
13
+
14
+ /**
15
+ * Throw this error from your sync handler to signal that the external API
16
+ * returned a rate-limit response (e.g. HTTP 429).
17
+ *
18
+ * When the platform receives this error it applies exponential backoff
19
+ * instead of retrying immediately. If `retryAfter` is provided, the
20
+ * platform will wait at least that many seconds before the next attempt.
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * handler: async () => {
25
+ * const res = await fetch("https://api.example.com/issues");
26
+ * if (res.status === 429) {
27
+ * const retryAfter = Number(res.headers.get("Retry-After"));
28
+ * throw new RateLimitError({
29
+ * retryAfter: Number.isFinite(retryAfter) ? retryAfter : undefined,
30
+ * });
31
+ * }
32
+ * // ...
33
+ * },
34
+ * ```
35
+ */
36
+ export class RateLimitError extends ExecutionError {
37
+ /**
38
+ * Optional number of seconds the external API asked us to wait
39
+ * before retrying. The platform takes the max of this value and its
40
+ * own exponential-backoff delay.
41
+ */
42
+ readonly retryAfter: number | undefined;
43
+
44
+ constructor(options?: { retryAfter?: number }) {
45
+ super("Rate limited by external API");
46
+ this.name = "RateLimitError";
47
+ this.retryAfter = options?.retryAfter;
48
+ }
49
+ }