mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
⚡ Improvements to #1356
This commit is contained in:
parent
6ccc104926
commit
73189eda87
|
@ -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 = {
|
let options: OptionsWithUri = {
|
||||||
headers: {
|
headers: {
|
||||||
'Accept': '*/*',
|
'Accept': '*/*',
|
||||||
'Content-Type': contentType
|
|
||||||
},
|
},
|
||||||
method,
|
method,
|
||||||
body,
|
body,
|
||||||
qs: query,
|
qs: query,
|
||||||
uri: "https://connect.signl4.com/webhook/" + teamSecret,
|
uri: `https://connect.signl4.com/webhook/${teamSecret}`,
|
||||||
json: false,
|
json: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!Object.keys(body).length) {
|
if (!Object.keys(body).length) {
|
||||||
|
@ -46,8 +48,10 @@ export async function SIGNL4ApiRequest(this: IExecuteFunctions, method: string,
|
||||||
}
|
}
|
||||||
options = Object.assign({}, options, option);
|
options = Object.assign({}, options, option);
|
||||||
|
|
||||||
|
console.log(options);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return JSON.parse(await this.helpers.request!(options));
|
return await this.helpers.request!(options);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
||||||
if (error.response && error.response.body && error.response.body.details) {
|
if (error.response && error.response.body && error.response.body.details) {
|
||||||
|
|
|
@ -266,67 +266,39 @@ export class Signl4 implements INodeType {
|
||||||
const message = this.getNodeParameter('message', i) as string;
|
const message = this.getNodeParameter('message', i) as string;
|
||||||
const additionalFields = this.getNodeParameter('additionalFields',i) as IDataObject;
|
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) {
|
if (additionalFields.title) {
|
||||||
data += "------Boundary-cc2050af-c42f-4cda-a0c3-ede7eaa89513\r\n";
|
data.title = additionalFields.title as string;
|
||||||
data += "Content-Disposition: form-data; name=\"title\"\r\n\r\n";
|
|
||||||
data += additionalFields.title as string + "\r\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// X-S4-Service
|
|
||||||
if (additionalFields.service) {
|
if (additionalFields.service) {
|
||||||
data += "------Boundary-cc2050af-c42f-4cda-a0c3-ede7eaa89513\r\n";
|
data.service = additionalFields.service as string;
|
||||||
data += "Content-Disposition: form-data; name=\"X-S4-Service\"\r\n\r\n";
|
|
||||||
data += additionalFields.service as string + "\r\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// X-S4-Location
|
|
||||||
if (additionalFields.locationFieldsUi) {
|
if (additionalFields.locationFieldsUi) {
|
||||||
const locationUi = (additionalFields.locationFieldsUi as IDataObject).locationFieldsValues as IDataObject;
|
const locationUi = (additionalFields.locationFieldsUi as IDataObject).locationFieldsValues as IDataObject;
|
||||||
if (locationUi) {
|
if (locationUi) {
|
||||||
data += "------Boundary-cc2050af-c42f-4cda-a0c3-ede7eaa89513\r\n";
|
data['X-S4-Location'] = `${locationUi.latitude},${locationUi.longitude}`;
|
||||||
data += "Content-Disposition: form-data; name=\"X-S4-Location\"\r\n\r\n";
|
|
||||||
data += `${locationUi.latitude},${locationUi.longitude}` + "\r\n";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// X-S4-AlertingScenario
|
|
||||||
if (additionalFields.alertingScenario) {
|
if (additionalFields.alertingScenario) {
|
||||||
data += "------Boundary-cc2050af-c42f-4cda-a0c3-ede7eaa89513\r\n";
|
data['X-S4-AlertingScenario'] = additionalFields.alertingScenario as string;
|
||||||
data += "Content-Disposition: form-data; name=\"X-S4-AlertingScenario\"\r\n\r\n";
|
|
||||||
data += additionalFields.alertingScenario as string + "\r\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// X-S4-Filtering
|
|
||||||
if (additionalFields.filtering) {
|
if (additionalFields.filtering) {
|
||||||
data += "------Boundary-cc2050af-c42f-4cda-a0c3-ede7eaa89513\r\n";
|
data['X-S4-Filtering'] = (additionalFields.filtering as boolean).toString();
|
||||||
data += "Content-Disposition: form-data; name=\"X-S4-Filtering\"\r\n\r\n";
|
|
||||||
data += (additionalFields.filtering as boolean).toString() + "\r\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// X-S4-ExternalID
|
|
||||||
if (additionalFields.externalId) {
|
if (additionalFields.externalId) {
|
||||||
data += "------Boundary-cc2050af-c42f-4cda-a0c3-ede7eaa89513\r\n";
|
data['X-S4-ExternalID'] = additionalFields.externalId as string;
|
||||||
data += "Content-Disposition: form-data; name=\"X-S4-ExternalID\"\r\n\r\n";
|
|
||||||
data += additionalFields.externalId as string + "\r\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Status
|
data['X-S4-Status'] = 'new';
|
||||||
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";
|
|
||||||
|
|
||||||
// Source System
|
data['X-S4-SourceSystem'] = 'n8n';
|
||||||
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";
|
|
||||||
|
|
||||||
// Attachments
|
// Attachments
|
||||||
const attachments = additionalFields.attachmentsUi as IDataObject;
|
const attachments = additionalFields.attachmentsUi as IDataObject;
|
||||||
|
@ -339,19 +311,20 @@ export class Signl4 implements INodeType {
|
||||||
|
|
||||||
if (binaryProperty) {
|
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)) {
|
if (!supportedFileExtension.includes(binaryProperty.fileExtension as string)) {
|
||||||
|
|
||||||
throw new Error(`Invalid extension, just ${supportedFileExtension.join(',')} are supported}`);
|
throw new Error(`Invalid extension, just ${supportedFileExtension.join(',')} are supported}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
data += "------Boundary-cc2050af-c42f-4cda-a0c3-ede7eaa89513\r\n";
|
data.attachment = {
|
||||||
data += "Content-Disposition: form-data; name=\"" + binaryProperty.fileName + "\"; filename=\"" + binaryProperty.fileName + "\"\r\n";
|
value: Buffer.from(binaryProperty.data, BINARY_ENCODING),
|
||||||
data += "Content-Type: " + binaryProperty.mimeType + "\r\n";
|
options: {
|
||||||
data += "Content-Transfer-Encoding: base64\r\n\r\n";
|
filename: binaryProperty.fileName,
|
||||||
|
contentType: binaryProperty.mimeType,
|
||||||
data += Buffer.from(binaryProperty.data, 'base64').toString('base64') + "\r\n";
|
},
|
||||||
|
};
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
throw new Error(`Binary property ${propertyName} does not exist on input`);
|
throw new Error(`Binary property ${propertyName} does not exist on input`);
|
||||||
|
@ -359,20 +332,14 @@ export class Signl4 implements INodeType {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
data += "------Boundary-cc2050af-c42f-4cda-a0c3-ede7eaa89513--\r\n";
|
|
||||||
|
|
||||||
const credentials = this.getCredentials('signl4Api');
|
|
||||||
|
|
||||||
const teamSecret = credentials?.teamSecret as string;
|
|
||||||
|
|
||||||
responseData = await SIGNL4ApiRequest.call(
|
responseData = await SIGNL4ApiRequest.call(
|
||||||
this,
|
this,
|
||||||
'POST',
|
'POST',
|
||||||
'multipart/form-data; boundary=----Boundary-cc2050af-c42f-4cda-a0c3-ede7eaa89513',
|
'',
|
||||||
data,
|
|
||||||
{},
|
|
||||||
teamSecret,
|
|
||||||
{},
|
{},
|
||||||
|
{
|
||||||
|
formData: data,
|
||||||
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
// Resolve alert
|
// Resolve alert
|
||||||
|
@ -384,29 +351,24 @@ export class Signl4 implements INodeType {
|
||||||
|
|
||||||
data['X-S4-Status'] = 'resolved';
|
data['X-S4-Status'] = 'resolved';
|
||||||
|
|
||||||
// Source system
|
|
||||||
data['X-S4-SourceSystem'] = 'n8n';
|
data['X-S4-SourceSystem'] = 'n8n';
|
||||||
|
|
||||||
const credentials = this.getCredentials('signl4Api');
|
|
||||||
|
|
||||||
const teamSecret = credentials?.teamSecret as string;
|
|
||||||
|
|
||||||
responseData = await SIGNL4ApiRequest.call(
|
responseData = await SIGNL4ApiRequest.call(
|
||||||
this,
|
this,
|
||||||
'POST',
|
'POST',
|
||||||
'application/json',
|
'',
|
||||||
JSON.stringify(data),
|
|
||||||
{},
|
|
||||||
teamSecret,
|
|
||||||
{},
|
{},
|
||||||
|
{
|
||||||
|
formData: data,
|
||||||
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
if (Array.isArray(responseData)) {
|
||||||
if (Array.isArray(responseData)) {
|
returnData.push.apply(returnData, responseData as IDataObject[]);
|
||||||
returnData.push.apply(returnData, responseData as IDataObject[]);
|
} else if (responseData !== undefined) {
|
||||||
} else if (responseData !== undefined) {
|
returnData.push(responseData as IDataObject);
|
||||||
returnData.push(responseData as IDataObject);
|
}
|
||||||
}
|
}
|
||||||
return [this.helpers.returnJsonArray(returnData)];
|
return [this.helpers.returnJsonArray(returnData)];
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue