From dc4204a740182552799ae8de50a78b7ed41b8ff5 Mon Sep 17 00:00:00 2001 From: Dmytro Stanchiev Date: Tue, 7 Apr 2026 13:10:59 -0400 Subject: [PATCH] refactor(lib): extract shared image constants and JSON parsing utilities Move image extensions, MIME types, and size limit into a dedicated constants module. Extract JSON-from-text parsing into a pure utility function for reuse across the codebase. --- src/lib/constants.ts | 9 +++++++++ src/lib/json-utils.ts | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 src/lib/constants.ts create mode 100644 src/lib/json-utils.ts diff --git a/src/lib/constants.ts b/src/lib/constants.ts new file mode 100644 index 0000000..8e13cc7 --- /dev/null +++ b/src/lib/constants.ts @@ -0,0 +1,9 @@ +/** Shared constants for image handling across the app. */ + +export const IMAGE_EXTENSIONS = [".png", ".jpg", ".jpeg", ".webp"]; + +export const IMAGE_MIME_TYPES = ["image/png", "image/jpeg", "image/webp"]; + +export const IMAGE_ACCEPT = IMAGE_MIME_TYPES.join(","); + +export const MAX_IMAGE_SIZE_BYTES = 10 * 1024 * 1024; // 10MB diff --git a/src/lib/json-utils.ts b/src/lib/json-utils.ts new file mode 100644 index 0000000..0bb3f4a --- /dev/null +++ b/src/lib/json-utils.ts @@ -0,0 +1,19 @@ +/** + * Extract JSON from text that may contain prose, markdown code blocks, or raw JSON. + * Pure function — same input = same output, no side effects. + */ +export const extractJsonFromText = (text: string): unknown => { + try { + return JSON.parse(text); + } catch { + const codeBlockMatch = text.match(/```(?:json)?\s*([\s\S]*?)```/); + if (codeBlockMatch) { + return JSON.parse(codeBlockMatch[1].trim()); + } + const arrayMatch = text.match(/\[[\s\S]*\]/); + if (arrayMatch) { + return JSON.parse(arrayMatch[0]); + } + throw new Error("No JSON found in response"); + } +};