mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
fix(ZendeskTrigger Node): Fix deprecated targets, replaced with webhooks (#3025)
* 🔨 fix for deprecated targets * ⚡ Move crendentials injection to the credential file Co-authored-by: ricardo <ricardoespinoza105@gmail.com>
This commit is contained in:
@@ -10,19 +10,27 @@ import {
|
||||
} from 'n8n-core';
|
||||
|
||||
import {
|
||||
IDataObject, NodeApiError, NodeOperationError,
|
||||
IDataObject,
|
||||
JsonObject,
|
||||
NodeApiError,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
export async function zendeskApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
||||
const authenticationMethod = this.getNodeParameter('authentication', 0);
|
||||
|
||||
let credentials;
|
||||
|
||||
if (authenticationMethod === 'apiToken') {
|
||||
credentials = await this.getCredentials('zendeskApi') as { subdomain: string };
|
||||
} else {
|
||||
credentials = await this.getCredentials('zendeskOAuth2Api') as { subdomain: string };
|
||||
}
|
||||
|
||||
let options: OptionsWithUri = {
|
||||
headers: {},
|
||||
method,
|
||||
qs,
|
||||
body,
|
||||
//@ts-ignore
|
||||
uri,
|
||||
uri: uri || getUri(resource, credentials.subdomain),
|
||||
json: true,
|
||||
qsStringifyOptions: {
|
||||
arrayFormat: 'brackets',
|
||||
@@ -33,23 +41,13 @@ export async function zendeskApiRequest(this: IHookFunctions | IExecuteFunctions
|
||||
if (Object.keys(options.body).length === 0) {
|
||||
delete options.body;
|
||||
}
|
||||
|
||||
const credentialType = authenticationMethod === 'apiToken' ? 'zendeskApi' : 'zendeskOAuth2Api';
|
||||
|
||||
try {
|
||||
if (authenticationMethod === 'apiToken') {
|
||||
const credentials = await this.getCredentials('zendeskApi');
|
||||
|
||||
const base64Key = Buffer.from(`${credentials.email}/token:${credentials.apiToken}`).toString('base64');
|
||||
options.uri = uri || `https://${credentials.subdomain}.zendesk.com/api/v2${resource}.json`;
|
||||
options.headers!['Authorization'] = `Basic ${base64Key}`;
|
||||
return await this.helpers.request!(options);
|
||||
} else {
|
||||
const credentials = await this.getCredentials('zendeskOAuth2Api');
|
||||
|
||||
options.uri = uri || `https://${credentials.subdomain}.zendesk.com/api/v2${resource}.json`;
|
||||
|
||||
return await this.helpers.requestOAuth2!.call(this, 'zendeskOAuth2Api', options);
|
||||
}
|
||||
return await this.helpers.requestWithAuthentication.call(this, credentialType, options);
|
||||
} catch(error) {
|
||||
throw new NodeApiError(this.getNode(), error);
|
||||
throw new NodeApiError(this.getNode(), error as JsonObject);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,3 +87,11 @@ export function validateJSON(json: string | undefined): any { // tslint:disable-
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function getUri(resource: string, subdomain: string) {
|
||||
if (resource.includes('webhooks')) {
|
||||
return `https://${subdomain}.zendesk.com/api/v2${resource}`;
|
||||
} else {
|
||||
return `https://${subdomain}.zendesk.com/api/v2${resource}.json`;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user