n8n-nodes-chatwoot
Version:
This is an n8n community node. It lets you use ChatWoot in your n8n workflows.
78 lines • 2.54 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.requestAccountOptions = exports.authorizationError = exports.apiRequest = void 0;
const n8n_workflow_1 = require("n8n-workflow");
class RequestError extends Error {
constructor(options, status, message) {
super(message);
this.options = options;
}
}
async function apiRequest(method, endpoint, body = {}, qs = {}, headers = {}) {
let baseUrl = this.getNodeParameter('baseUrl', 0, '');
if (!baseUrl) {
const credentials = await this.getCredentials('chatWootTokenApi');
baseUrl = baseUrl || credentials.baseUrl;
}
const endpointUri = baseUrl + endpoint;
const options = {
headers: {
Accept: 'application/json',
},
method,
qs,
url: endpointUri,
json: true,
};
if (Object.keys(headers).length !== 0) {
options.headers = Object.assign({}, options.headers, headers);
}
if (Object.keys(body).length !== 0) {
options.body = body;
}
qs = qs || {};
if (options.qs && Object.keys(options.qs).length === 0) {
delete options.qs;
}
try {
return await this.helpers.httpRequest(options);
}
catch (error) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), error);
}
}
exports.apiRequest = apiRequest;
function authorizationError(resp, realm, responseCode, message) {
if (message === undefined) {
message = 'Authorization problem!';
if (responseCode === 401) {
message = 'Authorization is required!';
}
else if (responseCode === 403) {
message = 'Authorization data is wrong!';
}
}
resp.writeHead(responseCode, { 'WWW-Authenticate': `Basic realm="${realm}"` });
resp.end(message);
return {
noWebhookResponse: true,
};
}
exports.authorizationError = authorizationError;
function requestAccountOptions(credentials) {
let baseUrl = credentials.baseUrl;
if (baseUrl.endsWith("/")) {
baseUrl = baseUrl.slice(0, -1);
}
const options = {
headers: {
'api_access_token': `${credentials.accessToken}`,
},
method: 'GET',
uri: `${baseUrl}/api/v1/accounts/${credentials.accountId}`,
json: true,
};
return options;
}
exports.requestAccountOptions = requestAccountOptions;
//# sourceMappingURL=GenericFunctions.js.map
;