From a17232632c36233e165db0e2479da12d66fad5b5 Mon Sep 17 00:00:00 2001 From: Lukasz Ostrowski Date: Tue, 7 Mar 2023 08:30:38 +0100 Subject: [PATCH] Fix webhook legacy fields --- .../saleor-async-webhook.test.ts | 34 +++++++++++++++++++ .../saleor-webhooks/saleor-async-webhook.ts | 25 ++++++++++++-- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/src/handlers/next/saleor-webhooks/saleor-async-webhook.test.ts b/src/handlers/next/saleor-webhooks/saleor-async-webhook.test.ts index cc6df75..f17f6a1 100644 --- a/src/handlers/next/saleor-webhooks/saleor-async-webhook.test.ts +++ b/src/handlers/next/saleor-webhooks/saleor-async-webhook.test.ts @@ -1,3 +1,4 @@ +import { ASTNode } from "graphql"; import { createMocks } from "node-mocks-http"; import { afterEach, describe, expect, it, vi } from "vitest"; @@ -150,4 +151,37 @@ describe("SaleorAsyncWebhook", () => { */ expect(testHandler).not.toHaveBeenCalled(); }); + + /** + * Pre 0.35.0 - then remove + */ + it("Allows legacy asyncEvent and subscriptionQueryAst fields, but fails if none provided", () => { + expect( + () => + new SaleorAsyncWebhook({ + asyncEvent: "ADDRESS_CREATED", + subscriptionQueryAst: {} as unknown as ASTNode, + apl: mockAPL, + webhookPath: "", + }) + ).not.toThrowError(); + + expect( + () => + new SaleorAsyncWebhook({ + subscriptionQueryAst: {} as unknown as ASTNode, + apl: mockAPL, + webhookPath: "", + }) + ).toThrowError(); + + expect( + () => + new SaleorAsyncWebhook({ + asyncEvent: "ADDRESS_CREATED", + apl: mockAPL, + webhookPath: "", + }) + ).toThrowError(); + }); }); diff --git a/src/handlers/next/saleor-webhooks/saleor-async-webhook.ts b/src/handlers/next/saleor-webhooks/saleor-async-webhook.ts index 4a4ba5a..8efa7a2 100644 --- a/src/handlers/next/saleor-webhooks/saleor-async-webhook.ts +++ b/src/handlers/next/saleor-webhooks/saleor-async-webhook.ts @@ -1,3 +1,4 @@ +import { ASTNode } from "graphql/index"; import { NextApiHandler } from "next"; import { AsyncWebhookEventType } from "../../../types"; @@ -9,19 +10,37 @@ export class SaleorAsyncWebhook extends SaleorWebhook & { + /** + * Omit new required fields and make them optional. Validate in constructor. + * In 0.35.0 remove old fields + */ + configuration: Omit, "event" | "query"> & { /** * @deprecated - use `event` instead. Will be removed in 0.35.0 */ asyncEvent?: AsyncWebhookEventType; + event?: AsyncWebhookEventType; + query?: string | ASTNode; } ) { + if (!configuration.event && !configuration.asyncEvent) { + throw new Error("event or asyncEvent must be provided. asyncEvent is deprecated"); + } + + if (!configuration.query && !configuration.subscriptionQueryAst) { + throw new Error( + "query or subscriptionQueryAst must be provided. subscriptionQueryAst is deprecated" + ); + } + super({ ...configuration, - event: configuration.event ?? configuration.asyncEvent, + event: configuration.event! ?? configuration.asyncEvent!, + query: configuration.query! ?? configuration.subscriptionQueryAst!, }); - this.event = configuration.event ?? configuration.asyncEvent; + this.event = configuration.event! ?? configuration.asyncEvent!; + this.query = configuration.query! ?? configuration.subscriptionQueryAst!; } createHandler(handlerFn: NextWebhookApiHandler): NextApiHandler {