fix "Make an API call"

This commit is contained in:
Christoph Dyllick-Brenzinger 2023-12-21 22:44:07 +01:00
parent 03594050f1
commit 9765219002
3 changed files with 48 additions and 24 deletions

View file

@ -1,25 +1,6 @@
import type { BaseProperties } from '../../Interfaces';
export const baseApiCallDescription: BaseProperties = [
{
displayName: 'Table Name',
name: 'tableName',
type: 'options',
placeholder: 'Select a table',
required: true,
typeOptions: {
loadOptionsMethod: 'getTableNames',
},
displayOptions: {
show: {
resource: ['base'],
operation: ['apiCall'],
},
},
default: '',
description:
'The name of SeaTable table to access. Choose from the list, or specify a name using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.',
},
{
displayName: 'HTTP Method',
name: 'apiMethod',
@ -34,8 +15,8 @@ export const baseApiCallDescription: BaseProperties = [
value: 'GET',
},
{
name: 'POST',
value: 'POST',
name: 'PUT',
value: 'PUT',
},
{
name: 'DELETE',
@ -133,4 +114,20 @@ export const baseApiCallDescription: BaseProperties = [
description:
'Only valid JSON is accepted. n8n will pass anything you enter as raw input. For example, {"foo", "bar"} is perfectly valid. Of cause you can use variables from n8n inside your JSON.',
},
{
displayName: 'Response object parameter name',
name: 'responseObjectName',
type: 'string',
placeholder: 'Leave it empty or use a value like "rows", "metadata", "views" etc.',
required: false,
displayOptions: {
show: {
resource: ['base'],
operation: ['apiCall'],
},
},
default: '',
description:
'When using the SeaTable API, you can specify a parameter to retrieve either the entire array of objects or a specific object within it. This allows you to choose whether to fetch the complete output or only the object related to the provided parameter.',
},
];

View file

@ -1,15 +1,40 @@
import type { IExecuteFunctions, IDataObject, INodeExecutionData } from 'n8n-workflow';
import { seaTableApiRequest } from '../../../GenericFunctions';
import { APITypes } from '../../../types';
export async function apiCall(
this: IExecuteFunctions,
index: number,
): Promise<INodeExecutionData[]> {
const apiMethod = this.getNodeParameter('apiMethod', index) as APITypes;
const apiEndpoint = this.getNodeParameter('apiEndpoint', index) as APITypes;
const responseObjectName = this.getNodeParameter('responseObjectName', index) as string;
// body params
const apiBody = this.getNodeParameter('apiBody', index) as any;
// query params
const apiParams: IDataObject = {};
const params = this.getNodeParameter('apiParams.apiParamsValues', index, []) as any;
for (const param of params) {
apiParams[`${param.key}`] = param.value;
}
console.log(apiParams);
const responseData = await seaTableApiRequest.call(
this,
{},
'GET',
'/dtable-server/api/v1/dtables/{{dtable_uuid}}/metadata/',
apiMethod,
apiEndpoint,
apiBody,
apiParams,
);
return this.helpers.returnJsonArray(responseData.metadata as IDataObject[]);
console.log(responseData);
// output
if (responseObjectName) {
return this.helpers.returnJsonArray(responseData[responseObjectName] as IDataObject[]);
} else {
return this.helpers.returnJsonArray(responseData as IDataObject[]);
}
}

View file

@ -95,3 +95,5 @@ export type TColumnsUiValues = Array<{
columnName: string;
columnValue: string;
}>;
export type APITypes = 'GET' | 'POST' | 'DELETE' | 'PUT';