From d1b19a380733581642155a1d4303b432fb639f40 Mon Sep 17 00:00:00 2001 From: Mateus Siqueira Date: Mon, 22 Jun 2026 15:01:50 -0300 Subject: [PATCH] fix(event): support both positional and object args in parse() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The parse() function only accepted a single object parameter via destructuring: parse({content, signature, user}) But its JSDoc documents positional parameters: parse(content, signature, user) When users follow the docs and call parse(content, signature), destructuring the content string yields undefined for all values, causing JSON.parse(undefined) → 'Unexpected token u in JSON at position 0'. Add a compatibility check at the top of the function: if the first argument is a plain object, use destructuring; otherwise treat them as positional. Also add input validation so undefined/null content or signature throws a clear error instead of a cryptic JSON syntax error. Fixes #163 --- sdk/event/event.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/sdk/event/event.js b/sdk/event/event.js index 1185749..1fd7bec 100644 --- a/sdk/event/event.js +++ b/sdk/event/event.js @@ -168,7 +168,7 @@ exports.update = async function (id, {isDelivered, user} = {}) { return rest.patchId(resource, id, payload, user); }; -exports.parse = async function ({content, signature, user} = {}) { +exports.parse = async function (content, signature, user) { /** * * Create single notification Event from a content string @@ -189,6 +189,17 @@ exports.parse = async function ({content, signature, user} = {}) { * */ + if (typeof content === 'object' && content !== null && !Array.isArray(content)) { + ({content, signature, user} = content); + } + + if (typeof content !== 'string' || content.length === 0) { + throw new Error('content must be a non-empty JSON string'); + } + if (typeof signature !== 'string' || signature.length === 0) { + throw new Error('signature must be a non-empty base-64 string'); + } + let event = Object.assign(new Event(), JSON.parse(content)['event']); try { signature = Ellipticcurve.Signature.fromBase64(signature);