larapi 0.1.0 → 0.1.1
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/README.md +1 -1
- package/lib/Core.d.ts +0 -1
- package/lib/Core.js +0 -7
- package/lib/Http.d.ts +8 -1
- package/lib/Http.js +98 -65
- package/lib/index.d.ts +3 -2
- package/lib/index.js +11 -1
- package/package.json +1 -1
- package/src/Core.ts +1 -2
- package/src/Http.ts +92 -72
- package/src/index.ts +6 -1
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@ npm i larapi
|
|
|
6
6
|
```
|
|
7
7
|
|
|
8
8
|
### First step
|
|
9
|
-
#### you can create
|
|
9
|
+
#### you can create instance or use static methods for first configuration and for further use
|
|
10
10
|
```js
|
|
11
11
|
const api = new API({
|
|
12
12
|
host: 'https://my-host'
|
package/lib/Core.d.ts
CHANGED
|
@@ -5,7 +5,6 @@ declare abstract class Core {
|
|
|
5
5
|
initData: TGroupsData;
|
|
6
6
|
protected events: Events;
|
|
7
7
|
host: string;
|
|
8
|
-
token: string;
|
|
9
8
|
constructor(context?: Core);
|
|
10
9
|
addListener(event: TListenerEvents, callback: (data: any) => void): Event;
|
|
11
10
|
protected getCache(key: string): TData | undefined;
|
package/lib/Core.js
CHANGED
|
@@ -30,15 +30,8 @@ class Core {
|
|
|
30
30
|
writable: true,
|
|
31
31
|
value: void 0
|
|
32
32
|
});
|
|
33
|
-
Object.defineProperty(this, "token", {
|
|
34
|
-
enumerable: true,
|
|
35
|
-
configurable: true,
|
|
36
|
-
writable: true,
|
|
37
|
-
value: void 0
|
|
38
|
-
});
|
|
39
33
|
this.events = new easy_event_emitter_1.default();
|
|
40
34
|
this.host = 'http://127.0.0.1/';
|
|
41
|
-
this.token = '';
|
|
42
35
|
this.cache = {};
|
|
43
36
|
this.initData = {};
|
|
44
37
|
this.setHost = this.setHost.bind(this);
|
package/lib/Http.d.ts
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
import Core from "./Core";
|
|
2
|
-
import { TMethod, TParams, TData } from "./types";
|
|
2
|
+
import { TMethod, TParams, TData, TRequestParams } from "./types";
|
|
3
3
|
declare class Http extends Core {
|
|
4
4
|
private cacheIndex;
|
|
5
|
+
currentCache?: TData;
|
|
6
|
+
requestParams: TRequestParams;
|
|
5
7
|
method: TMethod;
|
|
6
8
|
params: TParams;
|
|
9
|
+
path: string;
|
|
7
10
|
constructor(method: TMethod, params: TParams, context?: Core);
|
|
11
|
+
private initRequest;
|
|
12
|
+
private initCache;
|
|
8
13
|
private success;
|
|
9
14
|
private fail;
|
|
10
15
|
private error;
|
|
@@ -12,6 +17,8 @@ declare class Http extends Core {
|
|
|
12
17
|
private encodeUrlParams;
|
|
13
18
|
private cuteUndifinedParams;
|
|
14
19
|
request(): this;
|
|
20
|
+
addHeader(key: string, value: string): this;
|
|
21
|
+
deleteHeader(key: string): this;
|
|
15
22
|
updateCache(data: TData, fieldKey?: string | null): void;
|
|
16
23
|
clearCache(data?: TData, fieldKey?: string | null): void;
|
|
17
24
|
}
|
package/lib/Http.js
CHANGED
|
@@ -15,6 +15,18 @@ class Http extends Core_1.default {
|
|
|
15
15
|
writable: true,
|
|
16
16
|
value: void 0
|
|
17
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
|
+
});
|
|
18
30
|
Object.defineProperty(this, "method", {
|
|
19
31
|
enumerable: true,
|
|
20
32
|
configurable: true,
|
|
@@ -27,14 +39,63 @@ class Http extends Core_1.default {
|
|
|
27
39
|
writable: true,
|
|
28
40
|
value: void 0
|
|
29
41
|
});
|
|
42
|
+
Object.defineProperty(this, "path", {
|
|
43
|
+
enumerable: true,
|
|
44
|
+
configurable: true,
|
|
45
|
+
writable: true,
|
|
46
|
+
value: void 0
|
|
47
|
+
});
|
|
30
48
|
this.cacheIndex = '';
|
|
31
49
|
this.method = method;
|
|
32
50
|
this.params = params;
|
|
51
|
+
this.path = this.params.path;
|
|
52
|
+
this.requestParams = {
|
|
53
|
+
method: this.method
|
|
54
|
+
};
|
|
33
55
|
this.success = this.success.bind(this);
|
|
34
56
|
this.fail = this.fail.bind(this);
|
|
35
57
|
this.error = this.error.bind(this);
|
|
36
58
|
this.complete = this.complete.bind(this);
|
|
37
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
|
+
}
|
|
38
99
|
}
|
|
39
100
|
//Заголовки от 200 до 299
|
|
40
101
|
success(...args) {
|
|
@@ -84,76 +145,48 @@ class Http extends Core_1.default {
|
|
|
84
145
|
return data;
|
|
85
146
|
}
|
|
86
147
|
request() {
|
|
87
|
-
|
|
88
|
-
if (typeof XMLHttpRequest === 'undefined')
|
|
148
|
+
if (typeof XMLHttpRequest === 'undefined' || this.currentCache)
|
|
89
149
|
return this;
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
method
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
'Accept': 'application/json',
|
|
96
|
-
'Content-Type': 'application/json',
|
|
97
|
-
'Authorization': 'Bearer ' + this.token
|
|
98
|
-
};
|
|
99
|
-
this.params.data = this.params.data ? this.cuteUndifinedParams(this.params.data) : this.params.data;
|
|
100
|
-
if (this.method === 'POST') {
|
|
101
|
-
fetchParams.body = JSON.stringify(this.params.data);
|
|
102
|
-
}
|
|
103
|
-
if (this.method === 'GET') {
|
|
104
|
-
if (this.params.data)
|
|
105
|
-
path = this.params.path + '?' + this.encodeUrlParams(this.params.data);
|
|
106
|
-
}
|
|
107
|
-
this.cacheIndex = (0, md5_1.default)(path + JSON.stringify(this.params.data));
|
|
108
|
-
var dataCache;
|
|
109
|
-
if (this.params.globalName) {
|
|
110
|
-
if (this.initData[this.params.globalName]) {
|
|
111
|
-
this.setCache(this.cacheIndex, this.initData[this.params.globalName], (typeof this.params.cache === 'boolean' ? undefined : this.params.cache));
|
|
112
|
-
this.params.cache = (_a = this.params.cache) !== null && _a !== void 0 ? _a : true;
|
|
113
|
-
delete this.initData[this.params.globalName];
|
|
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]);
|
|
114
155
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
this.success(dataCache);
|
|
119
|
-
this.complete(dataCache);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
else {
|
|
123
|
-
this.deleteCache(this.cacheIndex);
|
|
124
|
-
}
|
|
125
|
-
if (!dataCache) {
|
|
126
|
-
try {
|
|
127
|
-
const xhr = new XMLHttpRequest();
|
|
128
|
-
xhr.open(this.method, this.host + path, true);
|
|
129
|
-
for (const header in fetchParams.headers) {
|
|
130
|
-
xhr.setRequestHeader(header, fetchParams.headers[header]);
|
|
156
|
+
xhr.onreadystatechange = () => {
|
|
157
|
+
if (xhr.readyState !== 4) {
|
|
158
|
+
return;
|
|
131
159
|
}
|
|
132
|
-
xhr.
|
|
133
|
-
|
|
134
|
-
|
|
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));
|
|
135
164
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
this.success(result);
|
|
142
|
-
}
|
|
143
|
-
else {
|
|
144
|
-
if (xhr.status >= 400 && xhr.status <= 499) {
|
|
145
|
-
fail(result);
|
|
146
|
-
}
|
|
147
|
-
this.error(result);
|
|
148
|
-
console.warn(result);
|
|
165
|
+
this.success(result);
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
if (xhr.status >= 400 && xhr.status <= 499) {
|
|
169
|
+
fail(result);
|
|
149
170
|
}
|
|
150
|
-
this.
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
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
|
+
}
|
|
181
|
+
return this;
|
|
182
|
+
}
|
|
183
|
+
addHeader(key, value) {
|
|
184
|
+
this.requestParams.headers = Object.assign(Object.assign({}, this.requestParams.headers), { [key]: value });
|
|
185
|
+
return this;
|
|
186
|
+
}
|
|
187
|
+
deleteHeader(key) {
|
|
188
|
+
if (this.requestParams.headers && (key in this.requestParams.headers)) {
|
|
189
|
+
delete this.requestParams.headers[key];
|
|
157
190
|
}
|
|
158
191
|
return this;
|
|
159
192
|
}
|
package/lib/index.d.ts
CHANGED
|
@@ -10,13 +10,14 @@ type TConfig = {
|
|
|
10
10
|
declare class API extends Core {
|
|
11
11
|
private static instance;
|
|
12
12
|
private _user;
|
|
13
|
+
token?: string;
|
|
13
14
|
constructor(config: TConfig);
|
|
14
15
|
http(method: TMethod, params: TParams): Http;
|
|
15
16
|
get(params: TParams): Http;
|
|
16
17
|
post(params: TParams): Http;
|
|
17
18
|
private init;
|
|
18
19
|
setToken(token: string): void;
|
|
19
|
-
getToken(): string;
|
|
20
|
+
getToken(): string | undefined;
|
|
20
21
|
getUid(): number;
|
|
21
22
|
getUser(): TUser;
|
|
22
23
|
get uid(): number;
|
|
@@ -35,7 +36,7 @@ declare class API extends Core {
|
|
|
35
36
|
static addListener: (event: import("./types").TListenerEvents, callback: (data: any) => void) => import("easy-event-emitter").Event;
|
|
36
37
|
static setInitData: (data: import("./types").TGroupsData) => void;
|
|
37
38
|
static clearCacheGroup: (group: string, data?: import("./types").TData | undefined, fieldKey?: string | null) => void;
|
|
38
|
-
static getToken: () => string;
|
|
39
|
+
static getToken: () => string | undefined;
|
|
39
40
|
static getUid: () => number;
|
|
40
41
|
static getUser: () => TUser;
|
|
41
42
|
static user: TUser;
|
package/lib/index.js
CHANGED
|
@@ -15,6 +15,12 @@ class API extends Core_1.default {
|
|
|
15
15
|
writable: true,
|
|
16
16
|
value: void 0
|
|
17
17
|
});
|
|
18
|
+
Object.defineProperty(this, "token", {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
configurable: true,
|
|
21
|
+
writable: true,
|
|
22
|
+
value: void 0
|
|
23
|
+
});
|
|
18
24
|
this.setHost(config.host);
|
|
19
25
|
this._user = {};
|
|
20
26
|
this.init();
|
|
@@ -30,7 +36,11 @@ class API extends Core_1.default {
|
|
|
30
36
|
this.getUser = this.getUser.bind(this);
|
|
31
37
|
}
|
|
32
38
|
http(method, params) {
|
|
33
|
-
|
|
39
|
+
const http = new Http_1.default(method, params, this);
|
|
40
|
+
if (this.token) {
|
|
41
|
+
http.addHeader('Authorization', 'Bearer ' + this.token);
|
|
42
|
+
}
|
|
43
|
+
return http;
|
|
34
44
|
}
|
|
35
45
|
get(params) {
|
|
36
46
|
return this.http('GET', params).request();
|
package/package.json
CHANGED
package/src/Core.ts
CHANGED
|
@@ -15,12 +15,11 @@ abstract class Core {
|
|
|
15
15
|
initData: TGroupsData;
|
|
16
16
|
protected events: Events;
|
|
17
17
|
host: string;
|
|
18
|
-
|
|
18
|
+
|
|
19
19
|
|
|
20
20
|
constructor(context?: Core) {
|
|
21
21
|
this.events = new EventEmitter();
|
|
22
22
|
this.host = 'http://127.0.0.1/';
|
|
23
|
-
this.token = '';
|
|
24
23
|
this.cache = {};
|
|
25
24
|
this.initData = {};
|
|
26
25
|
|
package/src/Http.ts
CHANGED
|
@@ -10,20 +10,71 @@ import {
|
|
|
10
10
|
|
|
11
11
|
class Http extends Core {
|
|
12
12
|
private cacheIndex: string;
|
|
13
|
+
currentCache?: TData;
|
|
14
|
+
requestParams: TRequestParams;
|
|
13
15
|
method: TMethod;
|
|
14
16
|
params: TParams;
|
|
17
|
+
path: string;
|
|
15
18
|
|
|
16
19
|
constructor(method: TMethod, params: TParams, context?: Core) {
|
|
17
20
|
super(context);
|
|
18
21
|
this.cacheIndex = '';
|
|
19
22
|
this.method = method;
|
|
20
23
|
this.params = params;
|
|
21
|
-
|
|
24
|
+
this.path = this.params.path;
|
|
25
|
+
this.requestParams = {
|
|
26
|
+
method: this.method
|
|
27
|
+
};
|
|
28
|
+
|
|
22
29
|
this.success = this.success.bind(this);
|
|
23
30
|
this.fail = this.fail.bind(this);
|
|
24
31
|
this.error = this.error.bind(this);
|
|
25
32
|
this.complete = this.complete.bind(this);
|
|
26
33
|
this.request = this.request.bind(this);
|
|
34
|
+
this.addListener = this.addListener.bind(this);
|
|
35
|
+
this.addHeader = this.addHeader.bind(this);
|
|
36
|
+
this.deleteHeader = this.deleteHeader.bind(this);
|
|
37
|
+
|
|
38
|
+
this.initRequest();
|
|
39
|
+
this.initCache();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
private initRequest(): void {
|
|
43
|
+
this.requestParams.headers = {
|
|
44
|
+
'Accept': 'application/json',
|
|
45
|
+
'Content-Type': 'application/json'
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
this.params.data = this.params.data ? this.cuteUndifinedParams(this.params.data) : this.params.data;
|
|
49
|
+
|
|
50
|
+
if (this.method === 'GET') {
|
|
51
|
+
if (this.params.data)
|
|
52
|
+
this.path = this.params.path + '?' + this.encodeUrlParams(this.params.data);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (this.method === 'POST') {
|
|
56
|
+
this.requestParams.body = JSON.stringify(this.params.data);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
private initCache(): void {
|
|
61
|
+
this.cacheIndex = md5(this.path + JSON.stringify(this.params.data));
|
|
62
|
+
if (this.params.globalName) {
|
|
63
|
+
if (this.initData[this.params.globalName]) {
|
|
64
|
+
this.setCache(this.cacheIndex, this.initData[this.params.globalName], (typeof this.params.cache === 'boolean' ? undefined : this.params.cache));
|
|
65
|
+
this.params.cache = this.params.cache ?? true;
|
|
66
|
+
delete this.initData[this.params.globalName];
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (this.params.cache) {
|
|
71
|
+
if (this.currentCache = this.getCache(this.cacheIndex)) {
|
|
72
|
+
this.success(this.currentCache);
|
|
73
|
+
this.complete(this.currentCache);
|
|
74
|
+
}
|
|
75
|
+
}else{
|
|
76
|
+
this.deleteCache(this.cacheIndex);
|
|
77
|
+
}
|
|
27
78
|
}
|
|
28
79
|
|
|
29
80
|
//Заголовки от 200 до 299
|
|
@@ -36,6 +87,7 @@ class Http extends Core {
|
|
|
36
87
|
this.params.data,
|
|
37
88
|
typeof this.params.cacheUpdate === 'string' ? undefined : this.params.cacheUpdate.fieldKey);
|
|
38
89
|
}
|
|
90
|
+
|
|
39
91
|
if (this.params.cacheClear && this.params.data) {
|
|
40
92
|
this.clearCacheGroup(
|
|
41
93
|
typeof this.params.cacheClear === 'string' ? this.params.cacheClear : this.params.cacheClear.group,
|
|
@@ -89,89 +141,57 @@ class Http extends Core {
|
|
|
89
141
|
}
|
|
90
142
|
|
|
91
143
|
public request(): this {
|
|
92
|
-
if (typeof XMLHttpRequest === 'undefined') return this;
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
method: this.method
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
fetchParams.headers = {
|
|
100
|
-
'Accept': 'application/json',
|
|
101
|
-
'Content-Type': 'application/json',
|
|
102
|
-
'Authorization': 'Bearer ' + this.token
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
this.params.data = this.params.data ? this.cuteUndifinedParams(this.params.data) : this.params.data;
|
|
106
|
-
|
|
107
|
-
if (this.method === 'POST') {
|
|
108
|
-
fetchParams.body = JSON.stringify(this.params.data);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
if (this.method === 'GET') {
|
|
112
|
-
if (this.params.data)
|
|
113
|
-
path = this.params.path + '?' + this.encodeUrlParams(this.params.data);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
this.cacheIndex = md5(path + JSON.stringify(this.params.data));
|
|
117
|
-
|
|
118
|
-
var dataCache;
|
|
144
|
+
if (typeof XMLHttpRequest === 'undefined' || this.currentCache) return this;
|
|
145
|
+
|
|
146
|
+
try {
|
|
147
|
+
const xhr = new XMLHttpRequest();
|
|
119
148
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
this.params.cache = this.params.cache ?? true;
|
|
124
|
-
delete this.initData[this.params.globalName];
|
|
149
|
+
xhr.open(this.method, this.host + this.path, true);
|
|
150
|
+
for(const header in this.requestParams.headers) {
|
|
151
|
+
xhr.setRequestHeader(header, this.requestParams.headers[header]);
|
|
125
152
|
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
if (this.params.cache) {
|
|
129
|
-
if (dataCache = this.getCache(this.cacheIndex)) {
|
|
130
|
-
this.success(dataCache);
|
|
131
|
-
this.complete(dataCache);
|
|
132
|
-
}
|
|
133
|
-
}else{
|
|
134
|
-
this.deleteCache(this.cacheIndex);
|
|
135
|
-
}
|
|
136
|
-
if (!dataCache) {
|
|
137
|
-
try {
|
|
138
|
-
const xhr = new XMLHttpRequest();
|
|
139
153
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
154
|
+
xhr.onreadystatechange = () => {
|
|
155
|
+
if (xhr.readyState !== 4) {
|
|
156
|
+
return;
|
|
143
157
|
}
|
|
144
|
-
|
|
145
|
-
xhr.
|
|
146
|
-
|
|
147
|
-
|
|
158
|
+
|
|
159
|
+
const result = this.isJsonString(xhr.responseText) ? JSON.parse(xhr.responseText) : {};
|
|
160
|
+
|
|
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));
|
|
148
164
|
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
if (this.params.cache) {
|
|
154
|
-
this.setCache(this.cacheIndex, result, (typeof this.params.cache === 'boolean' ? undefined : this.params.cache));
|
|
155
|
-
}
|
|
156
|
-
this.success(result);
|
|
157
|
-
}else{
|
|
158
|
-
if (xhr.status >= 400 && xhr.status <= 499) {
|
|
159
|
-
fail(result);
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
this.error(result);
|
|
163
|
-
console.warn(result);
|
|
165
|
+
this.success(result);
|
|
166
|
+
}else{
|
|
167
|
+
if (xhr.status >= 400 && xhr.status <= 499) {
|
|
168
|
+
fail(result);
|
|
164
169
|
}
|
|
165
|
-
|
|
170
|
+
|
|
171
|
+
this.error(result);
|
|
172
|
+
console.warn(result);
|
|
166
173
|
}
|
|
167
|
-
|
|
168
|
-
}catch(e) {
|
|
169
|
-
console.warn(e);
|
|
174
|
+
this.complete(result);
|
|
170
175
|
}
|
|
176
|
+
xhr.send(this.requestParams.body);
|
|
177
|
+
}catch(e) {
|
|
178
|
+
console.warn(e);
|
|
171
179
|
}
|
|
172
180
|
return this
|
|
173
181
|
}
|
|
174
182
|
|
|
183
|
+
public addHeader(key: string, value: string): this {
|
|
184
|
+
this.requestParams.headers = {...this.requestParams.headers, [key]: value};
|
|
185
|
+
return this;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
public deleteHeader(key: string): this {
|
|
189
|
+
if (this.requestParams.headers && (key in this.requestParams.headers)) {
|
|
190
|
+
delete this.requestParams.headers[key];
|
|
191
|
+
}
|
|
192
|
+
return this;
|
|
193
|
+
}
|
|
194
|
+
|
|
175
195
|
public updateCache(data: TData, fieldKey: string | null = 'id'): void {
|
|
176
196
|
const cache = this.getCacheByIndex(this.cacheIndex);
|
|
177
197
|
if (cache && cache.group) {
|
package/src/index.ts
CHANGED
|
@@ -15,6 +15,7 @@ class API extends Core {
|
|
|
15
15
|
host: ''
|
|
16
16
|
});
|
|
17
17
|
private _user: TUser;
|
|
18
|
+
token?: string;
|
|
18
19
|
|
|
19
20
|
constructor(config: TConfig) {
|
|
20
21
|
super();
|
|
@@ -35,7 +36,11 @@ class API extends Core {
|
|
|
35
36
|
}
|
|
36
37
|
|
|
37
38
|
public http(method: TMethod, params: TParams): Http {
|
|
38
|
-
|
|
39
|
+
const http = new Http(method, params, this);
|
|
40
|
+
if (this.token) {
|
|
41
|
+
http.addHeader('Authorization', 'Bearer ' + this.token);
|
|
42
|
+
}
|
|
43
|
+
return http;
|
|
39
44
|
}
|
|
40
45
|
|
|
41
46
|
public get(params: TParams): Http {
|