n8n/packages/nodes-base/nodes/Supabase/GenericFunctions.ts
Iván Ovejero 0448feec56
refactor: Apply eslint-plugin-n8n-nodes-base autofixable rules (#3174)
*  Initial setup

* 👕 Update `.eslintignore`

* 👕 Autofix node-param-default-missing (#3173)

* 🔥 Remove duplicate key

* 👕 Add exceptions

* 📦 Update package-lock.json

* 👕 Apply `node-class-description-inputs-wrong-trigger-node` (#3176)

* 👕 Apply `node-class-description-inputs-wrong-regular-node` (#3177)

* 👕 Apply `node-class-description-outputs-wrong` (#3178)

* 👕 Apply `node-execute-block-double-assertion-for-items` (#3179)

* 👕 Apply `node-param-default-wrong-for-collection` (#3180)

* 👕 Apply node-param-default-wrong-for-boolean (#3181)

* Autofixed default missing

* Autofixed booleans, worked well

*  Fix params

*  Undo exempted autofixes

* 📦 Update package-lock.json

* 👕 Apply node-class-description-missing-subtitle (#3182)

*  Fix missing comma

* 👕 Apply `node-param-default-wrong-for-fixed-collection` (#3184)

* 👕 Add exception for `node-class-description-missing-subtitle`

* 👕 Apply `node-param-default-wrong-for-multi-options` (#3185)

* 👕 Apply `node-param-collection-type-unsorted-items` (#3186)

* Missing coma

* 👕 Apply `node-param-default-wrong-for-simplify` (#3187)

* 👕 Apply `node-param-description-comma-separated-hyphen` (#3190)

* 👕 Apply `node-param-description-empty-string` (#3189)

* 👕 Apply `node-param-description-excess-inner-whitespace` (#3191)

* Rule looks good

* Add whitespace rule in eslint config

* :zao: fix

* 👕 Apply `node-param-description-identical-to-display-name` (#3193)

* 👕 Apply `node-param-description-missing-for-ignore-ssl-issues` (#3195)

*  Revert ":zao: fix"

This reverts commit ef8a76f3df.

* 👕 Apply `node-param-description-missing-for-simplify`  (#3196)

* 👕 Apply `node-param-description-missing-final-period` (#3194)

* Rule working as intended

* Add rule to eslint

* 👕 Apply node-param-description-missing-for-return-all (#3197)

*  Restore `lintfix` command

Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com>
Co-authored-by: Omar Ajoue <krynble@gmail.com>
Co-authored-by: agobrech <ael.gobrecht@gmail.com>
Co-authored-by: Michael Kret <michael.k@radency.com>
2022-04-22 18:29:51 +02:00

321 lines
7 KiB
TypeScript

import {
OptionsWithUri,
} from 'request';
import {
IExecuteFunctions,
IExecuteSingleFunctions,
IHookFunctions,
ILoadOptionsFunctions,
IWebhookFunctions,
} from 'n8n-core';
import {
ICredentialDataDecryptedObject,
ICredentialTestFunctions,
IDataObject,
INodeProperties,
NodeApiError,
} from 'n8n-workflow';
export async function supabaseApiRequest(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions | IHookFunctions | IWebhookFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, headers: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const credentials = await this.getCredentials('supabaseApi') as { host: string, serviceRole: string };
const options: OptionsWithUri = {
headers: {
apikey: credentials.serviceRole,
Authorization: 'Bearer ' + credentials.serviceRole,
Prefer: 'return=representation',
},
method,
qs,
body,
uri: uri || `${credentials.host}/rest/v1${resource}`,
json: true,
};
try {
if (Object.keys(headers).length !== 0) {
options.headers = Object.assign({}, options.headers, headers);
}
if (Object.keys(body).length === 0) {
delete options.body;
}
//@ts-ignore
return await this.helpers?.request(options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
}
const mapOperations: { [key: string]: string } = {
'create': 'created',
'update': 'updated',
'getAll': 'retrieved',
'delete': 'deleted',
};
export function getFilters(
resources: string[],
operations: string[],
{
includeNoneOption = true,
filterTypeDisplayName = 'Filter',
filterFixedCollectionDisplayName = 'Filters',
filterStringDisplayName = 'Filters (String)',
mustMatchOptions = [
{
name: 'Any Filter',
value: 'anyFilter',
},
{
name: 'All Filters',
value: 'allFilters',
},
],
}): INodeProperties[] {
return [
{
displayName: filterTypeDisplayName,
name: 'filterType',
type: 'options',
options: [
...(includeNoneOption ? [{ name: 'None', value: 'none' }] : []),
{
name: 'Build Manually',
value: 'manual',
},
{
name: 'String',
value: 'string',
},
],
displayOptions: {
show: {
resource: resources,
operation: operations,
},
},
default: 'manual',
},
{
displayName: 'Must Match',
name: 'matchType',
type: 'options',
options: mustMatchOptions,
displayOptions: {
show: {
resource: resources,
operation: operations,
filterType: [
'manual',
],
},
},
default: 'anyFilter',
},
{
displayName: filterFixedCollectionDisplayName,
name: 'filters',
type: 'fixedCollection',
typeOptions: {
multipleValues: true,
},
displayOptions: {
show: {
resource: resources,
operation: operations,
filterType: [
'manual',
],
},
},
default: {},
placeholder: 'Add Condition',
options: [
{
displayName: 'Conditions',
name: 'conditions',
values: [
{
displayName: 'Field Name',
name: 'keyName',
type: 'options',
typeOptions: {
loadOptionsDependsOn: [
'tableId',
],
loadOptionsMethod: 'getTableColumns',
},
default: '',
},
{
displayName: 'Condition',
name: 'condition',
type: 'options',
options: [
{
name: 'Equals',
value: 'eq',
},
{
name: 'Greater Than',
value: 'gt',
},
{
name: 'Greater Than or Equal',
value: 'gte',
},
{
name: 'Less Than',
value: 'lt',
},
{
name: 'Less Than or Equal',
value: 'lte',
},
{
name: 'Not Equals',
value: 'neq',
},
{
name: 'LIKE operator',
value: 'like',
description: 'use * in place of %',
},
{
name: 'ILIKE operator',
value: 'ilike',
description: 'use * in place of %',
},
{
name: 'Is',
value: 'is',
description: 'Checking for exact equality (null,true,false,unknown)',
},
{
name: 'Full-Text',
value: 'fullText',
},
],
default: '',
},
{
displayName: 'Search Function',
name: 'searchFunction',
type: 'options',
displayOptions: {
show: {
condition: [
'fullText',
],
},
},
options: [
{
name: 'to_tsquery',
value: 'fts',
},
{
name: 'plainto_tsquery',
value: 'plfts',
},
{
name: 'phraseto_tsquery',
value: 'phfts',
},
{
name: 'websearch_to_tsquery',
value: 'wfts',
},
],
default: '',
},
{
displayName: 'Field Value',
name: 'keyValue',
type: 'string',
default: '',
},
],
},
],
description: `Filter to decide which rows get ${mapOperations[operations[0] as string]}`,
},
{
displayName: 'See <a href="https://postgrest.org/en/v9.0/api.html#horizontal-filtering-rows" target="_blank">PostgREST guide</a> to creating filters',
name: 'jsonNotice',
type: 'notice',
displayOptions: {
show: {
resource: resources,
operation: operations,
filterType: [
'string',
],
},
},
default: '',
},
{
displayName: 'Filters (String)',
name: 'filterString',
type: 'string',
typeOptions: {
alwaysOpenEditWindow: true,
},
displayOptions: {
show: {
resource: resources,
operation: operations,
filterType: [
'string',
],
},
},
default: '',
placeholder: 'name=eq.jhon',
},
];
}
export const buildQuery = (obj: IDataObject, value: IDataObject) => {
if (value.condition === 'fullText') {
return Object.assign(obj, { [`${value.keyName}`]: `${value.searchFunction}.${value.keyValue}` });
}
return Object.assign(obj, { [`${value.keyName}`]: `${value.condition}.${value.keyValue}` });
};
export const buildOrQuery = (key: IDataObject) => {
if (key.condition === 'fullText') {
return `${key.keyName}.${key.searchFunction}.${key.keyValue}`;
}
return `${key.keyName}.${key.condition}.${key.keyValue}`;
};
export const buildGetQuery = (obj: IDataObject, value: IDataObject) => {
return Object.assign(obj, { [`${value.keyName}`]: `eq.${value.keyValue}` });
};
export async function validateCredentials(
this: ICredentialTestFunctions,
decryptedCredentials: ICredentialDataDecryptedObject): Promise<any> { // tslint:disable-line:no-any
const credentials = decryptedCredentials;
const { serviceRole } = credentials as {
serviceRole: string,
};
const options: OptionsWithUri = {
headers: {
apikey: serviceRole,
Authorization: 'Bearer ' + serviceRole,
},
method: 'GET',
uri: `${credentials.host}/rest/v1/`,
json: true,
};
return this.helpers.request!(options);
}