From 33cadd63746754fcb321fda207daf5f6706f0d59 Mon Sep 17 00:00:00 2001 From: Coki <92775570+HashCookie@users.noreply.github.com> Date: Sat, 15 Jun 2024 23:15:49 +0800 Subject: [PATCH] feat: Adjust response formatting to conditionally display 'Reason' field when not empty (#121) --- app/components/editor/hooks/useRunTest.tsx | 6 +++--- app/components/editor/index.tsx | 22 ++++++++++++++++++++-- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/app/components/editor/hooks/useRunTest.tsx b/app/components/editor/hooks/useRunTest.tsx index 21879c5..71b592e 100755 --- a/app/components/editor/hooks/useRunTest.tsx +++ b/app/components/editor/hooks/useRunTest.tsx @@ -84,7 +84,7 @@ function parseABACRequest(line: string): any[] { async function enforcer(props: RunTestProps) { const startTime = performance.now(); - const result = []; + const result: { request: string; okEx: boolean; reason: string[]; }[] = []; try { const e = await newEnforcer(newModel(props.model), props.policy ? new StringAdapter(props.policy) : undefined); @@ -172,8 +172,8 @@ async function enforcer(props: RunTestProps) { const rvals = parseABACRequest(n); const ctx = newEnforceContext(props.enforceContextData); - // @ts-ignore - result.push(await e.enforce(ctx, ...rvals)); + const [okEx, reason] = await e.enforceEx(ctx, ...rvals); + result.push({ request: n, okEx, reason }); } const stopTime = performance.now(); diff --git a/app/components/editor/index.tsx b/app/components/editor/index.tsx index a417bee..085571d 100755 --- a/app/components/editor/index.tsx +++ b/app/components/editor/index.tsx @@ -72,7 +72,16 @@ export const EditorScreen = () => { if (isValidElement(v)) { setEcho(v); } else if (Array.isArray(v)) { - setRequestResult(v.join('\n')); + const formattedResults = v.map((res) => { + if (typeof res === 'object') { + const reasonString = Array.isArray(res.reason) && res.reason.length > 0 + ? ` Reason: ${JSON.stringify(res.reason)}` + : ''; + return `${res.okEx}${reasonString}`; + } + return res; + }); + setRequestResult(formattedResults.join('\n')); } }, }); @@ -350,7 +359,16 @@ export const EditorScreen = () => { if (isValidElement(v)) { setEcho(v); } else if (Array.isArray(v)) { - setRequestResult(v.join('\n')); + const formattedResults = v.map((res) => { + if (typeof res === 'object') { + const reasonString = Array.isArray(res.reason) && res.reason.length > 0 + ? ` Reason: ${JSON.stringify(res.reason)}` + : ''; + return `${res.okEx}${reasonString}`; + } + return res; + }); + setRequestResult(formattedResults.join('\n')); } }, });