1- const BACKEND_URL = process . env . REACT_APP_BACKEND_URL ?? '' ;
2- const API_BASE_PATH = '/api/v1/admin' ;
1+ import { getApiBasePath , getDocServicePath } from '../utils/paths' ;
2+
3+ const API_BASE_PATH = getApiBasePath ( ) ;
4+ const DOCSERVICE_URL = getDocServicePath ( ) ;
35
46const isNetworkError = error => {
57 if ( ! error ) return false ;
@@ -21,33 +23,33 @@ const safeFetch = async (url, options = {}) => {
2123} ;
2224
2325export const fetchStatistics = async ( ) => {
24- const response = await safeFetch ( `${ BACKEND_URL } ${ API_BASE_PATH } /stat` ) ;
26+ const response = await safeFetch ( `${ API_BASE_PATH } /stat` , { credentials : 'include' } ) ;
2527 if ( ! response . ok ) throw new Error ( 'Failed to fetch statistics' ) ;
2628 return response . json ( ) ;
2729} ;
2830
2931export const fetchConfiguration = async ( ) => {
30- const response = await safeFetch ( `${ BACKEND_URL } ${ API_BASE_PATH } /config` , { credentials : 'include' } ) ;
32+ const response = await safeFetch ( `${ API_BASE_PATH } /config` , { credentials : 'include' } ) ;
3133 if ( response . status === 401 ) throw new Error ( 'UNAUTHORIZED' ) ;
3234 if ( ! response . ok ) throw new Error ( 'Failed to fetch configuration' ) ;
3335 return response . json ( ) ;
3436} ;
3537
3638export const fetchConfigurationSchema = async ( ) => {
37- const response = await safeFetch ( `${ BACKEND_URL } ${ API_BASE_PATH } /config/schema` , { credentials : 'include' } ) ;
38- if ( response . status === 401 ) throw new Error ( 'UNAUTHORIZED' ) ;
39+ const response = await safeFetch ( `${ API_BASE_PATH } /config/schema` , { credentials : 'include' } ) ;
3940 if ( ! response . ok ) throw new Error ( 'Failed to fetch configuration schema' ) ;
4041 return response . json ( ) ;
4142} ;
4243
4344export const updateConfiguration = async configData => {
44- const response = await safeFetch ( `${ BACKEND_URL } ${ API_BASE_PATH } /config` , {
45+ const response = await safeFetch ( `${ API_BASE_PATH } /config` , {
4546 method : 'PATCH' ,
4647 headers : { 'Content-Type' : 'application/json' } ,
4748 credentials : 'include' ,
4849 body : JSON . stringify ( configData )
4950 } ) ;
5051 if ( ! response . ok ) {
52+ if ( response . status === 401 ) throw new Error ( 'UNAUTHORIZED' ) ;
5153 let errorMessage = 'Configuration update failed' ;
5254 try {
5355 const errorData = await response . json ( ) ;
@@ -61,23 +63,22 @@ export const updateConfiguration = async configData => {
6163} ;
6264
6365export const fetchCurrentUser = async ( ) => {
64- const response = await safeFetch ( `${ BACKEND_URL } ${ API_BASE_PATH } /me` , { credentials : 'include' } ) ;
65- if ( ! response . ok ) throw new Error ( 'Failed to fetch current user' ) ;
66+ const response = await safeFetch ( `${ API_BASE_PATH } /me` , { credentials : 'include' } ) ;
6667 const data = await response . json ( ) ;
6768 if ( data && data . authorized === false ) {
68- throw new Error ( 'Unauthorized ' ) ;
69+ throw new Error ( 'UNAUTHORIZED ' ) ;
6970 }
7071 return data ;
7172} ;
7273
7374export const checkSetupRequired = async ( ) => {
74- const response = await safeFetch ( `${ BACKEND_URL } ${ API_BASE_PATH } /setup/required` , { credentials : 'include' } ) ;
75+ const response = await safeFetch ( `${ API_BASE_PATH } /setup/required` , { credentials : 'include' } ) ;
7576 if ( ! response . ok ) throw new Error ( 'Failed to check setup status' ) ;
7677 return response . json ( ) ;
7778} ;
7879
7980export const setupAdminPassword = async ( { bootstrapToken, password} ) => {
80- const response = await safeFetch ( `${ BACKEND_URL } ${ API_BASE_PATH } /setup` , {
81+ const response = await safeFetch ( `${ API_BASE_PATH } /setup` , {
8182 method : 'POST' ,
8283 headers : { 'Content-Type' : 'application/json' } ,
8384 credentials : 'include' ,
@@ -97,7 +98,7 @@ export const setupAdminPassword = async ({bootstrapToken, password}) => {
9798} ;
9899
99100export const login = async password => {
100- const response = await safeFetch ( `${ BACKEND_URL } ${ API_BASE_PATH } /login` , {
101+ const response = await safeFetch ( `${ API_BASE_PATH } /login` , {
101102 method : 'POST' ,
102103 headers : { 'Content-Type' : 'application/json' } ,
103104 credentials : 'include' ,
@@ -120,13 +121,14 @@ export const login = async password => {
120121} ;
121122
122123export const changePassword = async ( { currentPassword, newPassword} ) => {
123- const response = await safeFetch ( `${ BACKEND_URL } ${ API_BASE_PATH } /change-password` , {
124+ const response = await safeFetch ( `${ API_BASE_PATH } /change-password` , {
124125 method : 'POST' ,
125126 headers : { 'Content-Type' : 'application/json' } ,
126127 credentials : 'include' ,
127128 body : JSON . stringify ( { currentPassword, newPassword} )
128129 } ) ;
129130 if ( ! response . ok ) {
131+ if ( response . status === 401 ) throw new Error ( 'UNAUTHORIZED' ) ;
130132 let errorMessage = 'Password change failed' ;
131133 try {
132134 const errorData = await response . json ( ) ;
@@ -140,7 +142,7 @@ export const changePassword = async ({currentPassword, newPassword}) => {
140142} ;
141143
142144export const logout = async ( ) => {
143- const response = await safeFetch ( `${ BACKEND_URL } ${ API_BASE_PATH } /logout` , {
145+ const response = await safeFetch ( `${ API_BASE_PATH } /logout` , {
144146 method : 'POST' ,
145147 headers : { 'Content-Type' : 'application/json' } ,
146148 credentials : 'include'
@@ -150,12 +152,13 @@ export const logout = async () => {
150152} ;
151153
152154export const rotateWopiKeys = async ( ) => {
153- const response = await safeFetch ( `${ BACKEND_URL } ${ API_BASE_PATH } /wopi/rotate-keys` , {
155+ const response = await safeFetch ( `${ API_BASE_PATH } /wopi/rotate-keys` , {
154156 method : 'POST' ,
155157 headers : { 'Content-Type' : 'application/json' } ,
156158 credentials : 'include'
157159 } ) ;
158160 if ( ! response . ok ) {
161+ if ( response . status === 401 ) throw new Error ( 'UNAUTHORIZED' ) ;
159162 let errorMessage = 'Failed to rotate WOPI keys' ;
160163 try {
161164 const errorData = await response . json ( ) ;
@@ -169,26 +172,31 @@ export const rotateWopiKeys = async () => {
169172} ;
170173
171174export const checkHealth = async ( ) => {
172- const url = process . env . NODE_ENV === 'development' ? '/healthcheck-api' : '../healthcheck' ;
173- const response = await safeFetch ( url ) ;
175+ const response = await safeFetch ( `${ DOCSERVICE_URL } /healthcheck` ) ;
174176 if ( ! response . ok ) throw new Error ( 'DocService health check failed' ) ;
175177 const result = await response . text ( ) ;
176178 if ( result !== 'true' ) throw new Error ( 'DocService health check failed' ) ;
177179 return true ;
178180} ;
179181
180- export const resetConfiguration = async ( ) => {
181- const response = await safeFetch ( `${ BACKEND_URL } ${ API_BASE_PATH } /config/reset` , {
182+ export const resetConfiguration = async ( paths = [ '*' ] ) => {
183+ const pathsArray = Array . isArray ( paths ) ? paths : [ paths ] ;
184+
185+ const response = await safeFetch ( `${ API_BASE_PATH } /config/reset` , {
182186 method : 'POST' ,
183187 headers : { 'Content-Type' : 'application/json' } ,
184- credentials : 'include'
188+ credentials : 'include' ,
189+ body : JSON . stringify ( { paths : pathsArray } )
185190 } ) ;
186- if ( ! response . ok ) throw new Error ( 'Failed to reset configuration' ) ;
191+ if ( ! response . ok ) {
192+ if ( response . status === 401 ) throw new Error ( 'UNAUTHORIZED' ) ;
193+ throw new Error ( 'Failed to reset configuration' ) ;
194+ }
187195 return response . json ( ) ;
188196} ;
189197
190198export const generateDocServerToken = async body => {
191- const response = await safeFetch ( `${ BACKEND_URL } ${ API_BASE_PATH } /generate-docserver-token` , {
199+ const response = await safeFetch ( `${ API_BASE_PATH } /generate-docserver-token` , {
192200 method : 'POST' ,
193201 headers : { 'Content-Type' : 'application/json' } ,
194202 credentials : 'include' ,
@@ -204,8 +212,7 @@ const callCommandService = async body => {
204212 const { token} = await generateDocServerToken ( body ) ;
205213 body . token = token ;
206214
207- const url = process . env . REACT_APP_DOCSERVICE_URL ? `${ process . env . REACT_APP_DOCSERVICE_URL } /command` : '../command' ;
208- const response = await safeFetch ( url , {
215+ const response = await safeFetch ( `${ DOCSERVICE_URL } /command` , {
209216 method : 'POST' ,
210217 headers : {
211218 'Content-Type' : 'application/json' ,
0 commit comments