Skip to content

Commit a4e365a

Browse files
author
Mohamed Elghobaty
authored
improve webhook version mismatch detection (#164)
1 parent 6d0fdc2 commit a4e365a

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

.changeset/three-moons-kick.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@team-plain/typescript-sdk': patch
3+
---
4+
5+
Fix a bug with webhook version mismatch detection

src/tests/parse-webhook.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,22 @@ describe('Parse webhook', () => {
6666
'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'
6767
);
6868
});
69+
70+
it('returns a version mismatch error when the webhook target version is missing', () => {
71+
const invalidWebhook = {
72+
...threadCreatedPayload,
73+
74+
webhookMetadata: {
75+
...threadCreatedPayload.webhookMetadata,
76+
webhookTargetVersion: undefined,
77+
},
78+
};
79+
80+
const result = parsePlainWebhook(invalidWebhook);
81+
82+
expect(result.error).instanceOf(PlainWebhookVersionMismatchError);
83+
expect(result.error?.message).toBe(
84+
"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'"
85+
);
86+
});
6987
});

src/webhooks/parse.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,18 @@ function getParseError(
6161
const errorMessage = getErrorMessageForHumans(payload, originalAjvError);
6262

6363
if (isVersionMismatch(payload)) {
64-
return new PlainWebhookVersionMismatchError(errorMessage, getPayloadVersion(payload) ?? '');
64+
return new PlainWebhookVersionMismatchError(
65+
errorMessage,
66+
getPayloadVersion(payload) ?? 'unknown'
67+
);
6568
}
6669

6770
return new PlainWebhookPayloadError(errorMessage);
6871
}
6972

7073
function isVersionMismatch(payload: Record<string, unknown>): boolean {
71-
const payloadVersion = getPayloadVersion(payload);
72-
return typeof payloadVersion === 'string' && payloadVersion !== getSchemaVersion();
74+
const schemaVersion = getSchemaVersion();
75+
return typeof schemaVersion === 'string' && schemaVersion !== getPayloadVersion(payload);
7376
}
7477

7578
function getErrorMessageForHumans(

0 commit comments

Comments
 (0)