n8n-nodes-larkbase-v3
Version:
n8n node to connect with Larkbase API with UTF-8 support, date filtering, array filter values, text field processing, improved field type handling, pagination fixes, advanced data type conversion, proper date filtering with ExactDate format, updated node
204 lines • 8.21 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LarkbaseAuth = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const axios_1 = __importDefault(require("axios"));
function cleanTextFields(data) {
const result = {};
for (const [key, value] of Object.entries(data)) {
if (Array.isArray(value) && value.length > 0 && typeof value[0] === 'object' && value[0].type === 'text') {
result[key] = value.map(item => item.text).join('');
}
else {
result[key] = value;
}
}
return result;
}
class LarkbaseAuth {
constructor() {
this.description = {
displayName: 'Larkbase Auth',
name: 'larkbaseAuth',
icon: 'file:larkbase.svg',
group: ['transform'],
version: 1,
subtitle: '={{$parameter["operation"]}}',
description: 'Lấy token xác thực từ Larkbase API',
defaults: {
name: 'Larkbase Auth',
},
inputs: ['main'],
outputs: ['main'],
properties: [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
options: [
{
name: 'Get Access Token',
value: 'getAccessToken',
description: 'Lấy token xác thực từ Larkbase API',
action: 'Get access token',
},
],
default: 'getAccessToken',
description: 'Operation để thực hiện',
},
{
displayName: 'Authentication Method',
name: 'authMethod',
type: 'options',
options: [
{
name: 'Credentials',
value: 'credentials',
description: 'Sử dụng thông tin xác thực đã lưu',
},
{
name: 'Manual Input',
value: 'manual',
description: 'Nhập thông tin xác thực thủ công',
},
],
default: 'credentials',
description: 'Phương thức xác thực',
},
{
displayName: 'App ID',
name: 'appId',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
authMethod: [
'manual',
],
},
},
description: 'App ID của ứng dụng Feishu/Lark',
},
{
displayName: 'App Secret',
name: 'appSecret',
type: 'string',
default: '',
required: true,
typeOptions: {
password: true,
},
displayOptions: {
show: {
authMethod: [
'manual',
],
},
},
description: 'App Secret của ứng dụng Feishu/Lark',
},
],
credentials: [
{
name: 'larkbaseApi',
required: true,
displayOptions: {
show: {
authMethod: [
'credentials',
],
},
},
},
],
};
}
async execute() {
var _a, _b, _c, _d, _e;
const returnData = [];
const operation = this.getNodeParameter('operation', 0);
const authMethod = this.getNodeParameter('authMethod', 0);
if (operation === 'getAccessToken') {
let appId;
let appSecret;
if (authMethod === 'credentials') {
const credentials = await this.getCredentials('larkbaseApi');
appId = credentials.appId;
appSecret = credentials.appSecret;
}
else {
appId = this.getNodeParameter('appId', 0);
appSecret = this.getNodeParameter('appSecret', 0);
}
const url = 'https://open.larksuite.com/open-apis/auth/v3/tenant_access_token/internal';
console.log('DEBUG - Auth Request:');
console.log('URL:', url);
console.log('App ID:', appId);
const requestOptions = {
method: 'POST',
url,
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
data: {
app_id: appId,
app_secret: appSecret,
},
};
try {
const response = await axios_1.default.request(requestOptions);
console.log('DEBUG - Auth Response:');
console.log('Status:', response.status);
console.log('Data:', JSON.stringify(response.data, null, 2));
if (response.data.code !== 0) {
throw new Error(`Lỗi xác thực: ${response.data.msg}`);
}
const cleanedData = cleanTextFields(response.data);
let accessToken = cleanedData.tenant_access_token;
if (Array.isArray(accessToken)) {
if (accessToken.length > 0) {
if (typeof accessToken[0] === 'object' && accessToken[0].text) {
accessToken = accessToken[0].text;
}
else {
accessToken = String(accessToken[0]);
}
}
else {
accessToken = '';
}
}
returnData.push({
json: {
success: true,
access_token: accessToken,
expires_in: cleanedData.expire,
token_type: 'Bearer',
},
pairedItem: {
item: 0,
},
});
}
catch (error) {
console.error('DEBUG - Auth Error:');
if (axios_1.default.isAxiosError(error)) {
console.error('Status:', (_a = error.response) === null || _a === void 0 ? void 0 : _a.status);
console.error('Status Text:', (_b = error.response) === null || _b === void 0 ? void 0 : _b.statusText);
console.error('Response Data:', JSON.stringify((_c = error.response) === null || _c === void 0 ? void 0 : _c.data, null, 2));
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Lỗi xác thực: ${error.message} - Status: ${(_d = error.response) === null || _d === void 0 ? void 0 : _d.status} - Message: ${JSON.stringify((_e = error.response) === null || _e === void 0 ? void 0 : _e.data)}`, { itemIndex: 0 });
}
console.error('Error:', error);
throw error;
}
}
return [returnData];
}
}
exports.LarkbaseAuth = LarkbaseAuth;
//# sourceMappingURL=LarkbaseAuth.node.js.map
;