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

axios-server

v1.1.1

Published

基于axios封装的server服务类

Downloads

15

Readme

axios-server

axios-server是基于axios封装的server服务类,可以通过配置对象的形式来简化api函数封装,用户只需要配置好接口对象即可,而不需要一个个导出接口函数

使用方法

一、常规原始用法(不推荐) - 配置请求函数,单独导出


// utils/request.js
import Service from "axios-server"

const services = new Service({
    baseURL: "http://127.0.0.1:5050",
    timeout: 10,
    headers: {
        "Content-Type": "application/json"
    }
})

services.axiosInstance.interceptors.request.use((config) => {
    const token = "json-web-token"
    if (config.headers) {
        config.headers["x-token"] = token
    }
    return config
})
services.axiosInstance.interceptors.response.use((res) => {
    return res.data
}, err => {
    return Promise.reject(err.response?.data)
})
export default services;
// api/login.js
import services from '@utils/request.js'
// 提交请求事件
const login = (username, password): AxiosPromise<UserInfo> =>{
    services.commit({
        data: {username, password},
        url: "/users/login",
        method: "post"
    });
}
const userList = (pageSize, pageNum): AxiosPromise<UserInfo[]> =>{
    services.commit({
        params: {pageSize: 1, pageNum: 10},
        url: "/users/list",
        method: "get"
    });
}

// 直接调用get、post等方法请求
const search = (key): AxiosPromise<UserInfo[]> =>{
    services.post( "/users/search",{
      pageSize: 1,
      pageNum: 10
    });
}
const getUserInfo = (key): AxiosPromise<UserInfo[]> =>{
    services.get("/users/userInfo");
}

二、创新用法(推荐) - 配置对象的形式

// api/config.js
export default {
   baseURL: "http://124.221.204.219:8888",
    timeout: 10,
    headers: {
        "Content-Type": "application/json"
    }
}
// api/login.js
import config from './config.js'
import Service,{ handleService } from 'axios-server'


// 定义用户相关模块的接口
const prefix = '/server/main';  // 用户模块接口前缀
const apis = {
    // 获取登录用户信息
    getUserInfo: {
        action: 'getUserInfo',
    },
    // 获取用户信息
    getCurrentUserInfo: {
        action: 'getCurrentUserInfo',
        method: 'post'
    },
    // 获取用户创作内容数据
    getCurrentUserData: {
        action: 'getCurrentUserData',
        method: 'post'
    },
    // 是否关注用户
    isFollowUser: {
        action: '/publicApi/isFollowUser',
        method: 'post'
    },
    // 关注或取消关注用户
    unOrFollowUser: {
        action: '/publicApi/unOrFollowUser',
        method: 'post'
    }
};

// 定义鱼塘模块接口
const fishpondPrefix = '/server/web'; // 鱼塘模块接口前缀
const fishpondPath = '/fishpond';  // 鱼塘模块接口path(适用于当前模块下,前缀相同,但是又根据功能划分了不同接口路径的情况)
const fishpond:any = {
    // 获取用户鱼塘信息
    getUserFishpond: {
        action: '/publicApi/getUserFishpond',
        method: 'post'
    },
  	getFishpondList: {
        action: '/publicApi/getUserFishpond',
      	path: '/fishpond'  // 也可以在这单独写path,不单独写就用统一的fishpondPath,使用handleService方法进行处理
    },
}

handleService(fishpond,fishpondPath)  // 使用内部提供的方法,可以将fishpondPath拼接到每个接口的path上,内部也会判断,如果没有提供method,默认按照get类型进行处理
const service = Object.assign(new Service(config,apis, prefix),new Service(config,fishpond, fishpondPrefix));
export default service;

使用请求方法,以vue为例:

<script setup>
  import server from '@/api/index';
// 获取用户信息
const getUserInfo = async () => {
  let { Relust } = await server.getCurrentUserInfo({  // 不管是get还是post,参数都可以在对象里直接传,不用单独写params或者data
    userId: route.params && route.params.id,
    headers:{  // 也可以单独配置headers
      'x-user-info':'coderlibs'
    }
  })
  userInfo.value = Relust;
}