Hey Rob!
Blockquote Can you say a little more? As I read it, customFetch()
returns the return value of Promise.race()
, which returns a Promise.
I just was too focused on what was inside and disregarded that. My bad… it is a Promise.race()
But here’s where I am a little confused. In the custom fetch immediately after getting the signal property:
const signal = params.signal
It is deleted. I am new to the delete
keyword but from the MDN docs it doesn’t seem to be async so it should be deleted from this point forward.
Why?
Here’s what I got so far. I am actually having trouble with the getFaunaError
function right now.
interface ParamsType {
method: string // typical method
headers: {
['X-FaunaDB-API-Version']: string
}
Authorization: string // fauna key in Bearer
body: string // actual query
signal: AbortSignal
// a lot more - https://developer.mozilla.org/en-US/docs/Web/API/fetch
}
export function customFetch(
url: RequestInfo,
params: RequestInit,
): Promise<unknown | Response> {
const signal = params.signal
// delete params.signal
// not sure why we are deleting the signal ??
const abortPromise = new Promise((resolve) => {
if (signal) {
signal.onabort = () => resolve(null)
}
})
return Promise.race([abortPromise, fetch(url, params)])
}
When it comes to the error I found this on GitHub:
https://github.com/fauna/faunadb-js/blob/main/src/types/RequestResult.d.ts
But of course the type we need is a generic.
const { code, description } = error.requestResult.responseContent.errors[0]
Not sure how the errors
property/array is on responseContent
.
I’ll keep looking but I think I’ll shut it down for tonight. Sorry for the confusion from above.
UPDATE:
I found this: https://github.com/fauna/faunadb-js/blob/main/src/types/errors.d.ts
That seems to solve the issue so far. I changed the error function to this:
export function getFaunaError(
error: FaunaHttpErrorResponseContent,
): GetFaunaError {
const { code, description } = error.errors[0]
let status
switch (code) {
case 'instance not found':
status = 404
break
case 'instance not unique':
status = 409
break
case 'permission denied':
status = 403
break
case 'unauthorized':
case 'authentication failed':
status = 401
break
default:
status = 500
}
return { code, description, status }
}
Now because of the AbortController
the custom fetch function returns a Promise<unknown | Request>
… I’m assuming the request would abort if the worker hit it’s time to run.
But the fauna client constructor does not like taking this.
Still working on it