feat(Notion Node): Allow to ignore Notion URL properties if empty (#3564)

* Allows to ignore Notion URL properties if empty

* Fixes linting

* Fixes another linting error that was not caught locally

* Reorders options alphabetically
This commit is contained in:
Florian Bachmann 2022-07-20 13:34:52 +02:00 committed by GitHub
parent 577c73ee25
commit 6cb9aefb0b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 5 deletions

View file

@ -319,6 +319,19 @@ export const databasePageFields = [
default: '',
description: 'Email address',
},
{
displayName: 'Ignore If Empty',
name: 'ignoreIfEmpty',
type: 'boolean',
displayOptions: {
show: {
type: [
'url',
],
},
},
default: false,
},
{
displayName: 'URL',
name: 'urlValue',
@ -732,6 +745,19 @@ export const databasePageFields = [
},
default: '',
},
{
displayName: 'Ignore If Empty',
name: 'ignoreIfEmpty',
type: 'boolean',
displayOptions: {
show: {
type: [
'url',
],
},
},
default: false,
},
{
displayName: 'URL',
name: 'urlValue',

View file

@ -239,7 +239,9 @@ export function formatBlocks(blocks: IDataObject[]) {
// tslint:disable-next-line: no-any
function getPropertyKeyValue(value: any, type: string, timezone: string, version = 1) {
const ignoreIfEmpty = <T>(v: T, cb: (v: T) => any) => !v && value.ignoreIfEmpty ? undefined : cb(v); // tslint:disable-line: no-any
let result = {};
switch (type) {
case 'rich_text':
if (value.richText === false) {
@ -255,7 +257,7 @@ function getPropertyKeyValue(value: any, type: string, timezone: string, version
result = { type: 'number', number: value.numberValue };
break;
case 'url':
result = { type: 'url', url: value.urlValue };
result = ignoreIfEmpty(value.urlValue, url => ({ type: 'url', url }));
break;
case 'checkbox':
result = { type: 'checkbox', checkbox: value.checkboxValue };
@ -361,9 +363,13 @@ function getNameAndType(key: string) {
}
export function mapProperties(properties: IDataObject[], timezone: string, version = 1) {
return properties.reduce((obj, value) => Object.assign(obj, {
[`${(value.key as string).split('|')[0]}`]: getPropertyKeyValue(value, (value.key as string).split('|')[1], timezone, version),
}), {});
return properties
.filter((property): property is Record<string, { key: string; [k: string]: any }> => typeof property.key === 'string') // tslint:disable-line: no-any
.map(property => [`${property.key.split('|')[0]}`, getPropertyKeyValue(property, property.key.split('|')[1], timezone, version)] as const)
.filter(([, value]) => value)
.reduce((obj, [key, value]) => Object.assign(obj, {
[key]: value,
}), {});
}
export function mapSorting(data: [{ key: string, type: string, direction: string, timestamp: boolean }]) {
@ -425,7 +431,7 @@ function simplifyProperty(property: any) {
result = property.plain_text;
} else if (['rich_text', 'title'].includes(property.type)) {
if (Array.isArray(property[type]) && property[type].length !== 0) {
// tslint:disable-next-line: no-any
// tslint:disable-next-line: no-any
result = property[type].map((text: any) => simplifyProperty(text) as string).join('');
} else {
result = '';