@notionhq/custom-blocks 0.3.2 → 0.3.4

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 (100) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +13 -19
  3. package/dist/bridge/SandboxBridge.d.ts +0 -2
  4. package/dist/bridge/SandboxBridge.d.ts.map +1 -1
  5. package/dist/bridge/SandboxBridge.js +16 -39
  6. package/dist/bridge/SandboxInitialization.d.ts +1 -3
  7. package/dist/bridge/SandboxInitialization.d.ts.map +1 -1
  8. package/dist/bridge/SandboxInitialization.js +13 -15
  9. package/dist/bridge/appearance.d.ts +2 -10
  10. package/dist/bridge/appearance.d.ts.map +1 -1
  11. package/dist/bridge/appearance.js +2 -10
  12. package/dist/bridge/dataSources/query.d.ts +1 -2
  13. package/dist/bridge/dataSources/query.d.ts.map +1 -1
  14. package/dist/bridge/dataSources/query.js +15 -398
  15. package/dist/bridge/dataSources/queryFilter.d.ts +10 -0
  16. package/dist/bridge/dataSources/queryFilter.d.ts.map +1 -0
  17. package/dist/bridge/dataSources/queryFilter.js +123 -0
  18. package/dist/bridge/dataSources/resolveProperty.d.ts +1 -20
  19. package/dist/bridge/dataSources/resolveProperty.d.ts.map +1 -1
  20. package/dist/bridge/dataSources/resolveProperty.js +37 -32
  21. package/dist/bridge/dataSources/resolvePropertyAddress.d.ts +10 -0
  22. package/dist/bridge/dataSources/resolvePropertyAddress.d.ts.map +1 -0
  23. package/dist/bridge/dataSources/resolvePropertyAddress.js +55 -0
  24. package/dist/bridge/dataSources/resolveSorts.d.ts +10 -0
  25. package/dist/bridge/dataSources/resolveSorts.d.ts.map +1 -0
  26. package/dist/bridge/dataSources/resolveSorts.js +80 -0
  27. package/dist/bridge/hostState.d.ts +3 -7
  28. package/dist/bridge/hostState.d.ts.map +1 -1
  29. package/dist/bridge/hostState.js +0 -24
  30. package/dist/bridge/page.d.ts +9 -3
  31. package/dist/bridge/page.d.ts.map +1 -1
  32. package/dist/bridge/page.js +19 -2
  33. package/dist/bridge/readLocalManifest.d.ts +23 -0
  34. package/dist/bridge/readLocalManifest.d.ts.map +1 -0
  35. package/dist/bridge/readLocalManifest.js +46 -0
  36. package/dist/bridge/sandboxClient.d.ts +0 -2
  37. package/dist/bridge/sandboxClient.d.ts.map +1 -1
  38. package/dist/bridge/sandboxClient.js +50 -24
  39. package/dist/protocol/dataSources/dataSourcePage.d.ts +3 -4
  40. package/dist/protocol/dataSources/dataSourcePage.js +2 -3
  41. package/dist/protocol/index.d.ts +1 -0
  42. package/dist/protocol/index.js +1 -0
  43. package/dist/protocol/isLocalBlockServerUrl.d.ts +2 -0
  44. package/dist/protocol/isLocalBlockServerUrl.js +9 -0
  45. package/dist/protocol/messages/connect.d.ts +2 -2
  46. package/dist/protocol/messages/connect.js +2 -2
  47. package/dist/protocol/messages/createPage.d.ts +1 -1
  48. package/dist/protocol/messages/createPage.js +1 -1
  49. package/dist/protocol/messages/createPageResult.d.ts +3 -3
  50. package/dist/protocol/messages/createPageResult.js +4 -4
  51. package/dist/protocol/messages/getPage.d.ts +2 -2
  52. package/dist/protocol/messages/getPage.js +4 -4
  53. package/dist/protocol/messages/hostToSandbox.d.ts +3 -3
  54. package/dist/protocol/messages/hostToSandbox.js +10 -10
  55. package/dist/protocol/messages/init.d.ts +1 -1
  56. package/dist/protocol/messages/queryDataSource.d.ts +24 -0
  57. package/dist/protocol/messages/queryDataSource.js +2 -0
  58. package/dist/protocol/messages/queryDataSourceResult.d.ts +3 -3
  59. package/dist/protocol/messages/queryDataSourceResult.js +5 -5
  60. package/dist/protocol/messages/sandboxToHost.d.ts +8 -0
  61. package/dist/protocol/messages/updatePageResult.d.ts +3 -3
  62. package/dist/protocol/messages/updatePageResult.js +4 -4
  63. package/dist/protocol/pages/page.d.ts +4 -4
  64. package/dist/protocol/pages/page.js +2 -2
  65. package/dist/protocol/parent.d.ts +1 -1
  66. package/dist/protocol/protocolVersion.d.ts +3 -2
  67. package/dist/protocol/protocolVersion.js +4 -2
  68. package/dist/react/useDataSource.d.ts +2 -2
  69. package/dist/react/useDataSource.js +2 -2
  70. package/dist/types.d.ts +5 -5
  71. package/dist/types.d.ts.map +1 -1
  72. package/dist/version.js +1 -1
  73. package/docs/block-location.md +6 -4
  74. package/docs/bridge-interceptors.md +4 -0
  75. package/docs/build-tools.md +58 -0
  76. package/docs/data-sources.md +24 -18
  77. package/docs/deployment.md +23 -36
  78. package/docs/errors.md +75 -52
  79. package/docs/lifecycle.md +10 -3
  80. package/docs/pages.md +84 -76
  81. package/docs/users.md +4 -4
  82. package/package.json +4 -19
  83. package/src/bridge/SandboxBridge.ts +18 -48
  84. package/src/bridge/SandboxInitialization.ts +14 -20
  85. package/src/bridge/appearance.ts +2 -10
  86. package/src/bridge/dataSources/query.ts +17 -530
  87. package/src/bridge/dataSources/queryFilter.ts +181 -0
  88. package/src/bridge/dataSources/resolveProperty.ts +48 -39
  89. package/src/bridge/dataSources/resolvePropertyAddress.ts +62 -0
  90. package/src/bridge/dataSources/resolveSorts.ts +107 -0
  91. package/src/bridge/hostState.ts +3 -43
  92. package/src/bridge/page.ts +28 -4
  93. package/src/bridge/readLocalManifest.ts +60 -0
  94. package/src/bridge/sandboxClient.ts +63 -38
  95. package/src/react/useDataSource.ts +2 -2
  96. package/src/types.ts +5 -5
  97. package/dist/bridge/loadManifest.d.ts +0 -21
  98. package/dist/bridge/loadManifest.d.ts.map +0 -1
  99. package/dist/bridge/loadManifest.js +0 -85
  100. package/src/bridge/loadManifest.ts +0 -114
@@ -1,14 +1,8 @@
1
- import { CUSTOM_BLOCK_DATA_SOURCE_SORTABLE_PROPERTY_TYPES } from "../../protocol/dataSources/propertySchema.js";
2
- import { notionSortDirectionSchema } from "../../protocol/dataSources/sortDirection.js";
3
- import { CUSTOM_BLOCK_DATA_SOURCE_QUERY_MAX_SORTS, customBlockCheckboxFilterOperatorSchema, customBlockContainsFilterOperatorSchema, customBlockCreatedByFilterOperatorSchema, customBlockDateFilterOperatorSchema, customBlockNumberFilterOperatorSchema, customBlockOptionFilterOperatorSchema, customBlockPeopleFilterOperatorSchema, customBlockRelationFilterOperatorSchema, customBlockRelativeDateValueSchema, customBlockStatusFilterOperatorSchema, customBlockTextFilterOperatorSchema, } from "../../protocol/messages/queryDataSource.js";
4
- import * as v from "valibot";
5
- import { unreachable } from "../../utils.js";
1
+ import { resolveFilter } from "./queryFilter.js";
2
+ import { resolveSorts } from "./resolveSorts.js";
6
3
  const DEFAULT_DATA_SOURCE_QUERY_LIMIT = 20;
7
- const MAX_DATA_SOURCE_QUERY_LIMIT = 999;
8
- const MAX_DATA_SOURCE_QUERY_FILTER_CHILDREN = 25;
9
- const supportedDataSourceSortPropertyTypes = new Set(CUSTOM_BLOCK_DATA_SOURCE_SORTABLE_PROPERTY_TYPES);
10
4
  export function resolveDataSourceQuery(args) {
11
- const { dataSources, key, options, warn } = args;
5
+ const { dataSources, key, options } = args;
12
6
  const resolvedOptions = resolveDataSourceQueryOptions(options);
13
7
  if (resolvedOptions.status === "error") {
14
8
  return {
@@ -53,9 +47,16 @@ export function resolveDataSourceQuery(args) {
53
47
  },
54
48
  };
55
49
  }
56
- const limit = resolveDataSourceQueryLimit(queryOptions?.limit, warn);
50
+ const limit = resolveDataSourceQueryLimit(queryOptions?.limit);
57
51
  if (limit.status === "error") {
58
- return { status: "error", error: getInvalidQueryError(limit.error) };
52
+ return {
53
+ status: "error",
54
+ error: {
55
+ code: "data_source_query_invalid_limit",
56
+ message: limit.error,
57
+ isRetryable: false,
58
+ },
59
+ };
59
60
  }
60
61
  const filter = resolveFilter(queryOptions?.filter, dataSource);
61
62
  if (filter.status === "error") {
@@ -128,23 +129,16 @@ function resolveDataSourceQueryOptions(options) {
128
129
  },
129
130
  };
130
131
  }
131
- function resolveDataSourceQueryLimit(limit, warn = console.warn) {
132
+ function resolveDataSourceQueryLimit(limit) {
132
133
  if (limit === undefined) {
133
134
  return { status: "ok", limit: DEFAULT_DATA_SOURCE_QUERY_LIMIT };
134
135
  }
135
- if (typeof limit !== "number" ||
136
- !Number.isFinite(limit) ||
137
- !Number.isInteger(limit) ||
138
- limit < 1) {
136
+ if (typeof limit !== "number" || !Number.isSafeInteger(limit) || limit < 1) {
139
137
  return {
140
138
  status: "error",
141
- error: `Data source query limit must be a positive integer between 1 and ${MAX_DATA_SOURCE_QUERY_LIMIT}.`,
139
+ error: "Data source query limit must be a positive safe integer.",
142
140
  };
143
141
  }
144
- if (limit > MAX_DATA_SOURCE_QUERY_LIMIT) {
145
- warn?.(`Data source query limit ${limit} exceeds the maximum of ${MAX_DATA_SOURCE_QUERY_LIMIT}; clamping to ${MAX_DATA_SOURCE_QUERY_LIMIT}.`);
146
- return { status: "ok", limit: MAX_DATA_SOURCE_QUERY_LIMIT };
147
- }
148
142
  return { status: "ok", limit };
149
143
  }
150
144
  export function getDataSourceQueryOptionsIdentity(options) {
@@ -158,371 +152,6 @@ export function getDataSourceQueryOptionsIdentity(options) {
158
152
  function stableJsonStringify(value) {
159
153
  return JSON.stringify(normalizeJsonValue(value));
160
154
  }
161
- function resolveFilter(filter, dataSource) {
162
- if (filter === undefined) {
163
- return { status: "ok" };
164
- }
165
- if (typeof filter !== "object" || filter === null || Array.isArray(filter)) {
166
- return {
167
- status: "error",
168
- error: "Data source query filter must be an object.",
169
- };
170
- }
171
- const filterObject = filter;
172
- if (Object.hasOwn(filterObject, "and")) {
173
- if (Object.keys(filterObject).length !== 1 ||
174
- !Array.isArray(filterObject.and)) {
175
- return {
176
- status: "error",
177
- error: 'Data source query filter "and" must be an array.',
178
- };
179
- }
180
- if (filterObject.and.length > MAX_DATA_SOURCE_QUERY_FILTER_CHILDREN) {
181
- return {
182
- status: "error",
183
- error: `Data source query filter "and" may contain at most ${MAX_DATA_SOURCE_QUERY_FILTER_CHILDREN} children.`,
184
- };
185
- }
186
- const and = [];
187
- for (const child of filterObject.and) {
188
- const resolved = resolvePropertyFilter(child, dataSource);
189
- if (resolved.status === "error") {
190
- return resolved;
191
- }
192
- and.push(resolved.filter);
193
- }
194
- return { status: "ok", filter: { and } };
195
- }
196
- return resolvePropertyFilter(filterObject, dataSource);
197
- }
198
- function resolveSorts(sorts, dataSource) {
199
- if (sorts === undefined) {
200
- return { status: "ok" };
201
- }
202
- if (!Array.isArray(sorts)) {
203
- return {
204
- status: "error",
205
- error: "Data source query sorts must be an array.",
206
- };
207
- }
208
- if (sorts.length === 0) {
209
- return { status: "ok" };
210
- }
211
- if (sorts.length > CUSTOM_BLOCK_DATA_SOURCE_QUERY_MAX_SORTS) {
212
- return {
213
- status: "error",
214
- error: `Data source query sorts may contain at most ${CUSTOM_BLOCK_DATA_SOURCE_QUERY_MAX_SORTS} entries.`,
215
- };
216
- }
217
- const resolvedSorts = [];
218
- const seenPropertyIds = new Set();
219
- for (const sort of sorts) {
220
- const resolved = resolveSort(sort, dataSource);
221
- if (resolved.status === "error") {
222
- return resolved;
223
- }
224
- if (seenPropertyIds.has(resolved.sort.propertyId)) {
225
- return {
226
- status: "error",
227
- error: `Data source query sorts cannot contain duplicate property ID "${resolved.sort.propertyId}".`,
228
- };
229
- }
230
- seenPropertyIds.add(resolved.sort.propertyId);
231
- resolvedSorts.push(resolved.sort);
232
- }
233
- return { status: "ok", sorts: resolvedSorts };
234
- }
235
- function resolveSort(sort, dataSource) {
236
- if (typeof sort !== "object" || sort === null || Array.isArray(sort)) {
237
- return {
238
- status: "error",
239
- error: "Data source query sort must be an object.",
240
- };
241
- }
242
- const sortObject = sort;
243
- if (Object.keys(sortObject).some(key => !["key", "propertyId", "direction"].includes(key))) {
244
- return {
245
- status: "error",
246
- error: "Data source query sort contains unsupported fields.",
247
- };
248
- }
249
- const property = resolvePropertyAddress(sortObject, dataSource);
250
- if (property.status === "error") {
251
- return property;
252
- }
253
- if (!supportedDataSourceSortPropertyTypes.has(property.propertyType)) {
254
- return {
255
- status: "error",
256
- error: `Data source query sorts do not yet support property type "${property.propertyType}".`,
257
- };
258
- }
259
- const direction = sortObject.direction;
260
- if (!Object.hasOwn(sortObject, "direction") ||
261
- !v.is(notionSortDirectionSchema, direction)) {
262
- return {
263
- status: "error",
264
- error: 'Data source query sort direction must be "ascending" or "descending".',
265
- };
266
- }
267
- return {
268
- status: "ok",
269
- sort: { propertyId: property.propertyId, direction },
270
- };
271
- }
272
- function resolvePropertyFilter(filter, dataSource) {
273
- if (typeof filter !== "object" || filter === null || Array.isArray(filter)) {
274
- return {
275
- status: "error",
276
- error: "Data source query property filter must be an object.",
277
- };
278
- }
279
- const filterObject = filter;
280
- const property = resolvePropertyAddress(filterObject, dataSource);
281
- if (property.status === "error") {
282
- return property;
283
- }
284
- const branchKeys = Object.keys(filterObject).filter(key => key !== "key" && key !== "propertyId");
285
- if (branchKeys.length !== 1) {
286
- return {
287
- status: "error",
288
- error: "Data source query property filter must contain exactly one property branch.",
289
- };
290
- }
291
- const branch = branchKeys[0];
292
- const value = filterObject[branch];
293
- if (typeof value !== "object" || value === null || Array.isArray(value)) {
294
- return invalidOperator(branch);
295
- }
296
- const operatorKeys = Object.keys(value).filter(key => branch !== "date" || key !== "target");
297
- if (operatorKeys.length !== 1) {
298
- return invalidOperator(branch);
299
- }
300
- if (property.propertyType !== branch) {
301
- return {
302
- status: "error",
303
- error: `Data source query filter branch "${branch}" does not match property type "${property.propertyType}".`,
304
- };
305
- }
306
- return resolvePropertyFilterBranch({
307
- propertyId: property.propertyId,
308
- branch,
309
- value: value,
310
- });
311
- }
312
- function resolvePropertyFilterBranch(args) {
313
- switch (args.branch) {
314
- case "title":
315
- case "rich_text":
316
- case "url":
317
- case "email":
318
- case "phone_number":
319
- return resolveTextPropertyFilter({
320
- propertyId: args.propertyId,
321
- branch: args.branch,
322
- value: args.value,
323
- });
324
- case "number":
325
- return resolveNumberPropertyFilter(args.propertyId, args.value);
326
- case "checkbox":
327
- return resolveCheckboxPropertyFilter(args.propertyId, args.value);
328
- case "select":
329
- return resolveOptionPropertyFilter({
330
- propertyId: args.propertyId,
331
- branch: "select",
332
- value: args.value,
333
- });
334
- case "status": {
335
- const parsed = v.safeParse(customBlockStatusFilterOperatorSchema, args.value);
336
- return parsed.success
337
- ? {
338
- status: "ok",
339
- filter: {
340
- propertyId: args.propertyId,
341
- status: parsed.output,
342
- },
343
- }
344
- : invalidOperator("status");
345
- }
346
- case "multi_select":
347
- return resolveMultiSelectPropertyFilter(args.propertyId, args.value);
348
- case "date":
349
- return resolveDatePropertyFilter(args.propertyId, args.value);
350
- case "created_by":
351
- return resolveCreatedByPropertyFilter(args.propertyId, args.value);
352
- case "relation": {
353
- const parsed = v.safeParse(customBlockRelationFilterOperatorSchema, args.value);
354
- return parsed.success
355
- ? {
356
- status: "ok",
357
- filter: { propertyId: args.propertyId, relation: parsed.output },
358
- }
359
- : invalidOperator("relation");
360
- }
361
- case "people":
362
- return resolvePeoplePropertyFilter(args.propertyId, args.value);
363
- default:
364
- return {
365
- status: "error",
366
- error: `Data source query filter branch "${args.branch}" is not supported.`,
367
- };
368
- }
369
- }
370
- function resolveTextPropertyFilter(args) {
371
- const parsed = v.safeParse(customBlockTextFilterOperatorSchema, args.value);
372
- if (!parsed.success) {
373
- return invalidOperator(args.branch);
374
- }
375
- switch (args.branch) {
376
- case "title":
377
- return {
378
- status: "ok",
379
- filter: { propertyId: args.propertyId, title: parsed.output },
380
- };
381
- case "rich_text":
382
- return {
383
- status: "ok",
384
- filter: { propertyId: args.propertyId, rich_text: parsed.output },
385
- };
386
- case "url":
387
- return {
388
- status: "ok",
389
- filter: { propertyId: args.propertyId, url: parsed.output },
390
- };
391
- case "email":
392
- return {
393
- status: "ok",
394
- filter: { propertyId: args.propertyId, email: parsed.output },
395
- };
396
- case "phone_number":
397
- return {
398
- status: "ok",
399
- filter: { propertyId: args.propertyId, phone_number: parsed.output },
400
- };
401
- default:
402
- return unreachable(args.branch);
403
- }
404
- }
405
- function resolveNumberPropertyFilter(propertyId, value) {
406
- const parsed = v.safeParse(customBlockNumberFilterOperatorSchema, value);
407
- if (!parsed.success ||
408
- !Object.values(parsed.output).every(entry => typeof entry !== "number" || Number.isFinite(entry))) {
409
- return invalidOperator("number");
410
- }
411
- return {
412
- status: "ok",
413
- filter: { propertyId, number: parsed.output },
414
- };
415
- }
416
- function resolveCheckboxPropertyFilter(propertyId, value) {
417
- const parsed = v.safeParse(customBlockCheckboxFilterOperatorSchema, value);
418
- return parsed.success
419
- ? { status: "ok", filter: { propertyId, checkbox: parsed.output } }
420
- : invalidOperator("checkbox");
421
- }
422
- function resolveOptionPropertyFilter(args) {
423
- const parsed = v.safeParse(customBlockOptionFilterOperatorSchema, args.value);
424
- if (!parsed.success) {
425
- return invalidOperator(args.branch);
426
- }
427
- return {
428
- status: "ok",
429
- filter: { propertyId: args.propertyId, select: parsed.output },
430
- };
431
- }
432
- function resolveMultiSelectPropertyFilter(propertyId, value) {
433
- const parsed = v.safeParse(customBlockContainsFilterOperatorSchema, value);
434
- return parsed.success
435
- ? { status: "ok", filter: { propertyId, multi_select: parsed.output } }
436
- : invalidOperator("multi_select");
437
- }
438
- function resolveDatePropertyFilter(propertyId, value) {
439
- const parsed = v.safeParse(customBlockDateFilterOperatorSchema, value);
440
- if (!parsed.success ||
441
- !Object.entries(parsed.output).every(([key, entry]) => key === "target" ||
442
- entry === true ||
443
- typeof entry === "object" ||
444
- v.safeParse(customBlockRelativeDateValueSchema, entry).success ||
445
- isValidIsoDate(entry))) {
446
- return invalidOperator("date");
447
- }
448
- return {
449
- status: "ok",
450
- filter: { propertyId, date: parsed.output },
451
- };
452
- }
453
- function resolveCreatedByPropertyFilter(propertyId, value) {
454
- const parsed = v.safeParse(customBlockCreatedByFilterOperatorSchema, value);
455
- return parsed.success
456
- ? { status: "ok", filter: { propertyId, created_by: parsed.output } }
457
- : invalidOperator("created_by");
458
- }
459
- function resolvePeoplePropertyFilter(propertyId, value) {
460
- const parsed = v.safeParse(customBlockPeopleFilterOperatorSchema, value);
461
- return parsed.success
462
- ? { status: "ok", filter: { propertyId, people: parsed.output } }
463
- : invalidOperator("people");
464
- }
465
- function resolvePropertyAddress(value, dataSource) {
466
- const hasKey = Object.hasOwn(value, "key");
467
- const hasPropertyId = Object.hasOwn(value, "propertyId");
468
- if (hasKey === hasPropertyId) {
469
- return {
470
- status: "error",
471
- error: "Data source query filters and sorts must use exactly one of key or propertyId.",
472
- };
473
- }
474
- let propertyId;
475
- if (hasKey) {
476
- if (typeof value.key !== "string") {
477
- return {
478
- status: "error",
479
- error: "Data source query property key must be a string.",
480
- };
481
- }
482
- if (!Object.hasOwn(dataSource.propertyIdsByKey, value.key)) {
483
- return {
484
- status: "error",
485
- error: `Unknown property key "${value.key}" for data source "${dataSource.key}".`,
486
- };
487
- }
488
- const resolvedPropertyId = dataSource.propertyIdsByKey[value.key];
489
- if (resolvedPropertyId === undefined) {
490
- return {
491
- status: "error",
492
- error: `Property key "${value.key}" for data source "${dataSource.key}" is not bound.`,
493
- };
494
- }
495
- propertyId = resolvedPropertyId;
496
- }
497
- else {
498
- if (typeof value.propertyId !== "string") {
499
- return {
500
- status: "error",
501
- error: "Data source query propertyId must be a string.",
502
- };
503
- }
504
- propertyId = value.propertyId;
505
- }
506
- const properties = dataSource.collectionSchema?.propertiesById ?? {};
507
- if (!Object.hasOwn(properties, propertyId)) {
508
- return {
509
- status: "error",
510
- error: `Unknown property ID "${propertyId}" for data source "${dataSource.key}".`,
511
- };
512
- }
513
- const propertySchema = properties[propertyId];
514
- return {
515
- status: "ok",
516
- propertyId,
517
- propertyType: propertySchema.type,
518
- };
519
- }
520
- function invalidOperator(branch) {
521
- return {
522
- status: "error",
523
- error: `Data source query filter branch "${branch}" has an invalid operator or value.`,
524
- };
525
- }
526
155
  function normalizeJsonValue(value) {
527
156
  if (value === null ||
528
157
  typeof value === "string" ||
@@ -561,15 +190,3 @@ function normalizeJsonObject(value) {
561
190
  }
562
191
  return normalized;
563
192
  }
564
- function isValidIsoDate(value) {
565
- if (typeof value !== "string") {
566
- return false;
567
- }
568
- const dateOnly = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value);
569
- if (dateOnly !== null) {
570
- const [, year, month, day] = dateOnly;
571
- const date = new Date(Date.UTC(Number(year), Number(month) - 1, Number(day)));
572
- return date.toISOString().slice(0, 10) === value;
573
- }
574
- return (/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d{1,3})?)?(?:Z|[+-]\d{2}:\d{2})$/.test(value) && Number.isFinite(Date.parse(value)));
575
- }
@@ -0,0 +1,10 @@
1
+ import type { NotionDataSource } from "../../protocol/dataSources/dataSource.js";
2
+ import { type ResolvedCustomBlockDataSourceFilter } from "../../protocol/messages/queryDataSource.js";
3
+ export declare function resolveFilter(filter: unknown, dataSource: NotionDataSource): {
4
+ status: "ok";
5
+ filter?: ResolvedCustomBlockDataSourceFilter;
6
+ } | {
7
+ status: "error";
8
+ error: string;
9
+ };
10
+ //# sourceMappingURL=queryFilter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"queryFilter.d.ts","sourceRoot":"","sources":["../../../src/bridge/dataSources/queryFilter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,4DAA4D,CAAA;AAClG,OAAO,EAEN,KAAK,mCAAmC,EAGxC,MAAM,8DAA8D,CAAA;AAUrE,wBAAgB,aAAa,CAC5B,MAAM,EAAE,OAAO,EACf,UAAU,EAAE,gBAAgB,GAE1B;IAAE,MAAM,EAAE,IAAI,CAAC;IAAC,MAAM,CAAC,EAAE,mCAAmC,CAAA;CAAE,GAC9D;IAAE,MAAM,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAsCpC"}
@@ -0,0 +1,123 @@
1
+ import { customBlockRelativeDateValueSchema, resolvedCustomBlockDataSourcePropertyFilterSchema, } from "../../protocol/messages/queryDataSource.js";
2
+ import * as v from "valibot";
3
+ import { resolvePropertyAddress } from "./resolvePropertyAddress.js";
4
+ const MAX_DATA_SOURCE_QUERY_FILTER_CHILDREN = 25;
5
+ export function resolveFilter(filter, dataSource) {
6
+ if (filter === undefined) {
7
+ return { status: "ok" };
8
+ }
9
+ if (typeof filter !== "object" || filter === null || Array.isArray(filter)) {
10
+ return {
11
+ status: "error",
12
+ error: "Data source query filter must be an object.",
13
+ };
14
+ }
15
+ const filterObject = filter;
16
+ if (Object.hasOwn(filterObject, "and")) {
17
+ if (Object.keys(filterObject).length !== 1 ||
18
+ !Array.isArray(filterObject.and)) {
19
+ return {
20
+ status: "error",
21
+ error: 'Data source query filter "and" must be an array.',
22
+ };
23
+ }
24
+ if (filterObject.and.length > MAX_DATA_SOURCE_QUERY_FILTER_CHILDREN) {
25
+ return {
26
+ status: "error",
27
+ error: `Data source query filter "and" may contain at most ${MAX_DATA_SOURCE_QUERY_FILTER_CHILDREN} children.`,
28
+ };
29
+ }
30
+ const and = [];
31
+ for (const child of filterObject.and) {
32
+ const resolved = resolvePropertyFilter(child, dataSource);
33
+ if (resolved.status === "error") {
34
+ return resolved;
35
+ }
36
+ and.push(resolved.filter);
37
+ }
38
+ return { status: "ok", filter: { and } };
39
+ }
40
+ return resolvePropertyFilter(filterObject, dataSource);
41
+ }
42
+ function resolvePropertyFilter(filter, dataSource) {
43
+ if (typeof filter !== "object" || filter === null || Array.isArray(filter)) {
44
+ return {
45
+ status: "error",
46
+ error: "Data source query property filter must be an object.",
47
+ };
48
+ }
49
+ const filterObject = filter;
50
+ const property = resolvePropertyAddress(filterObject, dataSource);
51
+ if (property.status === "error") {
52
+ return property;
53
+ }
54
+ const branchKeys = Object.keys(filterObject).filter(key => key !== "key" && key !== "propertyId");
55
+ if (branchKeys.length !== 1) {
56
+ return {
57
+ status: "error",
58
+ error: "Data source query property filter must contain exactly one property branch.",
59
+ };
60
+ }
61
+ const branch = branchKeys[0];
62
+ const value = filterObject[branch];
63
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
64
+ return invalidOperator(branch);
65
+ }
66
+ const operatorKeys = Object.keys(value).filter(key => branch !== "date" || key !== "target");
67
+ if (operatorKeys.length !== 1) {
68
+ return invalidOperator(branch);
69
+ }
70
+ if (property.propertyType !== branch) {
71
+ return {
72
+ status: "error",
73
+ error: `Data source query filter branch "${branch}" does not match property type "${property.propertyType}".`,
74
+ };
75
+ }
76
+ return parsePropertyFilter({
77
+ propertyId: property.propertyId,
78
+ branch,
79
+ value,
80
+ });
81
+ }
82
+ function parsePropertyFilter(args) {
83
+ const { propertyId, branch, value } = args;
84
+ const parsed = v.safeParse(resolvedCustomBlockDataSourcePropertyFilterSchema, { propertyId, [branch]: value });
85
+ if (!parsed.success) {
86
+ if (resolvedCustomBlockDataSourcePropertyFilterSchema.options.every(schema => !Object.hasOwn(schema.entries, branch))) {
87
+ return {
88
+ status: "error",
89
+ error: `Data source query filter branch "${branch}" is not supported.`,
90
+ };
91
+ }
92
+ return invalidOperator(branch);
93
+ }
94
+ const filter = parsed.output;
95
+ if ("number" in filter &&
96
+ !Object.values(filter.number).every(entry => typeof entry !== "number" || Number.isFinite(entry))) {
97
+ return invalidOperator("number");
98
+ }
99
+ if ("date" in filter &&
100
+ !Object.entries(filter.date).every(([key, entry]) => key === "target" ||
101
+ entry === true ||
102
+ typeof entry === "object" ||
103
+ v.is(customBlockRelativeDateValueSchema, entry) ||
104
+ isValidIsoDate(entry))) {
105
+ return invalidOperator("date");
106
+ }
107
+ return { status: "ok", filter };
108
+ }
109
+ function invalidOperator(branch) {
110
+ return {
111
+ status: "error",
112
+ error: `Data source query filter branch "${branch}" has an invalid operator or value.`,
113
+ };
114
+ }
115
+ function isValidIsoDate(value) {
116
+ const dateOnly = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value);
117
+ if (dateOnly !== null) {
118
+ const [, year, month, day] = dateOnly;
119
+ const date = new Date(Date.UTC(Number(year), Number(month) - 1, Number(day)));
120
+ return date.toISOString().slice(0, 10) === value;
121
+ }
122
+ return (/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d{1,3})?)?(?:Z|[+-]\d{2}:\d{2})$/.test(value) && Number.isFinite(Date.parse(value)));
123
+ }
@@ -11,29 +11,10 @@ export type PropertyWriteMapResolutionResult = {
11
11
  };
12
12
  /**
13
13
  * Resolves a public SDK property write map into the ID-keyed bridge shape.
14
- *
15
- * Identifiers in `properties` may be raw property IDs or data source property keys; the latter
16
- * are looked up in the `dataSource`'s `propertyIdsByKey`. Each value is re-parsed through
17
- * the page property value schema with its `id` rewritten to the resolved property ID.
14
+ * A data source makes every identifier a property key; without one, identifiers are raw IDs.
18
15
  */
19
16
  export declare function resolvePropertyWriteMapForDataSource(args: {
20
17
  dataSource: NotionDataSource | undefined;
21
18
  properties: NotionPagePropertyInputMap;
22
- operationName: string;
23
19
  }): PropertyWriteMapResolutionResult;
24
- /**
25
- * Treats bound data source keys as aliases for property IDs. An unbound key may still be used as
26
- * a raw ID when it exactly matches a property in the data source schema.
27
- */
28
- export declare function resolvePropertyIdentifierForDataSource(args: {
29
- dataSource: NotionDataSource | undefined;
30
- identifier: string;
31
- operationName: string;
32
- }): {
33
- status: "success";
34
- propertyId: string;
35
- } | {
36
- status: "error";
37
- error: CustomBlockPropertyErrorInfo;
38
- };
39
20
  //# sourceMappingURL=resolveProperty.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"resolveProperty.d.ts","sourceRoot":"","sources":["../../../src/bridge/dataSources/resolveProperty.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,4DAA4D,CAAA;AAClG,OAAO,KAAK,EAEX,4BAA4B,EAC5B,MAAM,wDAAwD,CAAA;AAC/D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,yDAAyD,CAAA;AAGhG,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,gBAAgB,CAAA;AAEhE,MAAM,MAAM,gCAAgC,GACzC;IAAE,MAAM,EAAE,SAAS,CAAC;IAAC,UAAU,EAAE,iBAAiB,CAAC,YAAY,CAAC,CAAA;CAAE,GAClE;IAAE,MAAM,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,4BAA4B,CAAA;CAAE,CAAA;AAS3D;;;;;;GAMG;AACH,wBAAgB,oCAAoC,CAAC,IAAI,EAAE;IAC1D,UAAU,EAAE,gBAAgB,GAAG,SAAS,CAAA;IACxC,UAAU,EAAE,0BAA0B,CAAA;IACtC,aAAa,EAAE,MAAM,CAAA;CACrB,GAAG,gCAAgC,CAwDnC;AAED;;;GAGG;AACH,wBAAgB,sCAAsC,CAAC,IAAI,EAAE;IAC5D,UAAU,EAAE,gBAAgB,GAAG,SAAS,CAAA;IACxC,UAAU,EAAE,MAAM,CAAA;IAClB,aAAa,EAAE,MAAM,CAAA;CACrB,GACE;IAAE,MAAM,EAAE,SAAS,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAE,GACzC;IAAE,MAAM,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,4BAA4B,CAAA;CAAE,CAsB1D"}
1
+ {"version":3,"file":"resolveProperty.d.ts","sourceRoot":"","sources":["../../../src/bridge/dataSources/resolveProperty.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,4DAA4D,CAAA;AAClG,OAAO,KAAK,EAEX,4BAA4B,EAC5B,MAAM,wDAAwD,CAAA;AAC/D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,yDAAyD,CAAA;AAGhG,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,gBAAgB,CAAA;AAEhE,MAAM,MAAM,gCAAgC,GACzC;IAAE,MAAM,EAAE,SAAS,CAAC;IAAC,UAAU,EAAE,iBAAiB,CAAC,YAAY,CAAC,CAAA;CAAE,GAClE;IAAE,MAAM,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,4BAA4B,CAAA;CAAE,CAAA;AAS3D;;;GAGG;AACH,wBAAgB,oCAAoC,CAAC,IAAI,EAAE;IAC1D,UAAU,EAAE,gBAAgB,GAAG,SAAS,CAAA;IACxC,UAAU,EAAE,0BAA0B,CAAA;CACtC,GAAG,gCAAgC,CAuEnC"}