@luka-cat-mimi/n8n-nodes-feishu-project
Version:
飞书项目 N8N 集成插件,支持附件管理等功能
99 lines • 4.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const n8n_workflow_1 = require("n8n-workflow");
const ERROR_HELP_URL = 'https://project.feishu.cn/b/helpcenter/1p8d7djs/5aueo3jr';
const TOKEN_REFRESH_ERROR_CODES = [10022, 10211];
class RequestUtils {
static processResponse(res) {
var _a, _b, _c;
if (res instanceof Buffer || res instanceof ArrayBuffer || res instanceof Uint8Array) {
return res;
}
const code = (_a = res === null || res === void 0 ? void 0 : res.err_code) !== null && _a !== void 0 ? _a : res === null || res === void 0 ? void 0 : res.code;
const message = (_b = res === null || res === void 0 ? void 0 : res.err_msg) !== null && _b !== void 0 ? _b : res === null || res === void 0 ? void 0 : res.message;
if (code !== 0) {
throw new Error(`Feishu Project API Error: ${code}, ${message}`);
}
if (res.pagination) {
return res;
}
return (_c = res.data) !== null && _c !== void 0 ? _c : res;
}
static async originRequest(options, clearPluginToken = false) {
const credentialName = 'feishuProjectApi';
const credentials = await this.getCredentials(credentialName);
options.baseURL = `https://${credentials.baseUrl}`;
if (clearPluginToken) {
const additionalCredentialOptions = {
credentialsDecrypted: {
id: 'feishu-project-credentials',
name: credentialName,
type: 'feishuProjectApi',
data: {
...credentials,
pluginToken: '',
},
},
};
return this.helpers.httpRequestWithAuthentication.call(this, credentialName, options, additionalCredentialOptions);
}
return this.helpers.httpRequestWithAuthentication.call(this, credentialName, options);
}
static async request(options) {
if (options.json === undefined)
options.json = true;
return RequestUtils.originRequest
.call(this, options)
.then((res) => RequestUtils.processResponse(res))
.catch((error) => {
var _a, _b;
if (error.context && error.context.data) {
let errorData = {};
if (error.context.data.err_code !== undefined ||
error.context.data.code !== undefined) {
errorData = error.context.data;
}
else {
const buffer = Buffer.from(error.context.data);
if (buffer.length > 0) {
try {
errorData = JSON.parse(buffer.toString('utf-8'));
}
catch {
throw error;
}
}
else {
throw error;
}
}
const { err } = errorData;
const err_code = (_a = errorData.err_code) !== null && _a !== void 0 ? _a : errorData.code;
const err_msg = (_b = errorData.err_msg) !== null && _b !== void 0 ? _b : errorData.message;
if (TOKEN_REFRESH_ERROR_CODES.includes(err_code)) {
return RequestUtils.originRequest
.call(this, options, true)
.then((res) => RequestUtils.processResponse(res));
}
if (err_code !== 0) {
const description = (err === null || err === void 0 ? void 0 : err.msg)
? `${err.msg}${err.log_id ? ` (log_id: ${err.log_id})` : ''}`
: `错误码: ${err_code},请参考排查文档: ${ERROR_HELP_URL}`;
throw new n8n_workflow_1.NodeApiError(this.getNode(), error, {
message: `飞书项目 API 错误: ${err_code}, ${err_msg}`,
description,
});
}
}
if (error.message && error.message.includes('Feishu Project API Error')) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), error, {
message: error.message,
description: `请参考排查文档: ${ERROR_HELP_URL}`,
});
}
throw error;
});
}
}
exports.default = RequestUtils;
//# sourceMappingURL=RequestUtils.js.map