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

@frui.ts/apiclient

v1.0.0-rc.4

Published

Frui.ts helpers for HTTP communication

Downloads

330

Readme

@frui.ts/apiclient

Frui.ts provides a simple abstraction for issuing backend calls.

Please note that you are not forced to use this library, and you may make network calls from your repositories (or elsewhere) any way you want.

IApiConnector

IApiConnector is an abstraction for making actual HTTP requests. It provides raw functions (get, post, put, patch, delete) as well as their counterparts that serialize the content (postObject, putObject, patchObject, deleteObject).

FetchApiConnector

The default implementation FetchApiConnector uses window.fetch for network requests, JSON.stringify as a serializer, and a middleware that handles other than 2xx response status codes as errors by default. The default middleware is called handleErrorStatusMiddleware and throws FetchError when the status code is other than 2xx.

You can provide custom implementations of the fetch function, serializer, and middleware through the constructor. You can also customize content-type header.

If you want to use another HTTP client library such as Axios, you should implement the IApiConnector interface with a custom class AxiosApiConnector that would just translate the interface functions to Axios calls.

Also, if your backend uses XML, you should provide custom serializer and customize the contentType constructor parameter.

Usage

// creating new FetchApiConnector with default behavior
const connector = new FetchApiConnector();
// FetchApiConnector with custom fetch function
import { fetch as fetchPolyfill } from "whatwg-fetch";
const connector = new FetchApiConnector({ fetchFunction: fetchPolyfill });
// FetchApiConnector with a custom middleware
function middleware(response: Response) {
  if (response.status >= 500 && response.status < 600) {
    throw "Server error";
  } else {
    return response;
  }
}

const connector = new FetchApiConnector({ middleware });

RestRequestBuilder

RestRequestBuilder helps to compose URLs and properly serialize and deserialize the payload. It is a stateful object that mutates its inner URL. It uses the provided apiConnector to make the actual network call.

It provides three types of requests:

a. Entity calls

The request payload is serialized and the response payload is deserialized to the specified entity type:

get<T>(queryParams?: any): Promise<T>;
post<T>(content: any): Promise<T>;
put<T>(content: any): Promise<T>;
patch<T>(content: any): Promise<T>;

b. Request only calls

The request payload is serialized but the response is not touched.

getRaw(queryParams?: any): Promise<Response>;
postOnly(content: any): Promise<Response>;
putOnly(content: any): Promise<Response>;
patchOnly(content: any): Promise<Response>;
delete(content?: any): Promise<Response>;

c. Custom calls

You can provide custom payload for the request, response payload is not touched.

postData(data?: BodyInit): Promise<Response>;
putData(data?: BodyInit): Promise<Response>;
patchData(data?: BodyInit): Promise<Response>;

Usage

const builder = new RestRequestBuilder(apiConnector, "www.base.url", {});
const invoices = await builder.path(`customers/${customerId}/invoices/${invoiceId}`).get<Invoice[]>(); // GET to www.base.url/customers/123/invoices

builder.reset();

// you can also use fluent url builder
const fluentInvoices = await builder.one("customers", 123).all("invoices").get<Invoice[]>(); // GET to www.base.url/customers/123/invoices

builder.reset();

const users = await builder.all("users").post<User[]>({ foo: "bar" }); // POST to www.base.url/users with content { foo: "bar" }
// Bearer token in the header for all requests
const requestParams: RequestInit = {
  headers: { authorization: "Bearer xxxyyyzzz" },
};
const builder = new RestRequestBuilder(apiConnector, "www.base.url", requestParams);