larapi 0.1.2 → 0.1.4

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,12 +1,11 @@
1
- import { Events, Event } from 'easy-event-emitter';
2
- import { TCacheBody, TData, TGroupsData, TListenerEvents } from './types';
1
+ import { Events } from 'easy-event-emitter';
2
+ import type { TCacheBody, TData, TGroupsData } from './types';
3
3
  declare abstract class Core {
4
4
  private cache;
5
5
  initData: TGroupsData;
6
6
  protected events: Events;
7
7
  host: string;
8
8
  constructor(context?: Core);
9
- addListener(event: TListenerEvents, callback: (data: any) => void): Event;
10
9
  protected getCache(key: string): TData | undefined;
11
10
  protected isJsonString(str: string): boolean;
12
11
  setHost(host: string): void;
package/lib/Core.js CHANGED
@@ -35,14 +35,10 @@ class Core {
35
35
  this.cache = {};
36
36
  this.initData = {};
37
37
  this.setHost = this.setHost.bind(this);
38
- this.addListener = this.addListener.bind(this);
39
38
  this.setInitData = this.setInitData.bind(this);
40
39
  this.clearCacheGroup = this.clearCacheGroup.bind(this);
41
40
  Object.assign(this, context);
42
41
  }
43
- addListener(event, callback) {
44
- return this.events.addListener(event, callback);
45
- }
46
42
  getCache(key) {
47
43
  if (this.cache[key]) {
48
44
  if ((new Date()).getTime() <= (this.cache[key].time + 3600000000)) {
package/lib/Http.d.ts CHANGED
@@ -1,8 +1,10 @@
1
1
  import Core from "./Core";
2
- import { TMethod, TParams, TData, TRequestParams } from "./types";
2
+ import { Event, Events } from "easy-event-emitter";
3
+ import type { TMethod, TParams, TData, TRequestParams, TListenerEvents } from "./types";
3
4
  declare class Http extends Core {
4
5
  private cacheIndex;
5
6
  currentCache?: TData;
7
+ currentEvents: Events;
6
8
  requestParams: TRequestParams;
7
9
  method: TMethod;
8
10
  params: TParams;
@@ -10,14 +12,16 @@ declare class Http extends Core {
10
12
  constructor(method: TMethod, params: TParams, context?: Core);
11
13
  private initRequest;
12
14
  private initCache;
13
- private success;
14
- private fail;
15
- private error;
16
- private complete;
15
+ private setEmit;
16
+ success(...args: any): void;
17
+ fail(...args: any): void;
18
+ error(...args: any): void;
19
+ complete(...args: any): void;
17
20
  private encodeUrlParams;
18
21
  private cuteUndifinedParams;
19
22
  request(): this;
20
23
  addHeader(key: string, value: string): this;
24
+ addListener(event: TListenerEvents, callback: (data: any) => void): Event;
21
25
  deleteHeader(key: string): this;
22
26
  updateCache(data: TData, fieldKey?: string | null): void;
23
27
  clearCache(data?: TData, fieldKey?: string | null): void;
package/lib/Http.js CHANGED
@@ -6,6 +6,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  //@ts-ignore
7
7
  const md5_1 = __importDefault(require("md5"));
8
8
  const Core_1 = __importDefault(require("./Core"));
9
+ const Queue_1 = __importDefault(require("./Queue"));
10
+ const easy_event_emitter_1 = __importDefault(require("easy-event-emitter"));
9
11
  class Http extends Core_1.default {
10
12
  constructor(method, params, context) {
11
13
  super(context);
@@ -21,6 +23,12 @@ class Http extends Core_1.default {
21
23
  writable: true,
22
24
  value: void 0
23
25
  });
26
+ Object.defineProperty(this, "currentEvents", {
27
+ enumerable: true,
28
+ configurable: true,
29
+ writable: true,
30
+ value: void 0
31
+ });
24
32
  Object.defineProperty(this, "requestParams", {
25
33
  enumerable: true,
26
34
  configurable: true,
@@ -45,6 +53,7 @@ class Http extends Core_1.default {
45
53
  writable: true,
46
54
  value: void 0
47
55
  });
56
+ this.currentEvents = new easy_event_emitter_1.default();
48
57
  this.cacheIndex = '';
49
58
  this.method = method;
50
59
  this.params = params;
@@ -56,8 +65,8 @@ class Http extends Core_1.default {
56
65
  this.fail = this.fail.bind(this);
57
66
  this.error = this.error.bind(this);
58
67
  this.complete = this.complete.bind(this);
68
+ this.setEmit = this.setEmit.bind(this);
59
69
  this.request = this.request.bind(this);
60
- this.addListener = this.addListener.bind(this);
61
70
  this.addHeader = this.addHeader.bind(this);
62
71
  this.deleteHeader = this.deleteHeader.bind(this);
63
72
  this.initRequest();
@@ -69,11 +78,11 @@ class Http extends Core_1.default {
69
78
  'Content-Type': 'application/json'
70
79
  };
71
80
  this.params.data = this.params.data ? this.cuteUndifinedParams(this.params.data) : this.params.data;
72
- if (this.method === 'GET') {
81
+ if (this.method === 'GET' || this.method === 'HEAD') {
73
82
  if (this.params.data)
74
83
  this.path = this.params.path + '?' + this.encodeUrlParams(this.params.data);
75
84
  }
76
- if (this.method === 'POST') {
85
+ if (this.method === 'POST' || this.method === 'PUT') {
77
86
  this.requestParams.body = JSON.stringify(this.params.data);
78
87
  }
79
88
  }
@@ -97,6 +106,13 @@ class Http extends Core_1.default {
97
106
  this.deleteCache(this.cacheIndex);
98
107
  }
99
108
  }
109
+ setEmit(event, args) {
110
+ this.currentEvents.emit(event, args[0]);
111
+ this.events.emit(event, {
112
+ cacheIndex: this.cacheIndex,
113
+ args: args[0]
114
+ });
115
+ }
100
116
  //Заголовки от 200 до 299
101
117
  success(...args) {
102
118
  if (typeof this.params.success === 'function')
@@ -107,26 +123,27 @@ class Http extends Core_1.default {
107
123
  if (this.params.cacheClear && this.params.data) {
108
124
  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);
109
125
  }
126
+ this.setEmit('api-request-success', args);
110
127
  }
111
128
  //Заголовки от 400 до 499
112
129
  fail(...args) {
113
130
  if (typeof this.params.fail === 'function') {
114
131
  this.params.fail(...args);
115
132
  }
116
- this.events.emit('api-request-fail', args);
133
+ this.setEmit('api-request-fail', args);
117
134
  }
118
135
  //Все остальные
119
136
  error(...args) {
120
137
  if (typeof this.params.error === 'function') {
121
138
  this.params.error(...args);
122
139
  }
123
- this.events.emit('api-request-error', args);
140
+ this.setEmit('api-request-error', args);
124
141
  }
125
142
  complete(...args) {
126
143
  if (typeof this.params.complete === 'function') {
127
144
  this.params.complete(...args);
128
145
  }
129
- this.events.emit('api-request-complete', args);
146
+ this.setEmit('api-request-complete', args);
130
147
  }
131
148
  encodeUrlParams(data) {
132
149
  return Object.keys(data).map(function (k) {
@@ -145,7 +162,7 @@ class Http extends Core_1.default {
145
162
  return data;
146
163
  }
147
164
  request() {
148
- if (typeof XMLHttpRequest === 'undefined' || this.currentCache)
165
+ if (typeof XMLHttpRequest === 'undefined' || this.currentCache || Queue_1.default.push(this.cacheIndex, this))
149
166
  return this;
150
167
  try {
151
168
  const xhr = new XMLHttpRequest();
@@ -185,6 +202,9 @@ class Http extends Core_1.default {
185
202
  this.requestParams.headers = Object.assign(Object.assign({}, this.requestParams.headers), { [key]: value });
186
203
  return this;
187
204
  }
205
+ addListener(event, callback) {
206
+ return this.currentEvents.addListener(event, callback);
207
+ }
188
208
  deleteHeader(key) {
189
209
  if (this.requestParams.headers && (key in this.requestParams.headers)) {
190
210
  delete this.requestParams.headers[key];
package/lib/Queue.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ import Http from "./Http";
2
+ declare class Queue {
3
+ private static instance;
4
+ private queue;
5
+ constructor();
6
+ has(request: string): boolean;
7
+ get(request: string): Http | undefined;
8
+ push(request: string, instance: Http): boolean;
9
+ static has: (request: string) => boolean;
10
+ static get: (request: string) => Http | undefined;
11
+ static push: (request: string, instance: Http) => boolean;
12
+ }
13
+ export default Queue;
package/lib/Queue.js ADDED
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ var _a;
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ const Http_1 = __importDefault(require("./Http"));
8
+ class Queue {
9
+ constructor() {
10
+ Object.defineProperty(this, "queue", {
11
+ enumerable: true,
12
+ configurable: true,
13
+ writable: true,
14
+ value: {}
15
+ });
16
+ this.has = this.has.bind(this);
17
+ this.get = this.get.bind(this);
18
+ this.push = this.push.bind(this);
19
+ }
20
+ has(request) {
21
+ return request in this.queue;
22
+ }
23
+ get(request) {
24
+ return this.has(request) ? this.queue[request] : undefined;
25
+ }
26
+ push(request, instance) {
27
+ const refInstance = this.get(request);
28
+ if (!refInstance) {
29
+ this.queue[request] = instance;
30
+ }
31
+ else {
32
+ refInstance === null || refInstance === void 0 ? void 0 : refInstance.addListener('api-request-success', instance.success);
33
+ refInstance === null || refInstance === void 0 ? void 0 : refInstance.addListener('api-request-error', instance.error);
34
+ refInstance === null || refInstance === void 0 ? void 0 : refInstance.addListener('api-request-fail', instance.fail);
35
+ refInstance === null || refInstance === void 0 ? void 0 : refInstance.addListener('api-request-complete', instance.complete);
36
+ }
37
+ return refInstance instanceof Http_1.default;
38
+ }
39
+ }
40
+ _a = Queue;
41
+ Object.defineProperty(Queue, "instance", {
42
+ enumerable: true,
43
+ configurable: true,
44
+ writable: true,
45
+ value: new Queue
46
+ });
47
+ Object.defineProperty(Queue, "has", {
48
+ enumerable: true,
49
+ configurable: true,
50
+ writable: true,
51
+ value: _a.instance.has
52
+ });
53
+ Object.defineProperty(Queue, "get", {
54
+ enumerable: true,
55
+ configurable: true,
56
+ writable: true,
57
+ value: _a.instance.get
58
+ });
59
+ Object.defineProperty(Queue, "push", {
60
+ enumerable: true,
61
+ configurable: true,
62
+ writable: true,
63
+ value: _a.instance.push
64
+ });
65
+ exports.default = Queue;
package/lib/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import Core from "./Core";
2
- import { TMethod, TParams } from "./types";
2
+ import type { TMethod, TParams, TListenerEvents } from "./types";
3
+ import type { Event } from "easy-event-emitter";
3
4
  import Http from "./Http";
4
5
  type TUser = {
5
6
  [index: string]: any;
@@ -15,7 +16,12 @@ declare class API extends Core {
15
16
  http(method: TMethod, params: TParams): Http;
16
17
  get(params: TParams): Http;
17
18
  post(params: TParams): Http;
19
+ put(params: TParams): Http;
20
+ patch(params: TParams): Http;
21
+ delete(params: TParams): Http;
22
+ options(params: TParams): Http;
18
23
  private init;
24
+ addListener(event: TListenerEvents, callback: (data: any) => void): Event;
19
25
  setToken(token: string): void;
20
26
  getToken(): string | undefined;
21
27
  getUid(): number;
@@ -33,7 +39,7 @@ declare class API extends Core {
33
39
  static updateUser: (user: TUser) => void;
34
40
  static logout: () => void;
35
41
  static setToken: (token: string) => void;
36
- static addListener: (event: import("./types").TListenerEvents, callback: (data: any) => void) => import("easy-event-emitter").Event;
42
+ static addListener: (event: TListenerEvents, callback: (data: any) => void) => Event;
37
43
  static setInitData: (data: import("./types").TGroupsData) => void;
38
44
  static clearCacheGroup: (group: string, data?: import("./types").TData | undefined, fieldKey?: string | null) => void;
39
45
  static getToken: () => string | undefined;
@@ -41,5 +47,9 @@ declare class API extends Core {
41
47
  static getUser: () => TUser;
42
48
  static user: TUser;
43
49
  static uid: number;
50
+ /**
51
+ * @deprecated The method should not be used
52
+ */
53
+ static deleteCacheGroup: (group: string, data?: import("./types").TData | undefined, fieldKey?: string | null) => void;
44
54
  }
45
55
  export default API;
package/lib/index.js CHANGED
@@ -32,6 +32,10 @@ class API extends Core_1.default {
32
32
  this.http = this.http.bind(this);
33
33
  this.get = this.get.bind(this);
34
34
  this.post = this.post.bind(this);
35
+ this.put = this.put.bind(this);
36
+ this.patch = this.patch.bind(this);
37
+ this.delete = this.delete.bind(this);
38
+ this.options = this.options.bind(this);
35
39
  this.getUid = this.getUid.bind(this);
36
40
  this.getUser = this.getUser.bind(this);
37
41
  }
@@ -48,6 +52,18 @@ class API extends Core_1.default {
48
52
  post(params) {
49
53
  return this.http('POST', params).request();
50
54
  }
55
+ put(params) {
56
+ return this.http('PUT', params).request();
57
+ }
58
+ patch(params) {
59
+ return this.http('PATCH', params).request();
60
+ }
61
+ delete(params) {
62
+ return this.http('DELETE', params).request();
63
+ }
64
+ options(params) {
65
+ return this.http('OPTIONS', params).request();
66
+ }
51
67
  init() {
52
68
  if (typeof window === 'undefined')
53
69
  return;
@@ -61,6 +77,9 @@ class API extends Core_1.default {
61
77
  this.setToken(token);
62
78
  }
63
79
  }
80
+ addListener(event, callback) {
81
+ return this.events.addListener(event, callback);
82
+ }
64
83
  setToken(token) {
65
84
  if (typeof window === 'undefined')
66
85
  return;
@@ -208,5 +227,14 @@ Object.defineProperty(API, "uid", {
208
227
  writable: true,
209
228
  value: _a.instance.uid
210
229
  });
230
+ /**
231
+ * @deprecated The method should not be used
232
+ */
233
+ Object.defineProperty(API, "deleteCacheGroup", {
234
+ enumerable: true,
235
+ configurable: true,
236
+ writable: true,
237
+ value: _a.instance.clearCacheGroup
238
+ });
211
239
  globalThis.apiSetInitData = API.setInitData;
212
240
  exports.default = API;
package/lib/types.d.ts CHANGED
@@ -12,7 +12,7 @@ export type TData = {
12
12
  export type TGroupsData = {
13
13
  [index: string]: TData;
14
14
  };
15
- export type TMethod = 'GET' | 'POST';
15
+ export type TMethod = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS';
16
16
  export type TCacheControll = string | {
17
17
  group: string;
18
18
  fieldKey: string;
@@ -36,4 +36,4 @@ export type TRequestParams = {
36
36
  };
37
37
  body?: string;
38
38
  };
39
- export type TListenerEvents = 'api-request-fail' | 'api-request-error' | 'api-request-complete' | 'login' | 'logout' | 'user-update';
39
+ export type TListenerEvents = 'api-request-success' | 'api-request-fail' | 'api-request-error' | 'api-request-complete' | 'login' | 'logout' | 'user-update';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "larapi",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
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,13 +1,11 @@
1
1
  import EventEmitter,{
2
- Events,
3
- Event
2
+ Events
4
3
  } from 'easy-event-emitter';
5
- import {
4
+ import type {
6
5
  TCache,
7
6
  TCacheBody,
8
7
  TData,
9
- TGroupsData,
10
- TListenerEvents
8
+ TGroupsData
11
9
  } from './types';
12
10
 
13
11
  abstract class Core {
@@ -24,16 +22,11 @@ abstract class Core {
24
22
  this.initData = {};
25
23
 
26
24
  this.setHost = this.setHost.bind(this);
27
- this.addListener = this.addListener.bind(this);
28
25
  this.setInitData = this.setInitData.bind(this);
29
26
  this.clearCacheGroup = this.clearCacheGroup.bind(this);
30
27
  Object.assign(this, context);
31
28
  }
32
29
 
33
- public addListener(event: TListenerEvents, callback: (data: any) => void): Event {
34
- return this.events.addListener(event, callback);
35
- }
36
-
37
30
  protected getCache(key: string): TData | undefined {
38
31
  if (this.cache[key]) {
39
32
  if ((new Date()).getTime() <= (this.cache[key].time + 3600000000)) {
package/src/Http.ts CHANGED
@@ -1,16 +1,23 @@
1
1
  //@ts-ignore
2
2
  import md5 from 'md5';
3
3
  import Core from "./Core";
4
- import {
4
+ import Queue from './Queue';
5
+ import EventEmitter,{
6
+ Event,
7
+ Events
8
+ } from "easy-event-emitter";
9
+ import type {
5
10
  TMethod,
6
11
  TParams,
7
12
  TData,
8
- TRequestParams
13
+ TRequestParams,
14
+ TListenerEvents
9
15
  } from "./types";
10
16
 
11
17
  class Http extends Core {
12
18
  private cacheIndex: string;
13
19
  currentCache?: TData;
20
+ currentEvents: Events
14
21
  requestParams: TRequestParams;
15
22
  method: TMethod;
16
23
  params: TParams;
@@ -18,6 +25,7 @@ class Http extends Core {
18
25
 
19
26
  constructor(method: TMethod, params: TParams, context?: Core) {
20
27
  super(context);
28
+ this.currentEvents = new EventEmitter();
21
29
  this.cacheIndex = '';
22
30
  this.method = method;
23
31
  this.params = params;
@@ -30,8 +38,8 @@ class Http extends Core {
30
38
  this.fail = this.fail.bind(this);
31
39
  this.error = this.error.bind(this);
32
40
  this.complete = this.complete.bind(this);
41
+ this.setEmit = this.setEmit.bind(this);
33
42
  this.request = this.request.bind(this);
34
- this.addListener = this.addListener.bind(this);
35
43
  this.addHeader = this.addHeader.bind(this);
36
44
  this.deleteHeader = this.deleteHeader.bind(this);
37
45
 
@@ -47,12 +55,12 @@ class Http extends Core {
47
55
 
48
56
  this.params.data = this.params.data ? this.cuteUndifinedParams(this.params.data) : this.params.data;
49
57
 
50
- if (this.method === 'GET') {
58
+ if (this.method === 'GET' || this.method === 'HEAD') {
51
59
  if (this.params.data)
52
60
  this.path = this.params.path + '?' + this.encodeUrlParams(this.params.data);
53
61
  }
54
62
 
55
- if (this.method === 'POST') {
63
+ if (this.method === 'POST' || this.method === 'PUT') {
56
64
  this.requestParams.body = JSON.stringify(this.params.data);
57
65
  }
58
66
  }
@@ -77,8 +85,16 @@ class Http extends Core {
77
85
  }
78
86
  }
79
87
 
88
+ private setEmit(event: TListenerEvents, args: any) {
89
+ this.currentEvents.emit(event, args[0]);
90
+ this.events.emit(event, {
91
+ cacheIndex: this.cacheIndex,
92
+ args: args[0]
93
+ });
94
+ }
95
+
80
96
  //Заголовки от 200 до 299
81
- private success(...args: any) {
97
+ public success(...args: any) {
82
98
  if (typeof this.params.success === 'function')
83
99
  this.params.success(...args);
84
100
  if (this.params.cacheUpdate && this.params.data) {
@@ -94,32 +110,33 @@ class Http extends Core {
94
110
  this.params.data,
95
111
  typeof this.params.cacheClear === 'string' ? undefined : this.params.cacheClear.fieldKey);
96
112
  }
113
+ this.setEmit('api-request-success', args);
97
114
  }
98
115
 
99
116
  //Заголовки от 400 до 499
100
- private fail(...args: any) {
117
+ public fail(...args: any) {
101
118
  if (typeof this.params.fail === 'function') {
102
119
  this.params.fail(...args);
103
120
  }
104
121
 
105
- this.events.emit('api-request-fail', args);
122
+ this.setEmit('api-request-fail', args);
106
123
  }
107
124
 
108
125
  //Все остальные
109
- private error(...args: any) {
126
+ public error(...args: any) {
110
127
  if (typeof this.params.error === 'function') {
111
128
  this.params.error(...args);
112
129
  }
113
130
 
114
- this.events.emit('api-request-error', args);
131
+ this.setEmit('api-request-error', args);
115
132
  }
116
133
 
117
- private complete(...args: any) {
134
+ public complete(...args: any) {
118
135
  if (typeof this.params.complete === 'function') {
119
136
  this.params.complete(...args);
120
137
  }
121
138
 
122
- this.events.emit('api-request-complete', args);
139
+ this.setEmit('api-request-complete', args);
123
140
  }
124
141
 
125
142
  private encodeUrlParams(data: TData) {
@@ -141,7 +158,7 @@ class Http extends Core {
141
158
  }
142
159
 
143
160
  public request(): this {
144
- if (typeof XMLHttpRequest === 'undefined' || this.currentCache) return this;
161
+ if (typeof XMLHttpRequest === 'undefined' || this.currentCache || Queue.push(this.cacheIndex, this)) return this;
145
162
 
146
163
  try {
147
164
  const xhr = new XMLHttpRequest();
@@ -186,6 +203,10 @@ class Http extends Core {
186
203
  return this;
187
204
  }
188
205
 
206
+ public addListener(event: TListenerEvents, callback: (data: any) => void): Event {
207
+ return this.currentEvents.addListener(event, callback);
208
+ }
209
+
189
210
  public deleteHeader(key: string): this {
190
211
  if (this.requestParams.headers && (key in this.requestParams.headers)) {
191
212
  delete this.requestParams.headers[key];
package/src/Queue.ts ADDED
@@ -0,0 +1,43 @@
1
+ import Http from "./Http";
2
+
3
+ type TQueue = {
4
+ [index: string]: Http;
5
+ };
6
+
7
+ class Queue {
8
+ private static instance = new Queue;
9
+ private queue: TQueue = {};
10
+
11
+ constructor() {
12
+ this.has = this.has.bind(this);
13
+ this.get = this.get.bind(this);
14
+ this.push = this.push.bind(this);
15
+ }
16
+
17
+ public has(request: string) {
18
+ return request in this.queue;
19
+ }
20
+
21
+ public get(request: string) {
22
+ return this.has(request) ? this.queue[request] : undefined;
23
+ }
24
+
25
+ public push(request: string, instance: Http): boolean {
26
+ const refInstance = this.get(request);
27
+ if (!refInstance) {
28
+ this.queue[request] = instance;
29
+ }else{
30
+ refInstance?.addListener('api-request-success', instance.success);
31
+ refInstance?.addListener('api-request-error', instance.error);
32
+ refInstance?.addListener('api-request-fail', instance.fail);
33
+ refInstance?.addListener('api-request-complete', instance.complete);
34
+ }
35
+ return refInstance instanceof Http;
36
+ }
37
+
38
+ public static has = this.instance.has;
39
+ public static get = this.instance.get;
40
+ public static push = this.instance.push;
41
+ }
42
+
43
+ export default Queue;
package/src/index.ts CHANGED
@@ -1,5 +1,12 @@
1
1
  import Core from "./Core";
2
- import { TMethod, TParams } from "./types";
2
+ import type {
3
+ TMethod,
4
+ TParams,
5
+ TListenerEvents
6
+ } from "./types";
7
+ import type {
8
+ Event
9
+ } from "easy-event-emitter";
3
10
  import Http from "./Http";
4
11
 
5
12
  type TUser = {
@@ -31,6 +38,10 @@ class API extends Core {
31
38
  this.http = this.http.bind(this);
32
39
  this.get = this.get.bind(this);
33
40
  this.post = this.post.bind(this);
41
+ this.put = this.put.bind(this);
42
+ this.patch = this.patch.bind(this);
43
+ this.delete = this.delete.bind(this);
44
+ this.options = this.options.bind(this);
34
45
  this.getUid = this.getUid.bind(this);
35
46
  this.getUser = this.getUser.bind(this);
36
47
  }
@@ -51,6 +62,22 @@ class API extends Core {
51
62
  return this.http('POST', params).request();
52
63
  }
53
64
 
65
+ public put(params: TParams): Http {
66
+ return this.http('PUT', params).request();
67
+ }
68
+
69
+ public patch(params: TParams): Http {
70
+ return this.http('PATCH', params).request();
71
+ }
72
+
73
+ public delete(params: TParams): Http {
74
+ return this.http('DELETE', params).request();
75
+ }
76
+
77
+ public options(params: TParams): Http {
78
+ return this.http('OPTIONS', params).request();
79
+ }
80
+
54
81
  private init(): void {
55
82
  if (typeof window === 'undefined') return;
56
83
  const token = localStorage.getItem('token');
@@ -64,6 +91,10 @@ class API extends Core {
64
91
  }
65
92
  }
66
93
 
94
+ public addListener(event: TListenerEvents, callback: (data: any) => void): Event {
95
+ return this.events.addListener(event, callback);
96
+ }
97
+
67
98
  public setToken(token: string) {
68
99
  if (typeof window === 'undefined') return;
69
100
  localStorage.setItem('token', token);
@@ -127,6 +158,11 @@ class API extends Core {
127
158
  public static getUser = this.instance.getUser;
128
159
  public static user = this.instance.user;
129
160
  public static uid = this.instance.uid;
161
+
162
+ /**
163
+ * @deprecated The method should not be used
164
+ */
165
+ public static deleteCacheGroup = this.instance.clearCacheGroup;
130
166
  }
131
167
 
132
168
  (globalThis as any).apiSetInitData = API.setInitData;
package/src/types.ts CHANGED
@@ -16,7 +16,7 @@ export type TGroupsData = {
16
16
  [index: string]: TData;
17
17
  }
18
18
 
19
- export type TMethod = 'GET' | 'POST';
19
+ export type TMethod = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS';
20
20
 
21
21
  export type TCacheControll = string | {
22
22
  group: string;
@@ -44,4 +44,4 @@ export type TRequestParams = {
44
44
  body?: string;
45
45
  }
46
46
 
47
- export type TListenerEvents = 'api-request-fail' | 'api-request-error' | 'api-request-complete' | 'login' | 'logout' | 'user-update';
47
+ export type TListenerEvents = 'api-request-success' | 'api-request-fail' | 'api-request-error' | 'api-request-complete' | 'login' | 'logout' | 'user-update';