gauk
v1.0.0
Published
The axios inspired fetch wrapper
Downloads
18
Readme
Why not just use X?
Why I needed a new fetch wrapper:
- Wrappers throw on invalid responses, I want it to return
undefined
, if response status is successful - Wrappers are complicated, I need simple get,post,put and some
beforeRequest
hooks - Almost all wrappers are pretty big,
Gauk
is1kb gzip
Gauk
normalizes all headers- I need a fetch wrapper that throws on error response codes
Install
pnpm i gauk
Usage
Simple Usage
const gauk = new Gauk({
// provide default options
options: {
responseType: "json", // json is default
baseUrl: "/baseUrl",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
},
beforeRequest: [
// hooks are run serially and get the final url + final options
async (_url, options) => {
options.headers?.set("Foo-Bar", "foo");
},
],
});
const { data } = await gauk.get<Generic>("/url", {
// override options here
});
How request body is parsed into fetch body
The easiest way to explain is just to show the code, (I may forget to update this if it changes in the future, so you can just look at the code)
private parseRequestBody(body: any) {
let parsedBody: FormData | string | undefined = undefined;
if (body instanceof FormData || typeof body === "string") {
parsedBody = body;
} else {
parsedBody = JSON.stringify(body);
}
return parsedBody;
}
Checking for errors
If the response.ok === false
Gauk
will throw the exact response it would give you if it would have succeeded
Response type
The response type is exactly the same as the fetch, but there is an extra property data
that has parsed response
export type IResponse<T> = {
data: T | undefined;
} & Response;
API
class Gauk {
constructor({ options, beforeRequest }?: Init);
get<T>(url: string, optionsUser?: OptionsUser): Promise<IResponse<T>>;
del<T>(url: string, optionsUser?: OptionsUser): Promise<IResponse<T>>;
post<T>(url: string, body: any, optionsUser?: OptionsUser): Promise<IResponse<T>>;
put<T>(url: string, body: any, optionsUser?: OptionsUser): Promise<IResponse<T>>;
}