2.0.0-alpha.3 • Published 6 months ago

@millionfor/io v2.0.0-alpha.3

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

io

A generic IO toolchain for both browser and node with TypeScript support.

Installation

npm i @millionfor/io

Usage

JavaScript

import io from '@millionfor/io';

// Create a custom instance with configuration
const api = io.create({
  baseURL: 'https://api.example.com',
  timeout: 5000,
  cbSuccess: (data) => data.code === 0
});

// Make a GET request
api.get('/users', { page: 1 })
  .then(data => console.log(data))
  .catch(err => console.error(err));

// Make a POST request
api.post('/users', { name: 'John', age: 30 })
  .then(data => console.log(data))
  .catch(err => console.error(err));

// Use JSONP
api.jsonp('https://api.example.com/data', { id: 123 })
  .then(data => console.log(data))
  .catch(err => console.error(err));

TypeScript

import io from '@millionfor/io';
import { IOConfig, IOInstance } from '@millionfor/io';

// Create a custom instance with configuration
const config: IOConfig = {
  baseURL: 'https://api.example.com',
  timeout: 5000,
  cbSuccess: (data) => data.code === 0
};

const api: IOInstance = io.create(config);

// Make a GET request
api.get('/users', { page: 1 })
  .then(data => console.log(data))
  .catch(err => console.error(err));

// Make a POST request
api.post('/users', { name: 'John', age: 30 })
  .then(data => console.log(data))
  .catch(err => console.error(err));

// Use JSONP
api.jsonp('https://api.example.com/data', { id: 123 })
  .then(data => console.log(data))
  .catch(err => console.error(err));
import io from '@millionfor/io';
import { IOConfig, IOInstance } from '@millionfor/io';

// 创建API配置
const apiConfig: IOConfig = {
  baseURL: process.env.VUE_APP_API_BASE_URL || 'https://api.example.com',
  timeout: 10000,
  // 自定义成功判断条件
  cbSuccess: (response) => {
    return response && response.code === 0; // 假设后端返回code=0表示成功
  },
  // 请求拦截器
  headers: {
    'Content-Type': 'application/json'
  }
};

// 创建API实例
const api: IOInstance = io.create(apiConfig);

// 请求拦截器
api.interceptors.request.use(
  (config) => {
    // 在发送请求前做些什么,例如添加token
    const token = localStorage.getItem('token');
    if (token) {
      config.headers = config.headers || {};
      config.headers['Authorization'] = `Bearer ${token}`;
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 响应拦截器
api.interceptors.response.use(
  (response) => {
    // 对响应数据做点什么
    return response;
  },
  (error) => {
    // 对响应错误做点什么
    if (error.response && error.response.status === 401) {
      // 处理未授权错误,例如重定向到登录页
      window.location.href = '/login';
    }
    return Promise.reject(error);
  }
);

export default api;

License

MIT allex.wxn@gmail.com

1.0.2

10 months ago

1.0.3

10 months ago

2.0.0-alpha.3

6 months ago

2.0.0-alpha.0

6 months ago

2.0.0-alpha.1

6 months ago

2.0.0-alpha.2

6 months ago

1.0.1-bate.3

3 years ago

1.0.1-bate.4

3 years ago

1.0.1-bate.5

3 years ago

1.0.1-bate.6

3 years ago

1.0.1-bate.7

3 years ago

1.0.1-bate.2

4 years ago