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

axiosone

v0.1.0

Published

A simple package to consolidate all your axios request configs into callable functions

Downloads

21

Readme

axiosone

A simple module that turns all your axios request configs into callable functions.

Basically, instead of setting up the query everytime you want to call this query

axios.get("/example/posts", { ...some config });

you can do this instead.

axiosone.getPosts(...params);

Continue below to read how you can achieve this. See axios config for the request config object.

Installing

Using npm

npm install axiosone

Using yarn

yarn add axiosone

Examples (CommonJS)

The example below use es5 syntax, but you can also swap with es6+ syntax.

var axiosone = require("axiosone");

// Simple usage with simple axios request config.
axiosone.bindConfig({
  exampleQuery: {
    method: "get",
    url: "/yoururl"
  }
});

// Now call your query by accessing the query name as a function.
axiosone.exampleQuery()
  .then(response => {...})
  .catch(error => {...});

If you want to pass in parameters into your query function, then do the following:

axiosone.bindConfig({
  examplePostQuery: function(text) {
    method: "post",
    url: "/yoururl",
    data: {
      text: text,
    }
  }
});

// Then call like this!
axiosone.examplePostQuery("This is how you call your function.");

Suppose you want to reuse the same config over multiple requests. Then you can provide a list of configs to your query as shown below.

var sharedConfig = {
  ...
};

axiosone.bindConfig({
  exampleQuery: [
    sharedConfig,
    {
      method: "get",
      url: "/yoururl",
    }
  ],

  // If inputs are present, then you can also return a of list
  // of configs from the function.
  examplePostQuery: function(text) {
    return [
      sharedConfig,
      {
        method: "post",
        url: "/posts",
        data: {
          text: text,
        }
      }
    ]
  }
});

// axiosone will merge all your configs in order.
// Then call your function as usual.
axiosone.exampleQuery();
axiosone.examplePostQuery("message");

If you want all requests to use the same shared config, then call extendGlobalConfig method.

// This will automatically bind this config to all your declared axiosone functions.
axiosone.extendGlobalConfig({
  baseUrl: "http://localhost:8000",
  headers: {
    ['Content-Type']: "application/json"
  }
});

Using Typescript

Using Typescript will be more involved, but will allow type checks when passing inputs and receiving outputs from your queries.

import axiosone from "axiosone";

// This will make the function methods inside axiosone instance
// visible.
declare module "axiosone" {
  interface AxiosoneInstance {
    // Passing the config as parameter type will create an query function with parameters.
    login: AxiosoneQueryFunction<typeof config.login>; 
    // Without parameters.
    logout: AxiosoneQueryFunction;
  }
}

// Do not specify type for this config as the type check
// happens during `bindConfig`.
const config = {
  login: (email: string, password: string) => ({
    method: "POST",
    url: "user/login/",
    data: {
      email,
      password,
    },
  }),

  logout: {
    method: "POST",
    url: "user/logout/",
  },
};

axiosone.bindConfig(config);

// When calling your function with incorrect types as inputs...
axiosone.login("name", 1); // Will SCREAM TS-ERROR
axiosone.login("name", "password"); // no errors

// To specify output types, you can declare an input type when calling
// your query function.
const response = await axiosone.login<{ token: string }>(email, password);

Code Splitting (es6)

To separate config from your main code, you can declare bindConfig in a separate as a module before importing inside your main application file.

// apiModule.js
import axiosone from "axiosone"

axiosone.bindConfig({
  exampleQuery: {
    method: "get",
    url: "/user"
  }
});

// app.js (or anywhere to ensure the code above is executed)
import "./apiModule"

Credits

This small package is inspired by its dependency axios.

License

MIT