From 7b75f48a89e7f9df78e587aff1d5c505c60400f1 Mon Sep 17 00:00:00 2001 From: Mohamed Elghobaty Date: Fri, 27 Sep 2024 11:59:13 +0200 Subject: [PATCH] improve webhook version mismatch detection --- .changeset/three-moons-kick.md | 5 +++++ src/tests/parse-webhook.test.ts | 18 ++++++++++++++++++ src/webhooks/parse.ts | 9 ++++++--- 3 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 .changeset/three-moons-kick.md diff --git a/.changeset/three-moons-kick.md b/.changeset/three-moons-kick.md new file mode 100644 index 0000000..ad16c21 --- /dev/null +++ b/.changeset/three-moons-kick.md @@ -0,0 +1,5 @@ +--- +'@team-plain/typescript-sdk': patch +--- + +Fix a bug with webhook version mismatch detection diff --git a/src/tests/parse-webhook.test.ts b/src/tests/parse-webhook.test.ts index a633283..363ba75 100644 --- a/src/tests/parse-webhook.test.ts +++ b/src/tests/parse-webhook.test.ts @@ -66,4 +66,22 @@ describe('Parse webhook', () => { 'The webhook payload (version=NEW_VERSION) is incompatible with the current version of the SDK. Please upgrade both the SDK and the webhook target to the latest version. Refer to https://www.plain.com/docs/api-reference/webhooks/versions for more information. Original error: data/webhookMetadata/webhookTargetVersion must be equal to constant' ); }); + + it('returns a version mismatch error when the webhook target version is missing', () => { + const invalidWebhook = { + ...threadCreatedPayload, + + webhookMetadata: { + ...threadCreatedPayload.webhookMetadata, + webhookTargetVersion: undefined, + }, + }; + + const result = parsePlainWebhook(invalidWebhook); + + expect(result.error).instanceOf(PlainWebhookVersionMismatchError); + expect(result.error?.message).toBe( + "The webhook payload (version=unknown) is incompatible with the current version of the SDK. Please upgrade both the SDK and the webhook target to the latest version. Refer to https://www.plain.com/docs/api-reference/webhooks/versions for more information. Original error: data/webhookMetadata must have required property 'webhookTargetVersion'" + ); + }); }); diff --git a/src/webhooks/parse.ts b/src/webhooks/parse.ts index 556cffb..fc0c1f3 100644 --- a/src/webhooks/parse.ts +++ b/src/webhooks/parse.ts @@ -61,15 +61,18 @@ function getParseError( const errorMessage = getErrorMessageForHumans(payload, originalAjvError); if (isVersionMismatch(payload)) { - return new PlainWebhookVersionMismatchError(errorMessage, getPayloadVersion(payload) ?? ''); + return new PlainWebhookVersionMismatchError( + errorMessage, + getPayloadVersion(payload) ?? 'unknown' + ); } return new PlainWebhookPayloadError(errorMessage); } function isVersionMismatch(payload: Record): boolean { - const payloadVersion = getPayloadVersion(payload); - return typeof payloadVersion === 'string' && payloadVersion !== getSchemaVersion(); + const schemaVersion = getSchemaVersion(); + return typeof schemaVersion === 'string' && schemaVersion !== getPayloadVersion(payload); } function getErrorMessageForHumans(