Skip to content

Commit 5d10d98

Browse files
committed
fix: explicit Content-Type null check
1 parent 29ccab6 commit 5d10d98

File tree

3 files changed

+518
-633
lines changed

3 files changed

+518
-633
lines changed

packages/client/src/client.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,23 +122,34 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
122122
const getOptions = { headers: { Accept: 'application/x-ndjson' }, signal }
123123
const res = await this.#makeRequest(url.toString(), getOptions)
124124

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) {
126131
// https://specs.ipfs.tech/routing/http-routing-v1/#response-status-codes
127132
// 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+
}
130135

131-
if (res.status === 422) {
136+
if (res.status === 422) {
132137
// https://specs.ipfs.tech/routing/http-routing-v1/#response-status-codes
133138
// 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}`)
135142
}
136143

137144
if (res.body == null) {
138145
throw new BadResponseError('Routing response had no body')
139146
}
140147

141148
const contentType = res.headers.get('Content-Type')
149+
if (contentType == null) {
150+
throw new BadResponseError('No Content-Type header received')
151+
}
152+
142153
if (contentType?.startsWith('application/json')) {
143154
const body = await res.json()
144155

@@ -384,10 +395,9 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
384395
const requestMethod = options.method ?? 'GET'
385396
const key = `${requestMethod}-${url}`
386397

387-
// Only try to use cache for GET requests
388398
if (requestMethod === 'GET') {
389399
const cachedResponse = await this.cache?.match(url)
390-
if (cachedResponse != null) {
400+
if (cachedResponse?.ok === true) {
391401
// Check if the cached response has expired
392402
const expires = parseInt(cachedResponse.headers.get('x-cache-expires') ?? '0', 10)
393403
if (expires > Date.now()) {

0 commit comments

Comments
 (0)