larapi 0.0.6 → 0.0.8
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/lib/Core.d.ts +11 -12
- package/lib/Core.js +42 -139
- package/lib/Http.d.ts +18 -0
- package/lib/Http.js +164 -0
- package/lib/index.d.ts +9 -14
- package/lib/index.js +15 -4
- package/lib/types.d.ts +6 -1
- package/package.json +1 -1
- package/src/Core.ts +44 -158
- package/src/Http.ts +175 -0
- package/src/index.ts +13 -6
- package/src/types.ts +7 -1
package/lib/Core.d.ts
CHANGED
|
@@ -1,21 +1,20 @@
|
|
|
1
1
|
import { Events, Event } from 'easy-event-emitter';
|
|
2
|
-
import {
|
|
2
|
+
import { TCacheBody, TData, TGroupsData, TListenerEvents } from './types';
|
|
3
3
|
declare abstract class Core {
|
|
4
|
-
private
|
|
5
|
-
token: string;
|
|
6
|
-
cache: TCache;
|
|
4
|
+
private cache;
|
|
7
5
|
initData: TGroupsData;
|
|
8
6
|
protected events: Events;
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
} | undefined;
|
|
7
|
+
host: string;
|
|
8
|
+
token: string;
|
|
9
|
+
constructor(context?: Core);
|
|
13
10
|
addListener(event: TListenerEvents, callback: (data: any) => void): Event;
|
|
14
|
-
|
|
11
|
+
protected getCache(key: string): TData | undefined;
|
|
15
12
|
setHost(host: string): void;
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
13
|
+
protected setCache(key: string, data: TData, group?: string): void;
|
|
14
|
+
clearCacheGroup(group: string): void;
|
|
15
|
+
updateCacheGroup(group: string, fieldKey: string, data: TData): void;
|
|
16
|
+
protected getCacheByIndex(index: string): TCacheBody;
|
|
17
|
+
protected deleteCache(key: string): void;
|
|
19
18
|
setInitData(data: TGroupsData): void;
|
|
20
19
|
}
|
|
21
20
|
export default Core;
|
package/lib/Core.js
CHANGED
|
@@ -3,36 +3,34 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
//@ts-ignore
|
|
7
|
-
const md5_1 = __importDefault(require("md5"));
|
|
8
6
|
const easy_event_emitter_1 = __importDefault(require("easy-event-emitter"));
|
|
9
7
|
class Core {
|
|
10
|
-
constructor() {
|
|
11
|
-
Object.defineProperty(this, "
|
|
8
|
+
constructor(context) {
|
|
9
|
+
Object.defineProperty(this, "cache", {
|
|
12
10
|
enumerable: true,
|
|
13
11
|
configurable: true,
|
|
14
12
|
writable: true,
|
|
15
13
|
value: void 0
|
|
16
14
|
});
|
|
17
|
-
Object.defineProperty(this, "
|
|
15
|
+
Object.defineProperty(this, "initData", {
|
|
18
16
|
enumerable: true,
|
|
19
17
|
configurable: true,
|
|
20
18
|
writable: true,
|
|
21
19
|
value: void 0
|
|
22
20
|
});
|
|
23
|
-
Object.defineProperty(this, "
|
|
21
|
+
Object.defineProperty(this, "events", {
|
|
24
22
|
enumerable: true,
|
|
25
23
|
configurable: true,
|
|
26
24
|
writable: true,
|
|
27
25
|
value: void 0
|
|
28
26
|
});
|
|
29
|
-
Object.defineProperty(this, "
|
|
27
|
+
Object.defineProperty(this, "host", {
|
|
30
28
|
enumerable: true,
|
|
31
29
|
configurable: true,
|
|
32
30
|
writable: true,
|
|
33
31
|
value: void 0
|
|
34
32
|
});
|
|
35
|
-
Object.defineProperty(this, "
|
|
33
|
+
Object.defineProperty(this, "token", {
|
|
36
34
|
enumerable: true,
|
|
37
35
|
configurable: true,
|
|
38
36
|
writable: true,
|
|
@@ -43,135 +41,16 @@ class Core {
|
|
|
43
41
|
this.token = '';
|
|
44
42
|
this.cache = {};
|
|
45
43
|
this.initData = {};
|
|
46
|
-
this.request = this.request.bind(this);
|
|
47
44
|
this.setHost = this.setHost.bind(this);
|
|
48
45
|
this.addListener = this.addListener.bind(this);
|
|
49
46
|
this.setInitData = this.setInitData.bind(this);
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
var _a;
|
|
53
|
-
if (typeof XMLHttpRequest === 'undefined')
|
|
54
|
-
return;
|
|
55
|
-
//Заголовки от 200 до 299
|
|
56
|
-
const success = (...args) => {
|
|
57
|
-
if (typeof params.success === 'function')
|
|
58
|
-
params.success(...args);
|
|
59
|
-
};
|
|
60
|
-
//Заголовки от 400 до 499
|
|
61
|
-
const fail = (...args) => {
|
|
62
|
-
if (typeof params.fail === 'function') {
|
|
63
|
-
params.fail(...args);
|
|
64
|
-
}
|
|
65
|
-
this.events.emit('api-request-fail', args);
|
|
66
|
-
};
|
|
67
|
-
//Все остальные
|
|
68
|
-
const error = (...args) => {
|
|
69
|
-
if (typeof params.error === 'function') {
|
|
70
|
-
params.error(...args);
|
|
71
|
-
}
|
|
72
|
-
this.events.emit('api-request-error', args);
|
|
73
|
-
};
|
|
74
|
-
const complete = (...args) => {
|
|
75
|
-
if (typeof params.complete === 'function') {
|
|
76
|
-
params.complete(...args);
|
|
77
|
-
}
|
|
78
|
-
this.events.emit('api-request-complete', args);
|
|
79
|
-
};
|
|
80
|
-
const encodeUrlParams = (data) => {
|
|
81
|
-
return Object.keys(data).map(function (k) {
|
|
82
|
-
return encodeURIComponent(k) + "=" + encodeURIComponent(data[k]);
|
|
83
|
-
}).join('&');
|
|
84
|
-
};
|
|
85
|
-
const cuteUndifinedParams = (data) => {
|
|
86
|
-
if (typeof data !== 'object') {
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
for (const key in data) {
|
|
90
|
-
if (!data[key]) {
|
|
91
|
-
delete data[key];
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
return data;
|
|
95
|
-
};
|
|
96
|
-
var path = params.path;
|
|
97
|
-
const fetchParams = {
|
|
98
|
-
method: method
|
|
99
|
-
};
|
|
100
|
-
fetchParams.headers = {
|
|
101
|
-
'Accept': 'application/json',
|
|
102
|
-
'Content-Type': 'application/json',
|
|
103
|
-
'Authorization': 'Bearer ' + this.token
|
|
104
|
-
};
|
|
105
|
-
params.data = params.data ? cuteUndifinedParams(params.data) : params.data;
|
|
106
|
-
if (method === 'POST') {
|
|
107
|
-
fetchParams.body = JSON.stringify(params.data);
|
|
108
|
-
}
|
|
109
|
-
if (method === 'GET') {
|
|
110
|
-
if (params.data)
|
|
111
|
-
path = params.path + '?' + encodeUrlParams(params.data);
|
|
112
|
-
}
|
|
113
|
-
const pathCache = (0, md5_1.default)(path + JSON.stringify(params.data));
|
|
114
|
-
var dataCache;
|
|
115
|
-
if (params.globalName) {
|
|
116
|
-
if (this.initData[params.globalName]) {
|
|
117
|
-
this.setCache(pathCache, this.initData[params.globalName], (typeof params.cache === 'boolean' ? undefined : params.cache));
|
|
118
|
-
params.cache = (_a = params.cache) !== null && _a !== void 0 ? _a : true;
|
|
119
|
-
delete this.initData[params.globalName];
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
if (params.cache) {
|
|
123
|
-
if (dataCache = this.getCache(pathCache)) {
|
|
124
|
-
success(dataCache);
|
|
125
|
-
complete(dataCache);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
else {
|
|
129
|
-
this.deleteCache(pathCache);
|
|
130
|
-
}
|
|
131
|
-
if (!dataCache) {
|
|
132
|
-
try {
|
|
133
|
-
const xhr = new XMLHttpRequest();
|
|
134
|
-
xhr.open(method, this.host + path, true);
|
|
135
|
-
for (const header in fetchParams.headers) {
|
|
136
|
-
xhr.setRequestHeader(header, fetchParams.headers[header]);
|
|
137
|
-
}
|
|
138
|
-
xhr.onreadystatechange = () => {
|
|
139
|
-
if (xhr.readyState !== 4) {
|
|
140
|
-
return;
|
|
141
|
-
}
|
|
142
|
-
const result = xhr.responseText ? JSON.parse(xhr.responseText) : {};
|
|
143
|
-
if (xhr.status >= 200 && xhr.status <= 299) {
|
|
144
|
-
if (params.cache) {
|
|
145
|
-
this.setCache(pathCache, result, (typeof params.cache === 'boolean' ? undefined : params.cache));
|
|
146
|
-
}
|
|
147
|
-
success(result);
|
|
148
|
-
}
|
|
149
|
-
else {
|
|
150
|
-
if (xhr.status >= 400 && xhr.status <= 499) {
|
|
151
|
-
fail(result);
|
|
152
|
-
}
|
|
153
|
-
error(result);
|
|
154
|
-
console.warn(result);
|
|
155
|
-
}
|
|
156
|
-
complete(result);
|
|
157
|
-
};
|
|
158
|
-
xhr.send(fetchParams.body);
|
|
159
|
-
}
|
|
160
|
-
catch (e) {
|
|
161
|
-
console.warn(e);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
return {
|
|
165
|
-
clearCache: () => {
|
|
166
|
-
this.deleteCache(pathCache);
|
|
167
|
-
}
|
|
168
|
-
};
|
|
47
|
+
this.clearCacheGroup = this.clearCacheGroup.bind(this);
|
|
48
|
+
Object.assign(this, context);
|
|
169
49
|
}
|
|
170
50
|
addListener(event, callback) {
|
|
171
51
|
return this.events.addListener(event, callback);
|
|
172
52
|
}
|
|
173
|
-
getCache(
|
|
174
|
-
const key = (0, md5_1.default)(request);
|
|
53
|
+
getCache(key) {
|
|
175
54
|
if (this.cache[key]) {
|
|
176
55
|
if ((new Date()).getTime() <= (this.cache[key].time + 3600000000)) {
|
|
177
56
|
return this.cache[key].data;
|
|
@@ -184,29 +63,53 @@ class Core {
|
|
|
184
63
|
setHost(host) {
|
|
185
64
|
this.host = host;
|
|
186
65
|
}
|
|
187
|
-
setCache(
|
|
188
|
-
this.cache[
|
|
66
|
+
setCache(key, data, group) {
|
|
67
|
+
this.cache[key] = {
|
|
189
68
|
time: (new Date()).getTime(),
|
|
190
69
|
data: data,
|
|
191
70
|
group: group
|
|
192
71
|
};
|
|
193
72
|
}
|
|
194
|
-
|
|
73
|
+
clearCacheGroup(group) {
|
|
195
74
|
for (const key in this.cache) {
|
|
196
75
|
if (this.cache[key].group === group) {
|
|
197
76
|
delete this.cache[key];
|
|
198
77
|
}
|
|
199
78
|
}
|
|
200
79
|
}
|
|
201
|
-
|
|
202
|
-
const
|
|
203
|
-
|
|
80
|
+
updateCacheGroup(group, fieldKey, data) {
|
|
81
|
+
const update = (cacheData) => {
|
|
82
|
+
if (cacheData[fieldKey] === data[fieldKey]) {
|
|
83
|
+
for (const fieldKey in cacheData) {
|
|
84
|
+
if (fieldKey in data) {
|
|
85
|
+
cacheData[fieldKey] = data[fieldKey];
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return cacheData;
|
|
90
|
+
};
|
|
91
|
+
for (const key in this.cache) {
|
|
92
|
+
if (this.cache[key].group === group) {
|
|
93
|
+
if (Array.isArray(this.cache[key].data)) {
|
|
94
|
+
this.cache[key].data = Object.values(this.cache[key].data).map(update);
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
this.cache[key].data = update(this.cache[key].data);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
getCacheByIndex(index) {
|
|
103
|
+
return this.cache[index];
|
|
104
|
+
}
|
|
105
|
+
deleteCache(key) {
|
|
106
|
+
if (!this.cache[key])
|
|
204
107
|
return;
|
|
205
|
-
const group = this.cache[
|
|
108
|
+
const group = this.cache[key].group;
|
|
206
109
|
if (group) {
|
|
207
|
-
this.
|
|
110
|
+
this.clearCacheGroup(group);
|
|
208
111
|
}
|
|
209
|
-
delete this.cache[
|
|
112
|
+
delete this.cache[key];
|
|
210
113
|
}
|
|
211
114
|
setInitData(data) {
|
|
212
115
|
this.initData = data;
|
package/lib/Http.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import Core from "./Core";
|
|
2
|
+
import { TMethod, TParams, TData } from "./types";
|
|
3
|
+
declare class Http extends Core {
|
|
4
|
+
private cacheIndex;
|
|
5
|
+
method: TMethod;
|
|
6
|
+
params: TParams;
|
|
7
|
+
constructor(method: TMethod, params: TParams, context?: Core);
|
|
8
|
+
private success;
|
|
9
|
+
private fail;
|
|
10
|
+
private error;
|
|
11
|
+
private complete;
|
|
12
|
+
private encodeUrlParams;
|
|
13
|
+
private cuteUndifinedParams;
|
|
14
|
+
request(): this;
|
|
15
|
+
updateCache(fieldKey: string, data: TData): void;
|
|
16
|
+
clearCache(): void;
|
|
17
|
+
}
|
|
18
|
+
export default Http;
|
package/lib/Http.js
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
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, "method", {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
configurable: true,
|
|
21
|
+
writable: true,
|
|
22
|
+
value: void 0
|
|
23
|
+
});
|
|
24
|
+
Object.defineProperty(this, "params", {
|
|
25
|
+
enumerable: true,
|
|
26
|
+
configurable: true,
|
|
27
|
+
writable: true,
|
|
28
|
+
value: void 0
|
|
29
|
+
});
|
|
30
|
+
this.cacheIndex = '';
|
|
31
|
+
this.method = method;
|
|
32
|
+
this.params = params;
|
|
33
|
+
this.success = this.success.bind(this);
|
|
34
|
+
this.fail = this.fail.bind(this);
|
|
35
|
+
this.error = this.error.bind(this);
|
|
36
|
+
this.complete = this.complete.bind(this);
|
|
37
|
+
this.request = this.request.bind(this);
|
|
38
|
+
}
|
|
39
|
+
//Заголовки от 200 до 299
|
|
40
|
+
success(...args) {
|
|
41
|
+
if (typeof this.params.success === 'function')
|
|
42
|
+
this.params.success(...args);
|
|
43
|
+
}
|
|
44
|
+
//Заголовки от 400 до 499
|
|
45
|
+
fail(...args) {
|
|
46
|
+
if (typeof this.params.fail === 'function') {
|
|
47
|
+
this.params.fail(...args);
|
|
48
|
+
}
|
|
49
|
+
this.events.emit('api-request-fail', args);
|
|
50
|
+
}
|
|
51
|
+
//Все остальные
|
|
52
|
+
error(...args) {
|
|
53
|
+
if (typeof this.params.error === 'function') {
|
|
54
|
+
this.params.error(...args);
|
|
55
|
+
}
|
|
56
|
+
this.events.emit('api-request-error', args);
|
|
57
|
+
}
|
|
58
|
+
complete(...args) {
|
|
59
|
+
if (typeof this.params.complete === 'function') {
|
|
60
|
+
this.params.complete(...args);
|
|
61
|
+
}
|
|
62
|
+
this.events.emit('api-request-complete', args);
|
|
63
|
+
}
|
|
64
|
+
encodeUrlParams(data) {
|
|
65
|
+
return Object.keys(data).map(function (k) {
|
|
66
|
+
return encodeURIComponent(k) + "=" + encodeURIComponent(data[k]);
|
|
67
|
+
}).join('&');
|
|
68
|
+
}
|
|
69
|
+
cuteUndifinedParams(data) {
|
|
70
|
+
if (typeof data !== 'object') {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
for (const key in data) {
|
|
74
|
+
if (!data[key]) {
|
|
75
|
+
delete data[key];
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return data;
|
|
79
|
+
}
|
|
80
|
+
request() {
|
|
81
|
+
var _a;
|
|
82
|
+
if (typeof XMLHttpRequest === 'undefined')
|
|
83
|
+
return this;
|
|
84
|
+
var path = this.params.path;
|
|
85
|
+
const fetchParams = {
|
|
86
|
+
method: this.method
|
|
87
|
+
};
|
|
88
|
+
fetchParams.headers = {
|
|
89
|
+
'Accept': 'application/json',
|
|
90
|
+
'Content-Type': 'application/json',
|
|
91
|
+
'Authorization': 'Bearer ' + this.token
|
|
92
|
+
};
|
|
93
|
+
this.params.data = this.params.data ? this.cuteUndifinedParams(this.params.data) : this.params.data;
|
|
94
|
+
if (this.method === 'POST') {
|
|
95
|
+
fetchParams.body = JSON.stringify(this.params.data);
|
|
96
|
+
}
|
|
97
|
+
if (this.method === 'GET') {
|
|
98
|
+
if (this.params.data)
|
|
99
|
+
path = this.params.path + '?' + this.encodeUrlParams(this.params.data);
|
|
100
|
+
}
|
|
101
|
+
this.cacheIndex = (0, md5_1.default)(path + JSON.stringify(this.params.data));
|
|
102
|
+
var dataCache;
|
|
103
|
+
if (this.params.globalName) {
|
|
104
|
+
if (this.initData[this.params.globalName]) {
|
|
105
|
+
this.setCache(this.cacheIndex, this.initData[this.params.globalName], (typeof this.params.cache === 'boolean' ? undefined : this.params.cache));
|
|
106
|
+
this.params.cache = (_a = this.params.cache) !== null && _a !== void 0 ? _a : true;
|
|
107
|
+
delete this.initData[this.params.globalName];
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
if (this.params.cache) {
|
|
111
|
+
if (dataCache = this.getCache(this.cacheIndex)) {
|
|
112
|
+
this.success(dataCache);
|
|
113
|
+
this.complete(dataCache);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
this.deleteCache(this.cacheIndex);
|
|
118
|
+
}
|
|
119
|
+
if (!dataCache) {
|
|
120
|
+
try {
|
|
121
|
+
const xhr = new XMLHttpRequest();
|
|
122
|
+
xhr.open(this.method, this.host + path, true);
|
|
123
|
+
for (const header in fetchParams.headers) {
|
|
124
|
+
xhr.setRequestHeader(header, fetchParams.headers[header]);
|
|
125
|
+
}
|
|
126
|
+
xhr.onreadystatechange = () => {
|
|
127
|
+
if (xhr.readyState !== 4) {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
const result = xhr.responseText ? JSON.parse(xhr.responseText) : {};
|
|
131
|
+
if (xhr.status >= 200 && xhr.status <= 299) {
|
|
132
|
+
if (this.params.cache) {
|
|
133
|
+
this.setCache(this.cacheIndex, result, (typeof this.params.cache === 'boolean' ? undefined : this.params.cache));
|
|
134
|
+
}
|
|
135
|
+
this.success(result);
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
if (xhr.status >= 400 && xhr.status <= 499) {
|
|
139
|
+
fail(result);
|
|
140
|
+
}
|
|
141
|
+
this.error(result);
|
|
142
|
+
console.warn(result);
|
|
143
|
+
}
|
|
144
|
+
this.complete(result);
|
|
145
|
+
};
|
|
146
|
+
xhr.send(fetchParams.body);
|
|
147
|
+
}
|
|
148
|
+
catch (e) {
|
|
149
|
+
console.warn(e);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return this;
|
|
153
|
+
}
|
|
154
|
+
updateCache(fieldKey, data) {
|
|
155
|
+
const cache = this.getCacheByIndex(this.cacheIndex);
|
|
156
|
+
if (cache && cache.group) {
|
|
157
|
+
this.updateCacheGroup(cache.group, fieldKey, data);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
clearCache() {
|
|
161
|
+
this.deleteCache(this.cacheIndex);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
exports.default = Http;
|
package/lib/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import Core from "./Core";
|
|
2
|
-
import { TParams } from "./types";
|
|
2
|
+
import { TMethod, TParams } from "./types";
|
|
3
|
+
import Http from "./Http";
|
|
3
4
|
type TUser = {
|
|
4
5
|
[index: string]: any;
|
|
5
6
|
};
|
|
@@ -10,12 +11,9 @@ declare class API extends Core {
|
|
|
10
11
|
private static instance;
|
|
11
12
|
private _user;
|
|
12
13
|
constructor(config: TConfig);
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
post(params: TParams): {
|
|
17
|
-
clearCache: () => void;
|
|
18
|
-
} | undefined;
|
|
14
|
+
http(method: TMethod, params: TParams): Http;
|
|
15
|
+
get(params: TParams): Http;
|
|
16
|
+
post(params: TParams): Http;
|
|
19
17
|
private init;
|
|
20
18
|
setToken(token: string): void;
|
|
21
19
|
getToken(): string;
|
|
@@ -26,12 +24,9 @@ declare class API extends Core {
|
|
|
26
24
|
setUser(user: TUser): void;
|
|
27
25
|
updateUser(user: TUser): void;
|
|
28
26
|
logout(): void;
|
|
29
|
-
static
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
static post: (params: TParams) => {
|
|
33
|
-
clearCache: () => void;
|
|
34
|
-
} | undefined;
|
|
27
|
+
static http: (method: TMethod, params: TParams) => Http;
|
|
28
|
+
static get: (params: TParams) => Http;
|
|
29
|
+
static post: (params: TParams) => Http;
|
|
35
30
|
static setHost: (host: string) => void;
|
|
36
31
|
static setUser: (user: TUser) => void;
|
|
37
32
|
static updateUser: (user: TUser) => void;
|
|
@@ -39,7 +34,7 @@ declare class API extends Core {
|
|
|
39
34
|
static setToken: (token: string) => void;
|
|
40
35
|
static addListener: (event: import("./types").TListenerEvents, callback: (data: any) => void) => import("easy-event-emitter").Event;
|
|
41
36
|
static setInitData: (data: import("./types").TGroupsData) => void;
|
|
42
|
-
static
|
|
37
|
+
static clearCacheGroup: (group: string) => void;
|
|
43
38
|
static getToken: () => string;
|
|
44
39
|
static getUid: () => number;
|
|
45
40
|
static getUser: () => TUser;
|
package/lib/index.js
CHANGED
|
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
var _a;
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
7
|
const Core_1 = __importDefault(require("./Core"));
|
|
8
|
+
const Http_1 = __importDefault(require("./Http"));
|
|
8
9
|
class API extends Core_1.default {
|
|
9
10
|
constructor(config) {
|
|
10
11
|
super();
|
|
@@ -22,16 +23,20 @@ class API extends Core_1.default {
|
|
|
22
23
|
this.updateUser = this.updateUser.bind(this);
|
|
23
24
|
this.logout = this.logout.bind(this);
|
|
24
25
|
this.getToken = this.getToken.bind(this);
|
|
26
|
+
this.http = this.http.bind(this);
|
|
25
27
|
this.get = this.get.bind(this);
|
|
26
28
|
this.post = this.post.bind(this);
|
|
27
29
|
this.getUid = this.getUid.bind(this);
|
|
28
30
|
this.getUser = this.getUser.bind(this);
|
|
29
31
|
}
|
|
32
|
+
http(method, params) {
|
|
33
|
+
return new Http_1.default(method, params, this);
|
|
34
|
+
}
|
|
30
35
|
get(params) {
|
|
31
|
-
return this.
|
|
36
|
+
return this.http('GET', params).request();
|
|
32
37
|
}
|
|
33
38
|
post(params) {
|
|
34
|
-
return this.
|
|
39
|
+
return this.http('POST', params).request();
|
|
35
40
|
}
|
|
36
41
|
init() {
|
|
37
42
|
if (typeof window === 'undefined')
|
|
@@ -97,6 +102,12 @@ Object.defineProperty(API, "instance", {
|
|
|
97
102
|
host: ''
|
|
98
103
|
})
|
|
99
104
|
});
|
|
105
|
+
Object.defineProperty(API, "http", {
|
|
106
|
+
enumerable: true,
|
|
107
|
+
configurable: true,
|
|
108
|
+
writable: true,
|
|
109
|
+
value: _a.instance.http
|
|
110
|
+
});
|
|
100
111
|
Object.defineProperty(API, "get", {
|
|
101
112
|
enumerable: true,
|
|
102
113
|
configurable: true,
|
|
@@ -151,11 +162,11 @@ Object.defineProperty(API, "setInitData", {
|
|
|
151
162
|
writable: true,
|
|
152
163
|
value: _a.instance.setInitData
|
|
153
164
|
});
|
|
154
|
-
Object.defineProperty(API, "
|
|
165
|
+
Object.defineProperty(API, "clearCacheGroup", {
|
|
155
166
|
enumerable: true,
|
|
156
167
|
configurable: true,
|
|
157
168
|
writable: true,
|
|
158
|
-
value: _a.instance.
|
|
169
|
+
value: _a.instance.clearCacheGroup
|
|
159
170
|
});
|
|
160
171
|
Object.defineProperty(API, "getToken", {
|
|
161
172
|
enumerable: true,
|
package/lib/types.d.ts
CHANGED
package/package.json
CHANGED
package/src/Core.ts
CHANGED
|
@@ -1,182 +1,41 @@
|
|
|
1
|
-
//@ts-ignore
|
|
2
|
-
import md5 from 'md5';
|
|
3
1
|
import EventEmitter,{
|
|
4
2
|
Events,
|
|
5
3
|
Event
|
|
6
4
|
} from 'easy-event-emitter';
|
|
7
5
|
import {
|
|
8
6
|
TCache,
|
|
7
|
+
TCacheBody,
|
|
9
8
|
TData,
|
|
10
9
|
TGroupsData,
|
|
11
|
-
TMethod,
|
|
12
|
-
TParams,
|
|
13
|
-
TRequestParams,
|
|
14
10
|
TListenerEvents
|
|
15
11
|
} from './types';
|
|
16
12
|
|
|
17
13
|
abstract class Core {
|
|
18
|
-
private
|
|
19
|
-
token: string;
|
|
20
|
-
cache: TCache;
|
|
14
|
+
private cache: TCache;
|
|
21
15
|
initData: TGroupsData;
|
|
22
16
|
protected events: Events;
|
|
17
|
+
host: string;
|
|
18
|
+
token: string;
|
|
23
19
|
|
|
24
|
-
constructor() {
|
|
20
|
+
constructor(context?: Core) {
|
|
25
21
|
this.events = new EventEmitter();
|
|
26
22
|
this.host = 'http://127.0.0.1/';
|
|
27
23
|
this.token = '';
|
|
28
24
|
this.cache = {};
|
|
29
25
|
this.initData = {};
|
|
30
26
|
|
|
31
|
-
this.request = this.request.bind(this);
|
|
32
27
|
this.setHost = this.setHost.bind(this);
|
|
33
28
|
this.addListener = this.addListener.bind(this);
|
|
34
29
|
this.setInitData = this.setInitData.bind(this);
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
protected request(method: TMethod, params: TParams) {
|
|
38
|
-
if (typeof XMLHttpRequest === 'undefined') return;
|
|
39
|
-
|
|
40
|
-
//Заголовки от 200 до 299
|
|
41
|
-
const success = (...args: any) => {
|
|
42
|
-
if (typeof params.success === 'function')
|
|
43
|
-
params.success(...args);
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
//Заголовки от 400 до 499
|
|
47
|
-
const fail = (...args: any) => {
|
|
48
|
-
if (typeof params.fail === 'function') {
|
|
49
|
-
params.fail(...args);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
this.events.emit('api-request-fail', args);
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
//Все остальные
|
|
56
|
-
const error = (...args: any) => {
|
|
57
|
-
if (typeof params.error === 'function') {
|
|
58
|
-
params.error(...args);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
this.events.emit('api-request-error', args);
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
const complete = (...args: any) => {
|
|
65
|
-
if (typeof params.complete === 'function') {
|
|
66
|
-
params.complete(...args);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
this.events.emit('api-request-complete', args);
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
const encodeUrlParams = (data: TData) => {
|
|
73
|
-
return Object.keys(data).map(function(k) {
|
|
74
|
-
return encodeURIComponent(k) + "=" + encodeURIComponent(data[k]);
|
|
75
|
-
}).join('&');
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
const cuteUndifinedParams = (data: TData) => {
|
|
79
|
-
if (typeof data !== 'object') {
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
82
|
-
for(const key in data) {
|
|
83
|
-
if (!data[key]) {
|
|
84
|
-
delete data[key];
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
return data;
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
var path = params.path;
|
|
91
|
-
const fetchParams: TRequestParams = {
|
|
92
|
-
method: method
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
fetchParams.headers = {
|
|
96
|
-
'Accept': 'application/json',
|
|
97
|
-
'Content-Type': 'application/json',
|
|
98
|
-
'Authorization': 'Bearer ' + this.token
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
params.data = params.data ? cuteUndifinedParams(params.data) : params.data;
|
|
102
|
-
|
|
103
|
-
if (method === 'POST') {
|
|
104
|
-
fetchParams.body = JSON.stringify(params.data);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
if (method === 'GET') {
|
|
108
|
-
if (params.data)
|
|
109
|
-
path = params.path + '?' + encodeUrlParams(params.data);
|
|
110
|
-
}
|
|
111
|
-
const pathCache = md5(path + JSON.stringify(params.data));
|
|
112
|
-
var dataCache;
|
|
113
|
-
|
|
114
|
-
if (params.globalName) {
|
|
115
|
-
if (this.initData[params.globalName]) {
|
|
116
|
-
this.setCache(pathCache, this.initData[params.globalName], (typeof params.cache === 'boolean' ? undefined : params.cache));
|
|
117
|
-
params.cache = params.cache ?? true;
|
|
118
|
-
delete this.initData[params.globalName];
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
if (params.cache) {
|
|
123
|
-
if (dataCache = this.getCache(pathCache)) {
|
|
124
|
-
success(dataCache);
|
|
125
|
-
complete(dataCache);
|
|
126
|
-
}
|
|
127
|
-
}else{
|
|
128
|
-
this.deleteCache(pathCache);
|
|
129
|
-
}
|
|
130
|
-
if (!dataCache) {
|
|
131
|
-
try {
|
|
132
|
-
const xhr = new XMLHttpRequest();
|
|
133
|
-
|
|
134
|
-
xhr.open(method, this.host + path, true);
|
|
135
|
-
for(const header in fetchParams.headers) {
|
|
136
|
-
xhr.setRequestHeader(header, fetchParams.headers[header]);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
xhr.onreadystatechange = () => {
|
|
140
|
-
if (xhr.readyState !== 4) {
|
|
141
|
-
return;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
const result = xhr.responseText ? JSON.parse(xhr.responseText) : {};
|
|
145
|
-
|
|
146
|
-
if (xhr.status >= 200 && xhr.status <= 299) {
|
|
147
|
-
if (params.cache) {
|
|
148
|
-
this.setCache(pathCache, result, (typeof params.cache === 'boolean' ? undefined : params.cache));
|
|
149
|
-
}
|
|
150
|
-
success(result);
|
|
151
|
-
}else{
|
|
152
|
-
if (xhr.status >= 400 && xhr.status <= 499) {
|
|
153
|
-
fail(result);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
error(result);
|
|
157
|
-
console.warn(result);
|
|
158
|
-
}
|
|
159
|
-
complete(result);
|
|
160
|
-
}
|
|
161
|
-
xhr.send(fetchParams.body);
|
|
162
|
-
}catch(e) {
|
|
163
|
-
console.warn(e);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
}
|
|
167
|
-
return {
|
|
168
|
-
clearCache: () => {
|
|
169
|
-
this.deleteCache(pathCache);
|
|
170
|
-
}
|
|
171
|
-
}
|
|
30
|
+
this.clearCacheGroup = this.clearCacheGroup.bind(this);
|
|
31
|
+
Object.assign(this, context);
|
|
172
32
|
}
|
|
173
33
|
|
|
174
34
|
public addListener(event: TListenerEvents, callback: (data: any) => void): Event {
|
|
175
35
|
return this.events.addListener(event, callback);
|
|
176
36
|
}
|
|
177
37
|
|
|
178
|
-
|
|
179
|
-
const key = md5(request);
|
|
38
|
+
protected getCache(key: string): TData | undefined {
|
|
180
39
|
if (this.cache[key]) {
|
|
181
40
|
if ((new Date()).getTime() <= (this.cache[key].time + 3600000000)) {
|
|
182
41
|
return this.cache[key].data;
|
|
@@ -190,15 +49,15 @@ abstract class Core {
|
|
|
190
49
|
this.host = host;
|
|
191
50
|
}
|
|
192
51
|
|
|
193
|
-
|
|
194
|
-
this.cache[
|
|
52
|
+
protected setCache(key: string, data: TData, group?: string): void {
|
|
53
|
+
this.cache[key] = {
|
|
195
54
|
time: (new Date()).getTime(),
|
|
196
55
|
data: data,
|
|
197
56
|
group: group
|
|
198
57
|
};
|
|
199
58
|
}
|
|
200
59
|
|
|
201
|
-
public
|
|
60
|
+
public clearCacheGroup(group: string): void {
|
|
202
61
|
for(const key in this.cache) {
|
|
203
62
|
if (this.cache[key].group === group) {
|
|
204
63
|
delete this.cache[key];
|
|
@@ -206,16 +65,43 @@ abstract class Core {
|
|
|
206
65
|
}
|
|
207
66
|
}
|
|
208
67
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
68
|
+
public updateCacheGroup(group: string, fieldKey: string, data: TData): void {
|
|
69
|
+
|
|
70
|
+
const update = (cacheData: any) => {
|
|
71
|
+
if (cacheData[fieldKey] === data[fieldKey]) {
|
|
72
|
+
for(const fieldKey in cacheData) {
|
|
73
|
+
if (fieldKey in data) {
|
|
74
|
+
cacheData[fieldKey] = data[fieldKey];
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return cacheData;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
for(const key in this.cache) {
|
|
82
|
+
if (this.cache[key].group === group) {
|
|
83
|
+
if (Array.isArray(this.cache[key].data)) {
|
|
84
|
+
this.cache[key].data = Object.values(this.cache[key].data).map(update);
|
|
85
|
+
}else{
|
|
86
|
+
this.cache[key].data = update(this.cache[key].data);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
protected getCacheByIndex(index: string): TCacheBody {
|
|
93
|
+
return this.cache[index];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
protected deleteCache(key: string): void {
|
|
97
|
+
if (!this.cache[key])
|
|
212
98
|
return;
|
|
213
99
|
|
|
214
|
-
const group = this.cache[
|
|
100
|
+
const group = this.cache[key].group;
|
|
215
101
|
if (group) {
|
|
216
|
-
this.
|
|
102
|
+
this.clearCacheGroup(group);
|
|
217
103
|
}
|
|
218
|
-
delete this.cache[
|
|
104
|
+
delete this.cache[key];
|
|
219
105
|
}
|
|
220
106
|
|
|
221
107
|
public setInitData(data: TGroupsData): void {
|
package/src/Http.ts
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
//@ts-ignore
|
|
2
|
+
import md5 from 'md5';
|
|
3
|
+
import Core from "./Core";
|
|
4
|
+
import {
|
|
5
|
+
TMethod,
|
|
6
|
+
TParams,
|
|
7
|
+
TData,
|
|
8
|
+
TRequestParams
|
|
9
|
+
} from "./types";
|
|
10
|
+
|
|
11
|
+
class Http extends Core {
|
|
12
|
+
private cacheIndex: string;
|
|
13
|
+
method: TMethod;
|
|
14
|
+
params: TParams;
|
|
15
|
+
|
|
16
|
+
constructor(method: TMethod, params: TParams, context?: Core) {
|
|
17
|
+
super(context);
|
|
18
|
+
this.cacheIndex = '';
|
|
19
|
+
this.method = method;
|
|
20
|
+
this.params = params;
|
|
21
|
+
|
|
22
|
+
this.success = this.success.bind(this);
|
|
23
|
+
this.fail = this.fail.bind(this);
|
|
24
|
+
this.error = this.error.bind(this);
|
|
25
|
+
this.complete = this.complete.bind(this);
|
|
26
|
+
this.request = this.request.bind(this);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
//Заголовки от 200 до 299
|
|
30
|
+
private success(...args: any) {
|
|
31
|
+
if (typeof this.params.success === 'function')
|
|
32
|
+
this.params.success(...args);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
//Заголовки от 400 до 499
|
|
36
|
+
private fail(...args: any) {
|
|
37
|
+
if (typeof this.params.fail === 'function') {
|
|
38
|
+
this.params.fail(...args);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
this.events.emit('api-request-fail', args);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
//Все остальные
|
|
45
|
+
private error(...args: any) {
|
|
46
|
+
if (typeof this.params.error === 'function') {
|
|
47
|
+
this.params.error(...args);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
this.events.emit('api-request-error', args);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
private complete(...args: any) {
|
|
54
|
+
if (typeof this.params.complete === 'function') {
|
|
55
|
+
this.params.complete(...args);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
this.events.emit('api-request-complete', args);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
private encodeUrlParams(data: TData) {
|
|
62
|
+
return Object.keys(data).map(function(k) {
|
|
63
|
+
return encodeURIComponent(k) + "=" + encodeURIComponent(data[k]);
|
|
64
|
+
}).join('&');
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
private cuteUndifinedParams(data: TData) {
|
|
68
|
+
if (typeof data !== 'object') {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
for(const key in data) {
|
|
72
|
+
if (!data[key]) {
|
|
73
|
+
delete data[key];
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return data;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
public request(): this {
|
|
80
|
+
if (typeof XMLHttpRequest === 'undefined') return this;
|
|
81
|
+
|
|
82
|
+
var path = this.params.path;
|
|
83
|
+
const fetchParams: TRequestParams = {
|
|
84
|
+
method: this.method
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
fetchParams.headers = {
|
|
88
|
+
'Accept': 'application/json',
|
|
89
|
+
'Content-Type': 'application/json',
|
|
90
|
+
'Authorization': 'Bearer ' + this.token
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
this.params.data = this.params.data ? this.cuteUndifinedParams(this.params.data) : this.params.data;
|
|
94
|
+
|
|
95
|
+
if (this.method === 'POST') {
|
|
96
|
+
fetchParams.body = JSON.stringify(this.params.data);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (this.method === 'GET') {
|
|
100
|
+
if (this.params.data)
|
|
101
|
+
path = this.params.path + '?' + this.encodeUrlParams(this.params.data);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
this.cacheIndex = md5(path + JSON.stringify(this.params.data));
|
|
105
|
+
|
|
106
|
+
var dataCache;
|
|
107
|
+
|
|
108
|
+
if (this.params.globalName) {
|
|
109
|
+
if (this.initData[this.params.globalName]) {
|
|
110
|
+
this.setCache(this.cacheIndex, this.initData[this.params.globalName], (typeof this.params.cache === 'boolean' ? undefined : this.params.cache));
|
|
111
|
+
this.params.cache = this.params.cache ?? true;
|
|
112
|
+
delete this.initData[this.params.globalName];
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (this.params.cache) {
|
|
117
|
+
if (dataCache = this.getCache(this.cacheIndex)) {
|
|
118
|
+
this.success(dataCache);
|
|
119
|
+
this.complete(dataCache);
|
|
120
|
+
}
|
|
121
|
+
}else{
|
|
122
|
+
this.deleteCache(this.cacheIndex);
|
|
123
|
+
}
|
|
124
|
+
if (!dataCache) {
|
|
125
|
+
try {
|
|
126
|
+
const xhr = new XMLHttpRequest();
|
|
127
|
+
|
|
128
|
+
xhr.open(this.method, this.host + path, true);
|
|
129
|
+
for(const header in fetchParams.headers) {
|
|
130
|
+
xhr.setRequestHeader(header, fetchParams.headers[header]);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
xhr.onreadystatechange = () => {
|
|
134
|
+
if (xhr.readyState !== 4) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const result = xhr.responseText ? JSON.parse(xhr.responseText) : {};
|
|
139
|
+
|
|
140
|
+
if (xhr.status >= 200 && xhr.status <= 299) {
|
|
141
|
+
if (this.params.cache) {
|
|
142
|
+
this.setCache(this.cacheIndex, result, (typeof this.params.cache === 'boolean' ? undefined : this.params.cache));
|
|
143
|
+
}
|
|
144
|
+
this.success(result);
|
|
145
|
+
}else{
|
|
146
|
+
if (xhr.status >= 400 && xhr.status <= 499) {
|
|
147
|
+
fail(result);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
this.error(result);
|
|
151
|
+
console.warn(result);
|
|
152
|
+
}
|
|
153
|
+
this.complete(result);
|
|
154
|
+
}
|
|
155
|
+
xhr.send(fetchParams.body);
|
|
156
|
+
}catch(e) {
|
|
157
|
+
console.warn(e);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return this
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
public updateCache(fieldKey: string, data: TData): void {
|
|
164
|
+
const cache = this.getCacheByIndex(this.cacheIndex);
|
|
165
|
+
if (cache && cache.group) {
|
|
166
|
+
this.updateCacheGroup(cache.group, fieldKey, data);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
public clearCache(): void {
|
|
171
|
+
this.deleteCache(this.cacheIndex);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export default Http;
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import Core from "./Core";
|
|
2
|
-
import { TParams } from "./types";
|
|
2
|
+
import { TMethod, TParams } from "./types";
|
|
3
|
+
import Http from "./Http";
|
|
3
4
|
|
|
4
5
|
type TUser = {
|
|
5
6
|
[index: string]: any;
|
|
@@ -26,18 +27,23 @@ class API extends Core {
|
|
|
26
27
|
this.updateUser = this.updateUser.bind(this);
|
|
27
28
|
this.logout = this.logout.bind(this);
|
|
28
29
|
this.getToken = this.getToken.bind(this);
|
|
30
|
+
this.http = this.http.bind(this);
|
|
29
31
|
this.get = this.get.bind(this);
|
|
30
32
|
this.post = this.post.bind(this);
|
|
31
33
|
this.getUid = this.getUid.bind(this);
|
|
32
34
|
this.getUser = this.getUser.bind(this);
|
|
33
35
|
}
|
|
34
36
|
|
|
35
|
-
public
|
|
36
|
-
return
|
|
37
|
+
public http(method: TMethod, params: TParams): Http {
|
|
38
|
+
return new Http(method, params, this);
|
|
37
39
|
}
|
|
38
40
|
|
|
39
|
-
public
|
|
40
|
-
return this.
|
|
41
|
+
public get(params: TParams): Http {
|
|
42
|
+
return this.http('GET', params).request();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
public post(params: TParams): Http {
|
|
46
|
+
return this.http('POST', params).request();
|
|
41
47
|
}
|
|
42
48
|
|
|
43
49
|
private init(): void {
|
|
@@ -100,6 +106,7 @@ class API extends Core {
|
|
|
100
106
|
this.setToken('');
|
|
101
107
|
}
|
|
102
108
|
|
|
109
|
+
public static http = this.instance.http;
|
|
103
110
|
public static get = this.instance.get;
|
|
104
111
|
public static post = this.instance.post;
|
|
105
112
|
public static setHost = this.instance.setHost;
|
|
@@ -109,7 +116,7 @@ class API extends Core {
|
|
|
109
116
|
public static setToken = this.instance.setToken;
|
|
110
117
|
public static addListener = this.instance.addListener;
|
|
111
118
|
public static setInitData = this.instance.setInitData;
|
|
112
|
-
public static
|
|
119
|
+
public static clearCacheGroup = this.instance.clearCacheGroup;
|
|
113
120
|
public static getToken = this.instance.getToken;
|
|
114
121
|
public static getUid = this.instance.getUid;
|
|
115
122
|
public static getUser = this.instance.getUser;
|