From 682fe11666c98398d6be5b4a14607e109d0d0f59 Mon Sep 17 00:00:00 2001 From: Lynn Mecham Date: Tue, 28 Oct 2025 13:36:56 -0600 Subject: [PATCH] Refactor context handling to use Object.assign for merging parsed JSON values. Update version to 4.0.5 and add new test cases for enhanced JSON context structure. --- index.js | 2 +- package.json | 3 ++- test/index.utest.ts | 30 ++++++++++++++++++++++++++++-- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 16d15cc..eceaa2d 100644 --- a/index.js +++ b/index.js @@ -241,7 +241,7 @@ function getPassedContext(event, body) { // if the key is just `ctx_` or `ctx-` and there is a value, parse it as JSON if (!k[1] && value) { - ctx[key] = JSON.parse(value); + Object.assign(ctx, JSON.parse(value)); } else { // Check for any type conversions on the query parameter let type = event.queryStringParameters["t_" + key]; diff --git a/package.json b/package.json index 6ba6e8f..f100187 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "leo-auth", - "version": "4.0.4", + "version": "4.0.5", "description": "Auth sdk", "homepage": "https://leoplatform.io", "main": "index.js", @@ -8,6 +8,7 @@ "test": "test" }, "scripts": { + "test-all": "npm run compile && npm run coverage-all", "tests": "mocha --recursive -w", "test": "mocha --recursive -g", "coverage-all": "cross-env NODE_ENV=utest node ./node_modules/nyc/bin/nyc.js --all node ./node_modules/mocha/bin/mocha --timeout 5000 \"./{,!(node_modules)/**/}*.utest.js\"", diff --git a/test/index.utest.ts b/test/index.utest.ts index 1b796d3..895ba09 100644 --- a/test/index.utest.ts +++ b/test/index.utest.ts @@ -293,7 +293,33 @@ describe('index', function () { let sdk = require(".."); let user = await sdk.getUser({ queryStringParameters: { - ctx_: "{\"test\":\"value\"}" + ctx_: JSON.stringify({ + string: "value", + number: 42, + float: 3.14159, + boolean_true: true, + boolean_false: false, + null_value: null, + object: { + nested_string: "nested", + nested_number: -99, + nested_array: [1, 2, { deep: "object" }], + nested_object: { + another_level: { + foo: "bar" + } + } + }, + array: [ + "item1", + 123, + false, + { arr_nested: [1, 2, 3], arr_obj: { a: 1, b: 2 }}, + [["deep", "array"], [true, null]] + ], + empty_object: {}, + undefined_value: undefined // undefined will be omitted by JSON.stringify + }) }, requestContext: { identity: { @@ -301,7 +327,7 @@ describe('index', function () { } } }); - assert.deepEqual(user.context, { key: "identity-1234", ctx_: { test: "value" } }); + assert.deepEqual(user.context, { key: "identity-1234", string: "value", number: 42, float: 3.14159, boolean_true: true, boolean_false: false, null_value: null, object: { nested_string: "nested", nested_number: -99, nested_array: [1, 2, { deep: "object" }], nested_object: { another_level: { foo: "bar" } } }, array: [ "item1", 123, false, { arr_nested: [1, 2, 3], arr_obj: { a: 1, b: 2 } }, [["deep", "array"], [true, null]] ], empty_object: {}}); }); });