@foodism/http
TypeScript icon, indicating that this package has built-in type declarations

1.1.5 • Public • Published

ts版本http 请求库

安装

  npm install @foodism/http 

在项目中使用

import Service from "@foodism/http";

class Http extends Service {
  constructor(props?: any) {
  super();
  this.init();
}
init() {
  this.baseURL = "https://jsonplaceholder.typicode.com/";
  this.timeout = 1000;
  this.withCredentials = false;
  this.headers = {
    "Content-Type": "application/json",
  };
  super.setup();
}
}

const http = new Http();

interface Post {
  id: number;
  title: string;
}

async function getUsers() {
  // post 请求
  const data = await http.request<Post[]>({
    url: "/posts",
    method: "post",
    data: {
      userId: 10001,
      id: 100,
      title: "yagn-=titi",
      body: "body-yang",
    },
    headers: {
      "Content-Type": "application/json",
    },
  });
  // get 请求
  // 1. 把get请求的参数 拼接在url后面
  const data2 = await http.request({
    url: "/post?name=xx",
  });

  // 2. 把get请求的参数 放在data里面,自动拼接在url后面
  const data3 = await http.request({
    url: "/post",
    data: {
      name: "xx"
    }  
  });

  console.log("最终的值得");
  console.log(data);
}

getUsers();

Package Sidebar

Install

npm i @foodism/http

Weekly Downloads

1

Version

1.1.5

License

MIT

Unpacked Size

8.56 kB

Total Files

6

Last publish

Collaborators

  • andy1988