From 25489c25d4f0ae394bda98dd16825440bcacc195 Mon Sep 17 00:00:00 2001 From: mikesealey Date: Thu, 13 Feb 2025 14:31:39 +0000 Subject: [PATCH] handles encoded array of 1 or many IDs --- packages/string-templates/src/helpers/index.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/string-templates/src/helpers/index.ts b/packages/string-templates/src/helpers/index.ts index 8cd2de429f..d5627b5ce1 100644 --- a/packages/string-templates/src/helpers/index.ts +++ b/packages/string-templates/src/helpers/index.ts @@ -32,8 +32,21 @@ const HELPERS = [ }), // javascript helper new Helper(HelperFunctionNames.JS, processJS, false), - new Helper(HelperFunctionNames.DECODE_ID, value => { - return value + new Helper(HelperFunctionNames.DECODE_ID, (_id: string | { _id: string }) => { + if (!_id) { + return [] + } + // have to replace on the way back as we swapped out the double quotes + // when encoding, but JSON can't handle the single quotes + const id = typeof _id === "string" ? _id : _id._id + const decoded: string = decodeURIComponent(id).replace(/'/g, '"') + try { + const parsed = JSON.parse(decoded) + return Array.isArray(parsed) ? parsed : [parsed] + } catch (err) { + // wasn't json - likely was handlebars for a many to many + return [_id] + } }), // this help is applied to all statements new Helper(