Small improvements to OPTIONS request for webhooks

This commit is contained in:
Jan Oberhauser 2020-07-24 16:43:23 +02:00
parent 82d94873fc
commit 9c266e7aea
3 changed files with 15 additions and 16 deletions

View file

@ -161,21 +161,18 @@ export class ActiveWorkflowRunner {
}); });
} }
/** /**
* Gets all request methods associated with a single webhook * Gets all request methods associated with a single webhook
* @param path webhook path *
*/ * @param {string} path webhook path
* @returns {Promise<string[]>}
* @memberof ActiveWorkflowRunner
*/
async getWebhookMethods(path: string) : Promise<string[]> { async getWebhookMethods(path: string) : Promise<string[]> {
const webhooks = await Db.collections.Webhook?.find({ webhookPath: path}) as IWebhookDb[]; 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 // 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; return webhookMethods;
} }

View file

@ -1572,10 +1572,11 @@ class App {
// Cut away the "/webhook/" to get the registred part of the url // Cut away the "/webhook/" to get the registred part of the url
const requestUrl = (req as ICustomRequest).parsedUrl!.pathname!.slice(this.endpointWebhook.length + 2); const requestUrl = (req as ICustomRequest).parsedUrl!.pathname!.slice(this.endpointWebhook.length + 2);
let allowedMethods; let allowedMethods: string[];
try { try {
allowedMethods = await this.activeWorkflowRunner.getWebhookMethods(requestUrl); allowedMethods = await this.activeWorkflowRunner.getWebhookMethods(requestUrl);
allowedMethods.push('OPTIONS');
// Add custom "Allow" header to satisfy OPTIONS response. // Add custom "Allow" header to satisfy OPTIONS response.
res.append('Allow', allowedMethods); res.append('Allow', allowedMethods);
} catch (error) { } catch (error) {
@ -1654,9 +1655,10 @@ class App {
// Cut away the "/webhook-test/" to get the registred part of the url // 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); const requestUrl = (req as ICustomRequest).parsedUrl!.pathname!.slice(this.endpointWebhookTest.length + 2);
let allowedMethods; let allowedMethods: string[];
try { try {
allowedMethods = await this.testWebhooks.getWebhookMethods(requestUrl); allowedMethods = await this.testWebhooks.getWebhookMethods(requestUrl);
allowedMethods.push('OPTIONS');
// Add custom "Allow" header to satisfy OPTIONS response. // Add custom "Allow" header to satisfy OPTIONS response.
res.append('Allow', allowedMethods); res.append('Allow', allowedMethods);

View file

@ -87,10 +87,10 @@ export class ActiveWebhooks {
/** /**
* Gets all request methods associated with a single webhook * Gets all request methods associated with a single webhook
* @param path * @param path
*/ */
getWebhookMethods(path: string): string[] { getWebhookMethods(path: string): string[] {
let methods : string[] = []; const methods : string[] = [];
Object.keys(this.webhookUrls) Object.keys(this.webhookUrls)
.filter(key => key.includes(path)) .filter(key => key.includes(path))