1+ import { requestUrl } from "obsidian" ;
12import type {
23 PendingEmail ,
34 SyncResponse ,
45 MarkSyncedResponse ,
56 ObsidianMetadata ,
7+ SmsApiResponse ,
68} from "./types" ;
79
810export class RiverClient {
@@ -28,23 +30,23 @@ export class RiverClient {
2830 }
2931
3032 const url = `${ this . apiUrl } /api/staging/${ this . userId } /pending` ;
31- console . log ( `River: Fetching pending emails from ${ url } ` ) ;
33+ console . debug ( `River: Fetching pending emails from ${ url } ` ) ;
3234
33- const response = await fetch ( url ) ;
35+ const response = await requestUrl ( url ) ;
3436
35- if ( ! response . ok ) {
37+ if ( response . status >= 400 ) {
3638 throw new Error (
37- `Failed to fetch pending emails: ${ response . status } ${ response . statusText } ` ,
39+ `Failed to fetch pending emails: ${ response . status } ` ,
3840 ) ;
3941 }
4042
41- const data : SyncResponse = await response . json ( ) ;
43+ const data : SyncResponse = response . json ;
4244
4345 if ( ! data . success ) {
4446 throw new Error ( "API returned success=false" ) ;
4547 }
4648
47- console . log ( `River: Found ${ data . count } pending emails` ) ;
49+ console . debug ( `River: Found ${ data . count } pending emails` ) ;
4850
4951 // Mark all emails with source
5052 const emails = data . emails . map ( ( email ) => ( {
@@ -57,7 +59,7 @@ export class RiverClient {
5759
5860 // Merge and return
5961 const allMessages = [ ...emails , ...smsMessages ] ;
60- console . log (
62+ console . debug (
6163 `River: Total pending messages: ${ allMessages . length } (${ emails . length } emails, ${ smsMessages . length } SMS)` ,
6264 ) ;
6365 return allMessages ;
@@ -73,28 +75,28 @@ export class RiverClient {
7375
7476 try {
7577 const url = `${ this . smsApiUrl } /api/sms/staging/pending?userId=${ this . userId } ` ;
76- console . log ( `River: Fetching pending SMS from ${ url } ` ) ;
78+ console . debug ( `River: Fetching pending SMS from ${ url } ` ) ;
7779
78- const response = await fetch ( url ) ;
80+ const response = await requestUrl ( url ) ;
7981
80- if ( ! response . ok ) {
82+ if ( response . status >= 400 ) {
8183 console . warn (
8284 `River: Failed to fetch SMS (${ response . status } ), continuing with emails only` ,
8385 ) ;
8486 return [ ] ;
8587 }
8688
87- const data = await response . json ( ) ;
89+ const data = response . json ;
8890
8991 if ( ! data . messages || ! Array . isArray ( data . messages ) ) {
9092 console . warn ( "River: Invalid SMS response format" ) ;
9193 return [ ] ;
9294 }
9395
94- console . log ( `River: Found ${ data . messages . length } pending SMS` ) ;
96+ console . debug ( `River: Found ${ data . messages . length } pending SMS` ) ;
9597
9698 // Transform SMS messages to PendingEmail format
97- return data . messages . map ( ( sms : any ) => {
99+ return data . messages . map ( ( sms : SmsApiResponse ) => {
98100 const receivedDate = new Date ( sms . received_at ) ;
99101
100102 return {
@@ -132,19 +134,20 @@ export class RiverClient {
132134 if ( source === "sms" ) {
133135 // Mark SMS as synced
134136 const url = `${ this . smsApiUrl } /api/sms/staging/${ messageId } /synced` ;
135- console . log ( `River: Marking SMS ${ messageId } as synced` ) ;
137+ console . debug ( `River: Marking SMS ${ messageId } as synced` ) ;
136138
137- const response = await fetch ( url , {
139+ const response = await requestUrl ( {
140+ url,
138141 method : "POST" ,
139142 headers : {
140143 "Content-Type" : "application/json" ,
141144 } ,
142145 body : JSON . stringify ( { obsidianMetadata : metadata } ) ,
143146 } ) ;
144147
145- if ( ! response . ok ) {
148+ if ( response . status >= 400 ) {
146149 throw new Error (
147- `Failed to mark SMS as synced: ${ response . status } ${ response . statusText } ` ,
150+ `Failed to mark SMS as synced: ${ response . status } ` ,
148151 ) ;
149152 }
150153
@@ -153,23 +156,24 @@ export class RiverClient {
153156
154157 // Mark email as synced
155158 const url = `${ this . apiUrl } /api/staging/${ messageId } /mark-synced` ;
156- console . log ( `River: Marking email ${ messageId } as synced` ) ;
159+ console . debug ( `River: Marking email ${ messageId } as synced` ) ;
157160
158- const response = await fetch ( url , {
161+ const response = await requestUrl ( {
162+ url,
159163 method : "POST" ,
160164 headers : {
161165 "Content-Type" : "application/json" ,
162166 } ,
163167 body : JSON . stringify ( { obsidianMetadata : metadata } ) ,
164168 } ) ;
165169
166- if ( ! response . ok ) {
170+ if ( response . status >= 400 ) {
167171 throw new Error (
168- `Failed to mark email as synced: ${ response . status } ${ response . statusText } ` ,
172+ `Failed to mark email as synced: ${ response . status } ` ,
169173 ) ;
170174 }
171175
172- const data : MarkSyncedResponse = await response . json ( ) ;
176+ const data : MarkSyncedResponse = response . json ;
173177
174178 if ( ! data . success ) {
175179 throw new Error ( "Failed to mark email as synced" ) ;
@@ -182,8 +186,8 @@ export class RiverClient {
182186 async testConnection ( ) : Promise < boolean > {
183187 try {
184188 const url = `${ this . apiUrl } /health` ;
185- const response = await fetch ( url ) ;
186- return response . ok ;
189+ const response = await requestUrl ( url ) ;
190+ return response . status < 400 ;
187191 } catch ( error ) {
188192 console . error ( "River: Connection test failed:" , error ) ;
189193 return false ;
0 commit comments