mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 01:56:46 +00:00
⚡ Improvements to Mailchimp-Node
This commit is contained in:
@@ -34,33 +34,34 @@ export async function mailchimpApiRequest(this: IHookFunctions | IExecuteFunctio
|
||||
}
|
||||
|
||||
try {
|
||||
if (authenticationMethod === 'accessToken') {
|
||||
if (authenticationMethod === 'apiKey') {
|
||||
const credentials = this.getCredentials('mailchimpApi');
|
||||
|
||||
|
||||
if (credentials === undefined) {
|
||||
throw new Error('No credentials got returned!');
|
||||
}
|
||||
|
||||
|
||||
options.headers = Object.assign({}, headers, { Authorization: `apikey ${credentials.apiKey}` });
|
||||
|
||||
|
||||
if (!(credentials.apiKey as string).includes('-')) {
|
||||
throw new Error('The API key is not valid!');
|
||||
}
|
||||
|
||||
|
||||
const datacenter = (credentials.apiKey as string).split('-').pop();
|
||||
options.url = `https://${datacenter}.${host}${endpoint}`;
|
||||
|
||||
|
||||
return await this.helpers.request!(options);
|
||||
} else {
|
||||
const credentials = this.getCredentials('mailchimpOAuth2Api');
|
||||
const datacenter = credentials!.dataCenter;
|
||||
|
||||
options.url = `https://${datacenter}.${host}${endpoint}`;
|
||||
const credentials = this.getCredentials('mailchimpOAuth2Api') as IDataObject;
|
||||
|
||||
const { api_endpoint } = await getMetadata.call(this, credentials.oauthTokenData as IDataObject);
|
||||
|
||||
options.url = `${api_endpoint}/3.0${endpoint}`;
|
||||
//@ts-ignore
|
||||
return await this.helpers.requestOAuth2!.call(this, 'mailchimpOAuth2Api', options, 'bearer');
|
||||
return await this.helpers.requestOAuth2!.call(this, 'mailchimpOAuth2Api', options, 'Bearer');
|
||||
}
|
||||
} catch (error) {
|
||||
if (error.response.body && error.response.body.detail) {
|
||||
if (error.respose && error.response.body && error.response.body.detail) {
|
||||
throw new Error(`Mailchimp Error response [${error.statusCode}]: ${error.response.body.detail}`);
|
||||
}
|
||||
throw error;
|
||||
@@ -96,3 +97,17 @@ export function validateJSON(json: string | undefined): any { // tslint:disable-
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function getMetadata(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, oauthTokenData: IDataObject) {
|
||||
const credentials = this.getCredentials('mailchimpOAuth2Api') as IDataObject;
|
||||
const options: OptionsWithUrl = {
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'Authorization': `OAuth ${oauthTokenData.access_token}`,
|
||||
},
|
||||
method: 'GET',
|
||||
url: credentials.metadataUrl as string,
|
||||
json: true,
|
||||
};
|
||||
return this.helpers.request!(options);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user