Attaches a callback for only the rejection of the Promise.
Optionalonrejected: null | ((reason: any) => TResult | PromiseLike<TResult>)The callback to execute when the Promise is rejected.
A Promise for the completion of the callback.
Attaches a callback that is invoked when the Promise is settled (fulfilled or rejected). The resolved value cannot be modified from the callback.
Optionalonfinally: null | (() => void)The callback to execute when the Promise is settled (fulfilled or rejected).
A Promise for the completion of the callback.
Attaches callbacks for the resolution and/or rejection of the Promise.
Optionalonfulfilled: null | ((value: T) => TResult1 | PromiseLike<TResult1>)The callback to execute when the Promise is resolved.
Optionalonrejected: null | ((reason: any) => TResult2 | PromiseLike<TResult2>)The callback to execute when the Promise is rejected.
A Promise for the completion of which ever callback is executed.
Adds a handler which allows developers to modify the low-level fetch RequestInit object before the HTTP request is made. These handlers execute after beforeSend handlers. This is useful for one-off occasions when you need to access some aspect of the low-level Fetch API. If you're using this a lot it might make more sense for you to use the Fetch API directly.
//An example of how you might send both files and data in a single request.
const postFilesAndDataResponse = await soFetch.put<PostFilesAndDataResponse>("https://example.com/files-and-data").beforeFetchSend((init:RequestInit) => {
const formData = new FormData()
formData.append("company", "Antoinette");
formData.append("file1", myFile)
const headers = {...init.headers} as Record<string,string>
if (headers["content-type"]) {
delete headers["content-type"]
}
init.body = formData
init.headers = headers
return init
})
For more examples see https://sofetch.antoinette.agency
Adds a handler which will be executed before this HTTP request is sent. BeforeSend handlers added here will will be executed after those added on the config.
await soFetch("https://example.com/users",{name:"Sarah", id:1234}).beforeSend((req:SoFetchRequest) => {
console.info(`Sending ${req.method} request to URL ${req.url}`
})
For more examples see https://sofetch.antoinette.agency
Adds a handler which will be executed on receipt from the server of the specified status code. Multiple handlers will be executed in the order in which they are added. If you add an error handler for a specific status code here any corresponding handlers in the config will not be executed.
An HTTP status code
A function which accepts a Fetch Response as an argument
const unicorn = await soFetch("https://unicorns.com/1234")
.catchHttp(404, (res:Response) => {
console.error("This unicorn can't be found")
})
For more examples see https://sofetch.antoinette.agency
Adds a handler which will be executed after this HTTP request is completed. Handlers will fire regardless of whether the response status code indicated an error
await soFetch("https://example.com/users",{name:"Sarah", id:1234}).onRequestComplete((r: Response) => {
console.info(`Response received from ${r.url} with status ${r.status}`
})
For more examples see https://sofetch.antoinette.agency
An awaitable promise-like class that additionally allows event and error handlers to be attached to the HTTP request
Example