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

@busymango/fetch-driver

v0.2.8

Published

fetch with middlewares for the browser

Downloads

143

Readme

npm version install size npm package minimized gzipped size npm downloads

Table of Contents

Roadmap

  • [x] Make fetch from the browser
  • [x] Supports the Promise API
  • [x] Intercept request and response
  • [x] Transform request and response data
  • [x] Automatic transforms for JSON data

Browser Support

Chrome | Firefox | Safari | Opera | Edge | --- | --- | --- | --- | --- | >= 66 ✔ | >= 57 ✔ | >= 12.1 ✔ | >= 53 ✔ | >= 16 ✔ |

Why onion model

The basic principle of the Onion Model is to process HTTP requests through a series of middleware. Each middleware can perform specific operations before or after the request reaches the target handler. When a request is sent to the server, it passes through each middleware in sequence before being returned to the client.

By adding, removing, or modifying middleware, the request processing flow can be easily adjusted. This allows developers to customize request handling based on specific requirements.

The design of middleware allows them to be reused in different applications or projects. This saves development time and resources, and promotes code maintainability and scalability.

The structure of the Onion Model makes it easier to test each middleware individually. This enables quick identification and resolution of issues, improving code quality and reliability.

By adding logging and monitoring capabilities in appropriate middleware, it becomes possible to track, analyze, and monitor requests and responses. This helps diagnose issues, provide performance optimization, and enhance user experience.

In summary, the Onion Model offers a structured and customizable approach to handling HTTP requests. Its benefits include flexibility, scalability, and code reusability, allowing developers to better organize and manage the request processing flow.

Installing

Package manager

Using npm:

$ npm install @busymango/fetch-driver

Using yarn:

$ yarn add @busymango/fetch-driver

Using pnpm:

$ pnpm add @busymango/fetch-driver

Once the package is installed, you can import the library using import approach:

import FetchDriver from '@busymango/fetch-driver';

Example

Creating

You need create a instance of driver with a custom config.

import FetchDriver from '@busymango/fetch-driver';

const driver = new FetchDriver();

const { drive } = driver;

export { drive };

Use Promise

drive('/user?ID=12345').then(function (response) {
  // handle success
  console.log(response);
}).catch(function (error) {
  // handle error
  console.log(error);
}).finally(function () {
  // always executed
});

// Optionally the request above could also be done as
drive('/user', new URLSearchParams({
  ID: 12345
})).then(function (response) {
  console.log(response);
}).catch(function (error) {
  console.log(error);
}).finally(function () {
  // always executed
});

Use Async/Await

// Want to use async/await? Add the `async` keyword to your outer function/method.
async function getUser() {
  try {
    const response = await driver('/user?ID=12345');
    console.log(response);
  } catch (error) {
    console.error(error);
  }
}

Performing a POST request

drive.post('/user', {
  firstName: 'Fred',
  lastName: 'Flintstone'
}).then(function (response) {
  console.log(response);
}).catch(function (error) {
  console.log(error);
});

FetchDriver API

Requests can be made by passing the relevant config to fetch-driver.

drive(options)
drive({
  method: 'post',
  api: '/user/12345',
  data: {
    firstName: 'Fred',
    lastName: 'Flintstone'
  }
});
drive(api[, data, init])
// Will send a POST request
drive('/user/12345', {
  firstName: 'Fred',
  lastName: 'Flintstone'
});

Drive options

These are the available config options for making requests. Only the api is required.

export type DriveOptions = RequestInit & {
  // `api` is the server URL that will be used for the request
  api: string;
  // `data` is the data to be sent as the request body
  data?: object;
}

Middleware

Define general middleware

import { DriveMiddleware, FetchError } from '@busymango/fetch-driver'
import { catchMsg } from '@utils';

export const general: DriveMiddleware = async (context, next) => {
  try {
    await next();
  } catch (error) {
    if (error instanceof DOMException) {
      if (error.name === 'AbortError') {
        const params = { code: -1, context };
        throw new FetchError('Fetch timeout', params)
      }
    }
  }

  const { response, body } = context;

  if (!response) {
    throw new FetchError(
      'NetWork Error',
      { context, code: 500 },
    )
  }

  const { status, ok } = response;
  const res: unknown = body ?? await response.text();
  
  if (ok !== true) {
    throw new FetchError(
      catchMsg(res),
      { context },
    );
  }
}

Use general middleware

import FetchDriver from '@busymango/fetch-driver';

import { general } from './middlewares';

const { drive } = new FetchDriver([general]);

export { drive };

Abort fetch

await drive({
  api: '/user/12345',
  method: 'GET',
  timeout: 8000,
});

Promises

fetch-driver depends on a native ES6 Promise implementation to be supported. If your environment doesn't support ES6 Promises, you can polyfill.

License

MIT

是 fetch 的一个小包装器,旨在简化执行网络请求和处理响应的方式。 🪶 Small - core is less than 2KB g-zipped

💡 Intuitive - lean API, handles errors, headers and (de)serialization

🧊 Immutable - every call creates a cloned instance that can then be reused safely

🔌 Modular - plug middlewares to intercept requests

🦺 Type safe - strongly typed, written in TypeScript