larapi 0.1.2 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +3 -0
- package/dist/index.js.LICENSE.txt +6 -0
- package/dist/index.js.map +1 -0
- package/lib/Core.d.ts +2 -3
- package/lib/Http.d.ts +9 -5
- package/lib/Queue.d.ts +13 -0
- package/lib/index.d.ts +19 -7
- package/lib/types.d.ts +2 -2
- package/package.json +7 -5
- package/src/Core.ts +3 -10
- package/src/Http.ts +34 -13
- package/src/Queue.ts +43 -0
- package/src/index.ts +49 -11
- package/src/types.ts +2 -2
- package/webpack.config.js +32 -0
- package/lib/Core.js +0 -149
- package/lib/Http.js +0 -212
- package/lib/index.js +0 -212
- package/lib/types.js +0 -2
package/src/index.ts
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
1
|
import Core from "./Core";
|
|
2
|
-
import {
|
|
2
|
+
import type {
|
|
3
|
+
TMethod,
|
|
4
|
+
TParams,
|
|
5
|
+
TListenerEvents
|
|
6
|
+
} from "./types";
|
|
7
|
+
import type {
|
|
8
|
+
Event
|
|
9
|
+
} from "easy-event-emitter";
|
|
3
10
|
import Http from "./Http";
|
|
4
11
|
|
|
5
12
|
type TUser = {
|
|
13
|
+
id: number;
|
|
6
14
|
[index: string]: any;
|
|
7
15
|
}
|
|
8
16
|
|
|
@@ -10,17 +18,18 @@ type TConfig = {
|
|
|
10
18
|
host: string;
|
|
11
19
|
}
|
|
12
20
|
|
|
13
|
-
|
|
21
|
+
/** Laravel API Client */
|
|
22
|
+
class API<USER extends TUser = TUser> extends Core {
|
|
14
23
|
private static instance = new API({
|
|
15
24
|
host: ''
|
|
16
25
|
});
|
|
17
|
-
private _user:
|
|
26
|
+
private _user: USER;
|
|
18
27
|
token?: string;
|
|
19
28
|
|
|
20
29
|
constructor(config: TConfig) {
|
|
21
30
|
super();
|
|
22
31
|
this.setHost(config.host);
|
|
23
|
-
this._user = {};
|
|
32
|
+
this._user = {} as USER;
|
|
24
33
|
this.init();
|
|
25
34
|
|
|
26
35
|
this.setToken = this.setToken.bind(this);
|
|
@@ -31,6 +40,10 @@ class API extends Core {
|
|
|
31
40
|
this.http = this.http.bind(this);
|
|
32
41
|
this.get = this.get.bind(this);
|
|
33
42
|
this.post = this.post.bind(this);
|
|
43
|
+
this.put = this.put.bind(this);
|
|
44
|
+
this.patch = this.patch.bind(this);
|
|
45
|
+
this.delete = this.delete.bind(this);
|
|
46
|
+
this.options = this.options.bind(this);
|
|
34
47
|
this.getUid = this.getUid.bind(this);
|
|
35
48
|
this.getUser = this.getUser.bind(this);
|
|
36
49
|
}
|
|
@@ -51,12 +64,28 @@ class API extends Core {
|
|
|
51
64
|
return this.http('POST', params).request();
|
|
52
65
|
}
|
|
53
66
|
|
|
67
|
+
public put(params: TParams): Http {
|
|
68
|
+
return this.http('PUT', params).request();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
public patch(params: TParams): Http {
|
|
72
|
+
return this.http('PATCH', params).request();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
public delete(params: TParams): Http {
|
|
76
|
+
return this.http('DELETE', params).request();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
public options(params: TParams): Http {
|
|
80
|
+
return this.http('OPTIONS', params).request();
|
|
81
|
+
}
|
|
82
|
+
|
|
54
83
|
private init(): void {
|
|
55
84
|
if (typeof window === 'undefined') return;
|
|
56
85
|
const token = localStorage.getItem('token');
|
|
57
86
|
const strUser = localStorage.getItem('user');
|
|
58
87
|
if (strUser) {
|
|
59
|
-
const user:
|
|
88
|
+
const user: USER = JSON.parse(strUser);
|
|
60
89
|
this.setUser(user);
|
|
61
90
|
}
|
|
62
91
|
if (token) {
|
|
@@ -64,6 +93,10 @@ class API extends Core {
|
|
|
64
93
|
}
|
|
65
94
|
}
|
|
66
95
|
|
|
96
|
+
public addListener(event: TListenerEvents, callback: (data: any) => void): Event {
|
|
97
|
+
return this.events.addListener(event, callback);
|
|
98
|
+
}
|
|
99
|
+
|
|
67
100
|
public setToken(token: string) {
|
|
68
101
|
if (typeof window === 'undefined') return;
|
|
69
102
|
localStorage.setItem('token', token);
|
|
@@ -75,10 +108,10 @@ class API extends Core {
|
|
|
75
108
|
}
|
|
76
109
|
|
|
77
110
|
public getUid(): number {
|
|
78
|
-
return 'id' in this._user ? Number(this._user.id) : 0;
|
|
111
|
+
return ('id' in this._user) ? Number(this._user.id) : 0;
|
|
79
112
|
}
|
|
80
113
|
|
|
81
|
-
public getUser():
|
|
114
|
+
public getUser(): USER {
|
|
82
115
|
return this._user;
|
|
83
116
|
}
|
|
84
117
|
|
|
@@ -86,18 +119,18 @@ class API extends Core {
|
|
|
86
119
|
return this.getUid();
|
|
87
120
|
}
|
|
88
121
|
|
|
89
|
-
public get user():
|
|
122
|
+
public get user(): USER {
|
|
90
123
|
return this.getUser();
|
|
91
124
|
}
|
|
92
125
|
|
|
93
|
-
public setUser(user:
|
|
126
|
+
public setUser(user: USER) {
|
|
94
127
|
if (typeof window === 'undefined') return;
|
|
95
128
|
localStorage.setItem('user', JSON.stringify(user));
|
|
96
129
|
this._user = user;
|
|
97
130
|
this.events.emit('login', user);
|
|
98
131
|
}
|
|
99
132
|
|
|
100
|
-
public updateUser(user:
|
|
133
|
+
public updateUser(user: USER) {
|
|
101
134
|
this.events.emit('update', this.user);
|
|
102
135
|
this.setUser({...this.user, ...user});
|
|
103
136
|
}
|
|
@@ -107,7 +140,7 @@ class API extends Core {
|
|
|
107
140
|
localStorage.removeItem('user');
|
|
108
141
|
localStorage.removeItem('token');
|
|
109
142
|
this.events.emit('logout', this.user);
|
|
110
|
-
this.setUser({});
|
|
143
|
+
this.setUser({} as USER);
|
|
111
144
|
this.setToken('');
|
|
112
145
|
}
|
|
113
146
|
|
|
@@ -127,6 +160,11 @@ class API extends Core {
|
|
|
127
160
|
public static getUser = this.instance.getUser;
|
|
128
161
|
public static user = this.instance.user;
|
|
129
162
|
public static uid = this.instance.uid;
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* @deprecated The method should not be used
|
|
166
|
+
*/
|
|
167
|
+
public static deleteCacheGroup = this.instance.clearCacheGroup;
|
|
130
168
|
}
|
|
131
169
|
|
|
132
170
|
(globalThis as any).apiSetInitData = API.setInitData;
|
package/src/types.ts
CHANGED
|
@@ -16,7 +16,7 @@ export type TGroupsData = {
|
|
|
16
16
|
[index: string]: TData;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
export type TMethod = 'GET' | 'POST';
|
|
19
|
+
export type TMethod = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS';
|
|
20
20
|
|
|
21
21
|
export type TCacheControll = string | {
|
|
22
22
|
group: string;
|
|
@@ -44,4 +44,4 @@ export type TRequestParams = {
|
|
|
44
44
|
body?: string;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
export type TListenerEvents = 'api-request-fail' | 'api-request-error' | 'api-request-complete' | 'login' | 'logout' | 'user-update';
|
|
47
|
+
export type TListenerEvents = 'api-request-success' | 'api-request-fail' | 'api-request-error' | 'api-request-complete' | 'login' | 'logout' | 'user-update';
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
|
|
3
|
+
module.exports = {
|
|
4
|
+
entry: {
|
|
5
|
+
index: '/src/index.ts',
|
|
6
|
+
},
|
|
7
|
+
target: 'web',
|
|
8
|
+
mode: 'production', //production | development
|
|
9
|
+
devtool: "source-map",
|
|
10
|
+
module: {
|
|
11
|
+
rules: [
|
|
12
|
+
{
|
|
13
|
+
test: /\.tsx?$/,
|
|
14
|
+
use: [
|
|
15
|
+
'ts-loader'
|
|
16
|
+
],
|
|
17
|
+
exclude: /node_modules/,
|
|
18
|
+
}
|
|
19
|
+
],
|
|
20
|
+
},
|
|
21
|
+
resolve: {
|
|
22
|
+
extensions: ['.tsx', '.ts', '.js'],
|
|
23
|
+
},
|
|
24
|
+
|
|
25
|
+
output: {
|
|
26
|
+
filename: '[name].js',
|
|
27
|
+
path: path.resolve(__dirname, 'dist'),
|
|
28
|
+
library: 'API',
|
|
29
|
+
libraryTarget: 'umd',
|
|
30
|
+
auxiliaryComment: 'API client for laravel'
|
|
31
|
+
}
|
|
32
|
+
};
|
package/lib/Core.js
DELETED
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const easy_event_emitter_1 = __importDefault(require("easy-event-emitter"));
|
|
7
|
-
class Core {
|
|
8
|
-
constructor(context) {
|
|
9
|
-
Object.defineProperty(this, "cache", {
|
|
10
|
-
enumerable: true,
|
|
11
|
-
configurable: true,
|
|
12
|
-
writable: true,
|
|
13
|
-
value: void 0
|
|
14
|
-
});
|
|
15
|
-
Object.defineProperty(this, "initData", {
|
|
16
|
-
enumerable: true,
|
|
17
|
-
configurable: true,
|
|
18
|
-
writable: true,
|
|
19
|
-
value: void 0
|
|
20
|
-
});
|
|
21
|
-
Object.defineProperty(this, "events", {
|
|
22
|
-
enumerable: true,
|
|
23
|
-
configurable: true,
|
|
24
|
-
writable: true,
|
|
25
|
-
value: void 0
|
|
26
|
-
});
|
|
27
|
-
Object.defineProperty(this, "host", {
|
|
28
|
-
enumerable: true,
|
|
29
|
-
configurable: true,
|
|
30
|
-
writable: true,
|
|
31
|
-
value: void 0
|
|
32
|
-
});
|
|
33
|
-
this.events = new easy_event_emitter_1.default();
|
|
34
|
-
this.host = 'http://127.0.0.1/';
|
|
35
|
-
this.cache = {};
|
|
36
|
-
this.initData = {};
|
|
37
|
-
this.setHost = this.setHost.bind(this);
|
|
38
|
-
this.addListener = this.addListener.bind(this);
|
|
39
|
-
this.setInitData = this.setInitData.bind(this);
|
|
40
|
-
this.clearCacheGroup = this.clearCacheGroup.bind(this);
|
|
41
|
-
Object.assign(this, context);
|
|
42
|
-
}
|
|
43
|
-
addListener(event, callback) {
|
|
44
|
-
return this.events.addListener(event, callback);
|
|
45
|
-
}
|
|
46
|
-
getCache(key) {
|
|
47
|
-
if (this.cache[key]) {
|
|
48
|
-
if ((new Date()).getTime() <= (this.cache[key].time + 3600000000)) {
|
|
49
|
-
return this.cache[key].data;
|
|
50
|
-
}
|
|
51
|
-
else {
|
|
52
|
-
delete this.cache[key];
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
isJsonString(str) {
|
|
57
|
-
try {
|
|
58
|
-
JSON.parse(str);
|
|
59
|
-
}
|
|
60
|
-
catch (e) {
|
|
61
|
-
return false;
|
|
62
|
-
}
|
|
63
|
-
return true;
|
|
64
|
-
}
|
|
65
|
-
setHost(host) {
|
|
66
|
-
this.host = host;
|
|
67
|
-
}
|
|
68
|
-
setCache(key, data, group) {
|
|
69
|
-
this.cache[key] = {
|
|
70
|
-
time: (new Date()).getTime(),
|
|
71
|
-
data: data,
|
|
72
|
-
group: group
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
clearCacheGroup(group, data, fieldKey = 'id') {
|
|
76
|
-
if (data) {
|
|
77
|
-
const clear = (cacheData) => {
|
|
78
|
-
if ((fieldKey && (cacheData === null || cacheData === void 0 ? void 0 : cacheData[fieldKey]) === (data === null || data === void 0 ? void 0 : data[fieldKey])) || fieldKey === null) {
|
|
79
|
-
for (const key in cacheData) {
|
|
80
|
-
if (key in data) {
|
|
81
|
-
return false;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
return true;
|
|
86
|
-
};
|
|
87
|
-
for (const key in this.cache) {
|
|
88
|
-
if (this.cache[key].group === group) {
|
|
89
|
-
if (Array.isArray(this.cache[key].data)) {
|
|
90
|
-
this.cache[key].data = Object.values(this.cache[key].data).filter(clear);
|
|
91
|
-
}
|
|
92
|
-
else {
|
|
93
|
-
if (!clear(this.cache[key].data))
|
|
94
|
-
delete this.cache[key];
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
else {
|
|
100
|
-
for (const key in this.cache) {
|
|
101
|
-
if (this.cache[key].group === group) {
|
|
102
|
-
delete this.cache[key];
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
updateCacheGroup(group, data, fieldKey = 'id') {
|
|
108
|
-
const update = (cacheData) => {
|
|
109
|
-
const retData = cacheData;
|
|
110
|
-
for (const key of fieldKey.split('.')) {
|
|
111
|
-
cacheData = cacheData[key];
|
|
112
|
-
}
|
|
113
|
-
if ((fieldKey && cacheData[fieldKey] === data[fieldKey]) || fieldKey === null) {
|
|
114
|
-
for (const key in cacheData) {
|
|
115
|
-
if (key in data) {
|
|
116
|
-
cacheData[key] = data[key];
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
return retData;
|
|
121
|
-
};
|
|
122
|
-
for (const key in this.cache) {
|
|
123
|
-
if (this.cache[key].group === group) {
|
|
124
|
-
if (Array.isArray(this.cache[key].data)) {
|
|
125
|
-
this.cache[key].data = Object.values(this.cache[key].data).map(update);
|
|
126
|
-
}
|
|
127
|
-
else {
|
|
128
|
-
this.cache[key].data = update(this.cache[key].data);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
getCacheByIndex(index) {
|
|
134
|
-
return this.cache[index];
|
|
135
|
-
}
|
|
136
|
-
deleteCache(key) {
|
|
137
|
-
if (!this.cache[key])
|
|
138
|
-
return;
|
|
139
|
-
const group = this.cache[key].group;
|
|
140
|
-
if (group) {
|
|
141
|
-
this.clearCacheGroup(group);
|
|
142
|
-
}
|
|
143
|
-
delete this.cache[key];
|
|
144
|
-
}
|
|
145
|
-
setInitData(data) {
|
|
146
|
-
this.initData = data;
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
exports.default = Core;
|
package/lib/Http.js
DELETED
|
@@ -1,212 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
//@ts-ignore
|
|
7
|
-
const md5_1 = __importDefault(require("md5"));
|
|
8
|
-
const Core_1 = __importDefault(require("./Core"));
|
|
9
|
-
class Http extends Core_1.default {
|
|
10
|
-
constructor(method, params, context) {
|
|
11
|
-
super(context);
|
|
12
|
-
Object.defineProperty(this, "cacheIndex", {
|
|
13
|
-
enumerable: true,
|
|
14
|
-
configurable: true,
|
|
15
|
-
writable: true,
|
|
16
|
-
value: void 0
|
|
17
|
-
});
|
|
18
|
-
Object.defineProperty(this, "currentCache", {
|
|
19
|
-
enumerable: true,
|
|
20
|
-
configurable: true,
|
|
21
|
-
writable: true,
|
|
22
|
-
value: void 0
|
|
23
|
-
});
|
|
24
|
-
Object.defineProperty(this, "requestParams", {
|
|
25
|
-
enumerable: true,
|
|
26
|
-
configurable: true,
|
|
27
|
-
writable: true,
|
|
28
|
-
value: void 0
|
|
29
|
-
});
|
|
30
|
-
Object.defineProperty(this, "method", {
|
|
31
|
-
enumerable: true,
|
|
32
|
-
configurable: true,
|
|
33
|
-
writable: true,
|
|
34
|
-
value: void 0
|
|
35
|
-
});
|
|
36
|
-
Object.defineProperty(this, "params", {
|
|
37
|
-
enumerable: true,
|
|
38
|
-
configurable: true,
|
|
39
|
-
writable: true,
|
|
40
|
-
value: void 0
|
|
41
|
-
});
|
|
42
|
-
Object.defineProperty(this, "path", {
|
|
43
|
-
enumerable: true,
|
|
44
|
-
configurable: true,
|
|
45
|
-
writable: true,
|
|
46
|
-
value: void 0
|
|
47
|
-
});
|
|
48
|
-
this.cacheIndex = '';
|
|
49
|
-
this.method = method;
|
|
50
|
-
this.params = params;
|
|
51
|
-
this.path = this.params.path;
|
|
52
|
-
this.requestParams = {
|
|
53
|
-
method: this.method
|
|
54
|
-
};
|
|
55
|
-
this.success = this.success.bind(this);
|
|
56
|
-
this.fail = this.fail.bind(this);
|
|
57
|
-
this.error = this.error.bind(this);
|
|
58
|
-
this.complete = this.complete.bind(this);
|
|
59
|
-
this.request = this.request.bind(this);
|
|
60
|
-
this.addListener = this.addListener.bind(this);
|
|
61
|
-
this.addHeader = this.addHeader.bind(this);
|
|
62
|
-
this.deleteHeader = this.deleteHeader.bind(this);
|
|
63
|
-
this.initRequest();
|
|
64
|
-
this.initCache();
|
|
65
|
-
}
|
|
66
|
-
initRequest() {
|
|
67
|
-
this.requestParams.headers = {
|
|
68
|
-
'Accept': 'application/json',
|
|
69
|
-
'Content-Type': 'application/json'
|
|
70
|
-
};
|
|
71
|
-
this.params.data = this.params.data ? this.cuteUndifinedParams(this.params.data) : this.params.data;
|
|
72
|
-
if (this.method === 'GET') {
|
|
73
|
-
if (this.params.data)
|
|
74
|
-
this.path = this.params.path + '?' + this.encodeUrlParams(this.params.data);
|
|
75
|
-
}
|
|
76
|
-
if (this.method === 'POST') {
|
|
77
|
-
this.requestParams.body = JSON.stringify(this.params.data);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
initCache() {
|
|
81
|
-
var _a;
|
|
82
|
-
this.cacheIndex = (0, md5_1.default)(this.path + JSON.stringify(this.params.data));
|
|
83
|
-
if (this.params.globalName) {
|
|
84
|
-
if (this.initData[this.params.globalName]) {
|
|
85
|
-
this.setCache(this.cacheIndex, this.initData[this.params.globalName], (typeof this.params.cache === 'boolean' ? undefined : this.params.cache));
|
|
86
|
-
this.params.cache = (_a = this.params.cache) !== null && _a !== void 0 ? _a : true;
|
|
87
|
-
delete this.initData[this.params.globalName];
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
if (this.params.cache) {
|
|
91
|
-
if (this.currentCache = this.getCache(this.cacheIndex)) {
|
|
92
|
-
this.success(this.currentCache);
|
|
93
|
-
this.complete(this.currentCache);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
else {
|
|
97
|
-
this.deleteCache(this.cacheIndex);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
//Заголовки от 200 до 299
|
|
101
|
-
success(...args) {
|
|
102
|
-
if (typeof this.params.success === 'function')
|
|
103
|
-
this.params.success(...args);
|
|
104
|
-
if (this.params.cacheUpdate && this.params.data) {
|
|
105
|
-
this.updateCacheGroup(typeof this.params.cacheUpdate === 'string' ? this.params.cacheUpdate : this.params.cacheUpdate.group, this.params.data, typeof this.params.cacheUpdate === 'string' ? undefined : this.params.cacheUpdate.fieldKey);
|
|
106
|
-
}
|
|
107
|
-
if (this.params.cacheClear && this.params.data) {
|
|
108
|
-
this.clearCacheGroup(typeof this.params.cacheClear === 'string' ? this.params.cacheClear : this.params.cacheClear.group, this.params.data, typeof this.params.cacheClear === 'string' ? undefined : this.params.cacheClear.fieldKey);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
//Заголовки от 400 до 499
|
|
112
|
-
fail(...args) {
|
|
113
|
-
if (typeof this.params.fail === 'function') {
|
|
114
|
-
this.params.fail(...args);
|
|
115
|
-
}
|
|
116
|
-
this.events.emit('api-request-fail', args);
|
|
117
|
-
}
|
|
118
|
-
//Все остальные
|
|
119
|
-
error(...args) {
|
|
120
|
-
if (typeof this.params.error === 'function') {
|
|
121
|
-
this.params.error(...args);
|
|
122
|
-
}
|
|
123
|
-
this.events.emit('api-request-error', args);
|
|
124
|
-
}
|
|
125
|
-
complete(...args) {
|
|
126
|
-
if (typeof this.params.complete === 'function') {
|
|
127
|
-
this.params.complete(...args);
|
|
128
|
-
}
|
|
129
|
-
this.events.emit('api-request-complete', args);
|
|
130
|
-
}
|
|
131
|
-
encodeUrlParams(data) {
|
|
132
|
-
return Object.keys(data).map(function (k) {
|
|
133
|
-
return encodeURIComponent(k) + "=" + encodeURIComponent(data[k]);
|
|
134
|
-
}).join('&');
|
|
135
|
-
}
|
|
136
|
-
cuteUndifinedParams(data) {
|
|
137
|
-
if (typeof data !== 'object') {
|
|
138
|
-
return;
|
|
139
|
-
}
|
|
140
|
-
for (const key in data) {
|
|
141
|
-
if (!data[key]) {
|
|
142
|
-
delete data[key];
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
return data;
|
|
146
|
-
}
|
|
147
|
-
request() {
|
|
148
|
-
if (typeof XMLHttpRequest === 'undefined' || this.currentCache)
|
|
149
|
-
return this;
|
|
150
|
-
try {
|
|
151
|
-
const xhr = new XMLHttpRequest();
|
|
152
|
-
xhr.open(this.method, this.host + this.path, true);
|
|
153
|
-
for (const header in this.requestParams.headers) {
|
|
154
|
-
xhr.setRequestHeader(header, this.requestParams.headers[header]);
|
|
155
|
-
}
|
|
156
|
-
xhr.onreadystatechange = () => {
|
|
157
|
-
if (xhr.readyState !== 4) {
|
|
158
|
-
return;
|
|
159
|
-
}
|
|
160
|
-
const result = this.isJsonString(xhr.responseText) ? JSON.parse(xhr.responseText) : {};
|
|
161
|
-
if (xhr.status >= 200 && xhr.status <= 299) {
|
|
162
|
-
if (this.params.cache) {
|
|
163
|
-
this.setCache(this.cacheIndex, result, (typeof this.params.cache === 'boolean' ? undefined : this.params.cache));
|
|
164
|
-
}
|
|
165
|
-
this.success(result);
|
|
166
|
-
}
|
|
167
|
-
else {
|
|
168
|
-
if (xhr.status >= 400 && xhr.status <= 499) {
|
|
169
|
-
this.fail(result);
|
|
170
|
-
}
|
|
171
|
-
this.error(result);
|
|
172
|
-
console.warn(result);
|
|
173
|
-
}
|
|
174
|
-
this.complete(result);
|
|
175
|
-
};
|
|
176
|
-
xhr.send(this.requestParams.body);
|
|
177
|
-
}
|
|
178
|
-
catch (e) {
|
|
179
|
-
console.warn(e);
|
|
180
|
-
throw e;
|
|
181
|
-
}
|
|
182
|
-
return this;
|
|
183
|
-
}
|
|
184
|
-
addHeader(key, value) {
|
|
185
|
-
this.requestParams.headers = Object.assign(Object.assign({}, this.requestParams.headers), { [key]: value });
|
|
186
|
-
return this;
|
|
187
|
-
}
|
|
188
|
-
deleteHeader(key) {
|
|
189
|
-
if (this.requestParams.headers && (key in this.requestParams.headers)) {
|
|
190
|
-
delete this.requestParams.headers[key];
|
|
191
|
-
}
|
|
192
|
-
return this;
|
|
193
|
-
}
|
|
194
|
-
updateCache(data, fieldKey = 'id') {
|
|
195
|
-
const cache = this.getCacheByIndex(this.cacheIndex);
|
|
196
|
-
if (cache && cache.group) {
|
|
197
|
-
this.updateCacheGroup(cache.group, data, fieldKey);
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
clearCache(data, fieldKey = 'id') {
|
|
201
|
-
if (data) {
|
|
202
|
-
const cache = this.getCacheByIndex(this.cacheIndex);
|
|
203
|
-
if (cache && cache.group) {
|
|
204
|
-
this.clearCacheGroup(cache.group, data, fieldKey);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
else {
|
|
208
|
-
this.deleteCache(this.cacheIndex);
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
exports.default = Http;
|