项目作者: glangzh

项目描述 :
retrofit-cjs 是一个基于JavaScript装饰器(Decorator)和 axios 实现的网络请求库, 支持Vue / React / react-native 等常用框架
高级语言: JavaScript
项目地址: git://github.com/glangzh/retrofit-cjs.git


retrofit-cjs

retrofit-cjs 是一个基于JavaScript装饰器(Decorator)和 axios 实现的网络请求库, 支持Vue / React / react-native 等常用框架
tests
npm npm npm

使用方法

1. 安装

  1. npm i retrofit-cjs --save

Babel 转码器的支持

安装 babel-plugin-transform-decorators-legacy

  1. npm i babel-plugin-transform-decorators-legacy -D

配置 .babelrc 文件

  1. "plugins": ["transform-decorators-legacy"]

如果是使用 create-react-app 创建的项目,需要先弹出 webpack 配置

  1. npm run eject

安装 babel-plugin-transform-decorators-legacy,在 package.json 中配置 babel

  1. "babel": {
  2. "presets": [
  3. "react-app"
  4. ],
  5. "plugins": [
  6. "transform-decorators-legacy"
  7. ]
  8. }

vue-cli3 已默认支持 Decorator

2. 引入 retrofit-cjs

  1. import { GET, POST, Headers } from 'retrofit-cjs';

修饰器

属性方法修饰器

3. 使用

  1. 推荐
    ```js
    @AddResInterceptor((res)=>{
    // response result
    return res;
    }, (error)=>{
    // response error
    })
    @Config({timeout: 2000})
    @Headers({‘User-Agent’: ‘request’})
    @Create({
    baseURL: ‘https://cnodejs.org/api‘,
    timeout: 1000,
    headers: {

    1. 'X-Custom-Header': 'foobar'

    }
    })
    class TopicApi{
    static getInstance(){

    1. return new TopicApi();

    }

    @Cancel((cancel) => {

    1. // cancel(); //取消当前请求

    })
    @Config({timeout: 1000})
    @Headers({‘User-Agent’: ‘request’})
    @GET(‘/v1/topics’)
    // @GET(‘/v1/{0}’, ‘topics’)
    // @GET(‘https://cnodejs.org/api/v1/topics‘)
    // @GET({url: ‘/v1/topics’, params: {limit: 10}})
    getTopicList(res){

    1. // 处理结果
    2. return res;

    }

    @Debounce(2000)
    // @HTTP({
    // url: ‘/v1/topic/5433d5e4e737cbe96dcef312’,
    // method: ‘get’,
    // params: {}
    // })
    @GET(‘/v1/topic/{topicId}’)
    getTopicDetails(res){

    1. // response result

    }

    // 以表单方式提交数据
    @FormUrlEncoded
    @POST(‘/user’)
    // @POST({url: ‘/user’, data: {id: 1, name: ‘glang’}})
    addUser(res) {

    }
    }

let topicApi = TopicApi.getInstance();
// topicApi.getTopicDetails(‘topicId=5433d5e4e737cbe96dcef312’, {
// limit: 20
// });
// 参数会按 {} 自动匹配
topicApi.getTopicDetails({
topicId: ‘5433d5e4e737cbe96dcef312’,
limit: 20
});
topicApi.addUser({id: 1, name: ‘glang’});

  1. 2. react / react-native
  2. ```js
  3. import {Interval} from 'retrofit-cjs';
  4. @Create({
  5. baseURL: 'https://cnodejs.org/api'
  6. })
  7. class App extends Component{
  8. constructor(props) {
  9. super(props);
  10. // this.countdwon = this.countdwon.bind(this);
  11. }
  12. @GET('/v1/topics')
  13. getTopicList(res){
  14. // 处理结果
  15. }
  16. @Interval(1000, 60 * 1000)
  17. countdwon(){
  18. }
  19. }
  1. vue
    1. export default {
    2. name: "app",
    3. mounted() {
    4. this.getList();
    5. },
    6. methods: {
    7. // @Config 只影响当前网络请求
    8. @Config({
    9. baseURL: 'https://cnodejs.org/api',
    10. timeout: 1000
    11. })
    12. @GET("/v1/topics")
    13. getList(res, err) {
    14. //
    15. }
    16. }
    17. }

    @RetroPlugin" class="reference-link">@RetroPlugin

    使用Vue插件配置请求基本信息
    ```js
    // Vue 入口文件
    import Vue from ‘vue’
    import {RetroPlugin} from ‘retrofit-cjs’;

Vue.use(RetroPlugin, {
baseURL: ‘https://cnodejs.org/api‘,
timeout: 1000,
headers: {
‘X-Custom-Header’: ‘foobar’
}
});

  1. ### @AddReqInterceptor
  2. > 通过请求拦截器处理请求参数
  3. ```js
  4. @AddReqInterceptor((request)=>{
  5. request.transformRequest = [function (data) {
  6. let ret = ''
  7. for (let it in data) {
  8. ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
  9. }
  10. return ret
  11. }]
  12. return request;
  13. })
  14. class TopicApi{
  15. ...
  16. }

欢迎大佬们吐槽。

LICENSE

MIT@https://github.com/glangzh