@@ -122,23 +122,34 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
122
122
const getOptions = { headers : { Accept : 'application/x-ndjson' } , signal }
123
123
const res = await this . #makeRequest( url . toString ( ) , getOptions )
124
124
125
- if ( res . status === 404 ) {
125
+ if ( res == null ) {
126
+ throw new BadResponseError ( 'No response received' )
127
+ }
128
+
129
+ if ( ! res . ok ) {
130
+ if ( res . status === 404 ) {
126
131
// https://specs.ipfs.tech/routing/http-routing-v1/#response-status-codes
127
132
// 404 (Not Found): must be returned if no matching records are found
128
- throw new NotFoundError ( 'No matching records found' )
129
- }
133
+ throw new NotFoundError ( 'No matching records found' )
134
+ }
130
135
131
- if ( res . status === 422 ) {
136
+ if ( res . status === 422 ) {
132
137
// https://specs.ipfs.tech/routing/http-routing-v1/#response-status-codes
133
138
// 422 (Unprocessable Entity): request does not conform to schema or semantic constraints
134
- throw new InvalidRequestError ( 'Request does not conform to schema or semantic constraints' )
139
+ throw new InvalidRequestError ( 'Request does not conform to schema or semantic constraints' )
140
+ }
141
+ throw new BadResponseError ( `Unexpected status code: ${ res . status } ` )
135
142
}
136
143
137
144
if ( res . body == null ) {
138
145
throw new BadResponseError ( 'Routing response had no body' )
139
146
}
140
147
141
148
const contentType = res . headers . get ( 'Content-Type' )
149
+ if ( contentType == null ) {
150
+ throw new BadResponseError ( 'No Content-Type header received' )
151
+ }
152
+
142
153
if ( contentType ?. startsWith ( 'application/json' ) ) {
143
154
const body = await res . json ( )
144
155
@@ -384,10 +395,9 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
384
395
const requestMethod = options . method ?? 'GET'
385
396
const key = `${ requestMethod } -${ url } `
386
397
387
- // Only try to use cache for GET requests
388
398
if ( requestMethod === 'GET' ) {
389
399
const cachedResponse = await this . cache ?. match ( url )
390
- if ( cachedResponse != null ) {
400
+ if ( cachedResponse ?. ok === true ) {
391
401
// Check if the cached response has expired
392
402
const expires = parseInt ( cachedResponse . headers . get ( 'x-cache-expires' ) ?? '0' , 10 )
393
403
if ( expires > Date . now ( ) ) {
0 commit comments