larapi 0.0.8 → 0.1.0

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 ADDED
@@ -0,0 +1,77 @@
1
+ # Laravel API Client
2
+ ## The simple package for authorization with token and data cache
3
+ ## Install
4
+ ```
5
+ npm i larapi
6
+ ```
7
+
8
+ ### First step
9
+ #### you can create instanse or use static methods for first configuration and for further use
10
+ ```js
11
+ const api = new API({
12
+ host: 'https://my-host'
13
+ });
14
+ ...
15
+ API.setHost('https://my-host');
16
+ ```
17
+
18
+ ### Authorization
19
+ #### token useds in the header Authorization: Bearer
20
+ ```js
21
+ api.setToken('token');
22
+ api.setUser({...object-user});
23
+ ```
24
+ #### you can use method getUser or property user for get user object
25
+ ```js
26
+ api.getUser();
27
+ ...
28
+ api.user;
29
+ ```
30
+
31
+ ### Requests
32
+
33
+ #### Get
34
+ ```js
35
+ api.get({
36
+ path: '/test',
37
+ success: (e) => {
38
+ console.log(e)
39
+ }
40
+ });
41
+ ```
42
+
43
+ #### Post
44
+ ```js
45
+ api.post({
46
+ path: '/test',
47
+ data: {
48
+ message: 'hello world'
49
+ }
50
+ success: (e) => {
51
+ console.log(e)
52
+ }
53
+ });
54
+ ```
55
+
56
+ ### Cache
57
+
58
+ #### for create simple cache use boolean true in the "cache" property
59
+ ```js
60
+ const request = api.get({
61
+ path: '/test',
62
+ cache: true,
63
+ ...
64
+ });
65
+ ```
66
+
67
+ #### if you need clear current cache - use method "clearCache"
68
+ ```js
69
+ request.clearCache();
70
+ ```
71
+ #### or you can use method "updateCache" for update current cache data in the current request
72
+ #### first argument in the method is object updated data, and last argument is key field for search in the current cache, if you need update all data use null
73
+ ```js
74
+ request.updateCache({
75
+ message: 'update this string'
76
+ }, null);
77
+ ```
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, fieldKey: string, data: TData): void;
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,23 +79,52 @@ class Core {
70
79
  group: group
71
80
  };
72
81
  }
73
- clearCacheGroup(group) {
74
- for (const key in this.cache) {
75
- if (this.cache[key].group === group) {
76
- delete this.cache[key];
82
+ clearCacheGroup(group, data, fieldKey = 'id') {
83
+ if (data) {
84
+ const clear = (cacheData) => {
85
+ if ((fieldKey && (cacheData === null || cacheData === void 0 ? void 0 : cacheData[fieldKey]) === (data === null || data === void 0 ? void 0 : 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, fieldKey, data) {
114
+ updateCacheGroup(group, data, fieldKey = 'id') {
81
115
  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];
116
+ const retData = cacheData;
117
+ for (const key of fieldKey.split('.')) {
118
+ cacheData = cacheData[key];
119
+ }
120
+ if ((fieldKey && cacheData[fieldKey] === data[fieldKey]) || fieldKey === null) {
121
+ for (const key in cacheData) {
122
+ if (key in data) {
123
+ cacheData[key] = data[key];
86
124
  }
87
125
  }
88
126
  }
89
- return cacheData;
127
+ return retData;
90
128
  };
91
129
  for (const key in this.cache) {
92
130
  if (this.cache[key].group === group) {
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(fieldKey: string, data: TData): void;
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(fieldKey, data) {
160
+ updateCache(data, fieldKey = 'id') {
155
161
  const cache = this.getCacheByIndex(this.cacheIndex);
156
162
  if (cache && cache.group) {
157
- this.updateCacheGroup(cache.group, fieldKey, data);
163
+ this.updateCacheGroup(cache.group, data, fieldKey);
158
164
  }
159
165
  }
160
- clearCache() {
161
- this.deleteCache(this.cacheIndex);
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "larapi",
3
- "version": "0.0.8",
3
+ "version": "0.1.0",
4
4
  "description": "API client for laravel",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
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,25 +66,55 @@ abstract class Core {
57
66
  };
58
67
  }
59
68
 
60
- public clearCacheGroup(group: string): void {
61
- for(const key in this.cache) {
62
- if (this.cache[key].group === group) {
63
- delete this.cache[key];
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
+
84
+ for(const key in this.cache) {
85
+ if (this.cache[key].group === group) {
86
+ if (Array.isArray(this.cache[key].data)) {
87
+ this.cache[key].data = Object.values(this.cache[key].data).filter(clear);
88
+ }else{
89
+ if (!clear(this.cache[key].data))
90
+ delete this.cache[key];
91
+ }
92
+ }
93
+ }
94
+ }else{
95
+ for(const key in this.cache) {
96
+ if (this.cache[key].group === group) {
97
+ delete this.cache[key];
98
+ }
64
99
  }
65
100
  }
66
101
  }
67
102
 
68
- public updateCacheGroup(group: string, fieldKey: string, data: TData): void {
103
+ public updateCacheGroup(group: string, data: TData, fieldKey: string | null = 'id'): void {
69
104
 
70
105
  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];
106
+ const retData = cacheData;
107
+ for(const key of fieldKey!.split('.')) {
108
+ cacheData = cacheData[key]
109
+ }
110
+ if ((fieldKey && cacheData[fieldKey] === data[fieldKey]) || fieldKey === null) {
111
+ for(const key in cacheData) {
112
+ if (key in data) {
113
+ cacheData[key] = data[key];
75
114
  }
76
115
  }
77
116
  }
78
- return cacheData;
117
+ return retData;
79
118
  };
80
119
 
81
120
  for(const key in this.cache) {
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(fieldKey: string, data: TData): void {
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, fieldKey, data);
178
+ this.updateCacheGroup(cache.group, data, fieldKey);
167
179
  }
168
180
  }
169
181
 
170
- public clearCache(): void {
171
- this.deleteCache(this.cacheIndex);
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;