Gauk 🇱🇹
The simple axios inspired fetch wrapper
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 is 1kb 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({
options: {
responseType: "json",
baseUrl: "/baseUrl",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
},
beforeRequest: [
async (_url, options) => {
options.headers?.set("Foo-Bar", "foo");
},
],
});
const { data } = await gauk.get<Generic>("/url", {
});
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>>;
}