larapi 0.0.9 → 0.1.1

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 instance 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
@@ -5,7 +5,6 @@ declare abstract class Core {
5
5
  initData: TGroupsData;
6
6
  protected events: Events;
7
7
  host: string;
8
- token: string;
9
8
  constructor(context?: Core);
10
9
  addListener(event: TListenerEvents, callback: (data: any) => void): Event;
11
10
  protected getCache(key: string): TData | undefined;
package/lib/Core.js CHANGED
@@ -30,15 +30,8 @@ class Core {
30
30
  writable: true,
31
31
  value: void 0
32
32
  });
33
- Object.defineProperty(this, "token", {
34
- enumerable: true,
35
- configurable: true,
36
- writable: true,
37
- value: void 0
38
- });
39
33
  this.events = new easy_event_emitter_1.default();
40
34
  this.host = 'http://127.0.0.1/';
41
- this.token = '';
42
35
  this.cache = {};
43
36
  this.initData = {};
44
37
  this.setHost = this.setHost.bind(this);
@@ -82,7 +75,7 @@ class Core {
82
75
  clearCacheGroup(group, data, fieldKey = 'id') {
83
76
  if (data) {
84
77
  const clear = (cacheData) => {
85
- if ((fieldKey && cacheData[fieldKey] === data[fieldKey]) || fieldKey === null) {
78
+ if ((fieldKey && (cacheData === null || cacheData === void 0 ? void 0 : cacheData[fieldKey]) === (data === null || data === void 0 ? void 0 : data[fieldKey])) || fieldKey === null) {
86
79
  for (const key in cacheData) {
87
80
  if (key in data) {
88
81
  return false;
@@ -113,6 +106,10 @@ class Core {
113
106
  }
114
107
  updateCacheGroup(group, data, fieldKey = 'id') {
115
108
  const update = (cacheData) => {
109
+ const retData = cacheData;
110
+ for (const key of fieldKey.split('.')) {
111
+ cacheData = cacheData[key];
112
+ }
116
113
  if ((fieldKey && cacheData[fieldKey] === data[fieldKey]) || fieldKey === null) {
117
114
  for (const key in cacheData) {
118
115
  if (key in data) {
@@ -120,7 +117,7 @@ class Core {
120
117
  }
121
118
  }
122
119
  }
123
- return cacheData;
120
+ return retData;
124
121
  };
125
122
  for (const key in this.cache) {
126
123
  if (this.cache[key].group === group) {
package/lib/Http.d.ts CHANGED
@@ -1,10 +1,15 @@
1
1
  import Core from "./Core";
2
- import { TMethod, TParams, TData } from "./types";
2
+ import { TMethod, TParams, TData, TRequestParams } from "./types";
3
3
  declare class Http extends Core {
4
4
  private cacheIndex;
5
+ currentCache?: TData;
6
+ requestParams: TRequestParams;
5
7
  method: TMethod;
6
8
  params: TParams;
9
+ path: string;
7
10
  constructor(method: TMethod, params: TParams, context?: Core);
11
+ private initRequest;
12
+ private initCache;
8
13
  private success;
9
14
  private fail;
10
15
  private error;
@@ -12,6 +17,8 @@ declare class Http extends Core {
12
17
  private encodeUrlParams;
13
18
  private cuteUndifinedParams;
14
19
  request(): this;
20
+ addHeader(key: string, value: string): this;
21
+ deleteHeader(key: string): this;
15
22
  updateCache(data: TData, fieldKey?: string | null): void;
16
23
  clearCache(data?: TData, fieldKey?: string | null): void;
17
24
  }
package/lib/Http.js CHANGED
@@ -15,6 +15,18 @@ class Http extends Core_1.default {
15
15
  writable: true,
16
16
  value: void 0
17
17
  });
18
+ Object.defineProperty(this, "currentCache", {
19
+ enumerable: true,
20
+ configurable: true,
21
+ writable: true,
22
+ value: void 0
23
+ });
24
+ Object.defineProperty(this, "requestParams", {
25
+ enumerable: true,
26
+ configurable: true,
27
+ writable: true,
28
+ value: void 0
29
+ });
18
30
  Object.defineProperty(this, "method", {
19
31
  enumerable: true,
20
32
  configurable: true,
@@ -27,14 +39,63 @@ class Http extends Core_1.default {
27
39
  writable: true,
28
40
  value: void 0
29
41
  });
42
+ Object.defineProperty(this, "path", {
43
+ enumerable: true,
44
+ configurable: true,
45
+ writable: true,
46
+ value: void 0
47
+ });
30
48
  this.cacheIndex = '';
31
49
  this.method = method;
32
50
  this.params = params;
51
+ this.path = this.params.path;
52
+ this.requestParams = {
53
+ method: this.method
54
+ };
33
55
  this.success = this.success.bind(this);
34
56
  this.fail = this.fail.bind(this);
35
57
  this.error = this.error.bind(this);
36
58
  this.complete = this.complete.bind(this);
37
59
  this.request = this.request.bind(this);
60
+ this.addListener = this.addListener.bind(this);
61
+ this.addHeader = this.addHeader.bind(this);
62
+ this.deleteHeader = this.deleteHeader.bind(this);
63
+ this.initRequest();
64
+ this.initCache();
65
+ }
66
+ initRequest() {
67
+ this.requestParams.headers = {
68
+ 'Accept': 'application/json',
69
+ 'Content-Type': 'application/json'
70
+ };
71
+ this.params.data = this.params.data ? this.cuteUndifinedParams(this.params.data) : this.params.data;
72
+ if (this.method === 'GET') {
73
+ if (this.params.data)
74
+ this.path = this.params.path + '?' + this.encodeUrlParams(this.params.data);
75
+ }
76
+ if (this.method === 'POST') {
77
+ this.requestParams.body = JSON.stringify(this.params.data);
78
+ }
79
+ }
80
+ initCache() {
81
+ var _a;
82
+ this.cacheIndex = (0, md5_1.default)(this.path + JSON.stringify(this.params.data));
83
+ if (this.params.globalName) {
84
+ if (this.initData[this.params.globalName]) {
85
+ this.setCache(this.cacheIndex, this.initData[this.params.globalName], (typeof this.params.cache === 'boolean' ? undefined : this.params.cache));
86
+ this.params.cache = (_a = this.params.cache) !== null && _a !== void 0 ? _a : true;
87
+ delete this.initData[this.params.globalName];
88
+ }
89
+ }
90
+ if (this.params.cache) {
91
+ if (this.currentCache = this.getCache(this.cacheIndex)) {
92
+ this.success(this.currentCache);
93
+ this.complete(this.currentCache);
94
+ }
95
+ }
96
+ else {
97
+ this.deleteCache(this.cacheIndex);
98
+ }
38
99
  }
39
100
  //Заголовки от 200 до 299
40
101
  success(...args) {
@@ -84,76 +145,48 @@ class Http extends Core_1.default {
84
145
  return data;
85
146
  }
86
147
  request() {
87
- var _a;
88
- if (typeof XMLHttpRequest === 'undefined')
148
+ if (typeof XMLHttpRequest === 'undefined' || this.currentCache)
89
149
  return this;
90
- var path = this.params.path;
91
- const fetchParams = {
92
- method: this.method
93
- };
94
- fetchParams.headers = {
95
- 'Accept': 'application/json',
96
- 'Content-Type': 'application/json',
97
- 'Authorization': 'Bearer ' + this.token
98
- };
99
- this.params.data = this.params.data ? this.cuteUndifinedParams(this.params.data) : this.params.data;
100
- if (this.method === 'POST') {
101
- fetchParams.body = JSON.stringify(this.params.data);
102
- }
103
- if (this.method === 'GET') {
104
- if (this.params.data)
105
- path = this.params.path + '?' + this.encodeUrlParams(this.params.data);
106
- }
107
- this.cacheIndex = (0, md5_1.default)(path + JSON.stringify(this.params.data));
108
- var dataCache;
109
- if (this.params.globalName) {
110
- if (this.initData[this.params.globalName]) {
111
- this.setCache(this.cacheIndex, this.initData[this.params.globalName], (typeof this.params.cache === 'boolean' ? undefined : this.params.cache));
112
- this.params.cache = (_a = this.params.cache) !== null && _a !== void 0 ? _a : true;
113
- delete this.initData[this.params.globalName];
150
+ try {
151
+ const xhr = new XMLHttpRequest();
152
+ xhr.open(this.method, this.host + this.path, true);
153
+ for (const header in this.requestParams.headers) {
154
+ xhr.setRequestHeader(header, this.requestParams.headers[header]);
114
155
  }
115
- }
116
- if (this.params.cache) {
117
- if (dataCache = this.getCache(this.cacheIndex)) {
118
- this.success(dataCache);
119
- this.complete(dataCache);
120
- }
121
- }
122
- else {
123
- this.deleteCache(this.cacheIndex);
124
- }
125
- if (!dataCache) {
126
- try {
127
- const xhr = new XMLHttpRequest();
128
- xhr.open(this.method, this.host + path, true);
129
- for (const header in fetchParams.headers) {
130
- xhr.setRequestHeader(header, fetchParams.headers[header]);
156
+ xhr.onreadystatechange = () => {
157
+ if (xhr.readyState !== 4) {
158
+ return;
131
159
  }
132
- xhr.onreadystatechange = () => {
133
- if (xhr.readyState !== 4) {
134
- return;
160
+ const result = this.isJsonString(xhr.responseText) ? JSON.parse(xhr.responseText) : {};
161
+ if (xhr.status >= 200 && xhr.status <= 299) {
162
+ if (this.params.cache) {
163
+ this.setCache(this.cacheIndex, result, (typeof this.params.cache === 'boolean' ? undefined : this.params.cache));
135
164
  }
136
- const result = this.isJsonString(xhr.responseText) ? JSON.parse(xhr.responseText) : {};
137
- if (xhr.status >= 200 && xhr.status <= 299) {
138
- if (this.params.cache) {
139
- this.setCache(this.cacheIndex, result, (typeof this.params.cache === 'boolean' ? undefined : this.params.cache));
140
- }
141
- this.success(result);
142
- }
143
- else {
144
- if (xhr.status >= 400 && xhr.status <= 499) {
145
- fail(result);
146
- }
147
- this.error(result);
148
- console.warn(result);
165
+ this.success(result);
166
+ }
167
+ else {
168
+ if (xhr.status >= 400 && xhr.status <= 499) {
169
+ fail(result);
149
170
  }
150
- this.complete(result);
151
- };
152
- xhr.send(fetchParams.body);
153
- }
154
- catch (e) {
155
- console.warn(e);
156
- }
171
+ this.error(result);
172
+ console.warn(result);
173
+ }
174
+ this.complete(result);
175
+ };
176
+ xhr.send(this.requestParams.body);
177
+ }
178
+ catch (e) {
179
+ console.warn(e);
180
+ }
181
+ return this;
182
+ }
183
+ addHeader(key, value) {
184
+ this.requestParams.headers = Object.assign(Object.assign({}, this.requestParams.headers), { [key]: value });
185
+ return this;
186
+ }
187
+ deleteHeader(key) {
188
+ if (this.requestParams.headers && (key in this.requestParams.headers)) {
189
+ delete this.requestParams.headers[key];
157
190
  }
158
191
  return this;
159
192
  }
package/lib/index.d.ts CHANGED
@@ -10,13 +10,14 @@ type TConfig = {
10
10
  declare class API extends Core {
11
11
  private static instance;
12
12
  private _user;
13
+ token?: string;
13
14
  constructor(config: TConfig);
14
15
  http(method: TMethod, params: TParams): Http;
15
16
  get(params: TParams): Http;
16
17
  post(params: TParams): Http;
17
18
  private init;
18
19
  setToken(token: string): void;
19
- getToken(): string;
20
+ getToken(): string | undefined;
20
21
  getUid(): number;
21
22
  getUser(): TUser;
22
23
  get uid(): number;
@@ -35,7 +36,7 @@ declare class API extends Core {
35
36
  static addListener: (event: import("./types").TListenerEvents, callback: (data: any) => void) => import("easy-event-emitter").Event;
36
37
  static setInitData: (data: import("./types").TGroupsData) => void;
37
38
  static clearCacheGroup: (group: string, data?: import("./types").TData | undefined, fieldKey?: string | null) => void;
38
- static getToken: () => string;
39
+ static getToken: () => string | undefined;
39
40
  static getUid: () => number;
40
41
  static getUser: () => TUser;
41
42
  static user: TUser;
package/lib/index.js CHANGED
@@ -15,6 +15,12 @@ class API extends Core_1.default {
15
15
  writable: true,
16
16
  value: void 0
17
17
  });
18
+ Object.defineProperty(this, "token", {
19
+ enumerable: true,
20
+ configurable: true,
21
+ writable: true,
22
+ value: void 0
23
+ });
18
24
  this.setHost(config.host);
19
25
  this._user = {};
20
26
  this.init();
@@ -30,7 +36,11 @@ class API extends Core_1.default {
30
36
  this.getUser = this.getUser.bind(this);
31
37
  }
32
38
  http(method, params) {
33
- return new Http_1.default(method, params, this);
39
+ const http = new Http_1.default(method, params, this);
40
+ if (this.token) {
41
+ http.addHeader('Authorization', 'Bearer ' + this.token);
42
+ }
43
+ return http;
34
44
  }
35
45
  get(params) {
36
46
  return this.http('GET', params).request();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "larapi",
3
- "version": "0.0.9",
3
+ "version": "0.1.1",
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
@@ -15,12 +15,11 @@ abstract class Core {
15
15
  initData: TGroupsData;
16
16
  protected events: Events;
17
17
  host: string;
18
- token: string;
18
+
19
19
 
20
20
  constructor(context?: Core) {
21
21
  this.events = new EventEmitter();
22
22
  this.host = 'http://127.0.0.1/';
23
- this.token = '';
24
23
  this.cache = {};
25
24
  this.initData = {};
26
25
 
@@ -70,7 +69,7 @@ abstract class Core {
70
69
  if (data) {
71
70
 
72
71
  const clear = (cacheData: any) => {
73
- if ((fieldKey && cacheData[fieldKey] === data[fieldKey]) || fieldKey === null) {
72
+ if ((fieldKey && cacheData?.[fieldKey] === data?.[fieldKey]) || fieldKey === null) {
74
73
  for(const key in cacheData) {
75
74
  if (key in data) {
76
75
  return false
@@ -80,6 +79,7 @@ abstract class Core {
80
79
  return true;
81
80
  };
82
81
 
82
+
83
83
  for(const key in this.cache) {
84
84
  if (this.cache[key].group === group) {
85
85
  if (Array.isArray(this.cache[key].data)) {
@@ -102,6 +102,10 @@ abstract class Core {
102
102
  public updateCacheGroup(group: string, data: TData, fieldKey: string | null = 'id'): void {
103
103
 
104
104
  const update = (cacheData: any) => {
105
+ const retData = cacheData;
106
+ for(const key of fieldKey!.split('.')) {
107
+ cacheData = cacheData[key]
108
+ }
105
109
  if ((fieldKey && cacheData[fieldKey] === data[fieldKey]) || fieldKey === null) {
106
110
  for(const key in cacheData) {
107
111
  if (key in data) {
@@ -109,7 +113,7 @@ abstract class Core {
109
113
  }
110
114
  }
111
115
  }
112
- return cacheData;
116
+ return retData;
113
117
  };
114
118
 
115
119
  for(const key in this.cache) {
package/src/Http.ts CHANGED
@@ -10,20 +10,71 @@ import {
10
10
 
11
11
  class Http extends Core {
12
12
  private cacheIndex: string;
13
+ currentCache?: TData;
14
+ requestParams: TRequestParams;
13
15
  method: TMethod;
14
16
  params: TParams;
17
+ path: string;
15
18
 
16
19
  constructor(method: TMethod, params: TParams, context?: Core) {
17
20
  super(context);
18
21
  this.cacheIndex = '';
19
22
  this.method = method;
20
23
  this.params = params;
21
-
24
+ this.path = this.params.path;
25
+ this.requestParams = {
26
+ method: this.method
27
+ };
28
+
22
29
  this.success = this.success.bind(this);
23
30
  this.fail = this.fail.bind(this);
24
31
  this.error = this.error.bind(this);
25
32
  this.complete = this.complete.bind(this);
26
33
  this.request = this.request.bind(this);
34
+ this.addListener = this.addListener.bind(this);
35
+ this.addHeader = this.addHeader.bind(this);
36
+ this.deleteHeader = this.deleteHeader.bind(this);
37
+
38
+ this.initRequest();
39
+ this.initCache();
40
+ }
41
+
42
+ private initRequest(): void {
43
+ this.requestParams.headers = {
44
+ 'Accept': 'application/json',
45
+ 'Content-Type': 'application/json'
46
+ };
47
+
48
+ this.params.data = this.params.data ? this.cuteUndifinedParams(this.params.data) : this.params.data;
49
+
50
+ if (this.method === 'GET') {
51
+ if (this.params.data)
52
+ this.path = this.params.path + '?' + this.encodeUrlParams(this.params.data);
53
+ }
54
+
55
+ if (this.method === 'POST') {
56
+ this.requestParams.body = JSON.stringify(this.params.data);
57
+ }
58
+ }
59
+
60
+ private initCache(): void {
61
+ this.cacheIndex = md5(this.path + JSON.stringify(this.params.data));
62
+ if (this.params.globalName) {
63
+ if (this.initData[this.params.globalName]) {
64
+ this.setCache(this.cacheIndex, this.initData[this.params.globalName], (typeof this.params.cache === 'boolean' ? undefined : this.params.cache));
65
+ this.params.cache = this.params.cache ?? true;
66
+ delete this.initData[this.params.globalName];
67
+ }
68
+ }
69
+
70
+ if (this.params.cache) {
71
+ if (this.currentCache = this.getCache(this.cacheIndex)) {
72
+ this.success(this.currentCache);
73
+ this.complete(this.currentCache);
74
+ }
75
+ }else{
76
+ this.deleteCache(this.cacheIndex);
77
+ }
27
78
  }
28
79
 
29
80
  //Заголовки от 200 до 299
@@ -36,6 +87,7 @@ class Http extends Core {
36
87
  this.params.data,
37
88
  typeof this.params.cacheUpdate === 'string' ? undefined : this.params.cacheUpdate.fieldKey);
38
89
  }
90
+
39
91
  if (this.params.cacheClear && this.params.data) {
40
92
  this.clearCacheGroup(
41
93
  typeof this.params.cacheClear === 'string' ? this.params.cacheClear : this.params.cacheClear.group,
@@ -89,89 +141,57 @@ class Http extends Core {
89
141
  }
90
142
 
91
143
  public request(): this {
92
- if (typeof XMLHttpRequest === 'undefined') return this;
93
-
94
- var path = this.params.path;
95
- const fetchParams: TRequestParams = {
96
- method: this.method
97
- };
98
-
99
- fetchParams.headers = {
100
- 'Accept': 'application/json',
101
- 'Content-Type': 'application/json',
102
- 'Authorization': 'Bearer ' + this.token
103
- };
104
-
105
- this.params.data = this.params.data ? this.cuteUndifinedParams(this.params.data) : this.params.data;
106
-
107
- if (this.method === 'POST') {
108
- fetchParams.body = JSON.stringify(this.params.data);
109
- }
110
-
111
- if (this.method === 'GET') {
112
- if (this.params.data)
113
- path = this.params.path + '?' + this.encodeUrlParams(this.params.data);
114
- }
115
-
116
- this.cacheIndex = md5(path + JSON.stringify(this.params.data));
117
-
118
- var dataCache;
144
+ if (typeof XMLHttpRequest === 'undefined' || this.currentCache) return this;
145
+
146
+ try {
147
+ const xhr = new XMLHttpRequest();
119
148
 
120
- if (this.params.globalName) {
121
- if (this.initData[this.params.globalName]) {
122
- this.setCache(this.cacheIndex, this.initData[this.params.globalName], (typeof this.params.cache === 'boolean' ? undefined : this.params.cache));
123
- this.params.cache = this.params.cache ?? true;
124
- delete this.initData[this.params.globalName];
149
+ xhr.open(this.method, this.host + this.path, true);
150
+ for(const header in this.requestParams.headers) {
151
+ xhr.setRequestHeader(header, this.requestParams.headers[header]);
125
152
  }
126
- }
127
-
128
- if (this.params.cache) {
129
- if (dataCache = this.getCache(this.cacheIndex)) {
130
- this.success(dataCache);
131
- this.complete(dataCache);
132
- }
133
- }else{
134
- this.deleteCache(this.cacheIndex);
135
- }
136
- if (!dataCache) {
137
- try {
138
- const xhr = new XMLHttpRequest();
139
153
 
140
- xhr.open(this.method, this.host + path, true);
141
- for(const header in fetchParams.headers) {
142
- xhr.setRequestHeader(header, fetchParams.headers[header]);
154
+ xhr.onreadystatechange = () => {
155
+ if (xhr.readyState !== 4) {
156
+ return;
143
157
  }
144
-
145
- xhr.onreadystatechange = () => {
146
- if (xhr.readyState !== 4) {
147
- return;
158
+
159
+ const result = this.isJsonString(xhr.responseText) ? JSON.parse(xhr.responseText) : {};
160
+
161
+ if (xhr.status >= 200 && xhr.status <= 299) {
162
+ if (this.params.cache) {
163
+ this.setCache(this.cacheIndex, result, (typeof this.params.cache === 'boolean' ? undefined : this.params.cache));
148
164
  }
149
-
150
- const result = this.isJsonString(xhr.responseText) ? JSON.parse(xhr.responseText) : {};
151
-
152
- if (xhr.status >= 200 && xhr.status <= 299) {
153
- if (this.params.cache) {
154
- this.setCache(this.cacheIndex, result, (typeof this.params.cache === 'boolean' ? undefined : this.params.cache));
155
- }
156
- this.success(result);
157
- }else{
158
- if (xhr.status >= 400 && xhr.status <= 499) {
159
- fail(result);
160
- }
161
-
162
- this.error(result);
163
- console.warn(result);
165
+ this.success(result);
166
+ }else{
167
+ if (xhr.status >= 400 && xhr.status <= 499) {
168
+ fail(result);
164
169
  }
165
- this.complete(result);
170
+
171
+ this.error(result);
172
+ console.warn(result);
166
173
  }
167
- xhr.send(fetchParams.body);
168
- }catch(e) {
169
- console.warn(e);
174
+ this.complete(result);
170
175
  }
176
+ xhr.send(this.requestParams.body);
177
+ }catch(e) {
178
+ console.warn(e);
171
179
  }
172
180
  return this
173
181
  }
174
182
 
183
+ public addHeader(key: string, value: string): this {
184
+ this.requestParams.headers = {...this.requestParams.headers, [key]: value};
185
+ return this;
186
+ }
187
+
188
+ public deleteHeader(key: string): this {
189
+ if (this.requestParams.headers && (key in this.requestParams.headers)) {
190
+ delete this.requestParams.headers[key];
191
+ }
192
+ return this;
193
+ }
194
+
175
195
  public updateCache(data: TData, fieldKey: string | null = 'id'): void {
176
196
  const cache = this.getCacheByIndex(this.cacheIndex);
177
197
  if (cache && cache.group) {
package/src/index.ts CHANGED
@@ -15,6 +15,7 @@ class API extends Core {
15
15
  host: ''
16
16
  });
17
17
  private _user: TUser;
18
+ token?: string;
18
19
 
19
20
  constructor(config: TConfig) {
20
21
  super();
@@ -35,7 +36,11 @@ class API extends Core {
35
36
  }
36
37
 
37
38
  public http(method: TMethod, params: TParams): Http {
38
- return new Http(method, params, this);
39
+ const http = new Http(method, params, this);
40
+ if (this.token) {
41
+ http.addHeader('Authorization', 'Bearer ' + this.token);
42
+ }
43
+ return http;
39
44
  }
40
45
 
41
46
  public get(params: TParams): Http {