New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

gauk

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gauk

The axios inspired fetch wrapper

latest
Source
npmnpm
Version
1.0.0
Version published
Weekly downloads
5
400%
Maintainers
1
Weekly downloads
 
Created
Source

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({
    // 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>>;
}

FAQs

Package last updated on 03 Feb 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts