@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,35 +1,15 @@
1
1
  import type { NotionDataSource } from "@notionhq/custom-blocks-protocol/dataSources/dataSource.js"
2
- import { CUSTOM_BLOCK_DATA_SOURCE_SORTABLE_PROPERTY_TYPES } from "@notionhq/custom-blocks-protocol/dataSources/propertySchema.js"
3
- import { notionSortDirectionSchema } from "@notionhq/custom-blocks-protocol/dataSources/sortDirection.js"
4
2
  import type { NotionDataSourceId } from "@notionhq/custom-blocks-protocol/ids.js"
5
- import {
6
- CUSTOM_BLOCK_DATA_SOURCE_QUERY_MAX_SORTS,
7
- customBlockCheckboxFilterOperatorSchema,
8
- customBlockContainsFilterOperatorSchema,
9
- customBlockCreatedByFilterOperatorSchema,
10
- customBlockDateFilterOperatorSchema,
11
- customBlockNumberFilterOperatorSchema,
12
- customBlockOptionFilterOperatorSchema,
13
- customBlockPeopleFilterOperatorSchema,
14
- customBlockRelationFilterOperatorSchema,
15
- customBlockRelativeDateValueSchema,
16
- customBlockStatusFilterOperatorSchema,
17
- customBlockTextFilterOperatorSchema,
18
- type ResolvedCustomBlockDataSourceFilter,
19
- type ResolvedCustomBlockDataSourcePropertyFilter,
20
- type ResolvedCustomBlockDataSourceSort,
3
+ import type {
4
+ ResolvedCustomBlockDataSourceFilter,
5
+ ResolvedCustomBlockDataSourceSort,
21
6
  } from "@notionhq/custom-blocks-protocol/messages/queryDataSource.js"
22
7
  import type { CustomBlockQueryDataSourceErrorInfo } from "@notionhq/custom-blocks-protocol/messages/queryDataSourceResult.js"
23
- import * as v from "valibot"
24
8
  import type { UseDataSourceOptions } from "../../types.js"
25
- import { unreachable } from "../../utils.js"
9
+ import { resolveFilter } from "./queryFilter.js"
10
+ import { resolveSorts } from "./resolveSorts.js"
26
11
 
27
12
  const DEFAULT_DATA_SOURCE_QUERY_LIMIT = 20
28
- const MAX_DATA_SOURCE_QUERY_LIMIT = 999
29
- const MAX_DATA_SOURCE_QUERY_FILTER_CHILDREN = 25
30
- const supportedDataSourceSortPropertyTypes = new Set<string>(
31
- CUSTOM_BLOCK_DATA_SOURCE_SORTABLE_PROPERTY_TYPES,
32
- )
33
13
 
34
14
  export type ResolvedDataSourceQuery = {
35
15
  dataSourceId: NotionDataSourceId
@@ -53,17 +33,12 @@ type DataSourceQueryOptions = {
53
33
  sorts?: unknown
54
34
  }
55
35
 
56
- type ResolvePropertyFilterResult =
57
- | { status: "ok"; filter: ResolvedCustomBlockDataSourcePropertyFilter }
58
- | { status: "error"; error: string }
59
-
60
36
  export function resolveDataSourceQuery(args: {
61
37
  dataSources: NotionDataSource[]
62
38
  key: string
63
39
  options?: unknown
64
- warn?: (message: string) => void
65
40
  }): ResolveDataSourceQueryResult {
66
- const { dataSources, key, options, warn } = args
41
+ const { dataSources, key, options } = args
67
42
  const resolvedOptions = resolveDataSourceQueryOptions(options)
68
43
  if (resolvedOptions.status === "error") {
69
44
  return {
@@ -112,9 +87,16 @@ export function resolveDataSourceQuery(args: {
112
87
  }
113
88
  }
114
89
 
115
- const limit = resolveDataSourceQueryLimit(queryOptions?.limit, warn)
90
+ const limit = resolveDataSourceQueryLimit(queryOptions?.limit)
116
91
  if (limit.status === "error") {
117
- return { status: "error", error: getInvalidQueryError(limit.error) }
92
+ return {
93
+ status: "error",
94
+ error: {
95
+ code: "data_source_query_invalid_limit",
96
+ message: limit.error,
97
+ isRetryable: false,
98
+ },
99
+ }
118
100
  }
119
101
  const filter = resolveFilter(queryOptions?.filter, dataSource)
120
102
  if (filter.status === "error") {
@@ -205,28 +187,16 @@ function resolveDataSourceQueryOptions(
205
187
 
206
188
  function resolveDataSourceQueryLimit(
207
189
  limit: unknown,
208
- warn: ((message: string) => void) | undefined = console.warn,
209
190
  ): { status: "ok"; limit: number } | { status: "error"; error: string } {
210
191
  if (limit === undefined) {
211
192
  return { status: "ok", limit: DEFAULT_DATA_SOURCE_QUERY_LIMIT }
212
193
  }
213
- if (
214
- typeof limit !== "number" ||
215
- !Number.isFinite(limit) ||
216
- !Number.isInteger(limit) ||
217
- limit < 1
218
- ) {
194
+ if (typeof limit !== "number" || !Number.isSafeInteger(limit) || limit < 1) {
219
195
  return {
220
196
  status: "error",
221
- error: `Data source query limit must be a positive integer between 1 and ${MAX_DATA_SOURCE_QUERY_LIMIT}.`,
197
+ error: "Data source query limit must be a positive safe integer.",
222
198
  }
223
199
  }
224
- if (limit > MAX_DATA_SOURCE_QUERY_LIMIT) {
225
- warn?.(
226
- `Data source query limit ${limit} exceeds the maximum of ${MAX_DATA_SOURCE_QUERY_LIMIT}; clamping to ${MAX_DATA_SOURCE_QUERY_LIMIT}.`,
227
- )
228
- return { status: "ok", limit: MAX_DATA_SOURCE_QUERY_LIMIT }
229
- }
230
200
  return { status: "ok", limit }
231
201
  }
232
202
 
@@ -244,470 +214,6 @@ function stableJsonStringify(value: unknown): string {
244
214
  return JSON.stringify(normalizeJsonValue(value))
245
215
  }
246
216
 
247
- function resolveFilter(
248
- filter: unknown,
249
- dataSource: NotionDataSource,
250
- ):
251
- | { status: "ok"; filter?: ResolvedCustomBlockDataSourceFilter }
252
- | { status: "error"; error: string } {
253
- if (filter === undefined) {
254
- return { status: "ok" }
255
- }
256
- if (typeof filter !== "object" || filter === null || Array.isArray(filter)) {
257
- return {
258
- status: "error",
259
- error: "Data source query filter must be an object.",
260
- }
261
- }
262
- const filterObject = filter as Record<string, unknown>
263
- if (Object.hasOwn(filterObject, "and")) {
264
- if (
265
- Object.keys(filterObject).length !== 1 ||
266
- !Array.isArray(filterObject.and)
267
- ) {
268
- return {
269
- status: "error",
270
- error: 'Data source query filter "and" must be an array.',
271
- }
272
- }
273
- if (filterObject.and.length > MAX_DATA_SOURCE_QUERY_FILTER_CHILDREN) {
274
- return {
275
- status: "error",
276
- error: `Data source query filter "and" may contain at most ${MAX_DATA_SOURCE_QUERY_FILTER_CHILDREN} children.`,
277
- }
278
- }
279
- const and: ResolvedCustomBlockDataSourcePropertyFilter[] = []
280
- for (const child of filterObject.and) {
281
- const resolved = resolvePropertyFilter(child, dataSource)
282
- if (resolved.status === "error") {
283
- return resolved
284
- }
285
- and.push(resolved.filter)
286
- }
287
- return { status: "ok", filter: { and } }
288
- }
289
- return resolvePropertyFilter(filterObject, dataSource)
290
- }
291
-
292
- function resolveSorts(
293
- sorts: unknown,
294
- dataSource: NotionDataSource,
295
- ):
296
- | { status: "ok"; sorts?: ResolvedCustomBlockDataSourceSort[] }
297
- | { status: "error"; error: string } {
298
- if (sorts === undefined) {
299
- return { status: "ok" }
300
- }
301
- if (!Array.isArray(sorts)) {
302
- return {
303
- status: "error",
304
- error: "Data source query sorts must be an array.",
305
- }
306
- }
307
- if (sorts.length === 0) {
308
- return { status: "ok" }
309
- }
310
- if (sorts.length > CUSTOM_BLOCK_DATA_SOURCE_QUERY_MAX_SORTS) {
311
- return {
312
- status: "error",
313
- error: `Data source query sorts may contain at most ${CUSTOM_BLOCK_DATA_SOURCE_QUERY_MAX_SORTS} entries.`,
314
- }
315
- }
316
-
317
- const resolvedSorts: ResolvedCustomBlockDataSourceSort[] = []
318
- const seenPropertyIds = new Set<string>()
319
- for (const sort of sorts) {
320
- const resolved = resolveSort(sort, dataSource)
321
- if (resolved.status === "error") {
322
- return resolved
323
- }
324
- if (seenPropertyIds.has(resolved.sort.propertyId)) {
325
- return {
326
- status: "error",
327
- error: `Data source query sorts cannot contain duplicate property ID "${resolved.sort.propertyId}".`,
328
- }
329
- }
330
- seenPropertyIds.add(resolved.sort.propertyId)
331
- resolvedSorts.push(resolved.sort)
332
- }
333
- return { status: "ok", sorts: resolvedSorts }
334
- }
335
-
336
- function resolveSort(
337
- sort: unknown,
338
- dataSource: NotionDataSource,
339
- ):
340
- | { status: "ok"; sort: ResolvedCustomBlockDataSourceSort }
341
- | { status: "error"; error: string } {
342
- if (typeof sort !== "object" || sort === null || Array.isArray(sort)) {
343
- return {
344
- status: "error",
345
- error: "Data source query sort must be an object.",
346
- }
347
- }
348
- const sortObject = sort as Record<string, unknown>
349
- if (
350
- Object.keys(sortObject).some(
351
- key => !["key", "propertyId", "direction"].includes(key),
352
- )
353
- ) {
354
- return {
355
- status: "error",
356
- error: "Data source query sort contains unsupported fields.",
357
- }
358
- }
359
- const property = resolvePropertyAddress(sortObject, dataSource)
360
- if (property.status === "error") {
361
- return property
362
- }
363
- if (!supportedDataSourceSortPropertyTypes.has(property.propertyType)) {
364
- return {
365
- status: "error",
366
- error: `Data source query sorts do not yet support property type "${property.propertyType}".`,
367
- }
368
- }
369
- const direction = sortObject.direction
370
- if (
371
- !Object.hasOwn(sortObject, "direction") ||
372
- !v.is(notionSortDirectionSchema, direction)
373
- ) {
374
- return {
375
- status: "error",
376
- error:
377
- 'Data source query sort direction must be "ascending" or "descending".',
378
- }
379
- }
380
- return {
381
- status: "ok",
382
- sort: { propertyId: property.propertyId, direction },
383
- }
384
- }
385
-
386
- function resolvePropertyFilter(
387
- filter: unknown,
388
- dataSource: NotionDataSource,
389
- ): ResolvePropertyFilterResult {
390
- if (typeof filter !== "object" || filter === null || Array.isArray(filter)) {
391
- return {
392
- status: "error",
393
- error: "Data source query property filter must be an object.",
394
- }
395
- }
396
- const filterObject = filter as Record<string, unknown>
397
- const property = resolvePropertyAddress(filterObject, dataSource)
398
- if (property.status === "error") {
399
- return property
400
- }
401
- const branchKeys = Object.keys(filterObject).filter(
402
- key => key !== "key" && key !== "propertyId",
403
- )
404
- if (branchKeys.length !== 1) {
405
- return {
406
- status: "error",
407
- error:
408
- "Data source query property filter must contain exactly one property branch.",
409
- }
410
- }
411
- const branch = branchKeys[0]
412
- const value = filterObject[branch]
413
- if (typeof value !== "object" || value === null || Array.isArray(value)) {
414
- return invalidOperator(branch)
415
- }
416
- const operatorKeys = Object.keys(value).filter(
417
- key => branch !== "date" || key !== "target",
418
- )
419
- if (operatorKeys.length !== 1) {
420
- return invalidOperator(branch)
421
- }
422
- if (property.propertyType !== branch) {
423
- return {
424
- status: "error",
425
- error: `Data source query filter branch "${branch}" does not match property type "${property.propertyType}".`,
426
- }
427
- }
428
-
429
- return resolvePropertyFilterBranch({
430
- propertyId: property.propertyId,
431
- branch,
432
- value: value as Record<string, unknown>,
433
- })
434
- }
435
-
436
- function resolvePropertyFilterBranch(args: {
437
- propertyId: string
438
- branch: string
439
- value: Record<string, unknown>
440
- }): ResolvePropertyFilterResult {
441
- switch (args.branch) {
442
- case "title":
443
- case "rich_text":
444
- case "url":
445
- case "email":
446
- case "phone_number":
447
- return resolveTextPropertyFilter({
448
- propertyId: args.propertyId,
449
- branch: args.branch,
450
- value: args.value,
451
- })
452
- case "number":
453
- return resolveNumberPropertyFilter(args.propertyId, args.value)
454
- case "checkbox":
455
- return resolveCheckboxPropertyFilter(args.propertyId, args.value)
456
- case "select":
457
- return resolveOptionPropertyFilter({
458
- propertyId: args.propertyId,
459
- branch: "select",
460
- value: args.value,
461
- })
462
- case "status": {
463
- const parsed = v.safeParse(
464
- customBlockStatusFilterOperatorSchema,
465
- args.value,
466
- )
467
- return parsed.success
468
- ? {
469
- status: "ok",
470
- filter: {
471
- propertyId: args.propertyId,
472
- status: parsed.output,
473
- },
474
- }
475
- : invalidOperator("status")
476
- }
477
- case "multi_select":
478
- return resolveMultiSelectPropertyFilter(args.propertyId, args.value)
479
- case "date":
480
- return resolveDatePropertyFilter(args.propertyId, args.value)
481
- case "created_by":
482
- return resolveCreatedByPropertyFilter(args.propertyId, args.value)
483
- case "relation": {
484
- const parsed = v.safeParse(
485
- customBlockRelationFilterOperatorSchema,
486
- args.value,
487
- )
488
- return parsed.success
489
- ? {
490
- status: "ok",
491
- filter: { propertyId: args.propertyId, relation: parsed.output },
492
- }
493
- : invalidOperator("relation")
494
- }
495
- case "people":
496
- return resolvePeoplePropertyFilter(args.propertyId, args.value)
497
- default:
498
- return {
499
- status: "error",
500
- error: `Data source query filter branch "${args.branch}" is not supported.`,
501
- }
502
- }
503
- }
504
-
505
- function resolveTextPropertyFilter(args: {
506
- propertyId: string
507
- branch: "title" | "rich_text" | "url" | "email" | "phone_number"
508
- value: Record<string, unknown>
509
- }): ResolvePropertyFilterResult {
510
- const parsed = v.safeParse(customBlockTextFilterOperatorSchema, args.value)
511
- if (!parsed.success) {
512
- return invalidOperator(args.branch)
513
- }
514
- switch (args.branch) {
515
- case "title":
516
- return {
517
- status: "ok",
518
- filter: { propertyId: args.propertyId, title: parsed.output },
519
- }
520
- case "rich_text":
521
- return {
522
- status: "ok",
523
- filter: { propertyId: args.propertyId, rich_text: parsed.output },
524
- }
525
- case "url":
526
- return {
527
- status: "ok",
528
- filter: { propertyId: args.propertyId, url: parsed.output },
529
- }
530
- case "email":
531
- return {
532
- status: "ok",
533
- filter: { propertyId: args.propertyId, email: parsed.output },
534
- }
535
- case "phone_number":
536
- return {
537
- status: "ok",
538
- filter: { propertyId: args.propertyId, phone_number: parsed.output },
539
- }
540
- default:
541
- return unreachable(args.branch)
542
- }
543
- }
544
-
545
- function resolveNumberPropertyFilter(
546
- propertyId: string,
547
- value: Record<string, unknown>,
548
- ): ResolvePropertyFilterResult {
549
- const parsed = v.safeParse(customBlockNumberFilterOperatorSchema, value)
550
- if (
551
- !parsed.success ||
552
- !Object.values(parsed.output).every(
553
- entry => typeof entry !== "number" || Number.isFinite(entry),
554
- )
555
- ) {
556
- return invalidOperator("number")
557
- }
558
- return {
559
- status: "ok",
560
- filter: { propertyId, number: parsed.output },
561
- }
562
- }
563
-
564
- function resolveCheckboxPropertyFilter(
565
- propertyId: string,
566
- value: Record<string, unknown>,
567
- ): ResolvePropertyFilterResult {
568
- const parsed = v.safeParse(customBlockCheckboxFilterOperatorSchema, value)
569
- return parsed.success
570
- ? { status: "ok", filter: { propertyId, checkbox: parsed.output } }
571
- : invalidOperator("checkbox")
572
- }
573
-
574
- function resolveOptionPropertyFilter(args: {
575
- propertyId: string
576
- branch: "select"
577
- value: Record<string, unknown>
578
- }): ResolvePropertyFilterResult {
579
- const parsed = v.safeParse(customBlockOptionFilterOperatorSchema, args.value)
580
- if (!parsed.success) {
581
- return invalidOperator(args.branch)
582
- }
583
- return {
584
- status: "ok",
585
- filter: { propertyId: args.propertyId, select: parsed.output },
586
- }
587
- }
588
-
589
- function resolveMultiSelectPropertyFilter(
590
- propertyId: string,
591
- value: Record<string, unknown>,
592
- ): ResolvePropertyFilterResult {
593
- const parsed = v.safeParse(customBlockContainsFilterOperatorSchema, value)
594
- return parsed.success
595
- ? { status: "ok", filter: { propertyId, multi_select: parsed.output } }
596
- : invalidOperator("multi_select")
597
- }
598
-
599
- function resolveDatePropertyFilter(
600
- propertyId: string,
601
- value: Record<string, unknown>,
602
- ): ResolvePropertyFilterResult {
603
- const parsed = v.safeParse(customBlockDateFilterOperatorSchema, value)
604
- if (
605
- !parsed.success ||
606
- !Object.entries(parsed.output).every(
607
- ([key, entry]) =>
608
- key === "target" ||
609
- entry === true ||
610
- typeof entry === "object" ||
611
- v.safeParse(customBlockRelativeDateValueSchema, entry).success ||
612
- isValidIsoDate(entry),
613
- )
614
- ) {
615
- return invalidOperator("date")
616
- }
617
- return {
618
- status: "ok",
619
- filter: { propertyId, date: parsed.output },
620
- }
621
- }
622
-
623
- function resolveCreatedByPropertyFilter(
624
- propertyId: string,
625
- value: Record<string, unknown>,
626
- ): ResolvePropertyFilterResult {
627
- const parsed = v.safeParse(customBlockCreatedByFilterOperatorSchema, value)
628
- return parsed.success
629
- ? { status: "ok", filter: { propertyId, created_by: parsed.output } }
630
- : invalidOperator("created_by")
631
- }
632
-
633
- function resolvePeoplePropertyFilter(
634
- propertyId: string,
635
- value: Record<string, unknown>,
636
- ): ResolvePropertyFilterResult {
637
- const parsed = v.safeParse(customBlockPeopleFilterOperatorSchema, value)
638
- return parsed.success
639
- ? { status: "ok", filter: { propertyId, people: parsed.output } }
640
- : invalidOperator("people")
641
- }
642
-
643
- function resolvePropertyAddress(
644
- value: Record<string, unknown>,
645
- dataSource: NotionDataSource,
646
- ):
647
- | { status: "ok"; propertyId: string; propertyType: string }
648
- | { status: "error"; error: string } {
649
- const hasKey = Object.hasOwn(value, "key")
650
- const hasPropertyId = Object.hasOwn(value, "propertyId")
651
- if (hasKey === hasPropertyId) {
652
- return {
653
- status: "error",
654
- error:
655
- "Data source query filters and sorts must use exactly one of key or propertyId.",
656
- }
657
- }
658
- let propertyId: string
659
- if (hasKey) {
660
- if (typeof value.key !== "string") {
661
- return {
662
- status: "error",
663
- error: "Data source query property key must be a string.",
664
- }
665
- }
666
- if (!Object.hasOwn(dataSource.propertyIdsByKey, value.key)) {
667
- return {
668
- status: "error",
669
- error: `Unknown property key "${value.key}" for data source "${dataSource.key}".`,
670
- }
671
- }
672
- const resolvedPropertyId = dataSource.propertyIdsByKey[value.key]
673
- if (resolvedPropertyId === undefined) {
674
- return {
675
- status: "error",
676
- error: `Property key "${value.key}" for data source "${dataSource.key}" is not bound.`,
677
- }
678
- }
679
- propertyId = resolvedPropertyId
680
- } else {
681
- if (typeof value.propertyId !== "string") {
682
- return {
683
- status: "error",
684
- error: "Data source query propertyId must be a string.",
685
- }
686
- }
687
- propertyId = value.propertyId
688
- }
689
- const properties = dataSource.collectionSchema?.propertiesById ?? {}
690
- if (!Object.hasOwn(properties, propertyId)) {
691
- return {
692
- status: "error",
693
- error: `Unknown property ID "${propertyId}" for data source "${dataSource.key}".`,
694
- }
695
- }
696
- const propertySchema = properties[propertyId]
697
- return {
698
- status: "ok",
699
- propertyId,
700
- propertyType: propertySchema.type,
701
- }
702
- }
703
-
704
- function invalidOperator(branch: string): { status: "error"; error: string } {
705
- return {
706
- status: "error",
707
- error: `Data source query filter branch "${branch}" has an invalid operator or value.`,
708
- }
709
- }
710
-
711
217
  function normalizeJsonValue(value: unknown): unknown {
712
218
  if (
713
219
  value === null ||
@@ -750,22 +256,3 @@ function normalizeJsonObject(value: object): Record<string, unknown> {
750
256
  }
751
257
  return normalized
752
258
  }
753
-
754
- function isValidIsoDate(value: unknown): boolean {
755
- if (typeof value !== "string") {
756
- return false
757
- }
758
- const dateOnly = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value)
759
- if (dateOnly !== null) {
760
- const [, year, month, day] = dateOnly
761
- const date = new Date(
762
- Date.UTC(Number(year), Number(month) - 1, Number(day)),
763
- )
764
- return date.toISOString().slice(0, 10) === value
765
- }
766
- return (
767
- /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d{1,3})?)?(?:Z|[+-]\d{2}:\d{2})$/.test(
768
- value,
769
- ) && Number.isFinite(Date.parse(value))
770
- )
771
- }