larapi 0.0.9 → 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.js CHANGED
@@ -82,7 +82,7 @@ class Core {
82
82
  clearCacheGroup(group, data, fieldKey = 'id') {
83
83
  if (data) {
84
84
  const clear = (cacheData) => {
85
- if ((fieldKey && cacheData[fieldKey] === data[fieldKey]) || fieldKey === null) {
85
+ if ((fieldKey && (cacheData === null || cacheData === void 0 ? void 0 : cacheData[fieldKey]) === (data === null || data === void 0 ? void 0 : data[fieldKey])) || fieldKey === null) {
86
86
  for (const key in cacheData) {
87
87
  if (key in data) {
88
88
  return false;
@@ -113,6 +113,10 @@ class Core {
113
113
  }
114
114
  updateCacheGroup(group, data, fieldKey = 'id') {
115
115
  const update = (cacheData) => {
116
+ const retData = cacheData;
117
+ for (const key of fieldKey.split('.')) {
118
+ cacheData = cacheData[key];
119
+ }
116
120
  if ((fieldKey && cacheData[fieldKey] === data[fieldKey]) || fieldKey === null) {
117
121
  for (const key in cacheData) {
118
122
  if (key in data) {
@@ -120,7 +124,7 @@ class Core {
120
124
  }
121
125
  }
122
126
  }
123
- return cacheData;
127
+ return retData;
124
128
  };
125
129
  for (const key in this.cache) {
126
130
  if (this.cache[key].group === group) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "larapi",
3
- "version": "0.0.9",
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
@@ -70,7 +70,7 @@ abstract class Core {
70
70
  if (data) {
71
71
 
72
72
  const clear = (cacheData: any) => {
73
- if ((fieldKey && cacheData[fieldKey] === data[fieldKey]) || fieldKey === null) {
73
+ if ((fieldKey && cacheData?.[fieldKey] === data?.[fieldKey]) || fieldKey === null) {
74
74
  for(const key in cacheData) {
75
75
  if (key in data) {
76
76
  return false
@@ -80,6 +80,7 @@ abstract class Core {
80
80
  return true;
81
81
  };
82
82
 
83
+
83
84
  for(const key in this.cache) {
84
85
  if (this.cache[key].group === group) {
85
86
  if (Array.isArray(this.cache[key].data)) {
@@ -102,6 +103,10 @@ abstract class Core {
102
103
  public updateCacheGroup(group: string, data: TData, fieldKey: string | null = 'id'): void {
103
104
 
104
105
  const update = (cacheData: any) => {
106
+ const retData = cacheData;
107
+ for(const key of fieldKey!.split('.')) {
108
+ cacheData = cacheData[key]
109
+ }
105
110
  if ((fieldKey && cacheData[fieldKey] === data[fieldKey]) || fieldKey === null) {
106
111
  for(const key in cacheData) {
107
112
  if (key in data) {
@@ -109,7 +114,7 @@ abstract class Core {
109
114
  }
110
115
  }
111
116
  }
112
- return cacheData;
117
+ return retData;
113
118
  };
114
119
 
115
120
  for(const key in this.cache) {