larapi 0.0.8 → 0.0.9
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 +3 -2
- package/lib/Core.js +43 -9
- package/lib/Http.d.ts +2 -2
- package/lib/Http.js +19 -5
- package/lib/index.d.ts +1 -1
- package/lib/types.d.ts +6 -0
- package/package.json +1 -1
- package/src/Core.ts +43 -9
- package/src/Http.ts +24 -5
- package/src/types.ts +7 -0
package/lib/Core.d.ts
CHANGED
|
@@ -9,10 +9,11 @@ declare abstract class Core {
|
|
|
9
9
|
constructor(context?: Core);
|
|
10
10
|
addListener(event: TListenerEvents, callback: (data: any) => void): Event;
|
|
11
11
|
protected getCache(key: string): TData | undefined;
|
|
12
|
+
protected isJsonString(str: string): boolean;
|
|
12
13
|
setHost(host: string): void;
|
|
13
14
|
protected setCache(key: string, data: TData, group?: string): void;
|
|
14
|
-
clearCacheGroup(group: string): void;
|
|
15
|
-
updateCacheGroup(group: string,
|
|
15
|
+
clearCacheGroup(group: string, data?: TData, fieldKey?: string | null): void;
|
|
16
|
+
updateCacheGroup(group: string, data: TData, fieldKey?: string | null): void;
|
|
16
17
|
protected getCacheByIndex(index: string): TCacheBody;
|
|
17
18
|
protected deleteCache(key: string): void;
|
|
18
19
|
setInitData(data: TGroupsData): void;
|
package/lib/Core.js
CHANGED
|
@@ -60,6 +60,15 @@ class Core {
|
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
|
+
isJsonString(str) {
|
|
64
|
+
try {
|
|
65
|
+
JSON.parse(str);
|
|
66
|
+
}
|
|
67
|
+
catch (e) {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
63
72
|
setHost(host) {
|
|
64
73
|
this.host = host;
|
|
65
74
|
}
|
|
@@ -70,19 +79,44 @@ class Core {
|
|
|
70
79
|
group: group
|
|
71
80
|
};
|
|
72
81
|
}
|
|
73
|
-
clearCacheGroup(group) {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
82
|
+
clearCacheGroup(group, data, fieldKey = 'id') {
|
|
83
|
+
if (data) {
|
|
84
|
+
const clear = (cacheData) => {
|
|
85
|
+
if ((fieldKey && cacheData[fieldKey] === data[fieldKey]) || fieldKey === null) {
|
|
86
|
+
for (const key in cacheData) {
|
|
87
|
+
if (key in data) {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return true;
|
|
93
|
+
};
|
|
94
|
+
for (const key in this.cache) {
|
|
95
|
+
if (this.cache[key].group === group) {
|
|
96
|
+
if (Array.isArray(this.cache[key].data)) {
|
|
97
|
+
this.cache[key].data = Object.values(this.cache[key].data).filter(clear);
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
if (!clear(this.cache[key].data))
|
|
101
|
+
delete this.cache[key];
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
for (const key in this.cache) {
|
|
108
|
+
if (this.cache[key].group === group) {
|
|
109
|
+
delete this.cache[key];
|
|
110
|
+
}
|
|
77
111
|
}
|
|
78
112
|
}
|
|
79
113
|
}
|
|
80
|
-
updateCacheGroup(group,
|
|
114
|
+
updateCacheGroup(group, data, fieldKey = 'id') {
|
|
81
115
|
const update = (cacheData) => {
|
|
82
|
-
if (cacheData[fieldKey] === data[fieldKey]) {
|
|
83
|
-
for (const
|
|
84
|
-
if (
|
|
85
|
-
cacheData[
|
|
116
|
+
if ((fieldKey && cacheData[fieldKey] === data[fieldKey]) || fieldKey === null) {
|
|
117
|
+
for (const key in cacheData) {
|
|
118
|
+
if (key in data) {
|
|
119
|
+
cacheData[key] = data[key];
|
|
86
120
|
}
|
|
87
121
|
}
|
|
88
122
|
}
|
package/lib/Http.d.ts
CHANGED
|
@@ -12,7 +12,7 @@ declare class Http extends Core {
|
|
|
12
12
|
private encodeUrlParams;
|
|
13
13
|
private cuteUndifinedParams;
|
|
14
14
|
request(): this;
|
|
15
|
-
updateCache(
|
|
16
|
-
clearCache(): void;
|
|
15
|
+
updateCache(data: TData, fieldKey?: string | null): void;
|
|
16
|
+
clearCache(data?: TData, fieldKey?: string | null): void;
|
|
17
17
|
}
|
|
18
18
|
export default Http;
|
package/lib/Http.js
CHANGED
|
@@ -40,6 +40,12 @@ class Http extends Core_1.default {
|
|
|
40
40
|
success(...args) {
|
|
41
41
|
if (typeof this.params.success === 'function')
|
|
42
42
|
this.params.success(...args);
|
|
43
|
+
if (this.params.cacheUpdate && this.params.data) {
|
|
44
|
+
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);
|
|
45
|
+
}
|
|
46
|
+
if (this.params.cacheClear && this.params.data) {
|
|
47
|
+
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);
|
|
48
|
+
}
|
|
43
49
|
}
|
|
44
50
|
//Заголовки от 400 до 499
|
|
45
51
|
fail(...args) {
|
|
@@ -127,7 +133,7 @@ class Http extends Core_1.default {
|
|
|
127
133
|
if (xhr.readyState !== 4) {
|
|
128
134
|
return;
|
|
129
135
|
}
|
|
130
|
-
const result = xhr.responseText ? JSON.parse(xhr.responseText) : {};
|
|
136
|
+
const result = this.isJsonString(xhr.responseText) ? JSON.parse(xhr.responseText) : {};
|
|
131
137
|
if (xhr.status >= 200 && xhr.status <= 299) {
|
|
132
138
|
if (this.params.cache) {
|
|
133
139
|
this.setCache(this.cacheIndex, result, (typeof this.params.cache === 'boolean' ? undefined : this.params.cache));
|
|
@@ -151,14 +157,22 @@ class Http extends Core_1.default {
|
|
|
151
157
|
}
|
|
152
158
|
return this;
|
|
153
159
|
}
|
|
154
|
-
updateCache(
|
|
160
|
+
updateCache(data, fieldKey = 'id') {
|
|
155
161
|
const cache = this.getCacheByIndex(this.cacheIndex);
|
|
156
162
|
if (cache && cache.group) {
|
|
157
|
-
this.updateCacheGroup(cache.group,
|
|
163
|
+
this.updateCacheGroup(cache.group, data, fieldKey);
|
|
158
164
|
}
|
|
159
165
|
}
|
|
160
|
-
clearCache() {
|
|
161
|
-
|
|
166
|
+
clearCache(data, fieldKey = 'id') {
|
|
167
|
+
if (data) {
|
|
168
|
+
const cache = this.getCacheByIndex(this.cacheIndex);
|
|
169
|
+
if (cache && cache.group) {
|
|
170
|
+
this.clearCacheGroup(cache.group, data, fieldKey);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
this.deleteCache(this.cacheIndex);
|
|
175
|
+
}
|
|
162
176
|
}
|
|
163
177
|
}
|
|
164
178
|
exports.default = Http;
|
package/lib/index.d.ts
CHANGED
|
@@ -34,7 +34,7 @@ declare class API extends Core {
|
|
|
34
34
|
static setToken: (token: string) => void;
|
|
35
35
|
static addListener: (event: import("./types").TListenerEvents, callback: (data: any) => void) => import("easy-event-emitter").Event;
|
|
36
36
|
static setInitData: (data: import("./types").TGroupsData) => void;
|
|
37
|
-
static clearCacheGroup: (group: string) => void;
|
|
37
|
+
static clearCacheGroup: (group: string, data?: import("./types").TData | undefined, fieldKey?: string | null) => void;
|
|
38
38
|
static getToken: () => string;
|
|
39
39
|
static getUid: () => number;
|
|
40
40
|
static getUser: () => TUser;
|
package/lib/types.d.ts
CHANGED
|
@@ -13,10 +13,16 @@ export type TGroupsData = {
|
|
|
13
13
|
[index: string]: TData;
|
|
14
14
|
};
|
|
15
15
|
export type TMethod = 'GET' | 'POST';
|
|
16
|
+
export type TCacheControll = string | {
|
|
17
|
+
group: string;
|
|
18
|
+
fieldKey: string;
|
|
19
|
+
};
|
|
16
20
|
export type TParams = {
|
|
17
21
|
path: string;
|
|
18
22
|
data?: TData;
|
|
19
23
|
cache?: string | boolean;
|
|
24
|
+
cacheUpdate?: TCacheControll;
|
|
25
|
+
cacheClear?: TCacheControll;
|
|
20
26
|
globalName?: string;
|
|
21
27
|
success?: (...args: any) => void;
|
|
22
28
|
error?: (...args: any) => void;
|
package/package.json
CHANGED
package/src/Core.ts
CHANGED
|
@@ -45,6 +45,15 @@ abstract class Core {
|
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
+
protected isJsonString(str: string) {
|
|
49
|
+
try {
|
|
50
|
+
JSON.parse(str);
|
|
51
|
+
} catch (e) {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
|
|
48
57
|
public setHost(host: string): void {
|
|
49
58
|
this.host = host;
|
|
50
59
|
}
|
|
@@ -57,21 +66,46 @@ abstract class Core {
|
|
|
57
66
|
};
|
|
58
67
|
}
|
|
59
68
|
|
|
60
|
-
public clearCacheGroup(group: string): void {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
69
|
+
public clearCacheGroup(group: string, data?: TData, fieldKey: string | null = 'id'): void {
|
|
70
|
+
if (data) {
|
|
71
|
+
|
|
72
|
+
const clear = (cacheData: any) => {
|
|
73
|
+
if ((fieldKey && cacheData[fieldKey] === data[fieldKey]) || fieldKey === null) {
|
|
74
|
+
for(const key in cacheData) {
|
|
75
|
+
if (key in data) {
|
|
76
|
+
return false
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return true;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
for(const key in this.cache) {
|
|
84
|
+
if (this.cache[key].group === group) {
|
|
85
|
+
if (Array.isArray(this.cache[key].data)) {
|
|
86
|
+
this.cache[key].data = Object.values(this.cache[key].data).filter(clear);
|
|
87
|
+
}else{
|
|
88
|
+
if (!clear(this.cache[key].data))
|
|
89
|
+
delete this.cache[key];
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}else{
|
|
94
|
+
for(const key in this.cache) {
|
|
95
|
+
if (this.cache[key].group === group) {
|
|
96
|
+
delete this.cache[key];
|
|
97
|
+
}
|
|
64
98
|
}
|
|
65
99
|
}
|
|
66
100
|
}
|
|
67
101
|
|
|
68
|
-
public updateCacheGroup(group: string,
|
|
102
|
+
public updateCacheGroup(group: string, data: TData, fieldKey: string | null = 'id'): void {
|
|
69
103
|
|
|
70
104
|
const update = (cacheData: any) => {
|
|
71
|
-
if (cacheData[fieldKey] === data[fieldKey]) {
|
|
72
|
-
for(const
|
|
73
|
-
if (
|
|
74
|
-
cacheData[
|
|
105
|
+
if ((fieldKey && cacheData[fieldKey] === data[fieldKey]) || fieldKey === null) {
|
|
106
|
+
for(const key in cacheData) {
|
|
107
|
+
if (key in data) {
|
|
108
|
+
cacheData[key] = data[key];
|
|
75
109
|
}
|
|
76
110
|
}
|
|
77
111
|
}
|
package/src/Http.ts
CHANGED
|
@@ -30,6 +30,18 @@ class Http extends Core {
|
|
|
30
30
|
private success(...args: any) {
|
|
31
31
|
if (typeof this.params.success === 'function')
|
|
32
32
|
this.params.success(...args);
|
|
33
|
+
if (this.params.cacheUpdate && this.params.data) {
|
|
34
|
+
this.updateCacheGroup(
|
|
35
|
+
typeof this.params.cacheUpdate === 'string' ? this.params.cacheUpdate : this.params.cacheUpdate.group,
|
|
36
|
+
this.params.data,
|
|
37
|
+
typeof this.params.cacheUpdate === 'string' ? undefined : this.params.cacheUpdate.fieldKey);
|
|
38
|
+
}
|
|
39
|
+
if (this.params.cacheClear && this.params.data) {
|
|
40
|
+
this.clearCacheGroup(
|
|
41
|
+
typeof this.params.cacheClear === 'string' ? this.params.cacheClear : this.params.cacheClear.group,
|
|
42
|
+
this.params.data,
|
|
43
|
+
typeof this.params.cacheClear === 'string' ? undefined : this.params.cacheClear.fieldKey);
|
|
44
|
+
}
|
|
33
45
|
}
|
|
34
46
|
|
|
35
47
|
//Заголовки от 400 до 499
|
|
@@ -135,7 +147,7 @@ class Http extends Core {
|
|
|
135
147
|
return;
|
|
136
148
|
}
|
|
137
149
|
|
|
138
|
-
const result = xhr.responseText ? JSON.parse(xhr.responseText) : {};
|
|
150
|
+
const result = this.isJsonString(xhr.responseText) ? JSON.parse(xhr.responseText) : {};
|
|
139
151
|
|
|
140
152
|
if (xhr.status >= 200 && xhr.status <= 299) {
|
|
141
153
|
if (this.params.cache) {
|
|
@@ -160,15 +172,22 @@ class Http extends Core {
|
|
|
160
172
|
return this
|
|
161
173
|
}
|
|
162
174
|
|
|
163
|
-
public updateCache(
|
|
175
|
+
public updateCache(data: TData, fieldKey: string | null = 'id'): void {
|
|
164
176
|
const cache = this.getCacheByIndex(this.cacheIndex);
|
|
165
177
|
if (cache && cache.group) {
|
|
166
|
-
this.updateCacheGroup(cache.group,
|
|
178
|
+
this.updateCacheGroup(cache.group, data, fieldKey);
|
|
167
179
|
}
|
|
168
180
|
}
|
|
169
181
|
|
|
170
|
-
public clearCache(): void {
|
|
171
|
-
|
|
182
|
+
public clearCache(data?: TData, fieldKey: string | null = 'id'): void {
|
|
183
|
+
if (data) {
|
|
184
|
+
const cache = this.getCacheByIndex(this.cacheIndex);
|
|
185
|
+
if (cache && cache.group) {
|
|
186
|
+
this.clearCacheGroup(cache.group, data, fieldKey);
|
|
187
|
+
}
|
|
188
|
+
}else{
|
|
189
|
+
this.deleteCache(this.cacheIndex);
|
|
190
|
+
}
|
|
172
191
|
}
|
|
173
192
|
}
|
|
174
193
|
|
package/src/types.ts
CHANGED
|
@@ -18,10 +18,17 @@ export type TGroupsData = {
|
|
|
18
18
|
|
|
19
19
|
export type TMethod = 'GET' | 'POST';
|
|
20
20
|
|
|
21
|
+
export type TCacheControll = string | {
|
|
22
|
+
group: string;
|
|
23
|
+
fieldKey: string;
|
|
24
|
+
};
|
|
25
|
+
|
|
21
26
|
export type TParams = {
|
|
22
27
|
path: string;
|
|
23
28
|
data?: TData;
|
|
24
29
|
cache?: string | boolean;
|
|
30
|
+
cacheUpdate?: TCacheControll;
|
|
31
|
+
cacheClear?: TCacheControll;
|
|
25
32
|
globalName?: string;
|
|
26
33
|
success?: (...args: any) => void;
|
|
27
34
|
error?: (...args: any) => void;
|