Skip to content

Commit

Permalink
refactor: Rename CORSRequest to OptionsRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
tomi committed Aug 9, 2024
1 parent 1b0c0ba commit dcb9779
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
12 changes: 8 additions & 4 deletions packages/cli/src/webhooks/WebhookRequestHandler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import type express from 'express';
import type { IHttpRequestMethods } from 'n8n-workflow';
import type { IWebhookManager, WebhookCORSRequest, WebhookRequest } from '@/webhooks/webhook.types';
import type {
IWebhookManager,
WebhookOptionsRequest,
WebhookRequest,
} from '@/webhooks/webhook.types';
import * as ResponseHelper from '@/ResponseHelper';

export const WEBHOOK_METHODS: IHttpRequestMethods[] = [
Expand All @@ -19,7 +23,7 @@ class WebhookRequestHandler {
* Handles an incoming webhook request. Handles CORS and delegates the
* request to the webhook manager to execute the webhook.
*/
async handleRequest(req: WebhookRequest | WebhookCORSRequest, res: express.Response) {
async handleRequest(req: WebhookRequest | WebhookOptionsRequest, res: express.Response) {
const method = req.method;

if (method !== 'OPTIONS' && !WEBHOOK_METHODS.includes(method)) {
Expand Down Expand Up @@ -60,7 +64,7 @@ class WebhookRequestHandler {
}

private async setupCorsHeaders(
req: WebhookRequest | WebhookCORSRequest,
req: WebhookRequest | WebhookOptionsRequest,
res: express.Response,
): Promise<Error | null> {
const method = req.method;
Expand Down Expand Up @@ -116,7 +120,7 @@ class WebhookRequestHandler {
export function createWebhookHandlerFor(webhookManager: IWebhookManager) {
const handler = new WebhookRequestHandler(webhookManager);

return async (req: WebhookRequest | WebhookCORSRequest, res: express.Response) => {
return async (req: WebhookRequest | WebhookOptionsRequest, res: express.Response) => {
await handler.handleRequest(req, res);
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { IHttpRequestMethods } from 'n8n-workflow';
import type {
IWebhookManager,
IWebhookResponseCallbackData,
WebhookCORSRequest,
WebhookOptionsRequest,
WebhookRequest,
} from '@/webhooks/webhook.types';
import { createWebhookHandlerFor } from '@/webhooks/WebhookRequestHandler';
Expand All @@ -21,7 +21,7 @@ describe('WebhookRequestHandler', () => {
});

it('should throw for unsupported methods', async () => {
const req = mock<WebhookRequest | WebhookCORSRequest>({
const req = mock<WebhookRequest | WebhookOptionsRequest>({
method: 'CONNECT' as IHttpRequestMethods,
});
const res = mock<Response>();
Expand All @@ -38,7 +38,7 @@ describe('WebhookRequestHandler', () => {

describe('preflight requests', () => {
it('should handle missing header for requested method', async () => {
const req = mock<WebhookRequest | WebhookCORSRequest>({
const req = mock<WebhookRequest | WebhookOptionsRequest>({
method: 'OPTIONS',
headers: {
origin: 'https://example.com',
Expand All @@ -61,7 +61,7 @@ describe('WebhookRequestHandler', () => {
});

it('should handle default origin and max-age', async () => {
const req = mock<WebhookRequest | WebhookCORSRequest>({
const req = mock<WebhookRequest | WebhookOptionsRequest>({
method: 'OPTIONS',
headers: {
origin: 'https://example.com',
Expand All @@ -87,7 +87,7 @@ describe('WebhookRequestHandler', () => {

it('should handle wildcard origin', async () => {
const randomOrigin = randomString(10);
const req = mock<WebhookRequest | WebhookCORSRequest>({
const req = mock<WebhookRequest | WebhookOptionsRequest>({
method: 'OPTIONS',
headers: {
origin: randomOrigin,
Expand All @@ -114,7 +114,7 @@ describe('WebhookRequestHandler', () => {
});

it('should handle custom origin', async () => {
const req = mock<WebhookRequest | WebhookCORSRequest>({
const req = mock<WebhookRequest | WebhookOptionsRequest>({
method: 'OPTIONS',
headers: {
origin: 'https://example.com',
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/webhooks/webhook.types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Request, Response } from 'express';
import type { IDataObject, IHttpRequestMethods } from 'n8n-workflow';

export type WebhookCORSRequest = Request & { method: 'OPTIONS' };
export type WebhookOptionsRequest = Request & { method: 'OPTIONS' };

export type WebhookRequest = Request<{ path: string }> & {
method: IHttpRequestMethods;
Expand Down

0 comments on commit dcb9779

Please sign in to comment.