Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/three-moons-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@team-plain/typescript-sdk': patch
---

Fix a bug with webhook version mismatch detection
18 changes: 18 additions & 0 deletions src/tests/parse-webhook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'"
);
});
});
9 changes: 6 additions & 3 deletions src/webhooks/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>): boolean {
const payloadVersion = getPayloadVersion(payload);
return typeof payloadVersion === 'string' && payloadVersion !== getSchemaVersion();
const schemaVersion = getSchemaVersion();
return typeof schemaVersion === 'string' && schemaVersion !== getPayloadVersion(payload);
}

function getErrorMessageForHumans(
Expand Down