Improvements to #1356

This commit is contained in:
ricardo 2021-01-29 00:46:28 -05:00
parent 6ccc104926
commit 73189eda87
2 changed files with 42 additions and 76 deletions

View file

@ -24,18 +24,20 @@ import {
*
*/
export async function SIGNL4ApiRequest(this: IExecuteFunctions, method: string, contentType: string, body: string, query: IDataObject = {}, teamSecret?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
export async function SIGNL4ApiRequest(this: IExecuteFunctions, method: string, body: string, query: IDataObject = {}, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('signl4Api');
const teamSecret = credentials?.teamSecret as string;
let options: OptionsWithUri = {
headers: {
'Accept': '*/*',
'Content-Type': contentType
},
method,
body,
qs: query,
uri: "https://connect.signl4.com/webhook/" + teamSecret,
json: false,
uri: `https://connect.signl4.com/webhook/${teamSecret}`,
json: true,
};
if (!Object.keys(body).length) {
@ -46,8 +48,10 @@ export async function SIGNL4ApiRequest(this: IExecuteFunctions, method: string,
}
options = Object.assign({}, options, option);
console.log(options);
try {
return JSON.parse(await this.helpers.request!(options));
return await this.helpers.request!(options);
} catch (error) {
if (error.response && error.response.body && error.response.body.details) {

View file

@ -266,67 +266,39 @@ export class Signl4 implements INodeType {
const message = this.getNodeParameter('message', i) as string;
const additionalFields = this.getNodeParameter('additionalFields',i) as IDataObject;
let data = "";
const data: IDataObject = {
message,
};
// Message
data += "------Boundary-cc2050af-c42f-4cda-a0c3-ede7eaa89513\r\n";
data += "Content-Disposition: form-data; name=\"message\"\r\n\r\n";
data += message + "\r\n";
// Title
if (additionalFields.title) {
data += "------Boundary-cc2050af-c42f-4cda-a0c3-ede7eaa89513\r\n";
data += "Content-Disposition: form-data; name=\"title\"\r\n\r\n";
data += additionalFields.title as string + "\r\n";
data.title = additionalFields.title as string;
}
// X-S4-Service
if (additionalFields.service) {
data += "------Boundary-cc2050af-c42f-4cda-a0c3-ede7eaa89513\r\n";
data += "Content-Disposition: form-data; name=\"X-S4-Service\"\r\n\r\n";
data += additionalFields.service as string + "\r\n";
data.service = additionalFields.service as string;
}
// X-S4-Location
if (additionalFields.locationFieldsUi) {
const locationUi = (additionalFields.locationFieldsUi as IDataObject).locationFieldsValues as IDataObject;
if (locationUi) {
data += "------Boundary-cc2050af-c42f-4cda-a0c3-ede7eaa89513\r\n";
data += "Content-Disposition: form-data; name=\"X-S4-Location\"\r\n\r\n";
data += `${locationUi.latitude},${locationUi.longitude}` + "\r\n";
data['X-S4-Location'] = `${locationUi.latitude},${locationUi.longitude}`;
}
}
// X-S4-AlertingScenario
if (additionalFields.alertingScenario) {
data += "------Boundary-cc2050af-c42f-4cda-a0c3-ede7eaa89513\r\n";
data += "Content-Disposition: form-data; name=\"X-S4-AlertingScenario\"\r\n\r\n";
data += additionalFields.alertingScenario as string + "\r\n";
data['X-S4-AlertingScenario'] = additionalFields.alertingScenario as string;
}
// X-S4-Filtering
if (additionalFields.filtering) {
data += "------Boundary-cc2050af-c42f-4cda-a0c3-ede7eaa89513\r\n";
data += "Content-Disposition: form-data; name=\"X-S4-Filtering\"\r\n\r\n";
data += (additionalFields.filtering as boolean).toString() + "\r\n";
data['X-S4-Filtering'] = (additionalFields.filtering as boolean).toString();
}
// X-S4-ExternalID
if (additionalFields.externalId) {
data += "------Boundary-cc2050af-c42f-4cda-a0c3-ede7eaa89513\r\n";
data += "Content-Disposition: form-data; name=\"X-S4-ExternalID\"\r\n\r\n";
data += additionalFields.externalId as string + "\r\n";
data['X-S4-ExternalID'] = additionalFields.externalId as string;
}
// Status
data += "------Boundary-cc2050af-c42f-4cda-a0c3-ede7eaa89513\r\n";
data += "Content-Disposition: form-data; name=\"X-S4-Status\"\r\n\r\n";
data += "new\r\n";
data['X-S4-Status'] = 'new';
// Source System
data += "------Boundary-cc2050af-c42f-4cda-a0c3-ede7eaa89513\r\n";
data += "Content-Disposition: form-data; name=\"X-S4-SourceSystem\"\r\n\r\n";
data += "n8n\r\n";
data['X-S4-SourceSystem'] = 'n8n';
// Attachments
const attachments = additionalFields.attachmentsUi as IDataObject;
@ -339,40 +311,35 @@ export class Signl4 implements INodeType {
if (binaryProperty) {
const supportedFileExtension = ['png', 'jpg', 'bmp', 'gif', 'mp3', 'wav'];
const supportedFileExtension = ['png', 'jpg', 'jpeg', 'bmp', 'gif', 'mp3', 'wav'];
if (!supportedFileExtension.includes(binaryProperty.fileExtension as string)) {
throw new Error(`Invalid extension, just ${supportedFileExtension.join(',')} are supported}`);
}
data += "------Boundary-cc2050af-c42f-4cda-a0c3-ede7eaa89513\r\n";
data += "Content-Disposition: form-data; name=\"" + binaryProperty.fileName + "\"; filename=\"" + binaryProperty.fileName + "\"\r\n";
data += "Content-Type: " + binaryProperty.mimeType + "\r\n";
data += "Content-Transfer-Encoding: base64\r\n\r\n";
data += Buffer.from(binaryProperty.data, 'base64').toString('base64') + "\r\n";
data.attachment = {
value: Buffer.from(binaryProperty.data, BINARY_ENCODING),
options: {
filename: binaryProperty.fileName,
contentType: binaryProperty.mimeType,
},
};
} else {
throw new Error(`Binary property ${propertyName} does not exist on input`);
}
}
}
data += "------Boundary-cc2050af-c42f-4cda-a0c3-ede7eaa89513--\r\n";
const credentials = this.getCredentials('signl4Api');
const teamSecret = credentials?.teamSecret as string;
responseData = await SIGNL4ApiRequest.call(
this,
'POST',
'multipart/form-data; boundary=----Boundary-cc2050af-c42f-4cda-a0c3-ede7eaa89513',
data,
{},
teamSecret,
'',
{},
{
formData: data,
},
);
}
// Resolve alert
@ -384,29 +351,24 @@ export class Signl4 implements INodeType {
data['X-S4-Status'] = 'resolved';
// Source system
data['X-S4-SourceSystem'] = 'n8n';
const credentials = this.getCredentials('signl4Api');
const teamSecret = credentials?.teamSecret as string;
responseData = await SIGNL4ApiRequest.call(
this,
'POST',
'application/json',
JSON.stringify(data),
{},
teamSecret,
'',
{},
{
formData: data,
},
);
}
}
}
if (Array.isArray(responseData)) {
returnData.push.apply(returnData, responseData as IDataObject[]);
} else if (responseData !== undefined) {
returnData.push(responseData as IDataObject);
if (Array.isArray(responseData)) {
returnData.push.apply(returnData, responseData as IDataObject[]);
} else if (responseData !== undefined) {
returnData.push(responseData as IDataObject);
}
}
return [this.helpers.returnJsonArray(returnData)];
}