Fix webhook legacy fields

This commit is contained in:
Lukasz Ostrowski 2023-03-07 08:30:38 +01:00
parent 4574178fff
commit a17232632c
2 changed files with 56 additions and 3 deletions

View file

@ -1,3 +1,4 @@
import { ASTNode } from "graphql";
import { createMocks } from "node-mocks-http"; import { createMocks } from "node-mocks-http";
import { afterEach, describe, expect, it, vi } from "vitest"; import { afterEach, describe, expect, it, vi } from "vitest";
@ -150,4 +151,37 @@ describe("SaleorAsyncWebhook", () => {
*/ */
expect(testHandler).not.toHaveBeenCalled(); 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();
});
}); });

View file

@ -1,3 +1,4 @@
import { ASTNode } from "graphql/index";
import { NextApiHandler } from "next"; import { NextApiHandler } from "next";
import { AsyncWebhookEventType } from "../../../types"; import { AsyncWebhookEventType } from "../../../types";
@ -9,19 +10,37 @@ export class SaleorAsyncWebhook<TPayload = unknown> extends SaleorWebhook<TPaylo
protected readonly eventType = "async" as const; protected readonly eventType = "async" as const;
constructor( constructor(
configuration: WebhookConfig<AsyncWebhookEventType> & { /**
* Omit new required fields and make them optional. Validate in constructor.
* In 0.35.0 remove old fields
*/
configuration: Omit<WebhookConfig<AsyncWebhookEventType>, "event" | "query"> & {
/** /**
* @deprecated - use `event` instead. Will be removed in 0.35.0 * @deprecated - use `event` instead. Will be removed in 0.35.0
*/ */
asyncEvent?: AsyncWebhookEventType; 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({ super({
...configuration, ...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<TPayload>): NextApiHandler { createHandler(handlerFn: NextWebhookApiHandler<TPayload>): NextApiHandler {