From 54ee84a7260c2c6eb8ada3daa2897b9679fa8d46 Mon Sep 17 00:00:00 2001 From: Zoltan Hricz Date: Mon, 16 Mar 2026 15:00:01 +0100 Subject: [PATCH 1/3] chore: remove 'any' types and update metadata --- package.json | 1 + src/CortiEmbedded.ts | 4 ++- src/types/api.ts | 2 +- src/utils/PostMessageHandler.ts | 58 ++++++++++++++++++++++++++------- src/utils/errorFormatter.ts | 54 +++++++++++++++++++++++------- test/error-formatter.test.ts | 41 +++++++++++++++++++++++ 6 files changed, 135 insertions(+), 25 deletions(-) create mode 100644 test/error-formatter.test.ts diff --git a/package.json b/package.json index 6548e4b..a96c0e4 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ } }, "sideEffects": [ + "src/corti-embedded.ts", "dist/corti-embedded.js", "dist/web-bundle.js", "dist/bundle.js" diff --git a/src/CortiEmbedded.ts b/src/CortiEmbedded.ts index fb4bdcb..6a3181a 100644 --- a/src/CortiEmbedded.ts +++ b/src/CortiEmbedded.ts @@ -32,6 +32,8 @@ import { export class CortiEmbedded extends LitElement implements CortiEmbeddedAPI { static styles = [baseStyles, containerStyles]; + private static readonly IFRAME_SANDBOX_POLICY = + 'allow-forms allow-modals allow-scripts allow-same-origin'; @property({ type: String, reflect: true }) visibility = 'hidden'; @@ -566,7 +568,7 @@ export class CortiEmbedded extends LitElement implements CortiEmbeddedAPI { `; } diff --git a/src/utils/errorFormatter.ts b/src/utils/errorFormatter.ts index 8cce072..4bcac86 100644 --- a/src/utils/errorFormatter.ts +++ b/src/utils/errorFormatter.ts @@ -5,6 +5,12 @@ interface ValidationError { message?: string; } +type ValidationErrorLike = + | ({ expected: unknown } & Record) + | ({ code: unknown } & Record) + | ({ path: unknown } & Record) + | ({ message: unknown } & Record); + interface FormattedError { message: string; code?: string; @@ -15,6 +21,18 @@ function isRecord(value: unknown): value is Record { return typeof value === 'object' && value !== null; } +function getStringOrFiniteNumber(value: unknown): string | undefined { + if (typeof value === 'string') { + return value; + } + + if (typeof value === 'number' && Number.isFinite(value)) { + return String(value); + } + + return undefined; +} + /** * Attempts to parse a JSON string safely */ @@ -38,7 +56,7 @@ function extractStatusCode(message: string): string | undefined { /** * Checks if an object looks like a validation error */ -function isValidationError(obj: unknown): obj is ValidationError { +function isValidationError(obj: unknown): obj is ValidationErrorLike { return ( typeof obj === 'object' && obj !== null && @@ -146,14 +164,8 @@ export function formatError( const errorObj = error; const objectMessage = typeof errorObj.message === 'string' ? errorObj.message : undefined; - const objectCode = - typeof errorObj.code === 'string' ? errorObj.code : undefined; - const objectStatus = - typeof errorObj.status === 'string' - ? errorObj.status - : typeof errorObj.status === 'number' - ? String(errorObj.status) - : undefined; + const objectCode = getStringOrFiniteNumber(errorObj.code); + const objectStatus = getStringOrFiniteNumber(errorObj.status); // Handle objects with message and details structure (your original case) if (objectMessage) { @@ -191,12 +203,7 @@ export function formatError( } // Handle single validation error objects - if ( - 'expected' in errorObj || - 'code' in errorObj || - 'path' in errorObj || - 'message' in errorObj - ) { + if (isValidationError(errorObj)) { const validationError: ValidationError = { expected: typeof errorObj.expected === 'string' ? errorObj.expected : undefined, From 8563103eb8320eff8d0b1cff41d21c09e870c89f Mon Sep 17 00:00:00 2001 From: Zoltan Hricz Date: Tue, 17 Mar 2026 10:19:09 +0100 Subject: [PATCH 3/3] refactor: improve util function naming and value scoping for the embedded class --- src/CortiEmbedded.ts | 8 ++++---- src/utils/errorFormatter.ts | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/CortiEmbedded.ts b/src/CortiEmbedded.ts index b074eea..27c1a38 100644 --- a/src/CortiEmbedded.ts +++ b/src/CortiEmbedded.ts @@ -30,12 +30,12 @@ import { type PostMessageHandlerCallbacks, } from './utils/PostMessageHandler.js'; +const IFRAME_SANDBOX_POLICY = + 'allow-forms allow-modals allow-scripts allow-same-origin'; + export class CortiEmbedded extends LitElement implements CortiEmbeddedAPI { static styles = [baseStyles, containerStyles]; - private static readonly IFRAME_SANDBOX_POLICY = - 'allow-forms allow-modals allow-scripts allow-same-origin'; - @property({ type: String, reflect: true }) visibility = 'hidden'; @@ -570,7 +570,7 @@ export class CortiEmbedded extends LitElement implements CortiEmbeddedAPI {