2023-12-05 02:17:08 -08:00
|
|
|
import { ApplicationError } from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2022-07-10 02:06:20 -07:00
|
|
|
ICredentialDataDecryptedObject,
|
2020-06-30 13:35:31 -07:00
|
|
|
ICredentialType,
|
2022-07-10 02:06:20 -07:00
|
|
|
IHttpRequestOptions,
|
2021-06-12 09:39:55 -07:00
|
|
|
INodeProperties,
|
2020-06-30 13:35:31 -07:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
export class CustomerIoApi implements ICredentialType {
|
|
|
|
name = 'customerIoApi';
|
2022-12-02 12:54:28 -08:00
|
|
|
|
2020-06-30 13:35:31 -07:00
|
|
|
displayName = 'Customer.io API';
|
2022-12-02 12:54:28 -08:00
|
|
|
|
2020-08-17 05:42:09 -07:00
|
|
|
documentationUrl = 'customerIo';
|
2022-12-02 12:54:28 -08:00
|
|
|
|
2021-06-12 09:39:55 -07:00
|
|
|
properties: INodeProperties[] = [
|
2020-09-02 03:25:11 -07:00
|
|
|
{
|
|
|
|
displayName: 'Tracking API Key',
|
|
|
|
name: 'trackingApiKey',
|
2021-06-12 09:39:55 -07:00
|
|
|
type: 'string',
|
2023-08-01 04:08:25 -07:00
|
|
|
typeOptions: { password: true },
|
2020-09-02 03:25:11 -07:00
|
|
|
default: '',
|
2022-04-13 23:32:27 -07:00
|
|
|
description: 'Required for tracking API',
|
2020-09-02 03:32:12 -07:00
|
|
|
required: true,
|
2020-09-02 03:25:11 -07:00
|
|
|
},
|
2022-07-10 02:06:20 -07:00
|
|
|
{
|
|
|
|
displayName: 'Region',
|
|
|
|
name: 'region',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'EU region',
|
|
|
|
value: 'track-eu.customer.io',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Global region',
|
|
|
|
value: 'track.customer.io',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'track.customer.io',
|
|
|
|
description: 'Should be set based on your account region',
|
|
|
|
hint: 'The region will be omited when being used with the HTTP node',
|
|
|
|
required: true,
|
|
|
|
},
|
2020-09-02 03:25:11 -07:00
|
|
|
{
|
|
|
|
displayName: 'Tracking Site ID',
|
|
|
|
name: 'trackingSiteId',
|
2021-06-12 09:39:55 -07:00
|
|
|
type: 'string',
|
2020-09-02 03:25:11 -07:00
|
|
|
default: '',
|
2022-04-13 23:32:27 -07:00
|
|
|
description: 'Required for tracking API',
|
2020-09-02 03:25:11 -07:00
|
|
|
},
|
2020-06-30 13:35:31 -07:00
|
|
|
{
|
2020-08-04 01:32:51 -07:00
|
|
|
displayName: 'App API Key',
|
2020-09-02 03:25:11 -07:00
|
|
|
name: 'appApiKey',
|
2021-06-12 09:39:55 -07:00
|
|
|
type: 'string',
|
2023-08-01 04:08:25 -07:00
|
|
|
typeOptions: { password: true },
|
2020-06-30 13:35:31 -07:00
|
|
|
default: '',
|
2022-04-13 23:32:27 -07:00
|
|
|
description: 'Required for App API',
|
2020-06-30 13:35:31 -07:00
|
|
|
},
|
|
|
|
];
|
2022-12-02 12:54:28 -08:00
|
|
|
|
2022-07-24 08:36:17 -07:00
|
|
|
async authenticate(
|
|
|
|
credentials: ICredentialDataDecryptedObject,
|
|
|
|
requestOptions: IHttpRequestOptions,
|
|
|
|
): Promise<IHttpRequestOptions> {
|
2022-07-10 02:06:20 -07:00
|
|
|
// @ts-ignore
|
|
|
|
const url = requestOptions.url ? requestOptions.url : requestOptions.uri;
|
|
|
|
if (url.includes('track') || url.includes('api.customer.io')) {
|
2022-07-24 08:36:17 -07:00
|
|
|
const basicAuthKey = Buffer.from(
|
|
|
|
`${credentials.trackingSiteId}:${credentials.trackingApiKey}`,
|
|
|
|
).toString('base64');
|
2022-07-10 02:06:20 -07:00
|
|
|
// @ts-ignore
|
2022-07-24 08:36:17 -07:00
|
|
|
Object.assign(requestOptions.headers, { Authorization: `Basic ${basicAuthKey}` });
|
2022-07-10 02:06:20 -07:00
|
|
|
} else if (url.includes('beta-api.customer.io')) {
|
|
|
|
// @ts-ignore
|
2022-07-24 08:36:17 -07:00
|
|
|
Object.assign(requestOptions.headers, {
|
|
|
|
Authorization: `Bearer ${credentials.appApiKey as string}`,
|
|
|
|
});
|
2022-07-10 02:06:20 -07:00
|
|
|
} else {
|
2023-12-05 02:17:08 -08:00
|
|
|
throw new ApplicationError('Unknown way of authenticating', { level: 'warning' });
|
2022-07-10 02:06:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return requestOptions;
|
|
|
|
}
|
2020-06-30 13:35:31 -07:00
|
|
|
}
|