larapi 0.0.7 → 0.0.8

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,13 @@ 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
12
  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;
13
+ protected setCache(key: string, data: TData, group?: string): void;
14
+ clearCacheGroup(group: string): void;
15
+ updateCacheGroup(group: string, fieldKey: string, data: TData): void;
16
+ protected getCacheByIndex(index: string): TCacheBody;
17
+ protected deleteCache(key: string): void;
16
18
  setInitData(data: TGroupsData): void;
17
19
  }
18
20
  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;
@@ -66,29 +63,53 @@ class Core {
66
63
  setHost(host) {
67
64
  this.host = host;
68
65
  }
69
- setCache(request, data, group) {
70
- this.cache[(0, md5_1.default)(request)] = {
66
+ setCache(key, data, group) {
67
+ this.cache[key] = {
71
68
  time: (new Date()).getTime(),
72
69
  data: data,
73
70
  group: group
74
71
  };
75
72
  }
76
- deleteCacheGroup(group) {
73
+ clearCacheGroup(group) {
77
74
  for (const key in this.cache) {
78
75
  if (this.cache[key].group === group) {
79
76
  delete this.cache[key];
80
77
  }
81
78
  }
82
79
  }
83
- deleteCache(request) {
84
- const keyCache = (0, md5_1.default)(request);
85
- if (!this.cache[keyCache])
80
+ updateCacheGroup(group, fieldKey, data) {
81
+ 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];
86
+ }
87
+ }
88
+ }
89
+ return cacheData;
90
+ };
91
+ for (const key in this.cache) {
92
+ if (this.cache[key].group === group) {
93
+ if (Array.isArray(this.cache[key].data)) {
94
+ this.cache[key].data = Object.values(this.cache[key].data).map(update);
95
+ }
96
+ else {
97
+ this.cache[key].data = update(this.cache[key].data);
98
+ }
99
+ }
100
+ }
101
+ }
102
+ getCacheByIndex(index) {
103
+ return this.cache[index];
104
+ }
105
+ deleteCache(key) {
106
+ if (!this.cache[key])
86
107
  return;
87
- const group = this.cache[keyCache].group;
108
+ const group = this.cache[key].group;
88
109
  if (group) {
89
- this.deleteCacheGroup(group);
110
+ this.clearCacheGroup(group);
90
111
  }
91
- delete this.cache[keyCache];
112
+ delete this.cache[key];
92
113
  }
93
114
  setInitData(data) {
94
115
  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
+ updateCache(fieldKey: string, data: TData): void;
15
16
  clearCache(): void;
16
17
  }
17
18
  export default Http;
package/lib/Http.js CHANGED
@@ -151,6 +151,12 @@ class Http extends Core_1.default {
151
151
  }
152
152
  return this;
153
153
  }
154
+ updateCache(fieldKey, data) {
155
+ const cache = this.getCacheByIndex(this.cacheIndex);
156
+ if (cache && cache.group) {
157
+ this.updateCacheGroup(cache.group, fieldKey, data);
158
+ }
159
+ }
154
160
  clearCache() {
155
161
  this.deleteCache(this.cacheIndex);
156
162
  }
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) => 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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "larapi",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
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;
@@ -52,15 +49,15 @@ abstract class Core {
52
49
  this.host = host;
53
50
  }
54
51
 
55
- protected setCache(request: string, data: TData, group?: string): void {
56
- this.cache[md5(request)] = {
52
+ protected setCache(key: string, data: TData, group?: string): void {
53
+ this.cache[key] = {
57
54
  time: (new Date()).getTime(),
58
55
  data: data,
59
56
  group: group
60
57
  };
61
58
  }
62
59
 
63
- public deleteCacheGroup(group: string): void {
60
+ public clearCacheGroup(group: string): void {
64
61
  for(const key in this.cache) {
65
62
  if (this.cache[key].group === group) {
66
63
  delete this.cache[key];
@@ -68,16 +65,43 @@ abstract class Core {
68
65
  }
69
66
  }
70
67
 
71
- protected deleteCache(request: string): void {
72
- const keyCache = md5(request);
73
- if (!this.cache[keyCache])
68
+ public updateCacheGroup(group: string, fieldKey: string, data: TData): void {
69
+
70
+ 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];
75
+ }
76
+ }
77
+ }
78
+ return cacheData;
79
+ };
80
+
81
+ for(const key in this.cache) {
82
+ if (this.cache[key].group === group) {
83
+ if (Array.isArray(this.cache[key].data)) {
84
+ this.cache[key].data = Object.values(this.cache[key].data).map(update);
85
+ }else{
86
+ this.cache[key].data = update(this.cache[key].data);
87
+ }
88
+ }
89
+ }
90
+ }
91
+
92
+ protected getCacheByIndex(index: string): TCacheBody {
93
+ return this.cache[index];
94
+ }
95
+
96
+ protected deleteCache(key: string): void {
97
+ if (!this.cache[key])
74
98
  return;
75
99
 
76
- const group = this.cache[keyCache].group;
100
+ const group = this.cache[key].group;
77
101
  if (group) {
78
- this.deleteCacheGroup(group);
102
+ this.clearCacheGroup(group);
79
103
  }
80
- delete this.cache[keyCache];
104
+ delete this.cache[key];
81
105
  }
82
106
 
83
107
  public setInitData(data: TGroupsData): void {
package/src/Http.ts CHANGED
@@ -160,7 +160,14 @@ class Http extends Core {
160
160
  return this
161
161
  }
162
162
 
163
- public clearCache() {
163
+ public updateCache(fieldKey: string, data: TData): void {
164
+ const cache = this.getCacheByIndex(this.cacheIndex);
165
+ if (cache && cache.group) {
166
+ this.updateCacheGroup(cache.group, fieldKey, data);
167
+ }
168
+ }
169
+
170
+ public clearCache(): void {
164
171
  this.deleteCache(this.cacheIndex);
165
172
  }
166
173
  }
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 = {