n8n-walichat
Version:
n8n plugin for WaliChat
180 lines (179 loc) • 7.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RawRequest = void 0;
const BaseNode_1 = require("../Base/BaseNode");
const globalProperties_1 = require("../globalProperties");
class RawRequest extends BaseNode_1.BaseNode {
constructor() {
super(...arguments);
this.description = {
displayName: 'WaliChat Send API Request',
name: 'rawRequest',
group: ['transform'],
version: 1,
icon: 'file:../../../icon.png',
description: 'Send arbitrary HTTP requests to the WaliChat API',
defaults: {
name: 'Send API Request',
color: '#1A82e2',
},
inputs: ["main" /* NodeConnectionType.Main */],
outputs: ["main" /* NodeConnectionType.Main */],
properties: [
...globalProperties_1.globalProperties,
{
displayName: 'URL Path',
name: 'urlPath',
type: 'string',
default: '',
placeholder: 'Enter the URL path...',
description: 'The URL path to be appended to the base URL.',
required: true,
},
{
displayName: 'HTTP Method',
name: 'httpMethod',
type: 'options',
options: [
{ name: 'GET', value: 'GET' },
{ name: 'POST', value: 'POST' },
{ name: 'PUT', value: 'PUT' },
{ name: 'PATCH', value: 'PATCH' },
{ name: 'DELETE', value: 'DELETE' },
],
default: 'GET',
description: 'The HTTP method to use for the request.',
required: true,
},
{
displayName: 'Headers',
name: 'headers',
type: 'fixedCollection',
placeholder: 'Add Header',
description: 'The headers to send with the request.',
typeOptions: {
multipleValues: true,
},
default: {},
options: [
{
name: 'header',
displayName: 'Header',
values: [
{
displayName: 'Key',
name: 'key',
type: 'string',
default: '',
placeholder: 'Enter header key...',
description: 'The header key.',
},
{
displayName: 'Value',
name: 'value',
type: 'string',
default: '',
placeholder: 'Enter header value...',
description: 'The header value.',
},
],
},
],
},
{
displayName: 'URL Params',
name: 'urlParams',
type: 'fixedCollection',
placeholder: 'Add URL Param',
description: 'The URL parameters to send with the request.',
typeOptions: {
multipleValues: true,
},
default: {},
options: [
{
name: 'param',
displayName: 'Param',
values: [
{
displayName: 'Key',
name: 'key',
type: 'string',
default: '',
placeholder: 'Enter param key...',
description: 'The URL parameter key.',
},
{
displayName: 'Value',
name: 'value',
type: 'string',
default: '',
placeholder: 'Enter param value...',
description: 'The URL parameter value.',
},
],
},
],
},
{
displayName: 'Body',
name: 'body',
type: 'json',
default: '',
placeholder: 'Enter request body...',
description: 'The body to send with the request (for POST, PUT, PATCH methods).',
required: false,
},
],
};
}
async execute() {
const items = this.getInputData();
const returnData = [];
for (let i = 0; i < items.length; i++) {
const apiKey = this.getNodeParameter('apiKey', i);
const urlPath = this.getNodeParameter('urlPath', i);
const httpMethod = this.getNodeParameter('httpMethod', i);
const headers = this.getNodeParameter('headers.header', i, []);
const urlParams = this.getNodeParameter('urlParams.param', i, []);
const body = this.getNodeParameter('body', i, {});
const requestHeaders = {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json',
};
headers.forEach(header => {
requestHeaders[header.key] = header.value;
});
const requestParams = {};
urlParams.forEach(param => {
requestParams[param.key] = param.value;
});
const payload = {
path: urlPath,
method: httpMethod,
body: httpMethod !== 'GET' ? body : undefined,
query: requestParams,
headers: requestHeaders,
};
if (httpMethod !== 'GET' && body) {
payload.headers['Content-Type'] = 'application/json';
payload.body = body;
}
try {
const response = await super.request(payload);
returnData.push({
json: response,
});
}
catch (error) {
returnData.push({
json: {
error: error.message,
},
});
}
}
return [returnData];
}
}
exports.RawRequest = RawRequest;