larapi 0.0.6 → 0.0.7
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 +8 -11
- package/lib/Core.js +8 -126
- package/lib/Http.d.ts +17 -0
- package/lib/Http.js +158 -0
- package/lib/index.d.ts +8 -13
- package/lib/index.js +13 -2
- package/package.json +1 -1
- package/src/Core.ts +10 -148
- package/src/Http.ts +168 -0
- package/src/index.ts +12 -5
package/lib/Core.d.ts
CHANGED
|
@@ -1,21 +1,18 @@
|
|
|
1
1
|
import { Events, Event } from 'easy-event-emitter';
|
|
2
|
-
import {
|
|
2
|
+
import { 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(request: string): TData | undefined;
|
|
15
12
|
setHost(host: string): void;
|
|
16
|
-
|
|
13
|
+
protected setCache(request: string, data: TData, group?: string): void;
|
|
17
14
|
deleteCacheGroup(group: string): void;
|
|
18
|
-
|
|
15
|
+
protected deleteCache(request: string): void;
|
|
19
16
|
setInitData(data: TGroupsData): void;
|
|
20
17
|
}
|
|
21
18
|
export default Core;
|
package/lib/Core.js
CHANGED
|
@@ -7,32 +7,32 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
7
7
|
const md5_1 = __importDefault(require("md5"));
|
|
8
8
|
const easy_event_emitter_1 = __importDefault(require("easy-event-emitter"));
|
|
9
9
|
class Core {
|
|
10
|
-
constructor() {
|
|
11
|
-
Object.defineProperty(this, "
|
|
10
|
+
constructor(context) {
|
|
11
|
+
Object.defineProperty(this, "cache", {
|
|
12
12
|
enumerable: true,
|
|
13
13
|
configurable: true,
|
|
14
14
|
writable: true,
|
|
15
15
|
value: void 0
|
|
16
16
|
});
|
|
17
|
-
Object.defineProperty(this, "
|
|
17
|
+
Object.defineProperty(this, "initData", {
|
|
18
18
|
enumerable: true,
|
|
19
19
|
configurable: true,
|
|
20
20
|
writable: true,
|
|
21
21
|
value: void 0
|
|
22
22
|
});
|
|
23
|
-
Object.defineProperty(this, "
|
|
23
|
+
Object.defineProperty(this, "events", {
|
|
24
24
|
enumerable: true,
|
|
25
25
|
configurable: true,
|
|
26
26
|
writable: true,
|
|
27
27
|
value: void 0
|
|
28
28
|
});
|
|
29
|
-
Object.defineProperty(this, "
|
|
29
|
+
Object.defineProperty(this, "host", {
|
|
30
30
|
enumerable: true,
|
|
31
31
|
configurable: true,
|
|
32
32
|
writable: true,
|
|
33
33
|
value: void 0
|
|
34
34
|
});
|
|
35
|
-
Object.defineProperty(this, "
|
|
35
|
+
Object.defineProperty(this, "token", {
|
|
36
36
|
enumerable: true,
|
|
37
37
|
configurable: true,
|
|
38
38
|
writable: true,
|
|
@@ -43,129 +43,11 @@ class Core {
|
|
|
43
43
|
this.token = '';
|
|
44
44
|
this.cache = {};
|
|
45
45
|
this.initData = {};
|
|
46
|
-
this.request = this.request.bind(this);
|
|
47
46
|
this.setHost = this.setHost.bind(this);
|
|
48
47
|
this.addListener = this.addListener.bind(this);
|
|
49
48
|
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
|
-
};
|
|
49
|
+
this.deleteCacheGroup = this.deleteCacheGroup.bind(this);
|
|
50
|
+
Object.assign(this, context);
|
|
169
51
|
}
|
|
170
52
|
addListener(event, callback) {
|
|
171
53
|
return this.events.addListener(event, callback);
|
package/lib/Http.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import Core from "./Core";
|
|
2
|
+
import { TMethod, TParams } 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
|
+
clearCache(): void;
|
|
16
|
+
}
|
|
17
|
+
export default Http;
|
package/lib/Http.js
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
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
|
+
clearCache() {
|
|
155
|
+
this.deleteCache(this.cacheIndex);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
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;
|
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,
|
package/package.json
CHANGED
package/src/Core.ts
CHANGED
|
@@ -4,178 +4,40 @@ import EventEmitter,{
|
|
|
4
4
|
Events,
|
|
5
5
|
Event
|
|
6
6
|
} from 'easy-event-emitter';
|
|
7
|
+
// import ApiRequest from './ApiRequest';
|
|
7
8
|
import {
|
|
8
9
|
TCache,
|
|
9
10
|
TData,
|
|
10
11
|
TGroupsData,
|
|
11
|
-
TMethod,
|
|
12
|
-
TParams,
|
|
13
|
-
TRequestParams,
|
|
14
12
|
TListenerEvents
|
|
15
13
|
} from './types';
|
|
16
14
|
|
|
17
15
|
abstract class Core {
|
|
18
|
-
private
|
|
19
|
-
token: string;
|
|
20
|
-
cache: TCache;
|
|
16
|
+
private cache: TCache;
|
|
21
17
|
initData: TGroupsData;
|
|
22
18
|
protected events: Events;
|
|
19
|
+
host: string;
|
|
20
|
+
token: string;
|
|
23
21
|
|
|
24
|
-
constructor() {
|
|
22
|
+
constructor(context?: Core) {
|
|
25
23
|
this.events = new EventEmitter();
|
|
26
24
|
this.host = 'http://127.0.0.1/';
|
|
27
25
|
this.token = '';
|
|
28
26
|
this.cache = {};
|
|
29
27
|
this.initData = {};
|
|
30
28
|
|
|
31
|
-
this.request = this.request.bind(this);
|
|
32
29
|
this.setHost = this.setHost.bind(this);
|
|
33
30
|
this.addListener = this.addListener.bind(this);
|
|
34
31
|
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
|
-
}
|
|
32
|
+
this.deleteCacheGroup = this.deleteCacheGroup.bind(this);
|
|
33
|
+
Object.assign(this, context);
|
|
172
34
|
}
|
|
173
35
|
|
|
174
36
|
public addListener(event: TListenerEvents, callback: (data: any) => void): Event {
|
|
175
37
|
return this.events.addListener(event, callback);
|
|
176
38
|
}
|
|
177
39
|
|
|
178
|
-
|
|
40
|
+
protected getCache(request: string): TData | undefined {
|
|
179
41
|
const key = md5(request);
|
|
180
42
|
if (this.cache[key]) {
|
|
181
43
|
if ((new Date()).getTime() <= (this.cache[key].time + 3600000000)) {
|
|
@@ -190,7 +52,7 @@ abstract class Core {
|
|
|
190
52
|
this.host = host;
|
|
191
53
|
}
|
|
192
54
|
|
|
193
|
-
|
|
55
|
+
protected setCache(request: string, data: TData, group?: string): void {
|
|
194
56
|
this.cache[md5(request)] = {
|
|
195
57
|
time: (new Date()).getTime(),
|
|
196
58
|
data: data,
|
|
@@ -206,7 +68,7 @@ abstract class Core {
|
|
|
206
68
|
}
|
|
207
69
|
}
|
|
208
70
|
|
|
209
|
-
|
|
71
|
+
protected deleteCache(request: string): void {
|
|
210
72
|
const keyCache = md5(request);
|
|
211
73
|
if (!this.cache[keyCache])
|
|
212
74
|
return;
|
package/src/Http.ts
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
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 clearCache() {
|
|
164
|
+
this.deleteCache(this.cacheIndex);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
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;
|