larapi 0.0.1 → 0.0.2

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.
@@ -1,8 +1,5 @@
1
1
  import API from "../lib/index.js";
2
2
 
3
3
  test('Connect', () => {
4
- // const api = new API({
5
- // host: 'http://127.0.0.1:8000'
6
- // });
7
-
4
+ //todo
8
5
  });
@@ -0,0 +1,3 @@
1
+ {
2
+ "presets": ["@babel/preset-env"]
3
+ }
package/global.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ declare global {
2
+ function apiSetInitData(): void;
3
+ }
package/lib/Core.d.ts CHANGED
@@ -1,21 +1,21 @@
1
1
  import { Events, Event } from 'easy-event-emitter';
2
- import { TCache, TMethod, TParams, TListenerEvents } from './types';
2
+ import { TCache, TGroupsData, TMethod, TParams, TListenerEvents } from './types';
3
3
  declare abstract class Core {
4
4
  private host;
5
5
  token: string;
6
6
  cache: TCache;
7
+ initData: TGroupsData;
7
8
  protected events: Events;
8
9
  constructor();
9
10
  protected request(method: TMethod, params: TParams): {
10
11
  clearCache: () => void;
11
- };
12
+ } | undefined;
12
13
  addListener(event: TListenerEvents, callback: (data: any) => void): Event;
13
14
  private getCache;
14
15
  setHost(host: string): void;
15
16
  private setCache;
16
17
  private deleteCacheGroup;
17
18
  private deleteCache;
18
- setToken(token: string): void;
19
- getToken(): string;
19
+ setInitData(data: TGroupsData): void;
20
20
  }
21
21
  export default Core;
package/lib/Core.js CHANGED
@@ -26,6 +26,12 @@ class Core {
26
26
  writable: true,
27
27
  value: void 0
28
28
  });
29
+ Object.defineProperty(this, "initData", {
30
+ enumerable: true,
31
+ configurable: true,
32
+ writable: true,
33
+ value: void 0
34
+ });
29
35
  Object.defineProperty(this, "events", {
30
36
  enumerable: true,
31
37
  configurable: true,
@@ -36,11 +42,16 @@ class Core {
36
42
  this.host = 'http://127.0.0.1/';
37
43
  this.token = '';
38
44
  this.cache = {};
45
+ this.initData = {};
46
+ this.request = this.request.bind(this);
39
47
  this.setHost = this.setHost.bind(this);
40
- this.setToken = this.setToken.bind(this);
41
48
  this.addListener = this.addListener.bind(this);
49
+ this.setInitData = this.setInitData.bind(this);
42
50
  }
43
51
  request(method, params) {
52
+ var _a;
53
+ if (typeof XMLHttpRequest === 'undefined')
54
+ return;
44
55
  //Заголовки от 200 до 299
45
56
  const success = (...args) => {
46
57
  if (typeof params.success === 'function')
@@ -102,11 +113,11 @@ class Core {
102
113
  const pathCache = (0, md5_1.default)(path + JSON.stringify(params.data));
103
114
  var dataCache;
104
115
  if (params.globalName) {
105
- // if (__global && __global[params.globalName]) {
106
- // Core.setCache(pathCache, __global[params.globalName], (typeof params.cache === 'boolean' ? undefined : params.cache));
107
- // params.cache = params.cache ?? true;
108
- // delete __global[params.globalName];
109
- // }
116
+ if (this.initData[params.globalName]) {
117
+ this.setCache(pathCache, this.initData[params.globalName], (typeof params.cache === 'boolean' ? undefined : params.cache));
118
+ params.cache = (_a = params.cache) !== null && _a !== void 0 ? _a : true;
119
+ delete this.initData[params.globalName];
120
+ }
110
121
  }
111
122
  if (params.cache) {
112
123
  if (dataCache = this.getCache(pathCache)) {
@@ -118,32 +129,37 @@ class Core {
118
129
  this.deleteCache(pathCache);
119
130
  }
120
131
  if (!dataCache) {
121
- const xhr = new XMLHttpRequest();
122
- xhr.open(method, this.host + path, true);
123
- for (const header in fetchParams.headers) {
124
- xhr.setRequestHeader(header, fetchParams.headers[header]);
125
- }
126
- xhr.onreadystatechange = () => {
127
- if (xhr.readyState !== 4) {
128
- return;
132
+ try {
133
+ const xhr = new XMLHttpRequest();
134
+ xhr.open(method, this.host + path, true);
135
+ for (const header in fetchParams.headers) {
136
+ xhr.setRequestHeader(header, fetchParams.headers[header]);
129
137
  }
130
- const result = xhr.responseText ? JSON.parse(xhr.responseText) : {};
131
- if (xhr.status >= 200 && xhr.status <= 299) {
132
- if (params.cache) {
133
- this.setCache(pathCache, result, (typeof params.cache === 'boolean' ? undefined : params.cache));
138
+ xhr.onreadystatechange = () => {
139
+ if (xhr.readyState !== 4) {
140
+ return;
134
141
  }
135
- success(result);
136
- }
137
- else {
138
- if (xhr.status >= 400 && xhr.status <= 499) {
139
- fail(result);
142
+ const result = xhr.responseText ? JSON.parse(xhr.responseText) : {};
143
+ if (xhr.status >= 200 && xhr.status <= 299) {
144
+ if (params.cache) {
145
+ this.setCache(pathCache, result, (typeof params.cache === 'boolean' ? undefined : params.cache));
146
+ }
147
+ success(result);
140
148
  }
141
- error(result);
142
- console.error(result);
143
- }
144
- complete(result);
145
- };
146
- xhr.send(fetchParams.body);
149
+ else {
150
+ if (xhr.status >= 400 && xhr.status <= 499) {
151
+ fail(result);
152
+ }
153
+ error(result);
154
+ console.warn(result);
155
+ }
156
+ complete(result);
157
+ };
158
+ xhr.send(fetchParams.body);
159
+ }
160
+ catch (e) {
161
+ console.warn(e);
162
+ }
147
163
  }
148
164
  return {
149
165
  clearCache: () => {
@@ -192,12 +208,8 @@ class Core {
192
208
  }
193
209
  delete this.cache[keyCache];
194
210
  }
195
- setToken(token) {
196
- localStorage.setItem('token', token);
197
- this.token = token;
198
- }
199
- getToken() {
200
- return this.token;
211
+ setInitData(data) {
212
+ this.initData = data;
201
213
  }
202
214
  }
203
215
  exports.default = Core;
package/lib/index.d.ts CHANGED
@@ -12,21 +12,30 @@ declare class API extends Core {
12
12
  constructor(config: TConfig);
13
13
  get(params: TParams): {
14
14
  clearCache: () => void;
15
- };
15
+ } | undefined;
16
16
  post(params: TParams): {
17
17
  clearCache: () => void;
18
- };
18
+ } | undefined;
19
19
  private init;
20
+ setToken(token: string): void;
21
+ getToken(): string;
20
22
  get uid(): number;
21
23
  get user(): TUser;
22
24
  setUser(user: TUser): void;
23
25
  updateUser(user: TUser): void;
24
26
  logout(): void;
27
+ static get: (params: TParams) => {
28
+ clearCache: () => void;
29
+ } | undefined;
30
+ static post: (params: TParams) => {
31
+ clearCache: () => void;
32
+ } | undefined;
25
33
  static setHost: (host: string) => void;
26
34
  static setUser: (user: TUser) => void;
27
35
  static updateUser: (user: TUser) => void;
28
36
  static logout: () => void;
29
37
  static setToken: (token: string) => void;
30
38
  static addListener: (event: import("./types").TListenerEvents, callback: (data: any) => void) => import("easy-event-emitter").Event;
39
+ static setInitData: (data: import("./types").TGroupsData) => void;
31
40
  }
32
41
  export default API;
package/lib/index.js CHANGED
@@ -17,9 +17,12 @@ class API extends Core_1.default {
17
17
  this.setHost(config.host);
18
18
  this._user = {};
19
19
  this.init();
20
+ this.setToken = this.setToken.bind(this);
20
21
  this.setUser = this.setUser.bind(this);
21
22
  this.updateUser = this.updateUser.bind(this);
22
23
  this.logout = this.logout.bind(this);
24
+ this.get = this.get.bind(this);
25
+ this.post = this.post.bind(this);
23
26
  }
24
27
  get(params) {
25
28
  return this.request('GET', params);
@@ -28,6 +31,8 @@ class API extends Core_1.default {
28
31
  return this.request('POST', params);
29
32
  }
30
33
  init() {
34
+ if (typeof window === 'undefined')
35
+ return;
31
36
  const token = localStorage.getItem('token');
32
37
  const strUser = localStorage.getItem('user');
33
38
  if (strUser) {
@@ -38,6 +43,15 @@ class API extends Core_1.default {
38
43
  this.setToken(token);
39
44
  }
40
45
  }
46
+ setToken(token) {
47
+ if (typeof window === 'undefined')
48
+ return;
49
+ localStorage.setItem('token', token);
50
+ this.token = token;
51
+ }
52
+ getToken() {
53
+ return this.token;
54
+ }
41
55
  get uid() {
42
56
  return 'id' in this._user ? Number(this._user.id) : 0;
43
57
  }
@@ -45,6 +59,8 @@ class API extends Core_1.default {
45
59
  return this._user;
46
60
  }
47
61
  setUser(user) {
62
+ if (typeof window === 'undefined')
63
+ return;
48
64
  localStorage.setItem('user', JSON.stringify(user));
49
65
  this._user = user;
50
66
  this.events.emit('login', user);
@@ -54,9 +70,11 @@ class API extends Core_1.default {
54
70
  this.setUser(Object.assign(Object.assign({}, this.user), user));
55
71
  }
56
72
  logout() {
57
- this.events.emit('logout', this.user);
73
+ if (typeof window === 'undefined')
74
+ return;
58
75
  localStorage.removeItem('user');
59
76
  localStorage.removeItem('token');
77
+ this.events.emit('logout', this.user);
60
78
  this.setUser({});
61
79
  this.setToken('');
62
80
  }
@@ -70,6 +88,18 @@ Object.defineProperty(API, "instance", {
70
88
  host: ''
71
89
  })
72
90
  });
91
+ Object.defineProperty(API, "get", {
92
+ enumerable: true,
93
+ configurable: true,
94
+ writable: true,
95
+ value: _a.instance.get
96
+ });
97
+ Object.defineProperty(API, "post", {
98
+ enumerable: true,
99
+ configurable: true,
100
+ writable: true,
101
+ value: _a.instance.post
102
+ });
73
103
  Object.defineProperty(API, "setHost", {
74
104
  enumerable: true,
75
105
  configurable: true,
@@ -106,4 +136,11 @@ Object.defineProperty(API, "addListener", {
106
136
  writable: true,
107
137
  value: _a.instance.addListener
108
138
  });
139
+ Object.defineProperty(API, "setInitData", {
140
+ enumerable: true,
141
+ configurable: true,
142
+ writable: true,
143
+ value: _a.instance.setInitData
144
+ });
145
+ globalThis.apiSetInitData = API.setInitData;
109
146
  exports.default = API;
package/lib/types.d.ts CHANGED
@@ -4,6 +4,9 @@ export type TCache = {
4
4
  export type TData = {
5
5
  [index: string]: any;
6
6
  };
7
+ export type TGroupsData = {
8
+ [index: string]: TData;
9
+ };
7
10
  export type TMethod = 'GET' | 'POST';
8
11
  export type TParams = {
9
12
  path: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "larapi",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "API client for laravel",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -10,6 +10,7 @@
10
10
  },
11
11
  "jest": {
12
12
  "transform": {
13
+ "^.+\\.ts?$": "ts-jest",
13
14
  "^.+\\.[t|j]sx?$": "babel-jest"
14
15
  }
15
16
  },
@@ -31,9 +32,11 @@
31
32
  "homepage": "https://github.com/SergoMorello/larasopp.client#readme",
32
33
  "devDependencies": {
33
34
  "@babel/preset-env": "^7.22.9",
34
- "@types/jest": "^29.5.3",
35
+ "@babel/preset-typescript": "^7.22.5",
36
+ "@types/jest": "^29.4.3",
35
37
  "babel-jest": "^29.4.3",
36
38
  "jest": "^29.4.3",
39
+ "ts-jest": "^29.1.1",
37
40
  "typescript": "^5.1.6"
38
41
  },
39
42
  "dependencies": {
package/src/Core.ts CHANGED
@@ -7,6 +7,7 @@ import EventEmitter,{
7
7
  import {
8
8
  TCache,
9
9
  TData,
10
+ TGroupsData,
10
11
  TMethod,
11
12
  TParams,
12
13
  TRequestParams,
@@ -17,6 +18,7 @@ abstract class Core {
17
18
  private host: string;
18
19
  token: string;
19
20
  cache: TCache;
21
+ initData: TGroupsData;
20
22
  protected events: Events;
21
23
 
22
24
  constructor() {
@@ -24,13 +26,16 @@ abstract class Core {
24
26
  this.host = 'http://127.0.0.1/';
25
27
  this.token = '';
26
28
  this.cache = {};
29
+ this.initData = {};
27
30
 
31
+ this.request = this.request.bind(this);
28
32
  this.setHost = this.setHost.bind(this);
29
- this.setToken = this.setToken.bind(this);
30
33
  this.addListener = this.addListener.bind(this);
34
+ this.setInitData = this.setInitData.bind(this);
31
35
  }
32
36
 
33
37
  protected request(method: TMethod, params: TParams) {
38
+ if (typeof XMLHttpRequest === 'undefined') return;
34
39
 
35
40
  //Заголовки от 200 до 299
36
41
  const success = (...args: any) => {
@@ -107,11 +112,11 @@ abstract class Core {
107
112
  var dataCache;
108
113
 
109
114
  if (params.globalName) {
110
- // if (__global && __global[params.globalName]) {
111
- // Core.setCache(pathCache, __global[params.globalName], (typeof params.cache === 'boolean' ? undefined : params.cache));
112
- // params.cache = params.cache ?? true;
113
- // delete __global[params.globalName];
114
- // }
115
+ if (this.initData[params.globalName]) {
116
+ this.setCache(pathCache, this.initData[params.globalName], (typeof params.cache === 'boolean' ? undefined : params.cache));
117
+ params.cache = params.cache ?? true;
118
+ delete this.initData[params.globalName];
119
+ }
115
120
  }
116
121
 
117
122
  if (params.cache) {
@@ -123,37 +128,41 @@ abstract class Core {
123
128
  this.deleteCache(pathCache);
124
129
  }
125
130
  if (!dataCache) {
126
- const xhr = new XMLHttpRequest();
127
-
128
- xhr.open(method, this.host + path, true);
129
- for(const header in fetchParams.headers) {
130
- xhr.setRequestHeader(header, fetchParams.headers[header]);
131
- }
131
+ try {
132
+ const xhr = new XMLHttpRequest();
132
133
 
133
- xhr.onreadystatechange = () => {
134
- if (xhr.readyState !== 4) {
135
- return;
134
+ xhr.open(method, this.host + path, true);
135
+ for(const header in fetchParams.headers) {
136
+ xhr.setRequestHeader(header, fetchParams.headers[header]);
136
137
  }
137
-
138
- const result = xhr.responseText ? JSON.parse(xhr.responseText) : {};
139
-
140
- if (xhr.status >= 200 && xhr.status <= 299) {
141
- if (params.cache) {
142
- this.setCache(pathCache, result, (typeof params.cache === 'boolean' ? undefined : params.cache));
143
- }
144
- success(result);
145
- }else{
146
- if (xhr.status >= 400 && xhr.status <= 499) {
147
- fail(result);
138
+
139
+ xhr.onreadystatechange = () => {
140
+ if (xhr.readyState !== 4) {
141
+ return;
148
142
  }
149
143
 
150
- error(result);
151
- console.error(result);
144
+ const result = xhr.responseText ? JSON.parse(xhr.responseText) : {};
145
+
146
+ if (xhr.status >= 200 && xhr.status <= 299) {
147
+ if (params.cache) {
148
+ this.setCache(pathCache, result, (typeof params.cache === 'boolean' ? undefined : params.cache));
149
+ }
150
+ success(result);
151
+ }else{
152
+ if (xhr.status >= 400 && xhr.status <= 499) {
153
+ fail(result);
154
+ }
155
+
156
+ error(result);
157
+ console.warn(result);
158
+ }
159
+ complete(result);
152
160
  }
153
- complete(result);
161
+ xhr.send(fetchParams.body);
162
+ }catch(e) {
163
+ console.warn(e);
154
164
  }
155
-
156
- xhr.send(fetchParams.body);
165
+
157
166
  }
158
167
  return {
159
168
  clearCache: () => {
@@ -209,13 +218,8 @@ abstract class Core {
209
218
  delete this.cache[keyCache];
210
219
  }
211
220
 
212
- public setToken(token: string) {
213
- localStorage.setItem('token', token);
214
- this.token = token;
215
- }
216
-
217
- public getToken() {
218
- return this.token;
221
+ public setInitData(data: TGroupsData): void {
222
+ this.initData = data;
219
223
  }
220
224
  }
221
225
 
package/src/index.ts CHANGED
@@ -21,9 +21,12 @@ class API extends Core {
21
21
  this._user = {};
22
22
  this.init();
23
23
 
24
+ this.setToken = this.setToken.bind(this);
24
25
  this.setUser = this.setUser.bind(this);
25
26
  this.updateUser = this.updateUser.bind(this);
26
27
  this.logout = this.logout.bind(this);
28
+ this.get = this.get.bind(this);
29
+ this.post = this.post.bind(this);
27
30
  }
28
31
 
29
32
  public get(params: TParams) {
@@ -35,6 +38,7 @@ class API extends Core {
35
38
  }
36
39
 
37
40
  private init(): void {
41
+ if (typeof window === 'undefined') return;
38
42
  const token = localStorage.getItem('token');
39
43
  const strUser = localStorage.getItem('user');
40
44
  if (strUser) {
@@ -46,6 +50,16 @@ class API extends Core {
46
50
  }
47
51
  }
48
52
 
53
+ public setToken(token: string) {
54
+ if (typeof window === 'undefined') return;
55
+ localStorage.setItem('token', token);
56
+ this.token = token;
57
+ }
58
+
59
+ public getToken() {
60
+ return this.token;
61
+ }
62
+
49
63
  public get uid(): number {
50
64
  return 'id' in this._user ? Number(this._user.id) : 0;
51
65
  }
@@ -55,6 +69,7 @@ class API extends Core {
55
69
  }
56
70
 
57
71
  public setUser(user: TUser) {
72
+ if (typeof window === 'undefined') return;
58
73
  localStorage.setItem('user', JSON.stringify(user));
59
74
  this._user = user;
60
75
  this.events.emit('login', user);
@@ -66,19 +81,25 @@ class API extends Core {
66
81
  }
67
82
 
68
83
  public logout() {
69
- this.events.emit('logout', this.user);
84
+ if (typeof window === 'undefined') return;
70
85
  localStorage.removeItem('user');
71
86
  localStorage.removeItem('token');
87
+ this.events.emit('logout', this.user);
72
88
  this.setUser({});
73
89
  this.setToken('');
74
90
  }
75
91
 
92
+ public static get = this.instance.get;
93
+ public static post = this.instance.post;
76
94
  public static setHost = this.instance.setHost;
77
95
  public static setUser = this.instance.setUser;
78
96
  public static updateUser = this.instance.updateUser;
79
97
  public static logout = this.instance.logout;
80
98
  public static setToken = this.instance.setToken;
81
99
  public static addListener = this.instance.addListener;
100
+ public static setInitData = this.instance.setInitData;
82
101
  }
83
102
 
103
+ (globalThis as any).apiSetInitData = API.setInitData;
104
+
84
105
  export default API;
package/src/types.ts CHANGED
@@ -6,6 +6,10 @@ export type TData = {
6
6
  [index: string]: any;
7
7
  }
8
8
 
9
+ export type TGroupsData = {
10
+ [index: string]: TData;
11
+ }
12
+
9
13
  export type TMethod = 'GET' | 'POST';
10
14
 
11
15
  export type TParams = {
package/tsconfig.json CHANGED
@@ -11,8 +11,8 @@
11
11
  // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
12
12
 
13
13
  /* Language and Environment */
14
- "target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
15
- // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
14
+ "target": "ES2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
15
+ "lib": ["DOM"], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
16
16
  // "jsx": "preserve", /* Specify what JSX code is generated. */
17
17
  // "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
18
18
  // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
@@ -25,7 +25,7 @@
25
25
  // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
26
26
 
27
27
  /* Modules */
28
- "module": "commonjs", /* Specify what module code is generated. */
28
+ "module": "CommonJS", /* Specify what module code is generated. */
29
29
  // "rootDir": "./", /* Specify the root folder within your source files. */
30
30
  // "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */
31
31
  // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */