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

nordus

v0.0.14

Published

Promise based HTTP client for the node.js using Fetch

Downloads

15

Readme

Nordus

Promise based HTTP client for the node.js using Fetch

Compatibility

  • Node.js v18+

Installing

Package manager

Using npm:

$ npm install --save nordus

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

import nordus, { get, post } from 'nordus';

If you use require for importing:

const nordus = require('nordus');

Example

import nordus from 'nordus';
//const nordus = require('nordus'); // legacy way

// Make a request for a user with a given ID
nordus.get('https://jsonplaceholder.typicode.com/users/1')
  .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
nordus.get('https://jsonplaceholder.typicode.com/users/1', {
    params: {
      ID: 12345
    }
  })
  .then(function (response) {
    console.log(response.data);
  })
  .catch(function (error) {
    console.log(error);
  })
  .finally(function () {
    // always executed
  });

// Want to use async/await? Add the `async` keyword to your outer function/method.
async function getUser() {
  try {
    const response = await nordus.get('https://jsonplaceholder.typicode.com/users/1');
    console.log(response.data);
  } catch (error) {
    console.error(error);
  }
}

Performing multiple concurrent requests

function getUserAccount() {
  return nordus.get('https://jsonplaceholder.typicode.com/users/1');
}

function getComments() {
  return nordus.get('https://jsonplaceholder.typicode.com/comments/1');
}

const response = await Promise.all([getUserAccount(), getComments()])

You can create a new instance of nordus with a custom config.

const instance = nordus.create({
  baseURL: 'https://jsonplaceholder.typicode.com',
  headers: {'X-Custom-Header': 'foobar'}
});

try {
  const response = await instance.get('todos/1');
  console.log(response.data);
} catch (error) {
  console.error(error);
}

You can intercept requests or responses before they are handled by then or catch.

const instance = create(
  {
    baseURL: "https://jsonplaceholder.typicode.com/todos/1",
    interceptors: {
      request: (err: Error, request: Request) => {
        request.headers.set("access_token", "Bearer 123");
      },
      response: (err: Error, request: NordusResponse) => {
        console.log(request);
      },
    },
  }
);

You can add timeout on each request.

try {
  const response = await nordus.get('https://jsonplaceholder.typicode.com/todos/1', {
    timeout: 1000,
  });
  console.log(response.data);
} catch (error) {
  console.error(error);
}

Interceptors

You can intercept requests or responses before they are handled by then or catch.

// Add a request and response interceptor
await get("http://localhost:5000/todos/1", {
  interceptors: {
    request: async (err, request) => {
      // Do something before request is sent
      return request;
    },
    response: async (err, response) => {
      // Do something with response data
      return response;
    }
  },
});

You can add interceptors to a custom instance of nordus.

const instance = nordus.create();
instance.interceptors.request.use(() => {/*...*/});

If you need to remove an interceptor later you can.

const myInterceptor = instance.interceptors.request.use(() => {/*...*/});
instance.interceptors.request.eject(myInterceptor);

You can also clear all interceptors for requests or responses.

const instance = nordus.create();
instance.interceptors.request.use(() => {/*...*/});
instance.interceptors.request.clear(); // Removes interceptors from requests
instance.interceptors.response.use(() => {/*...*/});
instance.interceptors.response.clear(); // Removes interceptors from responses

Multiple Interceptors

Given you add multiple response interceptors and when the response was fulfilled

  • then each interceptor is executed
  • then they are executed in the order they were added
  • then only the last interceptor's result is returned
  • then every interceptor receives the result of its predecessor
  • and when the fulfillment-interceptor throws
    • then the following fulfillment-interceptor is not called
    • then the following rejection-interceptor is called
    • once caught, another following fulfill-interceptor is called again (just like in a promise chain).

Read the interceptor tests for seeing all this in code.

License

MIT