larapi 0.0.7 → 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 CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Events, Event } from 'easy-event-emitter';
2
- import { TData, TGroupsData, TListenerEvents } from './types';
2
+ import { TCacheBody, TData, TGroupsData, TListenerEvents } from './types';
3
3
  declare abstract class Core {
4
4
  private cache;
5
5
  initData: TGroupsData;
@@ -8,11 +8,14 @@ declare abstract class Core {
8
8
  token: string;
9
9
  constructor(context?: Core);
10
10
  addListener(event: TListenerEvents, callback: (data: any) => void): Event;
11
- protected getCache(request: string): TData | undefined;
11
+ protected getCache(key: string): TData | undefined;
12
+ protected isJsonString(str: string): boolean;
12
13
  setHost(host: string): void;
13
- protected setCache(request: string, data: TData, group?: string): void;
14
- deleteCacheGroup(group: string): void;
15
- protected deleteCache(request: string): void;
14
+ protected setCache(key: string, data: TData, group?: string): void;
15
+ clearCacheGroup(group: string, data?: TData, fieldKey?: string | null): void;
16
+ updateCacheGroup(group: string, data: TData, fieldKey?: string | null): void;
17
+ protected getCacheByIndex(index: string): TCacheBody;
18
+ protected deleteCache(key: string): void;
16
19
  setInitData(data: TGroupsData): void;
17
20
  }
18
21
  export default Core;
package/lib/Core.js CHANGED
@@ -3,8 +3,6 @@ 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
8
  constructor(context) {
@@ -46,14 +44,13 @@ class Core {
46
44
  this.setHost = this.setHost.bind(this);
47
45
  this.addListener = this.addListener.bind(this);
48
46
  this.setInitData = this.setInitData.bind(this);
49
- this.deleteCacheGroup = this.deleteCacheGroup.bind(this);
47
+ this.clearCacheGroup = this.clearCacheGroup.bind(this);
50
48
  Object.assign(this, context);
51
49
  }
52
50
  addListener(event, callback) {
53
51
  return this.events.addListener(event, callback);
54
52
  }
55
- getCache(request) {
56
- const key = (0, md5_1.default)(request);
53
+ getCache(key) {
57
54
  if (this.cache[key]) {
58
55
  if ((new Date()).getTime() <= (this.cache[key].time + 3600000000)) {
59
56
  return this.cache[key].data;
@@ -63,32 +60,90 @@ class Core {
63
60
  }
64
61
  }
65
62
  }
63
+ isJsonString(str) {
64
+ try {
65
+ JSON.parse(str);
66
+ }
67
+ catch (e) {
68
+ return false;
69
+ }
70
+ return true;
71
+ }
66
72
  setHost(host) {
67
73
  this.host = host;
68
74
  }
69
- setCache(request, data, group) {
70
- this.cache[(0, md5_1.default)(request)] = {
75
+ setCache(key, data, group) {
76
+ this.cache[key] = {
71
77
  time: (new Date()).getTime(),
72
78
  data: data,
73
79
  group: group
74
80
  };
75
81
  }
76
- deleteCacheGroup(group) {
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
+ }
111
+ }
112
+ }
113
+ }
114
+ updateCacheGroup(group, data, fieldKey = 'id') {
115
+ const update = (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];
120
+ }
121
+ }
122
+ }
123
+ return cacheData;
124
+ };
77
125
  for (const key in this.cache) {
78
126
  if (this.cache[key].group === group) {
79
- delete this.cache[key];
127
+ if (Array.isArray(this.cache[key].data)) {
128
+ this.cache[key].data = Object.values(this.cache[key].data).map(update);
129
+ }
130
+ else {
131
+ this.cache[key].data = update(this.cache[key].data);
132
+ }
80
133
  }
81
134
  }
82
135
  }
83
- deleteCache(request) {
84
- const keyCache = (0, md5_1.default)(request);
85
- if (!this.cache[keyCache])
136
+ getCacheByIndex(index) {
137
+ return this.cache[index];
138
+ }
139
+ deleteCache(key) {
140
+ if (!this.cache[key])
86
141
  return;
87
- const group = this.cache[keyCache].group;
142
+ const group = this.cache[key].group;
88
143
  if (group) {
89
- this.deleteCacheGroup(group);
144
+ this.clearCacheGroup(group);
90
145
  }
91
- delete this.cache[keyCache];
146
+ delete this.cache[key];
92
147
  }
93
148
  setInitData(data) {
94
149
  this.initData = data;
package/lib/Http.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import Core from "./Core";
2
- import { TMethod, TParams } from "./types";
2
+ import { TMethod, TParams, TData } from "./types";
3
3
  declare class Http extends Core {
4
4
  private cacheIndex;
5
5
  method: TMethod;
@@ -12,6 +12,7 @@ declare class Http extends Core {
12
12
  private encodeUrlParams;
13
13
  private cuteUndifinedParams;
14
14
  request(): this;
15
- clearCache(): void;
15
+ updateCache(data: TData, fieldKey?: string | null): void;
16
+ clearCache(data?: TData, fieldKey?: string | null): void;
16
17
  }
17
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,8 +157,22 @@ class Http extends Core_1.default {
151
157
  }
152
158
  return this;
153
159
  }
154
- clearCache() {
155
- this.deleteCache(this.cacheIndex);
160
+ updateCache(data, fieldKey = 'id') {
161
+ const cache = this.getCacheByIndex(this.cacheIndex);
162
+ if (cache && cache.group) {
163
+ this.updateCacheGroup(cache.group, data, fieldKey);
164
+ }
165
+ }
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
+ }
156
176
  }
157
177
  }
158
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 deleteCacheGroup: (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/index.js CHANGED
@@ -162,11 +162,11 @@ Object.defineProperty(API, "setInitData", {
162
162
  writable: true,
163
163
  value: _a.instance.setInitData
164
164
  });
165
- Object.defineProperty(API, "deleteCacheGroup", {
165
+ Object.defineProperty(API, "clearCacheGroup", {
166
166
  enumerable: true,
167
167
  configurable: true,
168
168
  writable: true,
169
- value: _a.instance.deleteCacheGroup
169
+ value: _a.instance.clearCacheGroup
170
170
  });
171
171
  Object.defineProperty(API, "getToken", {
172
172
  enumerable: true,
package/lib/types.d.ts CHANGED
@@ -1,5 +1,10 @@
1
+ export type TCacheBody = {
2
+ data: any;
3
+ group?: string;
4
+ time: number;
5
+ };
1
6
  export type TCache = {
2
- [index: string]: any;
7
+ [index: string]: TCacheBody;
3
8
  };
4
9
  export type TData = {
5
10
  [index: string]: any;
@@ -8,10 +13,16 @@ export type TGroupsData = {
8
13
  [index: string]: TData;
9
14
  };
10
15
  export type TMethod = 'GET' | 'POST';
16
+ export type TCacheControll = string | {
17
+ group: string;
18
+ fieldKey: string;
19
+ };
11
20
  export type TParams = {
12
21
  path: string;
13
22
  data?: TData;
14
23
  cache?: string | boolean;
24
+ cacheUpdate?: TCacheControll;
25
+ cacheClear?: TCacheControll;
15
26
  globalName?: string;
16
27
  success?: (...args: any) => void;
17
28
  error?: (...args: any) => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "larapi",
3
- "version": "0.0.7",
3
+ "version": "0.0.9",
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
@@ -1,12 +1,10 @@
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
- // import ApiRequest from './ApiRequest';
8
5
  import {
9
6
  TCache,
7
+ TCacheBody,
10
8
  TData,
11
9
  TGroupsData,
12
10
  TListenerEvents
@@ -29,7 +27,7 @@ abstract class Core {
29
27
  this.setHost = this.setHost.bind(this);
30
28
  this.addListener = this.addListener.bind(this);
31
29
  this.setInitData = this.setInitData.bind(this);
32
- this.deleteCacheGroup = this.deleteCacheGroup.bind(this);
30
+ this.clearCacheGroup = this.clearCacheGroup.bind(this);
33
31
  Object.assign(this, context);
34
32
  }
35
33
 
@@ -37,8 +35,7 @@ abstract class Core {
37
35
  return this.events.addListener(event, callback);
38
36
  }
39
37
 
40
- protected getCache(request: string): TData | undefined {
41
- const key = md5(request);
38
+ protected getCache(key: string): TData | undefined {
42
39
  if (this.cache[key]) {
43
40
  if ((new Date()).getTime() <= (this.cache[key].time + 3600000000)) {
44
41
  return this.cache[key].data;
@@ -48,36 +45,97 @@ abstract class Core {
48
45
  }
49
46
  }
50
47
 
48
+ protected isJsonString(str: string) {
49
+ try {
50
+ JSON.parse(str);
51
+ } catch (e) {
52
+ return false;
53
+ }
54
+ return true;
55
+ }
56
+
51
57
  public setHost(host: string): void {
52
58
  this.host = host;
53
59
  }
54
60
 
55
- protected setCache(request: string, data: TData, group?: string): void {
56
- this.cache[md5(request)] = {
61
+ protected setCache(key: string, data: TData, group?: string): void {
62
+ this.cache[key] = {
57
63
  time: (new Date()).getTime(),
58
64
  data: data,
59
65
  group: group
60
66
  };
61
67
  }
62
68
 
63
- public deleteCacheGroup(group: string): void {
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
+ }
98
+ }
99
+ }
100
+ }
101
+
102
+ public updateCacheGroup(group: string, data: TData, fieldKey: string | null = 'id'): void {
103
+
104
+ const update = (cacheData: any) => {
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];
109
+ }
110
+ }
111
+ }
112
+ return cacheData;
113
+ };
114
+
64
115
  for(const key in this.cache) {
65
116
  if (this.cache[key].group === group) {
66
- delete this.cache[key];
117
+ if (Array.isArray(this.cache[key].data)) {
118
+ this.cache[key].data = Object.values(this.cache[key].data).map(update);
119
+ }else{
120
+ this.cache[key].data = update(this.cache[key].data);
121
+ }
67
122
  }
68
123
  }
69
124
  }
70
125
 
71
- protected deleteCache(request: string): void {
72
- const keyCache = md5(request);
73
- if (!this.cache[keyCache])
126
+ protected getCacheByIndex(index: string): TCacheBody {
127
+ return this.cache[index];
128
+ }
129
+
130
+ protected deleteCache(key: string): void {
131
+ if (!this.cache[key])
74
132
  return;
75
133
 
76
- const group = this.cache[keyCache].group;
134
+ const group = this.cache[key].group;
77
135
  if (group) {
78
- this.deleteCacheGroup(group);
136
+ this.clearCacheGroup(group);
79
137
  }
80
- delete this.cache[keyCache];
138
+ delete this.cache[key];
81
139
  }
82
140
 
83
141
  public setInitData(data: TGroupsData): void {
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,8 +172,22 @@ class Http extends Core {
160
172
  return this
161
173
  }
162
174
 
163
- public clearCache() {
164
- this.deleteCache(this.cacheIndex);
175
+ public updateCache(data: TData, fieldKey: string | null = 'id'): void {
176
+ const cache = this.getCacheByIndex(this.cacheIndex);
177
+ if (cache && cache.group) {
178
+ this.updateCacheGroup(cache.group, data, fieldKey);
179
+ }
180
+ }
181
+
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
+ }
165
191
  }
166
192
  }
167
193
 
package/src/index.ts CHANGED
@@ -116,7 +116,7 @@ class API extends Core {
116
116
  public static setToken = this.instance.setToken;
117
117
  public static addListener = this.instance.addListener;
118
118
  public static setInitData = this.instance.setInitData;
119
- public static deleteCacheGroup = this.instance.deleteCacheGroup;
119
+ public static clearCacheGroup = this.instance.clearCacheGroup;
120
120
  public static getToken = this.instance.getToken;
121
121
  public static getUid = this.instance.getUid;
122
122
  public static getUser = this.instance.getUser;
package/src/types.ts CHANGED
@@ -1,5 +1,11 @@
1
+ export type TCacheBody = {
2
+ data: any;
3
+ group?: string;
4
+ time: number;
5
+ }
6
+
1
7
  export type TCache = {
2
- [index: string]: any;
8
+ [index: string]: TCacheBody;
3
9
  }
4
10
 
5
11
  export type TData = {
@@ -12,10 +18,17 @@ export type TGroupsData = {
12
18
 
13
19
  export type TMethod = 'GET' | 'POST';
14
20
 
21
+ export type TCacheControll = string | {
22
+ group: string;
23
+ fieldKey: string;
24
+ };
25
+
15
26
  export type TParams = {
16
27
  path: string;
17
28
  data?: TData;
18
29
  cache?: string | boolean;
30
+ cacheUpdate?: TCacheControll;
31
+ cacheClear?: TCacheControll;
19
32
  globalName?: string;
20
33
  success?: (...args: any) => void;
21
34
  error?: (...args: any) => void;