mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
⚡ Improvements to Mailchimp-Node
This commit is contained in:
parent
7f76ee9bcd
commit
e859b27a89
|
@ -11,20 +11,6 @@ export class MailchimpOAuth2Api implements ICredentialType {
|
||||||
];
|
];
|
||||||
displayName = 'Mailchimp OAuth2 API';
|
displayName = 'Mailchimp OAuth2 API';
|
||||||
properties = [
|
properties = [
|
||||||
{
|
|
||||||
displayName: 'Mailchimp Server',
|
|
||||||
name: 'server',
|
|
||||||
type: 'string' as NodePropertyTypes,
|
|
||||||
default: 'https://login.mailchimp.com/',
|
|
||||||
description: 'The server to connect to.',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
displayName: 'Datacenter',
|
|
||||||
name: 'dataCenter',
|
|
||||||
type: 'string' as NodePropertyTypes,
|
|
||||||
default: 'us10',
|
|
||||||
description: 'Datacenter that your Mailchimp application is hosted on. Found in the URL of your Mailchimp dashboard.',
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
displayName: 'Authorization URL',
|
displayName: 'Authorization URL',
|
||||||
name: 'authUrl',
|
name: 'authUrl',
|
||||||
|
|
|
@ -34,33 +34,34 @@ export async function mailchimpApiRequest(this: IHookFunctions | IExecuteFunctio
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (authenticationMethod === 'accessToken') {
|
if (authenticationMethod === 'apiKey') {
|
||||||
const credentials = this.getCredentials('mailchimpApi');
|
const credentials = this.getCredentials('mailchimpApi');
|
||||||
|
|
||||||
if (credentials === undefined) {
|
if (credentials === undefined) {
|
||||||
throw new Error('No credentials got returned!');
|
throw new Error('No credentials got returned!');
|
||||||
}
|
}
|
||||||
|
|
||||||
options.headers = Object.assign({}, headers, { Authorization: `apikey ${credentials.apiKey}` });
|
options.headers = Object.assign({}, headers, { Authorization: `apikey ${credentials.apiKey}` });
|
||||||
|
|
||||||
if (!(credentials.apiKey as string).includes('-')) {
|
if (!(credentials.apiKey as string).includes('-')) {
|
||||||
throw new Error('The API key is not valid!');
|
throw new Error('The API key is not valid!');
|
||||||
}
|
}
|
||||||
|
|
||||||
const datacenter = (credentials.apiKey as string).split('-').pop();
|
const datacenter = (credentials.apiKey as string).split('-').pop();
|
||||||
options.url = `https://${datacenter}.${host}${endpoint}`;
|
options.url = `https://${datacenter}.${host}${endpoint}`;
|
||||||
|
|
||||||
return await this.helpers.request!(options);
|
return await this.helpers.request!(options);
|
||||||
} else {
|
} else {
|
||||||
const credentials = this.getCredentials('mailchimpOAuth2Api');
|
const credentials = this.getCredentials('mailchimpOAuth2Api') as IDataObject;
|
||||||
const datacenter = credentials!.dataCenter;
|
|
||||||
|
const { api_endpoint } = await getMetadata.call(this, credentials.oauthTokenData as IDataObject);
|
||||||
options.url = `https://${datacenter}.${host}${endpoint}`;
|
|
||||||
|
options.url = `${api_endpoint}/3.0${endpoint}`;
|
||||||
//@ts-ignore
|
//@ts-ignore
|
||||||
return await this.helpers.requestOAuth2!.call(this, 'mailchimpOAuth2Api', options, 'bearer');
|
return await this.helpers.requestOAuth2!.call(this, 'mailchimpOAuth2Api', options, 'Bearer');
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} 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 new Error(`Mailchimp Error response [${error.statusCode}]: ${error.response.body.detail}`);
|
||||||
}
|
}
|
||||||
throw error;
|
throw error;
|
||||||
|
@ -96,3 +97,17 @@ export function validateJSON(json: string | undefined): any { // tslint:disable-
|
||||||
}
|
}
|
||||||
return result;
|
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);
|
||||||
|
}
|
||||||
|
|
|
@ -72,7 +72,7 @@ export class Mailchimp implements INodeType {
|
||||||
displayOptions: {
|
displayOptions: {
|
||||||
show: {
|
show: {
|
||||||
authentication: [
|
authentication: [
|
||||||
'accessToken',
|
'apiKey',
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -96,15 +96,15 @@ export class Mailchimp implements INodeType {
|
||||||
type: 'options',
|
type: 'options',
|
||||||
options: [
|
options: [
|
||||||
{
|
{
|
||||||
name: 'Access Token',
|
name: 'API Key',
|
||||||
value: 'accessToken',
|
value: 'apiKey',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'OAuth2',
|
name: 'OAuth2',
|
||||||
value: 'oAuth2',
|
value: 'oAuth2',
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
default: 'accessToken',
|
default: 'apiKey',
|
||||||
description: 'Method of authentication.',
|
description: 'Method of authentication.',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -1571,7 +1571,7 @@ export class Mailchimp implements INodeType {
|
||||||
responseData = { success: true };
|
responseData = { success: true };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Array.isArray(responseData)) {
|
if (Array.isArray(responseData)) {
|
||||||
returnData.push.apply(returnData, responseData as IDataObject[]);
|
returnData.push.apply(returnData, responseData as IDataObject[]);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -36,7 +36,7 @@ export class MailchimpTrigger implements INodeType {
|
||||||
displayOptions: {
|
displayOptions: {
|
||||||
show: {
|
show: {
|
||||||
authentication: [
|
authentication: [
|
||||||
'accessToken',
|
'apiKey',
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -74,15 +74,15 @@ export class MailchimpTrigger implements INodeType {
|
||||||
type: 'options',
|
type: 'options',
|
||||||
options: [
|
options: [
|
||||||
{
|
{
|
||||||
name: 'Access Token',
|
name: 'API Key',
|
||||||
value: 'accessToken',
|
value: 'apiKey',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'OAuth2',
|
name: 'OAuth2',
|
||||||
value: 'oAuth2',
|
value: 'oAuth2',
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
default: 'accessToken',
|
default: 'apiKey',
|
||||||
description: 'Method of authentication.',
|
description: 'Method of authentication.',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue