-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathresource-axios.js
122 lines (103 loc) · 3.59 KB
/
resource-axios.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.resource = factory());
}(this, (function () { 'use strict';
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
return typeof obj;
} : function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
/**
* create vue-resource's resource like object
*
* Default Actions
* get: {method: 'GET'}
* query: {method: 'GET'}
* save: {method: 'POST'}
* update: {method: 'PUT'}
* delete: {method: 'DELETE'}
*
* @param {String} base the resource base
* @param {Object} ac custom actions to overwrite or to add
* @param {Object} ax axios instance
* @returns {Object} the resource object
*/
var error = function error(msg) {
var tag = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'resource-axios';
throw new Error('[' + tag + '] ' + msg + ' | API: https://github.com/liuyanzhi08/resource-axios');
};
var check = function check(input, method) {
var inputType = typeof input === 'undefined' ? 'undefined' : _typeof(input);
if (inputType !== 'number' && inputType !== 'string' && inputType !== 'object') {
error('param should be a number, a string or a object which contains a `id`(or `_id`) attribute', method);
}
};
var getId = function getId(input, method) {
check(input, method);
var id = input;
if ((typeof input === 'undefined' ? 'undefined' : _typeof(input)) === 'object') {
id = input.id || input._id;
}
if (id === undefined) {
error('param type of object should contain a `id`(or `_id`) attribute', method);
}
return id;
};
var resourceAxios = (function (base) {
var ac = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var ax = arguments[2];
// support invoking like: `resource('/api', axios)`
// and `resource('/api', null, axios)`
var http = ax;
var actions = ac;
if (actions && actions.Axios) {
http = actions;
actions = {};
}
if (typeof http === 'undefined') {
error('axios is not imported. since v1.1.0, ' + 'you should require("axios") and call resource("/base/path", axios)', 'init');
}
var query = function query() {
var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
check(params, 'query');
return http.get(base, { params: params });
};
var get$$1 = function get$$1(input) {
if (input === undefined) {
return query();
}
var id = getId(input, 'get');
var path = base + '/' + id;
return http.get(path);
};
var save = function save(data) {
return http.post(base, data);
};
var update = function update(input, data) {
var id = getId(input, 'update');
var resolvedData = data;
if (resolvedData === undefined) {
resolvedData = input;
}
return http.put(base + '/' + id, resolvedData);
};
var _delete = function _delete(input) {
var id = getId(input, 'delete');
return http.delete(base + '/' + id);
};
var resource = {
get: get$$1,
query: query,
save: save,
update: update,
delete: _delete,
// alias methods
post: save,
put: update,
del: _delete
};
return Object.assign(resource, actions);
});
return resourceAxios;
})));