From cd2ca5552fe067a4b4ae116e435f4dfc29a5c169 Mon Sep 17 00:00:00 2001 From: Vicente Canales Date: Wed, 27 Mar 2024 08:52:26 -0300 Subject: [PATCH] refactor fetchThemeJson --- src/resolvers.js | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/resolvers.js b/src/resolvers.js index 135b04ff..e9b40690 100644 --- a/src/resolvers.js +++ b/src/resolvers.js @@ -11,8 +11,32 @@ export async function fetchThemeJson() { try { const response = await apiFetch( fetchOptions ); - const data = JSON.stringify( response, null, 2 ); - return JSON.stringify( JSON.parse( data )?.data, null, 2 ); + + if ( ! response?.data || 'SUCCESS' !== response?.status ) { + throw new Error( + `Failed to fetch theme data: ${ + response?.message || response?.status + }` + ); + } + + return JSON.stringify( response?.data, null, 2 ); + } catch ( e ) { + // @todo: handle error + } +} + +export async function saveThemeJson() { + const fetchOptions = { + path: '/create-block-theme/v1/save-theme-data', + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + }; + + try { + await apiFetch( fetchOptions ); } catch ( e ) { // @todo: handle error }