Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

N8N-3206 add xc token to nocodb #3073

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/cli/BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
# n8n Breaking Changes

This list shows all the versions which include breaking changes and how to upgrade.
## 0.172.0

### What changed?

Renamed credentials in the NocoDB node from nocoDb to nocoDbUserToken.
### When is action necessary?

If you are using the NocoDB node.

### How to upgrade:

Go to the workflows that use the NocoDB node and reintroduce the credentials.
## 0.171.0

### What changed?
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/src/CredentialsHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,9 @@ export class CredentialsHelper extends ICredentialsHelper {
for (const rule of credentialTestFunction.testRequest.rules) {
if (rule.type === 'responseSuccessBody') {
const responseData = response![0][0].json;
if (get(responseData, rule.properties.key) === rule.properties.value) {
const key = get(responseData, rule.properties.key);
// if key is not present in the response or it's but matches the condition
if (key === undefined || key === rule.properties.value) {
return {
status: 'Error',
message: rule.properties.message,
Expand Down
26 changes: 0 additions & 26 deletions packages/nodes-base/credentials/NocoDb.credentials.ts

This file was deleted.

51 changes: 51 additions & 0 deletions packages/nodes-base/credentials/NocoDbApiToken.credentials.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {
IAuthenticateHeaderAuth,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';


export class NocoDbApiToken implements ICredentialType {
name = 'nocoDbApiToken';
displayName = 'NocoDB API Token';
documentationUrl = 'nocoDb';
properties: INodeProperties[] = [
{
displayName: 'API Token',
name: 'apiToken',
type: 'string',
default: '',
},
{
displayName: 'Host',
name: 'host',
type: 'string',
default: '',
placeholder: 'http(s)://localhost:8080',
},
];
authenticate: IAuthenticateHeaderAuth = {
type: 'headerAuth',
properties: {
name: 'xc-token',
value: '={{$credentials.apiToken}}',
},
};
test: ICredentialTestRequest = {
request: {
baseURL: '={{$credentials?.host}}',
url: '/user/me',
},
rules: [
{
type: 'responseSuccessBody',
properties: {
key: 'isAuthorized',
value: false,
message: 'Invalid API Token',
},
},
],
};
}
51 changes: 51 additions & 0 deletions packages/nodes-base/credentials/NocoDbUserToken.credentials.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {
IAuthenticateHeaderAuth,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';


export class NocoDbUserToken implements ICredentialType {
name = 'nocoDbUserToken';
displayName = 'NocoDB User Token';
documentationUrl = 'nocoDb';
properties: INodeProperties[] = [
{
displayName: 'User Token',
name: 'userToken',
type: 'string',
default: '',
},
{
displayName: 'Host',
name: 'host',
type: 'string',
default: '',
placeholder: 'http(s)://localhost:8080',
},
];
authenticate: IAuthenticateHeaderAuth = {
type: 'headerAuth',
properties: {
name: 'xc-auth',
value: '={{$credentials.userToken}}',
},
};
test: ICredentialTestRequest = {
request: {
baseURL: '={{$credentials?.host}}',
url: '/user/me',
},
rules: [
{
type: 'responseSuccessBody',
properties: {
key: 'isAuthorized',
value: false,
message: 'Invalid User Token',
},
},
],
};
}
28 changes: 20 additions & 8 deletions packages/nodes-base/nodes/NocoDB/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
NodeOperationError,
} from 'n8n-workflow';


interface IAttachment {
url: string;
title: string;
Expand All @@ -35,19 +34,30 @@ interface IAttachment {
* @returns {Promise<any>}
*/
export async function apiRequest(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IPollFunctions, method: string, endpoint: string, body: object, query?: IDataObject, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const credentials = await this.getCredentials('nocoDb');
const authenticationMethod = this.getNodeParameter('authentication', 0);
let credentials;

if (authenticationMethod === 'apiToken') {
credentials = await this.getCredentials('nocoDbApiToken');
} else if (authenticationMethod === 'userToken') {
credentials = await this.getCredentials('nocoDbUserToken');
}

if (credentials === undefined) {
throw new NodeOperationError(this.getNode(), 'No credentials got returned!');
}

const baseUrl = credentials.host as string;

query = query || {};

const options: OptionsWithUri = {
headers: {
'xc-auth': credentials.apiToken,
},
headers: {},
method,
body,
qs: query,
uri: uri || `${credentials.host}${endpoint}`,
uri: uri || baseUrl.endsWith('/') ? `${baseUrl.slice(0, -1)}${endpoint}` : `${baseUrl}${endpoint}`,
json: true,

};

if (Object.keys(option).length !== 0) {
Expand All @@ -58,8 +68,10 @@ export async function apiRequest(this: IHookFunctions | IExecuteFunctions | ILoa
delete options.body;
}

const credentialType = authenticationMethod === 'apiToken' ? 'nocoDbApiToken' : 'nocoDbUserToken';

try {
return await this.helpers.request!(options);
return await this.helpers.requestWithAuthentication.call(this, credentialType, options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
Expand Down
37 changes: 35 additions & 2 deletions packages/nodes-base/nodes/NocoDB/NocoDB.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,44 @@ export class NocoDB implements INodeType {
outputs: ['main'],
credentials: [
{
name: 'nocoDb',
required: true,
name: 'nocoDbUserToken',
displayOptions: {
show: {
authentication: [
'userToken',
],
},
},
},
{
name: 'nocoDbApiToken',
displayOptions: {
show: {
authentication: [
'apiToken',
],
},
},
},
],
properties: [
{
displayName: 'Authentication',
name: 'authentication',
type: 'options',
options: [
{
name: 'User Token',
value: 'userToken',
},
{
name: 'API Token',
value: 'apiToken',
},
],
default: 'userToken',
description: 'The resource to operate on',
},
{
displayName: 'Resource',
name: 'resource',
Expand Down
3 changes: 2 additions & 1 deletion packages/nodes-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@
"dist/credentials/NetlifyApi.credentials.js",
"dist/credentials/NextCloudApi.credentials.js",
"dist/credentials/NextCloudOAuth2Api.credentials.js",
"dist/credentials/NocoDb.credentials.js",
"dist/credentials/NocoDbUserToken.credentials.js",
"dist/credentials/NocoDbApiToken.credentials.js",
"dist/credentials/NotionApi.credentials.js",
"dist/credentials/NotionOAuth2Api.credentials.js",
"dist/credentials/OAuth1Api.credentials.js",
Expand Down