mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-16 01:24:05 -08:00
61e26804ba
* ⚡ enabled array-type * ⚡ await-thenable on * ⚡ ban-types on * ⚡ default-param-last on * ⚡ dot-notation on * ⚡ member-delimiter-style on * ⚡ no-duplicate-imports on * ⚡ no-empty-interface on * ⚡ no-floating-promises on * ⚡ no-for-in-array on * ⚡ no-invalid-void-type on * ⚡ no-loop-func on * ⚡ no-shadow on * ⚡ ban-ts-comment re enabled * ⚡ @typescript-eslint/lines-between-class-members on * address my own comment * @typescript-eslint/return-await on * @typescript-eslint/promise-function-async on * @typescript-eslint/no-unnecessary-boolean-literal-compare on * @typescript-eslint/no-unnecessary-type-assertion on * prefer-const on * @typescript-eslint/prefer-optional-chain on Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
144 lines
2.9 KiB
TypeScript
144 lines
2.9 KiB
TypeScript
import { IExecuteFunctions, ILoadOptionsFunctions } from 'n8n-core';
|
|
|
|
import {
|
|
ICredentialsDecrypted,
|
|
ICredentialTestFunctions,
|
|
IHookFunctions,
|
|
INodeCredentialTestResult,
|
|
INodePropertyOptions,
|
|
JsonObject,
|
|
NodeApiError,
|
|
} from 'n8n-workflow';
|
|
|
|
/**
|
|
* Make an authenticated GraphQL request to Emelia.
|
|
*/
|
|
export async function emeliaGraphqlRequest(
|
|
this: IExecuteFunctions | ILoadOptionsFunctions,
|
|
body: object = {},
|
|
) {
|
|
const response = await emeliaApiRequest.call(this, 'POST', '/graphql', body);
|
|
|
|
if (response.errors) {
|
|
throw new NodeApiError(this.getNode(), response);
|
|
}
|
|
|
|
return response;
|
|
}
|
|
|
|
/**
|
|
* Make an authenticated REST API request to Emelia, used for trigger node.
|
|
*/
|
|
export async function emeliaApiRequest(
|
|
this: IExecuteFunctions | ILoadOptionsFunctions | IHookFunctions,
|
|
method: string,
|
|
endpoint: string,
|
|
body: object = {},
|
|
qs: object = {},
|
|
) {
|
|
const { apiKey } = (await this.getCredentials('emeliaApi')) as { apiKey: string };
|
|
|
|
const options = {
|
|
headers: {
|
|
Authorization: apiKey,
|
|
},
|
|
method,
|
|
body,
|
|
qs,
|
|
uri: `https://graphql.emelia.io${endpoint}`,
|
|
json: true,
|
|
};
|
|
|
|
try {
|
|
return this.helpers.request!.call(this, options);
|
|
} catch (error) {
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Load resources so that the user can select them easily.
|
|
*/
|
|
export async function loadResource(
|
|
this: ILoadOptionsFunctions,
|
|
resource: 'campaign' | 'contactList',
|
|
): Promise<INodePropertyOptions[]> {
|
|
const mapping: { [key in 'campaign' | 'contactList']: { query: string; key: string } } = {
|
|
campaign: {
|
|
query: `
|
|
query GetCampaigns {
|
|
campaigns {
|
|
_id
|
|
name
|
|
}
|
|
}`,
|
|
key: 'campaigns',
|
|
},
|
|
contactList: {
|
|
query: `
|
|
query GetContactLists {
|
|
contact_lists {
|
|
_id
|
|
name
|
|
}
|
|
}`,
|
|
key: 'contact_lists',
|
|
},
|
|
};
|
|
|
|
const responseData = await emeliaGraphqlRequest.call(this, { query: mapping[resource].query });
|
|
|
|
return responseData.data[mapping[resource].key].map(
|
|
(campaign: { name: string; _id: string }) => ({
|
|
name: campaign.name,
|
|
value: campaign._id,
|
|
}),
|
|
);
|
|
}
|
|
|
|
export async function emeliaApiTest(
|
|
this: ICredentialTestFunctions,
|
|
credential: ICredentialsDecrypted,
|
|
): Promise<INodeCredentialTestResult> {
|
|
const credentials = credential.data;
|
|
|
|
const body = {
|
|
query: `
|
|
query all_campaigns {
|
|
all_campaigns {
|
|
_id
|
|
name
|
|
status
|
|
createdAt
|
|
stats {
|
|
mailsSent
|
|
}
|
|
}
|
|
}`,
|
|
operationName: 'all_campaigns',
|
|
};
|
|
|
|
const options = {
|
|
headers: {
|
|
Authorization: credentials?.apiKey,
|
|
},
|
|
method: 'POST',
|
|
body,
|
|
uri: `https://graphql.emelia.io/graphql`,
|
|
json: true,
|
|
};
|
|
|
|
try {
|
|
await this.helpers.request!(options);
|
|
} catch (error) {
|
|
return {
|
|
status: 'Error',
|
|
message: `Connection details not valid: ${(error as JsonObject).message}`,
|
|
};
|
|
}
|
|
return {
|
|
status: 'OK',
|
|
message: 'Authentication successful!',
|
|
};
|
|
}
|