All Projects → tuax → tua-api

tuax / tua-api

Licence: MIT License
🏗 一款可配置的通用 api 请求函数生成工具(A common tool helps converting configs to api functions)

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to tua-api

Vue Music
使用vue2.0构建音乐播放器
Stars: ✭ 60 (+76.47%)
Mutual labels:  axios, jsonp
Vue Music
Music Player for Vue.js
Stars: ✭ 324 (+852.94%)
Mutual labels:  axios, jsonp
react-meeting-room
A simple web app to book meeting rooms inside an office, built using React and Google Calendar API
Stars: ✭ 101 (+197.06%)
Mutual labels:  axios
React-Redux-Saga-Advanced-Starter
Boilerplate for Advanced usage with React, Redux, React-Router-Redux, Redux-Saga, Immutable, Reselect, Recompose, Axios, HMR, Babel v7, Jest, Eslint, and more
Stars: ✭ 66 (+94.12%)
Mutual labels:  axios
alpha
Unified client for HTTP services
Stars: ✭ 21 (-38.24%)
Mutual labels:  axios
fetchingInReact
💎📷 Fetching data from Unsplash.com in React
Stars: ✭ 23 (-32.35%)
Mutual labels:  axios
Nodeactyl
A NodeJS API for Pterodactyl panel, this was originally designed for discord.js (Discord bots)
Stars: ✭ 107 (+214.71%)
Mutual labels:  axios
Covid19-Stats-IN
This app helps in tracking COVID-19 cases in India using covid19India apis
Stars: ✭ 13 (-61.76%)
Mutual labels:  axios
react-hooks-file-upload
React Hooks File Upload example with Progress Bar using Axios, Bootstrap
Stars: ✭ 30 (-11.76%)
Mutual labels:  axios
http-interceptors
The Web apps in this monorepo make HTTP requests and require uniform consistency in how they are executed and handled. This monorepo demonstrates the same app written with Angular and with Svelte. Each app uses HTTP interceptors. The Angular app uses HttpClient and its interceptors while the Svelte app uses Axios and its interceptors.
Stars: ✭ 46 (+35.29%)
Mutual labels:  axios
react-vite-admin
This Starter utilizes React, Recoil, React Query, React Hooks, Typescript, Axios And Vite. 全新技术栈的后台管理系统
Stars: ✭ 90 (+164.71%)
Mutual labels:  axios
nuxt-handson
Nuxt.js Hands-On
Stars: ✭ 24 (-29.41%)
Mutual labels:  axios
login push
vue+koa2+jwt实现单点登录 + todolist增删改查
Stars: ✭ 20 (-41.18%)
Mutual labels:  axios
cloudMusic
(移动端)Vue2.0+Nodejs网易云音乐,网易云音乐api强力驱动,高音质破解(持续更新中)
Stars: ✭ 14 (-58.82%)
Mutual labels:  axios
Code-VueWapDemo
“Vue教程--Wap端项目搭建从0到1”的源码
Stars: ✭ 19 (-44.12%)
Mutual labels:  axios
klourly
📡 A flexible, radius based application for taking attendance
Stars: ✭ 16 (-52.94%)
Mutual labels:  axios
music
🎵vue 像素级还原mac客户端网易云音乐
Stars: ✭ 34 (+0%)
Mutual labels:  axios
vue-douban
高仿豆瓣app
Stars: ✭ 22 (-35.29%)
Mutual labels:  axios
Eldin-Space-Vue
Personal Web Portfolio
Stars: ✭ 54 (+58.82%)
Mutual labels:  axios
vue-ssr-starter
Starter kit for projects with Webpack 4, Vue 2 and SSR
Stars: ✭ 53 (+55.88%)
Mutual labels:  axios

tua-api

让我们优雅地调用 api~

👉完整文档地址点这里👈

Build Status Coverage Status dependencies Downloads per month Version Next Version License

tua-api 是什么?

tua-api 是一个针对发起 api 请求提供辅助功能的库。采用 ES6+ 语法,并采用 jest 进行了完整的单元测试。

目前已适配:

  • web 端:axios, fetch-jsonp
  • Node 端:axios
  • 小程序端:wx.request
Edit tua-api github example

安装

web 端

安装本体

$ npm i -S tua-api
# OR
$ yarn add tua-api

然后直接导入即可

import TuaApi from 'tua-api'

配置武器

配置“武器”分为两种情况:

  • 已配置 CORS 跨域请求头,或是没有跨域需求时,无需任何操作(默认采用的就是 axios)。

  • 若是用不了 CORS,那么就需要设置 reqType: 'jsonp' 借助 jsonp 实现跨域

但是 jsonp 只支持使用 get 的方式请求,所以如果需要发送 post 或其他方式的请求,还是需要使用 axios(服务端还是需要配置 CORS)。

小程序端

安装本体即可

$ npm i -S tua-api
# OR
$ yarn add tua-api
import TuaApi from 'tua-api'

小程序还用不了 npm?@tua-mp/service 了解一下?

tua-api 能干什么?

tua-api 能实现统一管理 api 配置(例如一般放在 src/apis/ 下)。经过处理后,业务侧代码只需要这样写即可:

import { fooApi } from '@/apis/'

fooApi
    .bar({ a: '1', b: '2' }) // 发起请求,a、b 是请求参数
    .then(console.log)       // 收到响应
    .catch(console.error)    // 处理错误

不仅如此,还有一些其他功能:

  • 参数校验
  • 默认参数
  • 中间件(koa 风格)
  • ...
// 甚至可以更进一步和 tua-storage 配合使用
import TuaStorage from 'tua-storage'
import { getSyncFnMapByApis } from 'tua-api'

// 本地写好的各种接口配置
import * as apis from '@/apis'

const tuaStorage = new TuaStorage({
    syncFnMap: getSyncFnMapByApis(apis),
})

const fetchParam = {
    key: fooApi.bar.key,
    syncParams: { a: 'a', b: 'b' },

    // 过期时间,默认值为实例化时的值,以秒为单位
    expires: 10,

    // 是否直接调用同步函数更新数据,默认为 false
    // 适用于需要强制更新数据的场景,例如小程序中的下拉刷新
    isForceUpdate: true,

    // ...
}

tuaStorage
    .load(fetchParam)
    .then(console.log)
    .catch(console.error)

怎么写 api 配置?

拿以下 api 地址举例:

  • https://example-base.com/foo/bar/something/create
  • https://example-base.com/foo/bar/something/modify
  • https://example-base.com/foo/bar/something/delete

地址结构划分

以上地址,一般将其分为3部分:

  • baseUrl: 'https://example-base.com/foo/bar'
  • prefix: 'something'
  • pathList: [ 'create', 'modify', 'delete' ]

文件结构

api/ 一般是这样的文件结构:

.
└── apis
    ├── prefix-1.js
    ├── prefix-2.js
    ├── something.js // <-- 以上的 api 地址会放在这里,名字随意
    └── index.js

基础配置内容

// src/apis/something.js

export default {
    // 接口基础地址
    baseUrl: 'https://example-base.com/foo/bar',

    // 接口的中间路径
    prefix: 'something',

    // 接口地址数组
    pathList: [
        { path: 'create' },
        { path: 'modify' },
        { path: 'delete' },
    ],
}

更多配置请点击这里查看

配置导出

最后来看一下 apis/index.js 该怎么写:

import TuaApi from 'tua-api'

// 初始化
const tuaApi = new TuaApi({ ... })

// 使用中间件
tuaApi
    .use(async (ctx, next) => {
        // 请求发起前
        console.log('before: ', ctx)

        await next()

        // 响应返回后
        console.log('after: ', ctx)
    })
    // 链式调用
    .use(...)

export const fakeGet = tuaApi.getApi(require('./fake-get').default)
export const fakePost = tuaApi.getApi(require('./fake-post').default)

小程序端建议使用 @tua-mp/cli 一键生成 api。

$ tuamp add api <api-name>

配置的构成

tua-api 中配置分为四种:

其中优先级自然是:

默认配置 < 公共配置 < 自身配置 < 运行配置

👉更多配置点击这里👈

Contributors

Thanks goes to these wonderful people (emoji key):

StEve Young
StEve Young

💻 📖 🚇
evinma
evinma

💻

This project follows the all-contributors specification. Contributions of any kind welcome!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].