npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

network-stapler

v2.0.0

Published

microlib for organized client side network code

Downloads

5

Readme

network-stapler

This isn´t another full featured client library for making HTTP requests. It´s just a thin abstraction layer around fetch helping you to organize your network code in a more declarative way.

The concept of network-stapler follows the concept of Moya in a reduced form.

Usually when writing an application you may have an ad hoc network layer called like NetworkManager or APIClient. Each time you start with a new app you may copy paste the network client manager from your other apps to your new app and may enhance or change this and that. Those smaller and bigger changes make it difficult to maintain your applications since each network client in each application does the same but looks somehow different and is somehow different organized.

network-stapler is meant to be used as an consistent way to setup and organize your network client code.

Features

  • Compile-time check of correct use of API endpoint
  • Typed API responses
  • Build in mechanism for providing mock data to consumer

Basic Usage

Usually you want a single place for all your network request definitions so all URL or parameter changes are made on a single point and not in x files, each time you use a specific server endpoint.

So to not end up in a single file full of network requests or single requests spread around your codebase network-stapler splits network request definitions with the actual request mechanism. IAPITarget or ITypedAPITarget<T> represent an API endpoint and are as a best practice encapsulated within a function.

const UserAPI = {
    login(username: string, password: string): ITypedAPITarget<IUser> {
        return {
            url: "api/v1/login",
            method: "POST",
            body: {
                username,
                password
            },
            parse: (json) {
                if (json.accesstoken && json.userid) {
                    return json as IUser;
                } else {
                    throw new Error("invalid response");
                }
            }
        };
    },
    
    logout(): IAPITarget {
        return {
            url: "api/v1/logout",
            method: "GET",
        }
    }
}

You may have noticed that the login function returns ITypedAPITarget and the logout function IAPITarget The difference is between those interfaces is that ITypedAPITarget requires a parse function to transform the result json into a defined type. This makes the usage of network-stapler really handy because you can always work with a typed result of your network request and don´t need to check the result each time you make the result at multiple locations in your code. If you don´t care about the type of result body, simply use IAPITarget

After defining the available server endpoints, you need to create an instance of APIClient.

const options: IAPIClientOptions = {
    baseUrl: "https://yourserver.com"
};

const client = new APIClient(options);

You can share one APIClient across your application. You can also make multiple instances if you have multiple services from where you have to query data.

After creating your client you can make requests in three ways

const target: ITypedAPITarget<IUser> = UserAPI.login("aloco90", "awesomepassword");

client.request(target).then(result => {
    // result is the plain response object
}).catch(error => {
    // something went wrong
});

client.requestJSON(target).then(result => {
    // result is the response body as json
}).catch(error => {
    // something went wrong
});

client.requestType(target).then(result => {
    // result is the typed response body
    // in this case an implementation of `IUser`
}).catch(error => {
    // something went wrong
});

Installation

´network-stapler´ is available on npm

npm install network-stapler

Todos

  • [ ] documentation of IAPIClientOptions
  • [ ] full example
  • [ ] more unit tests

Contributing

  • Create something awesome, make the code better, add some functionality, whatever (this is the hardest part).
  • Fork it
  • Create new branch to make your changes
  • Commit all your changes to your branch
  • Submit a pull request