@@ -83,20 +83,58 @@ type ApiResult<T> = {
8383 data : T ;
8484} ;
8585
86+ type ErrorResponse = {
87+ message ?: string ;
88+ error ?: {
89+ message ?: string ;
90+ } ;
91+ } ;
92+
93+ function redirectToSignIn ( ) {
94+ const from = `${ globalThis . location . pathname } ${ globalThis . location . search } ` ;
95+ globalThis . location . href = `/signin?from=${ encodeURIComponent ( from ) } ` ;
96+ }
97+
98+ function normalizeApiError ( error : unknown , fallbackMessage : string ) : Error {
99+ if ( axios . isAxiosError ( error ) ) {
100+ if ( error . response ?. status === 401 ) {
101+ redirectToSignIn ( ) ;
102+ return new Error ( "Unauthorized" ) ;
103+ }
104+ const responseData = error . response ?. data as ErrorResponse | undefined ;
105+ return new Error (
106+ responseData ?. message ||
107+ responseData ?. error ?. message ||
108+ error . message ||
109+ fallbackMessage
110+ ) ;
111+ }
112+
113+ if ( error instanceof Error ) {
114+ return error ;
115+ }
116+
117+ return new Error ( fallbackMessage ) ;
118+ }
119+
86120/**
87121 * Filter pages with pagination for batch operations.
88122 */
89123export async function filterPages (
90124 query : BatchFilterQuery
91125) : Promise < BatchFilterResult > {
92- const res = await axios . post < ApiResult < BatchFilterResult > > (
93- "/api/page/batch/filter" ,
94- query
95- ) ;
96- if ( res . data . code !== 0 ) {
97- throw new Error ( res . data . message || "Failed to filter pages." ) ;
126+ try {
127+ const res = await axios . post < ApiResult < BatchFilterResult > > (
128+ "/api/page/batch/filter" ,
129+ query
130+ ) ;
131+ if ( res . data . code !== 0 ) {
132+ throw new Error ( res . data . message || "Failed to filter pages." ) ;
133+ }
134+ return res . data . data ;
135+ } catch ( error ) {
136+ throw normalizeApiError ( error , "Failed to filter pages." ) ;
98137 }
99- return res . data . data ;
100138}
101139
102140/**
@@ -105,21 +143,22 @@ export async function filterPages(
105143export async function batchMoveToCollection (
106144 request : BatchMoveRequest
107145) : Promise < BatchMoveResult > {
108- const res = await axios . post < ApiResult < BatchMoveResult > > (
109- "/api/page/batch/moveToCollection" ,
110- request
111- ) ;
112- if ( res . data . code !== 0 ) {
113- throw new Error ( res . data . message || "Failed to move pages." ) ;
146+ try {
147+ const res = await axios . post < ApiResult < BatchMoveResult > > (
148+ "/api/page/batch/moveToCollection" ,
149+ request
150+ ) ;
151+ if ( res . data . code !== 0 ) {
152+ throw new Error ( res . data . message || "Failed to move pages." ) ;
153+ }
154+ return res . data . data ;
155+ } catch ( error ) {
156+ throw normalizeApiError ( error , "Failed to move pages." ) ;
114157 }
115- return res . data . data ;
116158}
117159
118160/**
119- * Collected time mode options with labels for UI.
161+ * Collected time mode values for UI.
120162 */
121- export const COLLECTED_AT_MODE_OPTIONS : { value : CollectedAtMode ; label : string } [ ] = [
122- { value : "KEEP" , label : "Keep Original" } ,
123- { value : "USE_PUBLISH_TIME" , label : "Use Publish Time" } ,
124- ] ;
163+ export const COLLECTED_AT_MODES : CollectedAtMode [ ] = [ "KEEP" , "USE_PUBLISH_TIME" ] ;
125164
0 commit comments