n8n-nodes-zoho
Version:
Zoho support for n8n
96 lines • 3.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.throwOnErrorStatus = throwOnErrorStatus;
exports.zohoApiRequest = zohoApiRequest;
exports.zohoSubscriptionsApiRequest = zohoSubscriptionsApiRequest;
const n8n_workflow_1 = require("n8n-workflow");
function throwOnErrorStatus(responseData) {
var _a;
if (((_a = responseData === null || responseData === void 0 ? void 0 : responseData.data) === null || _a === void 0 ? void 0 : _a[0].status) === 'error') {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), responseData);
}
}
async function getAccessTokenData() {
const credentials = await this.getCredentials('zohoApi');
if (!credentials.oauthTokenData) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Missing Zoho OAuth2 token data in credentials.');
}
let { api_domain, access_token, refresh_token, expires_in } = credentials.oauthTokenData;
if (expires_in > 0) {
const urlObject = {
method: 'POST',
url: credentials.accessTokenUrl,
form: {
grant_type: 'refresh_token',
refresh_token,
client_id: credentials.clientId,
client_secret: credentials.clientSecret,
redirect_uri: credentials.redirectUri,
},
json: true,
};
const tokenResponse = await this.helpers.request(urlObject);
access_token = tokenResponse.access_token;
refresh_token = tokenResponse.refresh_token || refresh_token;
api_domain = tokenResponse.api_domain;
expires_in = tokenResponse.expires_in;
}
else {
}
return { api_domain, access_token, refresh_token, expires_in };
}
async function zohoApiRequest(method, baseURL, uri, body = {}, qs = {}) {
var _a;
const { access_token } = await getAccessTokenData.call(this);
const options = {
method,
baseURL,
uri,
headers: {
Authorization: 'Zoho-oauthtoken ' + access_token,
},
form: qs
};
try {
const responseData = await this.helpers.request(options);
throwOnErrorStatus.call(this, responseData);
return responseData;
}
catch (error) {
const args = ((_a = (error).cause) === null || _a === void 0 ? void 0 : _a.data)
? {
message: (error).cause.data.message || 'The Zoho API returned an error.',
description: JSON.stringify((error).cause.data, null, 2),
}
: undefined;
throw new n8n_workflow_1.NodeApiError(this.getNode(), error, args);
}
}
async function zohoSubscriptionsApiRequest(method, uri, body = {}, qs = {}, organizationId) {
const { access_token } = await getAccessTokenData.call(this);
const options = {
method,
uri,
headers: {
Authorization: 'Zoho-oauthtoken ' + access_token,
'X-com-zoho-subscriptions-organizationid': organizationId,
},
json: true,
};
if (Object.keys(qs).length) {
options.qs = qs;
}
if (Object.keys(body).length) {
options.body = body;
}
console.log('Subscription Request Options', options);
try {
const responseData = await this.helpers.request(options);
console.log(responseData);
return responseData;
}
catch (error) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), error);
}
}
//# sourceMappingURL=GenericFunctions.js.map