mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-23 11:44:06 -08:00
⚡ Small improvements to OPTIONS request for webhooks
This commit is contained in:
parent
82d94873fc
commit
9c266e7aea
|
@ -161,21 +161,18 @@ export class ActiveWorkflowRunner {
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all request methods associated with a single webhook
|
||||
* @param path webhook path
|
||||
*/
|
||||
/**
|
||||
* Gets all request methods associated with a single webhook
|
||||
*
|
||||
* @param {string} path webhook path
|
||||
* @returns {Promise<string[]>}
|
||||
* @memberof ActiveWorkflowRunner
|
||||
*/
|
||||
async getWebhookMethods(path: string) : Promise<string[]> {
|
||||
const webhooks = await Db.collections.Webhook?.find({ webhookPath: path}) as IWebhookDb[];
|
||||
|
||||
// check if something exist
|
||||
if (webhooks === undefined) {
|
||||
// The requested webhooks are not registered
|
||||
throw new ResponseHelper.ResponseError(`The requested webhook "${path}" is not registered.`, 404, 404);
|
||||
}
|
||||
|
||||
// Gather all request methods in string array
|
||||
let webhookMethods : string[] = webhooks.map(webhook => webhook.method);
|
||||
const webhookMethods: string[] = webhooks.map(webhook => webhook.method);
|
||||
return webhookMethods;
|
||||
}
|
||||
|
||||
|
|
|
@ -1572,10 +1572,11 @@ class App {
|
|||
// Cut away the "/webhook/" to get the registred part of the url
|
||||
const requestUrl = (req as ICustomRequest).parsedUrl!.pathname!.slice(this.endpointWebhook.length + 2);
|
||||
|
||||
let allowedMethods;
|
||||
let allowedMethods: string[];
|
||||
try {
|
||||
allowedMethods = await this.activeWorkflowRunner.getWebhookMethods(requestUrl);
|
||||
|
||||
allowedMethods.push('OPTIONS');
|
||||
|
||||
// Add custom "Allow" header to satisfy OPTIONS response.
|
||||
res.append('Allow', allowedMethods);
|
||||
} catch (error) {
|
||||
|
@ -1654,9 +1655,10 @@ class App {
|
|||
// Cut away the "/webhook-test/" to get the registred part of the url
|
||||
const requestUrl = (req as ICustomRequest).parsedUrl!.pathname!.slice(this.endpointWebhookTest.length + 2);
|
||||
|
||||
let allowedMethods;
|
||||
let allowedMethods: string[];
|
||||
try {
|
||||
allowedMethods = await this.testWebhooks.getWebhookMethods(requestUrl);
|
||||
allowedMethods.push('OPTIONS');
|
||||
|
||||
// Add custom "Allow" header to satisfy OPTIONS response.
|
||||
res.append('Allow', allowedMethods);
|
||||
|
|
|
@ -87,10 +87,10 @@ export class ActiveWebhooks {
|
|||
|
||||
/**
|
||||
* Gets all request methods associated with a single webhook
|
||||
* @param path
|
||||
* @param path
|
||||
*/
|
||||
getWebhookMethods(path: string): string[] {
|
||||
let methods : string[] = [];
|
||||
const methods : string[] = [];
|
||||
|
||||
Object.keys(this.webhookUrls)
|
||||
.filter(key => key.includes(path))
|
||||
|
|
Loading…
Reference in a new issue