@notionhq/custom-blocks 0.3.3 → 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.
- package/LICENSE +21 -0
- package/README.md +11 -11
- package/dist/bridge/SandboxBridge.d.ts +0 -2
- package/dist/bridge/SandboxBridge.d.ts.map +1 -1
- package/dist/bridge/SandboxBridge.js +5 -8
- package/dist/bridge/SandboxInitialization.d.ts +1 -3
- package/dist/bridge/SandboxInitialization.d.ts.map +1 -1
- package/dist/bridge/SandboxInitialization.js +13 -15
- package/dist/bridge/appearance.d.ts +2 -10
- package/dist/bridge/appearance.d.ts.map +1 -1
- package/dist/bridge/appearance.js +2 -10
- package/dist/bridge/dataSources/query.d.ts +1 -2
- package/dist/bridge/dataSources/query.d.ts.map +1 -1
- package/dist/bridge/dataSources/query.js +15 -398
- package/dist/bridge/dataSources/queryFilter.d.ts +10 -0
- package/dist/bridge/dataSources/queryFilter.d.ts.map +1 -0
- package/dist/bridge/dataSources/queryFilter.js +123 -0
- package/dist/bridge/dataSources/resolvePropertyAddress.d.ts +10 -0
- package/dist/bridge/dataSources/resolvePropertyAddress.d.ts.map +1 -0
- package/dist/bridge/dataSources/resolvePropertyAddress.js +55 -0
- package/dist/bridge/dataSources/resolveSorts.d.ts +10 -0
- package/dist/bridge/dataSources/resolveSorts.d.ts.map +1 -0
- package/dist/bridge/dataSources/resolveSorts.js +80 -0
- package/dist/bridge/hostState.d.ts +3 -7
- package/dist/bridge/hostState.d.ts.map +1 -1
- package/dist/bridge/hostState.js +0 -24
- package/dist/bridge/page.d.ts +3 -3
- package/dist/bridge/page.d.ts.map +1 -1
- package/dist/bridge/page.js +2 -2
- package/dist/bridge/readLocalManifest.d.ts +23 -0
- package/dist/bridge/readLocalManifest.d.ts.map +1 -0
- package/dist/bridge/readLocalManifest.js +46 -0
- package/dist/bridge/sandboxClient.d.ts +0 -2
- package/dist/bridge/sandboxClient.d.ts.map +1 -1
- package/dist/bridge/sandboxClient.js +50 -27
- package/dist/protocol/dataSources/dataSourcePage.d.ts +3 -4
- package/dist/protocol/dataSources/dataSourcePage.js +2 -3
- package/dist/protocol/index.d.ts +1 -0
- package/dist/protocol/index.js +1 -0
- package/dist/protocol/isLocalBlockServerUrl.d.ts +2 -0
- package/dist/protocol/isLocalBlockServerUrl.js +9 -0
- package/dist/protocol/messages/connect.d.ts +2 -2
- package/dist/protocol/messages/connect.js +2 -2
- package/dist/protocol/messages/createPage.d.ts +1 -1
- package/dist/protocol/messages/createPage.js +1 -1
- package/dist/protocol/messages/createPageResult.d.ts +3 -3
- package/dist/protocol/messages/createPageResult.js +4 -4
- package/dist/protocol/messages/getPage.d.ts +2 -2
- package/dist/protocol/messages/getPage.js +4 -4
- package/dist/protocol/messages/hostToSandbox.d.ts +3 -3
- package/dist/protocol/messages/hostToSandbox.js +10 -10
- package/dist/protocol/messages/init.d.ts +1 -1
- package/dist/protocol/messages/queryDataSource.d.ts +24 -0
- package/dist/protocol/messages/queryDataSource.js +2 -0
- package/dist/protocol/messages/queryDataSourceResult.d.ts +3 -3
- package/dist/protocol/messages/queryDataSourceResult.js +5 -5
- package/dist/protocol/messages/sandboxToHost.d.ts +8 -0
- package/dist/protocol/messages/updatePageResult.d.ts +3 -3
- package/dist/protocol/messages/updatePageResult.js +4 -4
- package/dist/protocol/pages/page.d.ts +4 -4
- package/dist/protocol/pages/page.js +2 -2
- package/dist/protocol/parent.d.ts +1 -1
- package/dist/protocol/protocolVersion.d.ts +2 -2
- package/dist/protocol/protocolVersion.js +2 -2
- package/dist/react/useDataSource.d.ts +2 -2
- package/dist/react/useDataSource.js +2 -2
- package/dist/types.d.ts +5 -5
- package/dist/types.d.ts.map +1 -1
- package/dist/version.js +1 -1
- package/docs/block-location.md +6 -4
- package/docs/bridge-interceptors.md +4 -0
- package/docs/build-tools.md +5 -2
- package/docs/data-sources.md +21 -17
- package/docs/deployment.md +16 -11
- package/docs/errors.md +11 -1
- package/docs/lifecycle.md +10 -3
- package/docs/pages.md +7 -3
- package/docs/users.md +4 -4
- package/package.json +3 -2
- package/src/bridge/SandboxBridge.ts +5 -10
- package/src/bridge/SandboxInitialization.ts +14 -20
- package/src/bridge/appearance.ts +2 -10
- package/src/bridge/dataSources/query.ts +17 -530
- package/src/bridge/dataSources/queryFilter.ts +181 -0
- package/src/bridge/dataSources/resolvePropertyAddress.ts +62 -0
- package/src/bridge/dataSources/resolveSorts.ts +107 -0
- package/src/bridge/hostState.ts +3 -43
- package/src/bridge/page.ts +4 -4
- package/src/bridge/readLocalManifest.ts +60 -0
- package/src/bridge/sandboxClient.ts +63 -41
- package/src/react/useDataSource.ts +2 -2
- package/src/types.ts +5 -5
- package/dist/bridge/loadManifest.d.ts +0 -21
- package/dist/bridge/loadManifest.d.ts.map +0 -1
- package/dist/bridge/loadManifest.js +0 -85
- package/src/bridge/loadManifest.ts +0 -114
|
@@ -1,14 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
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
|
|
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
|
|
50
|
+
const limit = resolveDataSourceQueryLimit(queryOptions?.limit);
|
|
57
51
|
if (limit.status === "error") {
|
|
58
|
-
return {
|
|
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
|
|
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:
|
|
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
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { NotionDataSource } from "../../protocol/dataSources/dataSource.js";
|
|
2
|
+
export declare function resolvePropertyAddress(value: Record<string, unknown>, dataSource: NotionDataSource): {
|
|
3
|
+
status: "ok";
|
|
4
|
+
propertyId: string;
|
|
5
|
+
propertyType: string;
|
|
6
|
+
} | {
|
|
7
|
+
status: "error";
|
|
8
|
+
error: string;
|
|
9
|
+
};
|
|
10
|
+
//# sourceMappingURL=resolvePropertyAddress.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolvePropertyAddress.d.ts","sourceRoot":"","sources":["../../../src/bridge/dataSources/resolvePropertyAddress.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,4DAA4D,CAAA;AAElG,wBAAgB,sBAAsB,CACrC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC9B,UAAU,EAAE,gBAAgB,GAE1B;IAAE,MAAM,EAAE,IAAI,CAAC;IAAC,UAAU,EAAE,MAAM,CAAC;IAAC,YAAY,EAAE,MAAM,CAAA;CAAE,GAC1D;IAAE,MAAM,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAsDpC"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
export function resolvePropertyAddress(value, dataSource) {
|
|
2
|
+
const hasKey = Object.hasOwn(value, "key");
|
|
3
|
+
const hasPropertyId = Object.hasOwn(value, "propertyId");
|
|
4
|
+
if (hasKey === hasPropertyId) {
|
|
5
|
+
return {
|
|
6
|
+
status: "error",
|
|
7
|
+
error: "Data source query filters and sorts must use exactly one of key or propertyId.",
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
let propertyId;
|
|
11
|
+
if (hasKey) {
|
|
12
|
+
if (typeof value.key !== "string") {
|
|
13
|
+
return {
|
|
14
|
+
status: "error",
|
|
15
|
+
error: "Data source query property key must be a string.",
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
if (!Object.hasOwn(dataSource.propertyIdsByKey, value.key)) {
|
|
19
|
+
return {
|
|
20
|
+
status: "error",
|
|
21
|
+
error: `Unknown property key "${value.key}" for data source "${dataSource.key}".`,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
const resolvedPropertyId = dataSource.propertyIdsByKey[value.key];
|
|
25
|
+
if (resolvedPropertyId === undefined) {
|
|
26
|
+
return {
|
|
27
|
+
status: "error",
|
|
28
|
+
error: `Property key "${value.key}" for data source "${dataSource.key}" is not bound.`,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
propertyId = resolvedPropertyId;
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
if (typeof value.propertyId !== "string") {
|
|
35
|
+
return {
|
|
36
|
+
status: "error",
|
|
37
|
+
error: "Data source query propertyId must be a string.",
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
propertyId = value.propertyId;
|
|
41
|
+
}
|
|
42
|
+
const properties = dataSource.collectionSchema?.propertiesById ?? {};
|
|
43
|
+
if (!Object.hasOwn(properties, propertyId)) {
|
|
44
|
+
return {
|
|
45
|
+
status: "error",
|
|
46
|
+
error: `Unknown property ID "${propertyId}" for data source "${dataSource.key}".`,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
const propertySchema = properties[propertyId];
|
|
50
|
+
return {
|
|
51
|
+
status: "ok",
|
|
52
|
+
propertyId,
|
|
53
|
+
propertyType: propertySchema.type,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { NotionDataSource } from "../../protocol/dataSources/dataSource.js";
|
|
2
|
+
import { type ResolvedCustomBlockDataSourceSort } from "../../protocol/messages/queryDataSource.js";
|
|
3
|
+
export declare function resolveSorts(sorts: unknown, dataSource: NotionDataSource): {
|
|
4
|
+
status: "ok";
|
|
5
|
+
sorts?: ResolvedCustomBlockDataSourceSort[];
|
|
6
|
+
} | {
|
|
7
|
+
status: "error";
|
|
8
|
+
error: string;
|
|
9
|
+
};
|
|
10
|
+
//# sourceMappingURL=resolveSorts.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolveSorts.d.ts","sourceRoot":"","sources":["../../../src/bridge/dataSources/resolveSorts.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,4DAA4D,CAAA;AAGlG,OAAO,EAEN,KAAK,iCAAiC,EACtC,MAAM,8DAA8D,CAAA;AAQrE,wBAAgB,YAAY,CAC3B,KAAK,EAAE,OAAO,EACd,UAAU,EAAE,gBAAgB,GAE1B;IAAE,MAAM,EAAE,IAAI,CAAC;IAAC,KAAK,CAAC,EAAE,iCAAiC,EAAE,CAAA;CAAE,GAC7D;IAAE,MAAM,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAqCpC"}
|