@jitl/notion-api
Version:
The missing companion library for the official Notion public API.
262 lines • 8.09 kB
JavaScript
"use strict";
// Sketch first.
Object.defineProperty(exports, "__esModule", { value: true });
exports.extendQueryParameters = exports.databaseSortBuilder = exports.databaseFilterBuilder = exports.propertyFilterBuilder = exports.ALL_PROPERTY_FILTER_OPERATORS = exports.PROPERTY_FILTER_OPERATORS = exports.ROLLUP_FILTER_OPERATORS = exports.FORMULA_FILTER_OPERATORS = exports.RELATION_FILTER_OPERATORS = exports.PEOPLE_FILTER_OPERATORS = exports.DATE_FILTER_OPERATORS = exports.MULTI_SELECT_FILTER_OPERATORS = exports.SELECT_FILTER_OPERATORS = exports.CHECKBOX_FILTER_OPERATORS = exports.NUMBER_FILTER_OPERATORS = exports.TEXT_FILTER_OPERATORS = exports.EXISTENCE_FILTER_OPERATORS = void 0;
const util_1 = require("@jitl/util");
const notion_api_1 = require("./notion-api");
/**
* Runtime type information for [[ExistenceFilterOperator]].
* @category Query
*/
exports.EXISTENCE_FILTER_OPERATORS = {
is_empty: true,
is_not_empty: true,
};
const EQUALITY_OPERATORS = {
equals: true,
does_not_equal: true,
};
const CONTAINS_OPERATORS = {
contains: true,
does_not_contain: true,
};
/**
* Runtime type information for [[TextFilterOperator]].
* @category Query
*/
exports.TEXT_FILTER_OPERATORS = {
...exports.EXISTENCE_FILTER_OPERATORS,
...EQUALITY_OPERATORS,
...CONTAINS_OPERATORS,
starts_with: true,
ends_with: true,
};
/**
* Runtime type information for [[NumberFilterOperator]].
* @category Query
*/
exports.NUMBER_FILTER_OPERATORS = {
...exports.EXISTENCE_FILTER_OPERATORS,
...EQUALITY_OPERATORS,
greater_than: true,
less_than: true,
greater_than_or_equal_to: true,
less_than_or_equal_to: true,
};
/**
* Runtime type information for [[NumberFilterOperator]].
* @category Query
*/
exports.CHECKBOX_FILTER_OPERATORS = EQUALITY_OPERATORS;
/**
* Runtime type information for [[SelectFilterOperator]].
* @category Query
*/
exports.SELECT_FILTER_OPERATORS = {
...exports.EXISTENCE_FILTER_OPERATORS,
...EQUALITY_OPERATORS,
};
/**
* Runtime type information for [[MultiSelectFilterOperator]].
* @category Query
*/
exports.MULTI_SELECT_FILTER_OPERATORS = {
...exports.EXISTENCE_FILTER_OPERATORS,
...CONTAINS_OPERATORS,
};
/**
* Runtime type information for [[DateFilterOperator]].
* @category Query
*/
exports.DATE_FILTER_OPERATORS = {
...exports.EXISTENCE_FILTER_OPERATORS,
equals: true,
before: true,
after: true,
on_or_before: true,
on_or_after: true,
past_week: true,
past_month: true,
past_year: true,
next_week: true,
next_month: true,
next_year: true,
};
/**
* Runtime type information for [[PeopleFilterOperator]].
* @category Query
*/
exports.PEOPLE_FILTER_OPERATORS = {
...exports.EXISTENCE_FILTER_OPERATORS,
...CONTAINS_OPERATORS,
};
/**
* Runtime type information for [[RelationFilterOperator]].
* @category Query
*/
exports.RELATION_FILTER_OPERATORS = {
...exports.EXISTENCE_FILTER_OPERATORS,
...CONTAINS_OPERATORS,
};
{
}
/**
* Runtime type information for [[FormulaFilterOperator]].
* @category Query
*/
exports.FORMULA_FILTER_OPERATORS = {
string: true,
checkbox: true,
number: true,
date: true,
};
/**
* Runtime type information for [[RollupFilterOperator]].
* @category Query
*/
exports.ROLLUP_FILTER_OPERATORS = {
any: true,
none: true,
every: true,
date: true,
number: true,
};
/**
* Runtime type information for [[PropertyToToFilterOperator]].
* @category Query
*/
exports.PROPERTY_FILTER_OPERATORS = {
number: exports.NUMBER_FILTER_OPERATORS,
title: exports.TEXT_FILTER_OPERATORS,
rich_text: exports.TEXT_FILTER_OPERATORS,
url: exports.TEXT_FILTER_OPERATORS,
select: exports.SELECT_FILTER_OPERATORS,
multi_select: exports.MULTI_SELECT_FILTER_OPERATORS,
people: exports.PEOPLE_FILTER_OPERATORS,
email: exports.TEXT_FILTER_OPERATORS,
phone_number: exports.TEXT_FILTER_OPERATORS,
date: exports.DATE_FILTER_OPERATORS,
files: exports.EXISTENCE_FILTER_OPERATORS,
checkbox: exports.CHECKBOX_FILTER_OPERATORS,
formula: exports.FORMULA_FILTER_OPERATORS,
relation: exports.RELATION_FILTER_OPERATORS,
created_time: exports.DATE_FILTER_OPERATORS,
created_by: exports.PEOPLE_FILTER_OPERATORS,
last_edited_time: exports.DATE_FILTER_OPERATORS,
last_edited_by: exports.PEOPLE_FILTER_OPERATORS,
rollup: exports.ROLLUP_FILTER_OPERATORS,
};
{
}
/**
* Runtime information for all known filter operators.
* @category Query
*/
exports.ALL_PROPERTY_FILTER_OPERATORS = Object.assign({}, ...Object.values(exports.PROPERTY_FILTER_OPERATORS));
function buildPropertyFilter(pointer, operator, value) {
// @ts-expect-error Too complex
const operatorValue = {
[operator]: value,
};
// @ts-expect-error Too complex
return {
property: pointer.id || pointer.name,
type: pointer.type,
[pointer.type]: operatorValue,
};
}
/**
* Helper object for building [[PropertyFilter]]s for the given `property`.
* @param property Property to build a filter for.
* @returns a property filter builder.
* @category Query
*/
function propertyFilterBuilder(property) {
const builders = Object.fromEntries((0, util_1.objectEntries)(exports.PROPERTY_FILTER_OPERATORS[property.type]).map(([operator]) => [
operator,
(value) => buildPropertyFilter(property, operator, value),
]));
builders.schema = property;
return builders;
}
exports.propertyFilterBuilder = propertyFilterBuilder;
{
const builder = propertyFilterBuilder({ type: 'number', id: 'foo', name: 'bar' });
builder.does_not_equal(1);
}
/**
* Helper object for building [[PropertyFilter]]s for the properties in the given `schema`.
* @param schema Database schema to build filters for.
* @returns A property filter builder for schema property, plus compound filter builders.
* @category Query
* @category Database
*/
function databaseFilterBuilder(schema) {
const properties = Object.fromEntries((0, util_1.objectEntries)(schema).map(([key, property]) => {
const builder = propertyFilterBuilder(property);
const builderWithSchema = {
...builder,
schema: property,
};
return [key, builderWithSchema];
}));
return {
...notion_api_1.Filter,
...properties,
schema,
};
}
exports.databaseFilterBuilder = databaseFilterBuilder;
/**
* @category Query
*/
function databaseSortBuilder(schema) {
const properties = Object.fromEntries((0, util_1.objectEntries)(schema).map(([key, property]) => {
const builder = {
ascending: {
property: property.id || property.name,
direction: 'ascending',
},
descending: {
property: property.id || property.name,
direction: 'descending',
},
};
const builderWithSchema = {
...builder,
schema: property,
};
return [key, builderWithSchema];
}));
return {
...properties,
created_time: notion_api_1.Sort.created_time,
last_edited_time: notion_api_1.Sort.last_edited_time,
};
}
exports.databaseSortBuilder = databaseSortBuilder;
////////////////////////////////////////////////////////////////////////////////
// Query scope
////////////////////////////////////////////////////////////////////////////////
/**
* Extend a base query with additional filters, sorts, etc.
* Filters will be `and`ed together and sorts concatenated.
* @category Query
* @param base
* @param extension
* @returns
*/
function extendQueryParameters(base, extension) {
const filter = notion_api_1.Filter.and(base.filter, extension.filter);
const sorts = base.sorts && extension.sorts
? [...extension.sorts, ...base.sorts]
: extension.sorts || base.sorts;
return {
...base,
...extension,
filter,
sorts,
};
}
exports.extendQueryParameters = extendQueryParameters;
//# sourceMappingURL=query.js.map