n8n/packages/nodes-base/nodes/Copper/GenericFunctions.ts

64 lines
1.8 KiB
TypeScript
Raw Normal View History

import { createHash } from 'crypto';
2020-01-17 14:48:54 -08:00
import { OptionsWithUri } from 'request';
2020-01-17 14:48:54 -08:00
import {
IExecuteFunctions,
IExecuteSingleFunctions,
IHookFunctions,
ILoadOptionsFunctions,
IWebhookFunctions,
} from 'n8n-core';
import {
ICredentialDataDecryptedObject,
IDataObject,
} from 'n8n-workflow';
2020-01-17 14:48:54 -08:00
export async function copperApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions | IWebhookFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('copperApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
2020-01-17 14:48:54 -08:00
let options: OptionsWithUri = {
headers: {
'X-PW-AccessToken': credentials.apiKey,
'X-PW-Application': 'developer_api',
'X-PW-UserEmail': credentials.email,
'Content-Type': 'application/json',
},
method,
qs,
body,
uri: uri ||`https://api.prosperworks.com/developer_api/v1${resource}`,
2020-10-22 06:46:03 -07:00
json: true,
2020-01-17 14:48:54 -08:00
};
options = Object.assign({}, options, option);
if (Object.keys(options.body).length === 0) {
delete options.body;
}
try {
return await this.helpers.request!(options);
} catch (error) {
let errorMessage = error.message;
if (error.response.body && error.response.body.message) {
errorMessage = error.response.body.message;
}
throw new Error('Copper Error: ' + errorMessage);
2020-01-17 14:48:54 -08:00
}
}
/**
* Creates a secret from the credentials
*
* @export
* @param {ICredentialDataDecryptedObject} credentials
* @returns
*/
export function getAutomaticSecret(credentials: ICredentialDataDecryptedObject) {
const data = `${credentials.email},${credentials.apiKey}`;
2021-01-13 11:20:30 -08:00
return createHash('md5').update(data).digest('hex');
}