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

web-api

v0.2.0

Published

A generic web API wrapping window.fetch

Downloads

79

Readme

web-api

WARNING: This is an experimental WIP

The purpose of this library is to abstract the usage of window.fetch in a more involved environment, where pre and post transformations are made. It's an initial attempt at generalizing some concepts, so that request processing code can be shared across projects. Hopefully this will make creating an API interfaces is quicker and easier.

I envision this being a project where people can discuss various approaches and contribute to building a solid best practices guide for handling the request part of getting data. (Redux already manages the handling of the data well, but doesn't give much guidance on how to request the data).

Getting started

Although it isn't necessary, creating a base class for a kind of API (this example is using the Json placeholder API), helps to keep global API variables and functionality at that level.

Create a base API class which extends BaseWebAPI.

PlaceholderAPI.js

import { BaseWebAPI } from 'web-api';

class PlaceholderAPI extends BaseWebAPI {

  constructor(options = {}) {
    const urlSchema = 'http'; // The default protocol used in BaseWebAPI is https
    const hostURL = 'jsonplaceholder.typicode.com'

    const customOptions = {
      ...options
      host: hostURL,
      protocol: urlSchema
    }; // Use Object.assign to extend any passed in options with our own custom options

    super(customOptions);
  }

}

export default PlaceholderAPI;

Next we will create a class that groups together all endpoints related to posts. This can be any arbitrary grouping, but colocating endpoints that pertain to the same resource makes intuitive sense, when using a REST API. This is the point at which a developer defines the interface between the front end and the back end API service. The font end need not mimic the structure of the back end.

PostsAPI.js

import { BaseWebAPI } from 'web-api';
import { createAPI } from 'web-api';

class PostsAPI extends BaseWebAPI {

  constructor(options) {
    super(options);
  }

  async getAllPosts() {
     return await this.get('/posts') // `this.get` creates a GET request to the resource and appends the url `/posts` to `http://jsonplaceholder.typicode.com`.
  }

}

export default createAPI(PostsAPI);

We can then reference this in the rest of our code to get the processed response.

somefile.js

import { PostsAPI } from PostsAPI;

const allPosts = PostsAPI.getAllPosts();

Basic concepts

  • Create a declarative API on which transformations of a request and a response can be defined.
  • Transformations are applied in sequence and can be defined globally, or for a specific request.