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

@emooa/http

v0.0.2

Published

Emooa axios http

Downloads

43

Readme

Http 网络请求

@emooa/http 是一个基于 Axios 二次封装的网络请求库,提供统一的错误弹窗处理,作用于浏览器中。它结合后端接口服务,针对返回体进行错误告警,支持自定义返回的状态码、国际化,以及提示内容等。

特性

  • 基于 Axios 封装
  • 从浏览器创建 XMLHttpRequests
  • 支持 Promise API
  • 支持拦截请求和响应
  • 中英文语言
  • 支持针对不同字段的返回结构体
  • 支持自定义提示内容
  • 支持针对不同状态码提示不同信息

Installation

npm install @emooa/http
// or
yarn add @emooa/http

Usage

简单例子:

// http.ts
import Http from '@emooa/http';

const axios = new Http();
const http = axios.create();

export default http;

// index.ts
import React from 'react';
import { Button, Space } from '@emooa/ui';
import http from './http';

const App: React.FC = () => {
  const fetchData = async () => {
    const data = await http.get('/api/ok');
    console.log(data);
  };
  const fetchData404 = async () => {
    const data = await http.get('/api/404');
    console.log(data);
  };

  return (
    <Space>
      <Button type="primary" onClick={fetchData} status="success">
        Load Success
      </Button>
      <Button type="primary" onClick={fetchData404} status="danger">
        Load 404
      </Button>
    </Space>
  );
};

export default App;

复杂的例子:

// http.ts
import { isEmpty } from 'lodash';
import Http from '@emooa/http';

const axios = new Http({
  mapping: {
    code: 'code',
    message: ['notice', 'message'],
    ok: 0,
  },
  modal: {
    style: { top: 140 },
    info: {
      401: {
        style: { top: 140 },
        content: (
          <div className="pb-10 pt-20">
            <span>
              您好,您当前帐户未登录,请点击下方 <span class="color-primary">重新登录</span> 按钮。
            </span>
          </div>
        ),
        onOk: () => {
          window.location.href = `/login`;
        },
        okText: '登录',
      },
    },
  },
});

const http = axios.create({
  baseURL: '/',
  headers: {
    'X-Client-Token': '******',
  },
});

http.interceptors.request.use(config => {
  config.headers!['TOKEN'] = '******';
  return config;
});

http.interceptors.response.use(response => {
  return response.data;
});

export default http;

更多例子见: Examples.

API

Options

| 参数 | 定义 | 类型 | 默认值 | | ------------ | -------------- | ------------------------ | ---------- | | locale | 国际化语言 | zhCN \| en | zhCN | | colorPrimary | 主题色 | CSSProperties['color'] | #1677ff | | mapping | 返回体映射关系 | Mapping | - | | modal | 返回体映射关系 | Modal | - |

Mapping

| 参数 | 定义 | 类型 | 默认值 | | -------- | ---------------------------------------------- | -------------------- | ---------- | | code | 指定某个字段来代表接口返回的状态 Key | string | code | | ok | 指定返回状态码 | string \| number | 0 | | message | 指定某个字段返回提示消息,一般用于接口错误提示 | string \| string[] | - |

Modal

继承 Emool UI ModalProps

type Modal = ModalProps & {
  /**
   * 自定义提示内容
   */
  content?: React.ReactNode;
  /**
   * 非错误弹窗,属于通知类的弹窗样式,401 可能需要单独提示
   */
  info?: {
    [key: number | string]: ModalProps & { content?: React.ReactNode };
  };
};

License

MIT Licensed
Copyright (c) 2023 Emooa