@notionhq/workers 0.4.0 → 0.6.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 (204) hide show
  1. package/dist/alpha/builder.d.ts +149 -0
  2. package/dist/alpha/builder.d.ts.map +1 -0
  3. package/dist/alpha/builder.js +260 -0
  4. package/dist/alpha/builder.test.d.ts +2 -0
  5. package/dist/alpha/builder.test.d.ts.map +1 -0
  6. package/dist/alpha/cli/build.d.ts +13 -0
  7. package/dist/alpha/cli/build.d.ts.map +1 -0
  8. package/dist/alpha/cli/build.js +36 -0
  9. package/dist/alpha/cli/build.test.d.ts +2 -0
  10. package/dist/alpha/cli/build.test.d.ts.map +1 -0
  11. package/dist/alpha/cli/codegen.d.ts +10 -0
  12. package/dist/alpha/cli/codegen.d.ts.map +1 -0
  13. package/dist/alpha/cli/codegen.js +41 -0
  14. package/dist/alpha/cli/codegen.test.d.ts +2 -0
  15. package/dist/alpha/cli/codegen.test.d.ts.map +1 -0
  16. package/dist/alpha/cli/discover.d.ts +35 -0
  17. package/dist/alpha/cli/discover.d.ts.map +1 -0
  18. package/dist/alpha/cli/discover.js +39 -0
  19. package/dist/alpha/cli/discover.test.d.ts +2 -0
  20. package/dist/alpha/cli/discover.test.d.ts.map +1 -0
  21. package/dist/alpha/cli/emit-manifest.d.ts +33 -0
  22. package/dist/alpha/cli/emit-manifest.d.ts.map +1 -0
  23. package/dist/alpha/cli/emit-manifest.js +266 -0
  24. package/dist/alpha/cli/emit-manifest.test.d.ts +2 -0
  25. package/dist/alpha/cli/emit-manifest.test.d.ts.map +1 -0
  26. package/dist/alpha/cli/index.d.ts +3 -0
  27. package/dist/alpha/cli/index.d.ts.map +1 -0
  28. package/dist/alpha/cli/index.js +36 -0
  29. package/dist/alpha/context.d.ts +8 -0
  30. package/dist/alpha/context.d.ts.map +1 -0
  31. package/dist/alpha/context.js +33 -0
  32. package/dist/alpha/database.d.ts +90 -0
  33. package/dist/alpha/database.d.ts.map +1 -0
  34. package/dist/alpha/database.js +9 -0
  35. package/dist/alpha/error.d.ts +41 -0
  36. package/dist/alpha/error.d.ts.map +1 -0
  37. package/dist/alpha/error.js +25 -0
  38. package/dist/alpha/icon-names.d.ts +6 -0
  39. package/dist/alpha/icon-names.d.ts.map +1 -0
  40. package/dist/alpha/icon-names.js +0 -0
  41. package/dist/alpha/json-schema.d.ts +117 -0
  42. package/dist/alpha/json-schema.d.ts.map +1 -0
  43. package/dist/alpha/json-schema.js +0 -0
  44. package/dist/alpha/manifest.d.ts +44 -0
  45. package/dist/alpha/manifest.d.ts.map +1 -0
  46. package/dist/alpha/manifest.js +0 -0
  47. package/dist/alpha/output.d.ts +7 -0
  48. package/dist/alpha/output.d.ts.map +1 -0
  49. package/dist/alpha/output.js +8 -0
  50. package/dist/alpha/pacer.d.ts +74 -0
  51. package/dist/alpha/pacer.d.ts.map +1 -0
  52. package/dist/alpha/pacer.js +66 -0
  53. package/dist/alpha/pacer.test.d.ts +2 -0
  54. package/dist/alpha/pacer.test.d.ts.map +1 -0
  55. package/dist/alpha/schedule.d.ts +6 -0
  56. package/dist/alpha/schedule.d.ts.map +1 -0
  57. package/dist/alpha/schedule.js +43 -0
  58. package/dist/alpha/schedule.test.d.ts +2 -0
  59. package/dist/alpha/schedule.test.d.ts.map +1 -0
  60. package/dist/alpha/schema-builder.d.ts +79 -0
  61. package/dist/alpha/schema-builder.d.ts.map +1 -0
  62. package/dist/alpha/schema-builder.js +135 -0
  63. package/dist/alpha/schema.d.ts +185 -0
  64. package/dist/alpha/schema.d.ts.map +1 -0
  65. package/dist/alpha/schema.js +115 -0
  66. package/dist/alpha/sync.d.ts +233 -0
  67. package/dist/alpha/sync.d.ts.map +1 -0
  68. package/dist/alpha/sync.js +70 -0
  69. package/dist/alpha/sync.test.d.ts +2 -0
  70. package/dist/alpha/sync.test.d.ts.map +1 -0
  71. package/dist/alpha/tool.d.ts +132 -0
  72. package/dist/alpha/tool.d.ts.map +1 -0
  73. package/dist/alpha/tool.js +156 -0
  74. package/dist/alpha/tool.test.d.ts +2 -0
  75. package/dist/alpha/tool.test.d.ts.map +1 -0
  76. package/dist/alpha/triggers.d.ts +365 -0
  77. package/dist/alpha/triggers.d.ts.map +1 -0
  78. package/dist/alpha/triggers.generated.d.ts +246 -0
  79. package/dist/alpha/triggers.generated.d.ts.map +1 -0
  80. package/dist/alpha/triggers.generated.js +239 -0
  81. package/dist/alpha/triggers.js +0 -0
  82. package/dist/alpha/types.d.ts +228 -0
  83. package/dist/alpha/types.d.ts.map +1 -0
  84. package/dist/alpha/types.js +0 -0
  85. package/dist/alpha/workflow.d.ts +70 -0
  86. package/dist/alpha/workflow.d.ts.map +1 -0
  87. package/dist/alpha/workflow.js +39 -0
  88. package/dist/alpha/workflow.test.d.ts +2 -0
  89. package/dist/alpha/workflow.test.d.ts.map +1 -0
  90. package/dist/builder.d.ts +7 -1
  91. package/dist/builder.d.ts.map +1 -1
  92. package/dist/builder.js +12 -3
  93. package/dist/capabilities/ai_connector.d.ts.map +1 -1
  94. package/dist/capabilities/automation.d.ts.map +1 -1
  95. package/dist/capabilities/context.d.ts.map +1 -1
  96. package/dist/capabilities/context.js +1 -1
  97. package/dist/capabilities/custom-block.d.ts +124 -0
  98. package/dist/capabilities/custom-block.d.ts.map +1 -0
  99. package/dist/capabilities/custom-block.js +22 -0
  100. package/dist/capabilities/oauth.d.ts.map +1 -1
  101. package/dist/capabilities/output.d.ts.map +1 -1
  102. package/dist/capabilities/output.js +2 -4
  103. package/dist/capabilities/stateful-capability.d.ts.map +1 -1
  104. package/dist/capabilities/stateful-capability.js +3 -7
  105. package/dist/capabilities/sync.d.ts +11 -1
  106. package/dist/capabilities/sync.d.ts.map +1 -1
  107. package/dist/capabilities/sync.js +2 -8
  108. package/dist/capabilities/tool.d.ts.map +1 -1
  109. package/dist/capabilities/tool.js +6 -20
  110. package/dist/capabilities/webhook.d.ts.map +1 -1
  111. package/dist/capabilities/workflow.d.ts +50 -0
  112. package/dist/capabilities/workflow.d.ts.map +1 -0
  113. package/dist/capabilities/workflow.js +40 -0
  114. package/dist/capabilities/workflow.test.d.ts +2 -0
  115. package/dist/capabilities/workflow.test.d.ts.map +1 -0
  116. package/dist/index.d.ts +5 -3
  117. package/dist/index.d.ts.map +1 -1
  118. package/dist/index.js +2 -1
  119. package/dist/json-schema.d.ts.map +1 -1
  120. package/dist/schema-builder.d.ts.map +1 -1
  121. package/dist/schema.d.ts.map +1 -1
  122. package/dist/triggers.d.ts +365 -0
  123. package/dist/triggers.d.ts.map +1 -0
  124. package/dist/triggers.generated.d.ts +246 -0
  125. package/dist/triggers.generated.d.ts.map +1 -0
  126. package/dist/triggers.generated.js +239 -0
  127. package/dist/triggers.js +0 -0
  128. package/dist/types.d.ts +18 -0
  129. package/dist/types.d.ts.map +1 -1
  130. package/dist/worker.d.ts +72 -2
  131. package/dist/worker.d.ts.map +1 -1
  132. package/dist/worker.js +85 -4
  133. package/package.json +81 -14
  134. package/src/alpha/ajv-formats.d.ts +7 -0
  135. package/src/alpha/builder.test.ts +197 -0
  136. package/src/alpha/builder.ts +338 -0
  137. package/src/alpha/cli/build.test.ts +240 -0
  138. package/src/alpha/cli/build.ts +57 -0
  139. package/src/alpha/cli/codegen.test.ts +31 -0
  140. package/src/alpha/cli/codegen.ts +51 -0
  141. package/src/alpha/cli/discover.test.ts +43 -0
  142. package/src/alpha/cli/discover.ts +81 -0
  143. package/src/alpha/cli/emit-manifest.test.ts +45 -0
  144. package/src/alpha/cli/emit-manifest.ts +409 -0
  145. package/src/alpha/cli/index.ts +41 -0
  146. package/src/alpha/context.ts +61 -0
  147. package/src/alpha/database.ts +102 -0
  148. package/src/alpha/error.ts +49 -0
  149. package/src/alpha/icon-names.ts +890 -0
  150. package/src/alpha/json-schema.ts +180 -0
  151. package/src/alpha/manifest.ts +47 -0
  152. package/src/alpha/output.ts +8 -0
  153. package/src/alpha/pacer.test.ts +81 -0
  154. package/src/alpha/pacer.ts +181 -0
  155. package/src/alpha/schedule.test.ts +31 -0
  156. package/src/alpha/schedule.ts +53 -0
  157. package/src/alpha/schema-builder.ts +193 -0
  158. package/src/alpha/schema.ts +222 -0
  159. package/src/alpha/sync.test.ts +191 -0
  160. package/src/alpha/sync.ts +340 -0
  161. package/src/alpha/tool.test.ts +116 -0
  162. package/src/alpha/tool.ts +319 -0
  163. package/src/alpha/triggers.generated.ts +489 -0
  164. package/src/alpha/triggers.ts +500 -0
  165. package/src/alpha/types.ts +284 -0
  166. package/src/alpha/workflow.test.ts +84 -0
  167. package/src/alpha/workflow.ts +131 -0
  168. package/src/builder.test.ts +28 -60
  169. package/src/builder.ts +20 -7
  170. package/src/capabilities/ai_connector.test.ts +9 -32
  171. package/src/capabilities/ai_connector.ts +5 -17
  172. package/src/capabilities/automation.test.ts +3 -12
  173. package/src/capabilities/automation.ts +3 -11
  174. package/src/capabilities/context.ts +5 -8
  175. package/src/capabilities/custom-block.ts +181 -0
  176. package/src/capabilities/oauth.test.ts +1 -0
  177. package/src/capabilities/oauth.ts +1 -3
  178. package/src/capabilities/output.ts +1 -3
  179. package/src/capabilities/stateful-capability.test.ts +1 -0
  180. package/src/capabilities/stateful-capability.ts +3 -7
  181. package/src/capabilities/sync.test.ts +76 -42
  182. package/src/capabilities/sync.ts +16 -40
  183. package/src/capabilities/tool.test.ts +106 -146
  184. package/src/capabilities/tool.ts +17 -39
  185. package/src/capabilities/webhook.ts +3 -10
  186. package/src/capabilities/workflow.test.ts +163 -0
  187. package/src/capabilities/workflow.ts +112 -0
  188. package/src/index.ts +18 -6
  189. package/src/json-schema.test.ts +2 -4
  190. package/src/json-schema.ts +7 -6
  191. package/src/pacer.test.ts +1 -3
  192. package/src/pacer_internal.ts +1 -5
  193. package/src/schema-builder.test.ts +10 -35
  194. package/src/schema-builder.ts +6 -16
  195. package/src/schema.ts +2 -10
  196. package/src/triggers.generated.ts +489 -0
  197. package/src/triggers.ts +504 -0
  198. package/src/types.ts +21 -7
  199. package/src/worker.test.ts +147 -0
  200. package/src/worker.ts +155 -32
  201. package/dist/block.d.ts +0 -321
  202. package/dist/block.d.ts.map +0 -1
  203. package/src/block.ts +0 -529
  204. /package/dist/{block.js → alpha/ajv-formats.d.js} +0 -0
@@ -0,0 +1,185 @@
1
+ import type { Database } from "./database.js";
2
+ import type { DateFormat, Icon, NumberFormat, SelectOption, StatusGroup } from "./types.js";
3
+ /**
4
+ * A schema defining the structure of a single property in a data source.
5
+ */
6
+ export type PropertyConfiguration = {
7
+ type: "title";
8
+ } | {
9
+ type: "text";
10
+ } | {
11
+ type: "url";
12
+ } | {
13
+ type: "email";
14
+ } | {
15
+ type: "phone_number";
16
+ } | {
17
+ type: "checkbox";
18
+ } | {
19
+ type: "file";
20
+ } | {
21
+ type: "number";
22
+ format?: NumberFormat;
23
+ } | {
24
+ type: "date";
25
+ date_format?: DateFormat;
26
+ } | {
27
+ type: "select";
28
+ options: SelectOption[];
29
+ } | {
30
+ type: "multi_select";
31
+ options: SelectOption[];
32
+ } | {
33
+ type: "status";
34
+ groups: StatusGroup[];
35
+ } | {
36
+ type: "people";
37
+ } | {
38
+ type: "place";
39
+ } | {
40
+ type: "relation";
41
+ /**
42
+ * The related database, referenced by identity. It must be declared
43
+ * under `src/databases/`; the build resolves the reference to the
44
+ * database's key when emitting the manifest.
45
+ */
46
+ database: Database;
47
+ config: {
48
+ twoWay: false;
49
+ } | {
50
+ twoWay: true;
51
+ relatedPropertyName: string;
52
+ };
53
+ };
54
+ /**
55
+ * The property definitions for a database, keyed by property name.
56
+ */
57
+ export type PropertySchema = Record<string, PropertyConfiguration>;
58
+ /**
59
+ * A database schema.
60
+ */
61
+ export type Schema<S extends PropertySchema = PropertySchema> = {
62
+ /**
63
+ * Optional icon to use as the icon for the database page.
64
+ * If not provided, defaults to 📋.
65
+ */
66
+ databaseIcon?: Icon;
67
+ properties: S;
68
+ /**
69
+ * Optional configuration for sub-item relations.
70
+ * If provided, the parent and child properties must be part of a two-way self-relation.
71
+ */
72
+ subItems?: {
73
+ parentPropertyName: string;
74
+ childPropertyName: string;
75
+ };
76
+ };
77
+ /**
78
+ * Builders for property definitions in a database schema.
79
+ *
80
+ * @example
81
+ * ```ts
82
+ * import { Schema } from "@notionhq/workers/alpha/schema";
83
+ *
84
+ * const schema = {
85
+ * properties: {
86
+ * "Task Name": Schema.title(),
87
+ * "Task ID": Schema.richText(),
88
+ * Status: Schema.select([
89
+ * { name: "Open", color: "default" },
90
+ * { name: "Done", color: "green" },
91
+ * ]),
92
+ * },
93
+ * };
94
+ * ```
95
+ */
96
+ export declare const Schema: {
97
+ /**
98
+ * Creates a title property definition.
99
+ */
100
+ title(): PropertyConfiguration;
101
+ /**
102
+ * Creates a rich text property definition.
103
+ */
104
+ richText(): PropertyConfiguration;
105
+ /**
106
+ * Creates a URL property definition.
107
+ */
108
+ url(): PropertyConfiguration;
109
+ /**
110
+ * Creates an email property definition.
111
+ */
112
+ email(): PropertyConfiguration;
113
+ /**
114
+ * Creates a phone number property definition.
115
+ */
116
+ phoneNumber(): PropertyConfiguration;
117
+ /**
118
+ * Creates a checkbox property definition.
119
+ */
120
+ checkbox(): PropertyConfiguration;
121
+ /**
122
+ * Creates a file property definition.
123
+ */
124
+ file(): PropertyConfiguration;
125
+ /**
126
+ * Creates a number property definition with optional formatting.
127
+ */
128
+ number(format?: NumberFormat): PropertyConfiguration;
129
+ /**
130
+ * Creates a date property definition with optional formatting.
131
+ */
132
+ date(date_format?: DateFormat): PropertyConfiguration;
133
+ /**
134
+ * Creates a select property definition with predefined options.
135
+ */
136
+ select(options: SelectOption[]): PropertyConfiguration;
137
+ /**
138
+ * Creates a multi-select property definition with predefined options.
139
+ */
140
+ multiSelect(options: SelectOption[]): PropertyConfiguration;
141
+ /**
142
+ * Creates a status property definition with groups.
143
+ */
144
+ status(config: {
145
+ groups: StatusGroup[];
146
+ }): PropertyConfiguration;
147
+ /**
148
+ * Creates a people property definition.
149
+ */
150
+ people(): PropertyConfiguration;
151
+ /**
152
+ * Creates a place property definition for storing geographic locations.
153
+ */
154
+ place(): PropertyConfiguration;
155
+ /**
156
+ * Creates a relation property definition that references another database.
157
+ * The related database must be declared under `src/databases/` and is
158
+ * referenced by importing it:
159
+ *
160
+ * @example
161
+ * ```ts
162
+ * // src/databases/tasks.ts
163
+ * import projects from "./projects.js";
164
+ *
165
+ * export default createDatabase({
166
+ * type: "managed",
167
+ * primaryKey: "Task ID",
168
+ * schema: {
169
+ * properties: {
170
+ * Title: Schema.title(),
171
+ * "Task ID": Schema.richText(),
172
+ * Project: Schema.relation(projects),
173
+ * },
174
+ * },
175
+ * });
176
+ * ```
177
+ */
178
+ relation(database: Database<any>, config?: {
179
+ twoWay: false;
180
+ } | {
181
+ twoWay: true;
182
+ relatedPropertyName: string;
183
+ }): PropertyConfiguration;
184
+ };
185
+ //# sourceMappingURL=schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/alpha/schema.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAC9C,OAAO,KAAK,EAAE,UAAU,EAAE,IAAI,EAAE,YAAY,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE5F;;GAEG;AACH,MAAM,MAAM,qBAAqB,GAC9B;IAAE,IAAI,EAAE,OAAO,CAAA;CAAE,GACjB;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,GAChB;IAAE,IAAI,EAAE,KAAK,CAAA;CAAE,GACf;IAAE,IAAI,EAAE,OAAO,CAAA;CAAE,GACjB;IAAE,IAAI,EAAE,cAAc,CAAA;CAAE,GACxB;IAAE,IAAI,EAAE,UAAU,CAAA;CAAE,GACpB;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,GAChB;IACA,IAAI,EAAE,QAAQ,CAAC;IACf,MAAM,CAAC,EAAE,YAAY,CAAC;CACrB,GACD;IACA,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,CAAC,EAAE,UAAU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G;uBACgB,UAAU,GAAG,qBAAqB;IAIrD;;OAEG;oBACa,YAAY,EAAE,GAAG,qBAAqB;IAItD;;OAEG;yBACkB,YAAY,EAAE,GAAG,qBAAqB;IAI3D;;OAEG;mBACY;QAAE,MAAM,EAAE,WAAW,EAAE,CAAA;KAAE,GAAG,qBAAqB;IAIhE;;OAEG;cACO,qBAAqB;IAI/B;;OAEG;aACM,qBAAqB;IAI9B;;;;;;;;;;;;;;;;;;;;;;OAsBG;uBAIQ,QAAQ,CAAC,GAAG,CAAC,WACd;QAAE,MAAM,EAAE,KAAK,CAAA;KAAE,GAAG;QAAE,MAAM,EAAE,IAAI,CAAC;QAAC,mBAAmB,EAAE,MAAM,CAAA;KAAE,GACxE,qBAAqB;CAGxB,CAAC"}
@@ -0,0 +1,115 @@
1
+ const Schema = {
2
+ /**
3
+ * Creates a title property definition.
4
+ */
5
+ title() {
6
+ return { type: "title" };
7
+ },
8
+ /**
9
+ * Creates a rich text property definition.
10
+ */
11
+ richText() {
12
+ return { type: "text" };
13
+ },
14
+ /**
15
+ * Creates a URL property definition.
16
+ */
17
+ url() {
18
+ return { type: "url" };
19
+ },
20
+ /**
21
+ * Creates an email property definition.
22
+ */
23
+ email() {
24
+ return { type: "email" };
25
+ },
26
+ /**
27
+ * Creates a phone number property definition.
28
+ */
29
+ phoneNumber() {
30
+ return { type: "phone_number" };
31
+ },
32
+ /**
33
+ * Creates a checkbox property definition.
34
+ */
35
+ checkbox() {
36
+ return { type: "checkbox" };
37
+ },
38
+ /**
39
+ * Creates a file property definition.
40
+ */
41
+ file() {
42
+ return { type: "file" };
43
+ },
44
+ /**
45
+ * Creates a number property definition with optional formatting.
46
+ */
47
+ number(format) {
48
+ return format ? { type: "number", format } : { type: "number" };
49
+ },
50
+ /**
51
+ * Creates a date property definition with optional formatting.
52
+ */
53
+ date(date_format) {
54
+ return date_format ? { type: "date", date_format } : { type: "date" };
55
+ },
56
+ /**
57
+ * Creates a select property definition with predefined options.
58
+ */
59
+ select(options) {
60
+ return { type: "select", options };
61
+ },
62
+ /**
63
+ * Creates a multi-select property definition with predefined options.
64
+ */
65
+ multiSelect(options) {
66
+ return { type: "multi_select", options };
67
+ },
68
+ /**
69
+ * Creates a status property definition with groups.
70
+ */
71
+ status(config) {
72
+ return { type: "status", groups: config.groups };
73
+ },
74
+ /**
75
+ * Creates a people property definition.
76
+ */
77
+ people() {
78
+ return { type: "people" };
79
+ },
80
+ /**
81
+ * Creates a place property definition for storing geographic locations.
82
+ */
83
+ place() {
84
+ return { type: "place" };
85
+ },
86
+ /**
87
+ * Creates a relation property definition that references another database.
88
+ * The related database must be declared under `src/databases/` and is
89
+ * referenced by importing it:
90
+ *
91
+ * @example
92
+ * ```ts
93
+ * // src/databases/tasks.ts
94
+ * import projects from "./projects.js";
95
+ *
96
+ * export default createDatabase({
97
+ * type: "managed",
98
+ * primaryKey: "Task ID",
99
+ * schema: {
100
+ * properties: {
101
+ * Title: Schema.title(),
102
+ * "Task ID": Schema.richText(),
103
+ * Project: Schema.relation(projects),
104
+ * },
105
+ * },
106
+ * });
107
+ * ```
108
+ */
109
+ relation(database, config) {
110
+ return { type: "relation", database, config: config ?? { twoWay: false } };
111
+ }
112
+ };
113
+ export {
114
+ Schema
115
+ };
@@ -0,0 +1,233 @@
1
+ import type { CapabilityContext } from "./context.js";
2
+ import type { Database } from "./database.js";
3
+ import { type PacerEntry } from "./pacer.js";
4
+ import type { PropertyConfiguration, PropertySchema, Schema } from "./schema.js";
5
+ import type { Cover, HandlerOptions, Icon, NormalizedSchedule, PeopleValue, PlaceValue, RelationValue, Schedule, TextValue } from "./types.js";
6
+ /**
7
+ * Maps a property configuration to its corresponding value type.
8
+ */
9
+ type PropertyValueType<T extends PropertyConfiguration> = T extends {
10
+ type: "people";
11
+ } ? PeopleValue : T extends {
12
+ type: "place";
13
+ } ? PlaceValue : T extends {
14
+ type: "relation";
15
+ } ? RelationValue : TextValue;
16
+ /**
17
+ * Sync mode determines how the sync handles data lifecycle.
18
+ */
19
+ export type SyncMode = "replace" | "incremental";
20
+ /**
21
+ * A change representing a record to be created or updated.
22
+ */
23
+ export type SyncChangeUpsert<S extends PropertySchema = PropertySchema> = {
24
+ /** The type of change. Use `"upsert"` to create or update a record. */
25
+ type: "upsert";
26
+ /** A unique identifier for this record, used to match against existing pages. */
27
+ key: string;
28
+ /**
29
+ * The property values for this record.
30
+ * Keys must match the property names defined in the schema.
31
+ * Use the Builder helpers (e.g. `Builder.title()`, `Builder.richText()`)
32
+ * to create values.
33
+ */
34
+ properties: {
35
+ [Property in keyof S]: PropertyValueType<S[Property]>;
36
+ };
37
+ /**
38
+ * When this record was last updated in the upstream system.
39
+ * ISO 8601 string (e.g. `"2026-03-19T12:00:00Z"`).
40
+ * Used for conflict resolution when multiple syncs write to the same
41
+ * database. Recommended, but optional.
42
+ */
43
+ upstreamUpdatedAt?: string;
44
+ /**
45
+ * Optional icon to use as the icon for this row's page.
46
+ * Use the `Builder.emojiIcon()`, `Builder.notionIcon()`, or
47
+ * `Builder.imageIcon()` helpers.
48
+ */
49
+ icon?: Icon;
50
+ /**
51
+ * Optional cover to use as the cover for this row's page.
52
+ * Use the `Builder.imageCover()` helper.
53
+ */
54
+ cover?: Cover;
55
+ /**
56
+ * Optional markdown content to add to the page body.
57
+ * This will be converted to Notion blocks and added as page content.
58
+ */
59
+ pageContentMarkdown?: string;
60
+ };
61
+ /**
62
+ * A change representing a record to be deleted.
63
+ * Only applicable when using `mode: "incremental"`.
64
+ */
65
+ export type SyncChangeDelete = {
66
+ /** The type of change. Use `"delete"` to remove a record. */
67
+ type: "delete";
68
+ /** The unique identifier of the record to delete. */
69
+ key: string;
70
+ };
71
+ /**
72
+ * A change to be applied to the synced database.
73
+ */
74
+ export type SyncChange<S extends PropertySchema = PropertySchema> = SyncChangeUpsert<S> | SyncChangeDelete;
75
+ /**
76
+ * Result returned from a sync handler.
77
+ */
78
+ export type SyncHandlerResult<S extends PropertySchema = PropertySchema, State = unknown> = {
79
+ /** The batch of changes to apply in this execution. */
80
+ changes: SyncChange<S>[];
81
+ /**
82
+ * Indicates whether there is more data to fetch. When `true`, the
83
+ * handler is called again with `nextState`.
84
+ *
85
+ * @default false
86
+ */
87
+ hasMore?: boolean;
88
+ /**
89
+ * Optional state data to pass to the next execution.
90
+ * Required if `hasMore` is `true`, ignored otherwise.
91
+ * This can be any type of data (cursor, page number, timestamp, etc.);
92
+ * the same data is provided as `state` in the next execution.
93
+ */
94
+ nextState?: State;
95
+ };
96
+ /**
97
+ * Configuration passed to {@link createSync}.
98
+ */
99
+ export type SyncConfiguration<S extends PropertySchema = PropertySchema, State = unknown> = {
100
+ /**
101
+ * The database to sync data into, declared with `createDatabase(...)`.
102
+ * Declare it inline for a database private to this sync, or import a
103
+ * shared instance that other syncs (and `Schema.relation(...)`) also
104
+ * reference.
105
+ */
106
+ database: Database<S>;
107
+ /**
108
+ * How the sync handles data lifecycle:
109
+ * - "replace": Each run returns the complete dataset; unseen pages are deleted.
110
+ * - "incremental": Each run returns changes only; use delete markers to remove pages.
111
+ *
112
+ * @default "replace"
113
+ */
114
+ mode?: SyncMode;
115
+ /**
116
+ * How often the sync should run.
117
+ * - "continuous": Run as frequently as the system allows
118
+ * - "manual": Only run when explicitly triggered
119
+ * - Interval string: Run at specified intervals, e.g. "1h", "30m", "1d"
120
+ *
121
+ * @default "30m"
122
+ */
123
+ schedule?: Schedule;
124
+ /**
125
+ * Fetches the data to sync from the third-party service.
126
+ *
127
+ * This function can return all data at once, or implement pagination by
128
+ * returning a batch of changes with `hasMore: true` and a `nextState`;
129
+ * the runtime calls it again with that state until `hasMore` is `false`.
130
+ *
131
+ * @param state - User-defined state from the previous execution (undefined on first call)
132
+ * @param context - Runtime context, including the Notion client
133
+ */
134
+ handler: (state: State | undefined, context: CapabilityContext) => Promise<SyncHandlerResult<S, State>>;
135
+ };
136
+ /**
137
+ * The pre-manifest configuration of a sync: everything from
138
+ * {@link SyncConfiguration} except the handler, with the schedule
139
+ * normalized for the backend and the database's schema type erased.
140
+ *
141
+ * The `database` here is still an object reference; the build resolves it
142
+ * to the database's *name* when emitting the manifest.
143
+ */
144
+ export type SyncManifestConfig = {
145
+ database: {
146
+ _tag: "database";
147
+ name: string;
148
+ config: {
149
+ type: "managed";
150
+ initialTitle?: string;
151
+ primaryKey: string;
152
+ schema: Schema;
153
+ };
154
+ };
155
+ mode: SyncMode | undefined;
156
+ schedule: NormalizedSchedule | undefined;
157
+ };
158
+ /**
159
+ * The runtime context the platform passes when invoking a sync.
160
+ */
161
+ export type SyncRuntimeContext = {
162
+ /** The user-defined state (cursor, pagination state, etc.). */
163
+ state?: unknown;
164
+ /** Legacy field for user-defined state. */
165
+ userContext?: unknown;
166
+ /** Pacer state from the server for rate limiting. */
167
+ pacers?: Record<string, PacerEntry>;
168
+ };
169
+ /**
170
+ * The result a sync run reports to the platform, via the notion_output
171
+ * envelope on stdout (or directly, under `concreteOutput`). This is the
172
+ * same wire shape v1 syncs produce.
173
+ */
174
+ export type SyncRunResult = {
175
+ changes: (SyncChange & {
176
+ targetDatabaseKey: string;
177
+ })[];
178
+ hasMore: boolean;
179
+ nextUserContext?: unknown;
180
+ nextPacerStates: Record<string, PacerEntry>;
181
+ };
182
+ /**
183
+ * A sync capability as returned by {@link createSync}.
184
+ *
185
+ * Note that a sync carries no key of its own — the build tool derives the
186
+ * capability key from the file the sync is default-exported from.
187
+ */
188
+ export type Sync = {
189
+ _tag: "sync";
190
+ config: SyncManifestConfig;
191
+ handler: (runtimeContext?: SyncRuntimeContext, options?: HandlerOptions) => Promise<SyncRunResult>;
192
+ };
193
+ /**
194
+ * Create a sync capability.
195
+ *
196
+ * Default-export the result from a file directly under `src/syncs/` —
197
+ * the file's basename becomes the sync's key.
198
+ *
199
+ * @example
200
+ * ```ts
201
+ * // src/syncs/tasks.ts
202
+ * import { createDatabase } from "@notionhq/workers/alpha/database";
203
+ * import { Schema } from "@notionhq/workers/alpha/schema";
204
+ * import { createSync } from "@notionhq/workers/alpha/sync";
205
+ *
206
+ * export default createSync({
207
+ * database: createDatabase("tasks", {
208
+ * type: "managed",
209
+ * primaryKey: "Task ID",
210
+ * schema: {
211
+ * properties: {
212
+ * Title: Schema.title(),
213
+ * "Task ID": Schema.richText(),
214
+ * },
215
+ * },
216
+ * }),
217
+ * mode: "replace",
218
+ * schedule: "24h",
219
+ * handler: async () => ({
220
+ * changes: [
221
+ * {
222
+ * type: "upsert",
223
+ * key: "1",
224
+ * properties: { Title: [["Item 1"]], "Task ID": [["1"]] },
225
+ * },
226
+ * ],
227
+ * }),
228
+ * });
229
+ * ```
230
+ */
231
+ export declare function createSync<S extends PropertySchema, State = unknown>(configuration: SyncConfiguration<S, State>): Sync;
232
+ export {};
233
+ //# sourceMappingURL=sync.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sync.d.ts","sourceRoot":"","sources":["../../src/alpha/sync.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAEtD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAG9C,OAAO,EAA+B,KAAK,UAAU,EAAE,MAAM,YAAY,CAAC;AAE1E,OAAO,KAAK,EAAE,qBAAqB,EAAE,cAAc,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACjF,OAAO,KAAK,EACX,KAAK,EACL,cAAc,EACd,IAAI,EACJ,kBAAkB,EAClB,WAAW,EACX,UAAU,EACV,aAAa,EACb,QAAQ,EACR,SAAS,EACT,MAAM,YAAY,CAAC;AAEpB;;GAEG;AACH,KAAK,iBAAiB,CAAC,CAAC,SAAS,qBAAqB,IAAI,CAAC,SAAS;IACnE,IAAI,EAAE,QAAQ,CAAC;CACf,GACE,WAAW,GACX,CAAC,SAAS;IAAE,IAAI,EAAE,OAAO,CAAA;CAAE,GAC1B,UAAU,GACV,CAAC,SAAS;IAAE,IAAI,EAAE,UAAU,CAAA;CAAE,GAC7B,aAAa,GACb,SAAS,CAAC;AAEf;;GAE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EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;CAC5C,CAAC;AASF;;;;;GAKG;AACH,MAAM,MAAM,IAAI,GAAG;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,kBAAkB,CAAC;IAC3B,OAAO,EAAE,CACR,cAAc,CAAC,EAAE,kBAAkB,EACnC,OAAO,CAAC,EAAE,cAAc,KACpB,OAAO,CAAC,aAAa,CAAC,CAAC;CAC5B,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,wBAAgB,UAAU,CAAC,CAAC,SAAS,cAAc,EAAE,KAAK,GAAG,OAAO,EACnE,aAAa,EAAE,iBAAiB,CAAC,CAAC,EAAE,KAAK,CAAC,GACxC,IAAI,CAsEN"}
@@ -0,0 +1,70 @@
1
+ import { createCapabilityContext } from "./context.js";
2
+ import { ExecutionError, RateLimitError } from "./error.js";
3
+ import { writeOutput } from "./output.js";
4
+ import { initPacerState, pacerStates } from "./pacer.js";
5
+ import { parseSchedule } from "./schedule.js";
6
+ function createSync(configuration) {
7
+ return {
8
+ _tag: "sync",
9
+ config: {
10
+ database: configuration.database,
11
+ mode: configuration.mode,
12
+ schedule: configuration.schedule ? parseSchedule(configuration.schedule) : void 0
13
+ },
14
+ async handler(runtimeContext, options) {
15
+ const state = runtimeContext?.state ?? runtimeContext?.userContext;
16
+ initPacerState(runtimeContext?.pacers);
17
+ let handlerResult;
18
+ try {
19
+ handlerResult = await configuration.handler(state, createCapabilityContext("sync"));
20
+ } catch (err) {
21
+ if (err instanceof RateLimitError) {
22
+ if (!options?.concreteOutput) {
23
+ const envelope = {
24
+ _tag: "error",
25
+ error: {
26
+ _tag: "rate_limit",
27
+ name: err.name,
28
+ message: err.message,
29
+ ...err.retryAfter !== void 0 ? { retryAfter: err.retryAfter } : {}
30
+ }
31
+ };
32
+ writeOutput(envelope);
33
+ }
34
+ throw err;
35
+ }
36
+ const error = new ExecutionError(err);
37
+ if (!options?.concreteOutput) {
38
+ const envelope = {
39
+ _tag: "error",
40
+ error: {
41
+ _tag: "generic",
42
+ name: error.name,
43
+ message: error.message
44
+ }
45
+ };
46
+ writeOutput(envelope);
47
+ }
48
+ throw error;
49
+ }
50
+ const changes = handlerResult.changes.map((change) => ({
51
+ ...change,
52
+ targetDatabaseKey: configuration.database.name
53
+ }));
54
+ const result = {
55
+ changes,
56
+ hasMore: handlerResult.hasMore ?? false,
57
+ nextUserContext: handlerResult.nextState,
58
+ nextPacerStates: pacerStates()
59
+ };
60
+ if (options?.concreteOutput) {
61
+ return result;
62
+ }
63
+ writeOutput({ _tag: "success", value: result });
64
+ return result;
65
+ }
66
+ };
67
+ }
68
+ export {
69
+ createSync
70
+ };
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=sync.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sync.test.d.ts","sourceRoot":"","sources":["../../src/alpha/sync.test.ts"],"names":[],"mappings":""}