feat(baserow): upload files via url

This commit is contained in:
Cedric Ziel 2024-07-28 10:27:15 +02:00
parent ce1ae6a7c0
commit 3d43f7ff5f
4 changed files with 100 additions and 10 deletions

View file

@ -1,6 +1,6 @@
{
"node": "n8n-nodes-base.baserow",
"nodeVersion": "1.0",
"nodeVersion": "1.1",
"codexVersion": "1.0",
"categories": ["Data & Storage"],
"resources": {

View file

@ -20,9 +20,11 @@ import { operationFields } from './OperationDescription';
import type {
BaserowCredentials,
FieldsUiValues,
FileOperation,
GetAllAdditionalOptions,
LoadedResource,
Operation,
Resource,
RowOperation,
Row,
} from './types';
@ -53,6 +55,10 @@ export class Baserow implements INodeType {
type: 'options',
noDataExpression: true,
options: [
{
name: 'File',
value: 'file',
},
{
name: 'Row',
value: 'row',
@ -104,6 +110,32 @@ export class Baserow implements INodeType {
],
default: 'getAll',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: ['file'],
},
},
options: [
{
name: 'Upload',
value: 'upload',
description: 'Upload a file',
action: 'Upload a file',
},
{
name: 'Upload via URL',
value: 'upload-via-url',
description: 'Upload a file via URL',
action: 'Upload a file via URL',
},
],
default: 'upload',
},
...operationFields,
],
};
@ -157,7 +189,8 @@ export class Baserow implements INodeType {
const items = this.getInputData();
const mapper = new TableFieldMapper();
const returnData: INodeExecutionData[] = [];
const operation = this.getNodeParameter('operation', 0) as Operation;
const resource = this.getNodeParameter('resource', 0) as Resource;
const operation = this.getNodeParameter('operation', 0) as RowOperation | FileOperation;
const tableId = this.getNodeParameter('tableId', 0) as string;
const credentials = (await this.getCredentials('baserowApi')) as BaserowCredentials;
@ -167,7 +200,7 @@ export class Baserow implements INodeType {
for (let i = 0; i < items.length; i++) {
try {
if (operation === 'getAll') {
if (resource === 'row' && operation === 'getAll') {
// ----------------------------------
// getAll
// ----------------------------------
@ -217,7 +250,7 @@ export class Baserow implements INodeType {
{ itemData: { item: i } },
);
returnData.push(...executionData);
} else if (operation === 'get') {
} else if (resource === 'row' && operation === 'get') {
// ----------------------------------
// get
// ----------------------------------
@ -234,7 +267,7 @@ export class Baserow implements INodeType {
{ itemData: { item: i } },
);
returnData.push(...executionData);
} else if (operation === 'create') {
} else if (resource === 'row' && operation === 'create') {
// ----------------------------------
// create
// ----------------------------------
@ -273,7 +306,7 @@ export class Baserow implements INodeType {
{ itemData: { item: i } },
);
returnData.push(...executionData);
} else if (operation === 'update') {
} else if (resource === 'row' && operation === 'update') {
// ----------------------------------
// update
// ----------------------------------
@ -314,7 +347,7 @@ export class Baserow implements INodeType {
{ itemData: { item: i } },
);
returnData.push(...executionData);
} else if (operation === 'delete') {
} else if (resource === 'row' && operation === 'delete') {
// ----------------------------------
// delete
// ----------------------------------
@ -331,6 +364,23 @@ export class Baserow implements INodeType {
{ itemData: { item: i } },
);
returnData.push(...executionData);
} else if (resource === 'file' && operation === 'upload-via-url') {
// ----------------------------------
// upload-via-url
// ----------------------------------
// https://api.baserow.io/api/redoc/#tag/User-files/operation/upload_via_url
const url = this.getNodeParameter('url', i) as string;
const endpoint = '/api/user-files/upload-via-url/';
const body = { url };
const file = await baserowApiRequest.call(this, 'POST', endpoint, jwtToken, body);
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray(file),
{ itemData: { item: i } },
);
returnData.push(...executionData);
}
} catch (error) {
if (this.continueOnFail()) {

View file

@ -6,6 +6,11 @@ export const operationFields: INodeProperties[] = [
// ----------------------------------
{
displayName: 'Database Name or ID',
displayOptions: {
show: {
resource: ['row'],
},
},
name: 'databaseId',
type: 'options',
default: '',
@ -18,6 +23,11 @@ export const operationFields: INodeProperties[] = [
},
{
displayName: 'Table Name or ID',
displayOptions: {
show: {
resource: ['row'],
},
},
name: 'tableId',
type: 'options',
default: '',
@ -442,4 +452,32 @@ export const operationFields: INodeProperties[] = [
},
],
},
{
displayName: 'File',
displayOptions: {
show: {
resource: ['file'],
operation: ['upload'],
},
},
name: 'file',
type: 'resourceLocator',
default: '',
required: true,
description: 'The file to upload',
},
{
displayName: 'File URL',
displayOptions: {
show: {
resource: ['file'],
operation: ['upload-via-url'],
},
},
name: 'url',
type: 'string',
default: '',
required: true,
description: 'The URL of the file to upload',
},
];

View file

@ -35,7 +35,9 @@ export type Row = Record<string, string>;
export type FieldsUiValues = Array<{
fieldId: string;
fieldValue: string;
fieldValue: string | string[];
}>;
export type Operation = 'create' | 'delete' | 'update' | 'get' | 'getAll';
export type Resource = 'file' | 'row' | 'table' | 'database';
export type RowOperation = 'create' | 'delete' | 'update' | 'get' | 'getAll';
export type FileOperation = 'upload' | 'upload-via-url';