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 🙏

© 2025 – Pkg Stats / Ryan Hefner

firm-ajax

v1.0.2

Published

Using npm:

Readme

firmAjax

安装

Using npm:

$ npm install firm-ajax

Using bower:

$ bower install firm-ajax

Using yarn:

$ yarn firm-ajax

例子

GET请求

import firmAjax from "firm-ajax";

firmAjax
  .get("/api?id=12345")
  .then((response) => {
    console.log(response);
  })
  .catch((error) => {
    console.log(error);
  });

firmAjax
  .get("/api", {
    params: {
      id: 12345,
    },
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

// 用 async await
async function getUser() {
  try {
    const response = await firmAjax.get("/api?id=12345");
    console.log(response);
  } catch (error) {
    console.error(error);
  }
}

POST请求

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

firmAjax api

firmAjax 方法

firmAjax({
  url: "/get",
  params: {
    aa: 45654,
    cc: 6546,
  },
}).then((data) => {
  console.log(data);
});

use中间件

// 发起第一个请求中间件,返回值是给`allRequestEnd`中间件用
firmAjax.use("firstRequest", () => {
  // 这个中间件是一个页面里发起第一个请求,可以用于显示加载提示等等操作
  console.log("第一个发起请求");
  return {
    loading: {},
  };
});

// 所有请求结束,res是{firstResult: firstRequest中间件返回值,totalTime:所有请求完所有时间}
firmAjax.use("allRequestEnd", (res) => {
  // 所有请求完所有时间,可以防止加载提示框闪烁,如果totalTime小于350ms,可以用定时器延迟,firstResult可以是加载提示框对象,这样就可以用于加载提示框
  const { totalTime, firstResult } = res;
  if (totalTime < 450) {
    setTimeout(() => {
      firstResult.close();
    }, 450 - totalTime);
  } else {
    firstResult.close();
  }
});

// 请求前执行中间 config是配置参数
firmAjax.use("beforeRequest", (config) => {
  const { headers = {} } = config;
  config.headers = {
    ...headers,
  };

  // 必须有return,否则修改无效
  return config;
});

// 返回值之前执行中间件,response是返回值,该函数必须返回值,否则修改无效
firmAjax.use("beforeResponse", (response) => {
  const { data } = response;
  if (data.Code === 0) {
    return data.Data;
  }
  return Promise.reject("请求错误");
});

// 请求失败执行中间件,error是返回值,该函数必须返回值,否则修改无效
firmAjax.use("error", (error) => {
  console.log(error);
  console.log(error.code);
  return error;
});

// 监听上传的中间件,要在config中配置isUploadProgress: true
firmAjax.use("upload", (event) => {
  const { url, schedule, method } = event;
  // url: config中的url
  // method: config中的method
  // schedule: 上传进度
  console.log(event.schedule);
});

// 监听下载的中间件,要在config中配置isDownloadProgress: true
firmAjax.use("download", (event) => {
  const { url, schedule, method } = event;
  // url: config中的url
  // method: config中的method
  // schedule: 下载进度
  console.log(event);
});

设置公共的配置项 firmAjax.create(config)

firmAjax.setPublicConfig({
  baseUrl: "https://a.com",
  ...
});

firmAjax 静态方法

firmAjax.request(config)
firmAjax.get(url[, config])
firmAjax.delete(url[, config])
firmAjax.head(url[, config])
firmAjax.options(url[, config])
firmAjax.post(url[, data[, config]])
firmAjax.put(url[, data[, config]])
firmAjax.patch(url[, data[, config]])