From 2755474bfe5931a8de8a30bb8239310dc5fc6075 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jurij=20Juki=C4=87?= Date: Thu, 23 Oct 2025 16:22:25 +0200 Subject: [PATCH] hyperapp can return any type, not just result --- src/build/caller_utils_ts_generator.rs | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/build/caller_utils_ts_generator.rs b/src/build/caller_utils_ts_generator.rs index 8329e509..71547636 100644 --- a/src/build/caller_utils_ts_generator.rs +++ b/src/build/caller_utils_ts_generator.rs @@ -706,7 +706,7 @@ fn generate_typescript_function( ) }; - // Function returns the unwrapped type since parseResultResponse extracts it + // Function returns the unwrapped type since parseResponse extracts it let function_impl = format!( "/**\n * {}\n{} * @returns Promise with result\n * @throws ApiError if the request fails\n */\nexport async function {}({}): Promise<{}> {{\n{}\n\n return await apiRequest<{}, {}>('{}', 'POST', data);\n}}", camel_function_name, @@ -789,16 +789,20 @@ pub fn create_typescript_caller_utils(base_dir: &Path, api_dir: &Path) -> Result ts_content.push_str("// Parser for the Result-style responses\n"); ts_content.push_str("// eslint-disable-next-line @typescript-eslint/no-explicit-any\n"); - ts_content.push_str("export function parseResultResponse(response: any): T {\n"); + ts_content.push_str("export function parseResponse(response: any): T {\n"); + ts_content.push_str(" try {\n"); ts_content.push_str( - " if ('Ok' in response && response.Ok !== undefined && response.Ok !== null) {\n", + " if ('Ok' in response && response.Ok !== undefined && response.Ok !== null) {\n", ); - ts_content.push_str(" return response.Ok as T;\n"); - ts_content.push_str(" }\n\n"); - ts_content.push_str(" if ('Err' in response && response.Err !== undefined) {\n"); - ts_content.push_str(" throw new ApiError(`API returned an error`, response.Err);\n"); - ts_content.push_str(" }\n\n"); - ts_content.push_str(" throw new ApiError('Invalid API response format');\n"); + ts_content.push_str(" return response.Ok as T;\n"); + ts_content.push_str(" }\n\n"); + ts_content.push_str(" if ('Err' in response && response.Err !== undefined) {\n"); + ts_content.push_str(" throw new ApiError(`API returned an error`, response.Err);\n"); + ts_content.push_str(" }\n"); + ts_content.push_str(" } catch (e) {\n"); + ts_content.push_str(" return response as T;\n"); + ts_content.push_str(" }\n"); + ts_content.push_str(" return response as T;\n"); ts_content.push_str("}\n\n"); ts_content.push_str("/**\n"); @@ -828,7 +832,7 @@ pub fn create_typescript_caller_utils(base_dir: &Path, api_dir: &Path) -> Result .push_str(" throw new ApiError(`HTTP request failed with status: ${result.status}`);\n"); ts_content.push_str(" }\n\n"); ts_content.push_str(" const jsonResponse = await result.json();\n"); - ts_content.push_str(" return parseResultResponse(jsonResponse);\n"); + ts_content.push_str(" return parseResponse(jsonResponse);\n"); ts_content.push_str("}\n\n"); // Collect types grouped by hyperapp