larapi 0.0.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.
@@ -0,0 +1,8 @@
1
+ import API from "../lib/index.js";
2
+
3
+ test('Connect', () => {
4
+ // const api = new API({
5
+ // host: 'http://127.0.0.1:8000'
6
+ // });
7
+
8
+ });
package/lib/Core.d.ts ADDED
@@ -0,0 +1,21 @@
1
+ import { Events, Event } from 'easy-event-emitter';
2
+ import { TCache, TMethod, TParams, TListenerEvents } from './types';
3
+ declare abstract class Core {
4
+ private host;
5
+ token: string;
6
+ cache: TCache;
7
+ protected events: Events;
8
+ constructor();
9
+ protected request(method: TMethod, params: TParams): {
10
+ clearCache: () => void;
11
+ };
12
+ addListener(event: TListenerEvents, callback: (data: any) => void): Event;
13
+ private getCache;
14
+ setHost(host: string): void;
15
+ private setCache;
16
+ private deleteCacheGroup;
17
+ private deleteCache;
18
+ setToken(token: string): void;
19
+ getToken(): string;
20
+ }
21
+ export default Core;
package/lib/Core.js ADDED
@@ -0,0 +1,203 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ //@ts-ignore
7
+ const md5_1 = __importDefault(require("md5"));
8
+ const easy_event_emitter_1 = __importDefault(require("easy-event-emitter"));
9
+ class Core {
10
+ constructor() {
11
+ Object.defineProperty(this, "host", {
12
+ enumerable: true,
13
+ configurable: true,
14
+ writable: true,
15
+ value: void 0
16
+ });
17
+ Object.defineProperty(this, "token", {
18
+ enumerable: true,
19
+ configurable: true,
20
+ writable: true,
21
+ value: void 0
22
+ });
23
+ Object.defineProperty(this, "cache", {
24
+ enumerable: true,
25
+ configurable: true,
26
+ writable: true,
27
+ value: void 0
28
+ });
29
+ Object.defineProperty(this, "events", {
30
+ enumerable: true,
31
+ configurable: true,
32
+ writable: true,
33
+ value: void 0
34
+ });
35
+ this.events = new easy_event_emitter_1.default();
36
+ this.host = 'http://127.0.0.1/';
37
+ this.token = '';
38
+ this.cache = {};
39
+ this.setHost = this.setHost.bind(this);
40
+ this.setToken = this.setToken.bind(this);
41
+ this.addListener = this.addListener.bind(this);
42
+ }
43
+ request(method, params) {
44
+ //Заголовки от 200 до 299
45
+ const success = (...args) => {
46
+ if (typeof params.success === 'function')
47
+ params.success(...args);
48
+ };
49
+ //Заголовки от 400 до 499
50
+ const fail = (...args) => {
51
+ if (typeof params.fail === 'function') {
52
+ params.fail(...args);
53
+ }
54
+ this.events.emit('api-request-fail', args);
55
+ };
56
+ //Все остальные
57
+ const error = (...args) => {
58
+ if (typeof params.error === 'function') {
59
+ params.error(...args);
60
+ }
61
+ this.events.emit('api-request-error', args);
62
+ };
63
+ const complete = (...args) => {
64
+ if (typeof params.complete === 'function') {
65
+ params.complete(...args);
66
+ }
67
+ this.events.emit('api-request-complete', args);
68
+ };
69
+ const encodeUrlParams = (data) => {
70
+ return Object.keys(data).map(function (k) {
71
+ return encodeURIComponent(k) + "=" + encodeURIComponent(data[k]);
72
+ }).join('&');
73
+ };
74
+ const cuteUndifinedParams = (data) => {
75
+ if (typeof data !== 'object') {
76
+ return;
77
+ }
78
+ for (const key in data) {
79
+ if (!data[key]) {
80
+ delete data[key];
81
+ }
82
+ }
83
+ return data;
84
+ };
85
+ var path = params.path;
86
+ const fetchParams = {
87
+ method: method
88
+ };
89
+ fetchParams.headers = {
90
+ 'Accept': 'application/json',
91
+ 'Content-Type': 'application/json',
92
+ 'Authorization': 'Bearer ' + this.token
93
+ };
94
+ params.data = params.data ? cuteUndifinedParams(params.data) : params.data;
95
+ if (method === 'POST') {
96
+ fetchParams.body = JSON.stringify(params.data);
97
+ }
98
+ if (method === 'GET') {
99
+ if (params.data)
100
+ path = params.path + '?' + encodeUrlParams(params.data);
101
+ }
102
+ const pathCache = (0, md5_1.default)(path + JSON.stringify(params.data));
103
+ var dataCache;
104
+ 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
+ // }
110
+ }
111
+ if (params.cache) {
112
+ if (dataCache = this.getCache(pathCache)) {
113
+ success(dataCache);
114
+ complete(dataCache);
115
+ }
116
+ }
117
+ else {
118
+ this.deleteCache(pathCache);
119
+ }
120
+ 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;
129
+ }
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));
134
+ }
135
+ success(result);
136
+ }
137
+ else {
138
+ if (xhr.status >= 400 && xhr.status <= 499) {
139
+ fail(result);
140
+ }
141
+ error(result);
142
+ console.error(result);
143
+ }
144
+ complete(result);
145
+ };
146
+ xhr.send(fetchParams.body);
147
+ }
148
+ return {
149
+ clearCache: () => {
150
+ this.deleteCache(pathCache);
151
+ }
152
+ };
153
+ }
154
+ addListener(event, callback) {
155
+ return this.events.addListener(event, callback);
156
+ }
157
+ getCache(request) {
158
+ const key = (0, md5_1.default)(request);
159
+ if (this.cache[key]) {
160
+ if ((new Date()).getTime() <= (this.cache[key].time + 3600000000)) {
161
+ return this.cache[key].data;
162
+ }
163
+ else {
164
+ delete this.cache[key];
165
+ }
166
+ }
167
+ }
168
+ setHost(host) {
169
+ this.host = host;
170
+ }
171
+ setCache(request, data, group) {
172
+ this.cache[(0, md5_1.default)(request)] = {
173
+ time: (new Date()).getTime(),
174
+ data: data,
175
+ group: group
176
+ };
177
+ }
178
+ deleteCacheGroup(group) {
179
+ for (const key in this.cache) {
180
+ if (this.cache[key].group === group) {
181
+ delete this.cache[key];
182
+ }
183
+ }
184
+ }
185
+ deleteCache(request) {
186
+ const keyCache = (0, md5_1.default)(request);
187
+ if (!this.cache[keyCache])
188
+ return;
189
+ const group = this.cache[keyCache].group;
190
+ if (group) {
191
+ this.deleteCacheGroup(group);
192
+ }
193
+ delete this.cache[keyCache];
194
+ }
195
+ setToken(token) {
196
+ localStorage.setItem('token', token);
197
+ this.token = token;
198
+ }
199
+ getToken() {
200
+ return this.token;
201
+ }
202
+ }
203
+ exports.default = Core;
package/lib/index.d.ts ADDED
@@ -0,0 +1,32 @@
1
+ import Core from "./Core";
2
+ import { TParams } from "./types";
3
+ type TUser = {
4
+ [index: string]: any;
5
+ };
6
+ type TConfig = {
7
+ host: string;
8
+ };
9
+ declare class API extends Core {
10
+ private static instance;
11
+ private _user;
12
+ constructor(config: TConfig);
13
+ get(params: TParams): {
14
+ clearCache: () => void;
15
+ };
16
+ post(params: TParams): {
17
+ clearCache: () => void;
18
+ };
19
+ private init;
20
+ get uid(): number;
21
+ get user(): TUser;
22
+ setUser(user: TUser): void;
23
+ updateUser(user: TUser): void;
24
+ logout(): void;
25
+ static setHost: (host: string) => void;
26
+ static setUser: (user: TUser) => void;
27
+ static updateUser: (user: TUser) => void;
28
+ static logout: () => void;
29
+ static setToken: (token: string) => void;
30
+ static addListener: (event: import("./types").TListenerEvents, callback: (data: any) => void) => import("easy-event-emitter").Event;
31
+ }
32
+ export default API;
package/lib/index.js ADDED
@@ -0,0 +1,109 @@
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 Core_1 = __importDefault(require("./Core"));
8
+ class API extends Core_1.default {
9
+ constructor(config) {
10
+ super();
11
+ Object.defineProperty(this, "_user", {
12
+ enumerable: true,
13
+ configurable: true,
14
+ writable: true,
15
+ value: void 0
16
+ });
17
+ this.setHost(config.host);
18
+ this._user = {};
19
+ this.init();
20
+ this.setUser = this.setUser.bind(this);
21
+ this.updateUser = this.updateUser.bind(this);
22
+ this.logout = this.logout.bind(this);
23
+ }
24
+ get(params) {
25
+ return this.request('GET', params);
26
+ }
27
+ post(params) {
28
+ return this.request('POST', params);
29
+ }
30
+ init() {
31
+ const token = localStorage.getItem('token');
32
+ const strUser = localStorage.getItem('user');
33
+ if (strUser) {
34
+ const user = JSON.parse(strUser);
35
+ this.setUser(user);
36
+ }
37
+ if (token) {
38
+ this.setToken(token);
39
+ }
40
+ }
41
+ get uid() {
42
+ return 'id' in this._user ? Number(this._user.id) : 0;
43
+ }
44
+ get user() {
45
+ return this._user;
46
+ }
47
+ setUser(user) {
48
+ localStorage.setItem('user', JSON.stringify(user));
49
+ this._user = user;
50
+ this.events.emit('login', user);
51
+ }
52
+ updateUser(user) {
53
+ this.events.emit('update', this.user);
54
+ this.setUser(Object.assign(Object.assign({}, this.user), user));
55
+ }
56
+ logout() {
57
+ this.events.emit('logout', this.user);
58
+ localStorage.removeItem('user');
59
+ localStorage.removeItem('token');
60
+ this.setUser({});
61
+ this.setToken('');
62
+ }
63
+ }
64
+ _a = API;
65
+ Object.defineProperty(API, "instance", {
66
+ enumerable: true,
67
+ configurable: true,
68
+ writable: true,
69
+ value: new API({
70
+ host: ''
71
+ })
72
+ });
73
+ Object.defineProperty(API, "setHost", {
74
+ enumerable: true,
75
+ configurable: true,
76
+ writable: true,
77
+ value: _a.instance.setHost
78
+ });
79
+ Object.defineProperty(API, "setUser", {
80
+ enumerable: true,
81
+ configurable: true,
82
+ writable: true,
83
+ value: _a.instance.setUser
84
+ });
85
+ Object.defineProperty(API, "updateUser", {
86
+ enumerable: true,
87
+ configurable: true,
88
+ writable: true,
89
+ value: _a.instance.updateUser
90
+ });
91
+ Object.defineProperty(API, "logout", {
92
+ enumerable: true,
93
+ configurable: true,
94
+ writable: true,
95
+ value: _a.instance.logout
96
+ });
97
+ Object.defineProperty(API, "setToken", {
98
+ enumerable: true,
99
+ configurable: true,
100
+ writable: true,
101
+ value: _a.instance.setToken
102
+ });
103
+ Object.defineProperty(API, "addListener", {
104
+ enumerable: true,
105
+ configurable: true,
106
+ writable: true,
107
+ value: _a.instance.addListener
108
+ });
109
+ exports.default = API;
package/lib/types.d.ts ADDED
@@ -0,0 +1,25 @@
1
+ export type TCache = {
2
+ [index: string]: any;
3
+ };
4
+ export type TData = {
5
+ [index: string]: any;
6
+ };
7
+ export type TMethod = 'GET' | 'POST';
8
+ export type TParams = {
9
+ path: string;
10
+ data?: TData;
11
+ cache?: string | boolean;
12
+ globalName?: string;
13
+ success?: (...args: any) => void;
14
+ error?: (...args: any) => void;
15
+ fail?: (...args: any) => void;
16
+ complete?: (...args: any) => void;
17
+ };
18
+ export type TRequestParams = {
19
+ method: TMethod;
20
+ headers?: {
21
+ [index: string]: string;
22
+ };
23
+ body?: string;
24
+ };
25
+ export type TListenerEvents = 'api-request-fail' | 'api-request-error' | 'api-request-complete' | 'login' | 'logout' | 'user-update';
package/lib/types.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "larapi",
3
+ "version": "0.0.1",
4
+ "description": "API client for laravel",
5
+ "main": "lib/index.js",
6
+ "types": "lib/index.d.ts",
7
+ "scripts": {
8
+ "test": "jest",
9
+ "build": "tsc"
10
+ },
11
+ "jest": {
12
+ "transform": {
13
+ "^.+\\.[t|j]sx?$": "babel-jest"
14
+ }
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/SergoMorello/larasopp.client.git"
19
+ },
20
+ "keywords": [
21
+ "api",
22
+ "laravel",
23
+ "cache",
24
+ "events"
25
+ ],
26
+ "author": "Sergey Serov",
27
+ "license": "MIT",
28
+ "bugs": {
29
+ "url": "https://github.com/SergoMorello/larasopp.client/issues"
30
+ },
31
+ "homepage": "https://github.com/SergoMorello/larasopp.client#readme",
32
+ "devDependencies": {
33
+ "@babel/preset-env": "^7.22.9",
34
+ "@types/jest": "^29.5.3",
35
+ "babel-jest": "^29.4.3",
36
+ "jest": "^29.4.3",
37
+ "typescript": "^5.1.6"
38
+ },
39
+ "dependencies": {
40
+ "easy-event-emitter": "^1.0.7",
41
+ "md5": "^2.3.0"
42
+ }
43
+ }
package/src/Core.ts ADDED
@@ -0,0 +1,222 @@
1
+ //@ts-ignore
2
+ import md5 from 'md5';
3
+ import EventEmitter,{
4
+ Events,
5
+ Event
6
+ } from 'easy-event-emitter';
7
+ import {
8
+ TCache,
9
+ TData,
10
+ TMethod,
11
+ TParams,
12
+ TRequestParams,
13
+ TListenerEvents
14
+ } from './types';
15
+
16
+ abstract class Core {
17
+ private host: string;
18
+ token: string;
19
+ cache: TCache;
20
+ protected events: Events;
21
+
22
+ constructor() {
23
+ this.events = new EventEmitter();
24
+ this.host = 'http://127.0.0.1/';
25
+ this.token = '';
26
+ this.cache = {};
27
+
28
+ this.setHost = this.setHost.bind(this);
29
+ this.setToken = this.setToken.bind(this);
30
+ this.addListener = this.addListener.bind(this);
31
+ }
32
+
33
+ protected request(method: TMethod, params: TParams) {
34
+
35
+ //Заголовки от 200 до 299
36
+ const success = (...args: any) => {
37
+ if (typeof params.success === 'function')
38
+ params.success(...args);
39
+ };
40
+
41
+ //Заголовки от 400 до 499
42
+ const fail = (...args: any) => {
43
+ if (typeof params.fail === 'function') {
44
+ params.fail(...args);
45
+ }
46
+
47
+ this.events.emit('api-request-fail', args);
48
+ };
49
+
50
+ //Все остальные
51
+ const error = (...args: any) => {
52
+ if (typeof params.error === 'function') {
53
+ params.error(...args);
54
+ }
55
+
56
+ this.events.emit('api-request-error', args);
57
+ };
58
+
59
+ const complete = (...args: any) => {
60
+ if (typeof params.complete === 'function') {
61
+ params.complete(...args);
62
+ }
63
+
64
+ this.events.emit('api-request-complete', args);
65
+ };
66
+
67
+ const encodeUrlParams = (data: TData) => {
68
+ return Object.keys(data).map(function(k) {
69
+ return encodeURIComponent(k) + "=" + encodeURIComponent(data[k]);
70
+ }).join('&');
71
+ };
72
+
73
+ const cuteUndifinedParams = (data: TData) => {
74
+ if (typeof data !== 'object') {
75
+ return;
76
+ }
77
+ for(const key in data) {
78
+ if (!data[key]) {
79
+ delete data[key];
80
+ }
81
+ }
82
+ return data;
83
+ };
84
+
85
+ var path = params.path;
86
+ const fetchParams: TRequestParams = {
87
+ method: method
88
+ };
89
+
90
+ fetchParams.headers = {
91
+ 'Accept': 'application/json',
92
+ 'Content-Type': 'application/json',
93
+ 'Authorization': 'Bearer ' + this.token
94
+ };
95
+
96
+ params.data = params.data ? cuteUndifinedParams(params.data) : params.data;
97
+
98
+ if (method === 'POST') {
99
+ fetchParams.body = JSON.stringify(params.data);
100
+ }
101
+
102
+ if (method === 'GET') {
103
+ if (params.data)
104
+ path = params.path + '?' + encodeUrlParams(params.data);
105
+ }
106
+ const pathCache = md5(path + JSON.stringify(params.data));
107
+ var dataCache;
108
+
109
+ 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
+ }
116
+
117
+ if (params.cache) {
118
+ if (dataCache = this.getCache(pathCache)) {
119
+ success(dataCache);
120
+ complete(dataCache);
121
+ }
122
+ }else{
123
+ this.deleteCache(pathCache);
124
+ }
125
+ 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
+ }
132
+
133
+ xhr.onreadystatechange = () => {
134
+ if (xhr.readyState !== 4) {
135
+ return;
136
+ }
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);
148
+ }
149
+
150
+ error(result);
151
+ console.error(result);
152
+ }
153
+ complete(result);
154
+ }
155
+
156
+ xhr.send(fetchParams.body);
157
+ }
158
+ return {
159
+ clearCache: () => {
160
+ this.deleteCache(pathCache);
161
+ }
162
+ }
163
+ }
164
+
165
+ public addListener(event: TListenerEvents, callback: (data: any) => void): Event {
166
+ return this.events.addListener(event, callback);
167
+ }
168
+
169
+ private getCache(request: string): TData | undefined {
170
+ const key = md5(request);
171
+ if (this.cache[key]) {
172
+ if ((new Date()).getTime() <= (this.cache[key].time + 3600000000)) {
173
+ return this.cache[key].data;
174
+ }else{
175
+ delete this.cache[key];
176
+ }
177
+ }
178
+ }
179
+
180
+ public setHost(host: string): void {
181
+ this.host = host;
182
+ }
183
+
184
+ private setCache(request: string, data: TData, group?: string): void {
185
+ this.cache[md5(request)] = {
186
+ time: (new Date()).getTime(),
187
+ data: data,
188
+ group: group
189
+ };
190
+ }
191
+
192
+ private deleteCacheGroup(group: string): void {
193
+ for(const key in this.cache) {
194
+ if (this.cache[key].group === group) {
195
+ delete this.cache[key];
196
+ }
197
+ }
198
+ }
199
+
200
+ private deleteCache(request: string): void {
201
+ const keyCache = md5(request);
202
+ if (!this.cache[keyCache])
203
+ return;
204
+
205
+ const group = this.cache[keyCache].group;
206
+ if (group) {
207
+ this.deleteCacheGroup(group);
208
+ }
209
+ delete this.cache[keyCache];
210
+ }
211
+
212
+ public setToken(token: string) {
213
+ localStorage.setItem('token', token);
214
+ this.token = token;
215
+ }
216
+
217
+ public getToken() {
218
+ return this.token;
219
+ }
220
+ }
221
+
222
+ export default Core;
package/src/index.ts ADDED
@@ -0,0 +1,84 @@
1
+ import Core from "./Core";
2
+ import { TParams } from "./types";
3
+
4
+ type TUser = {
5
+ [index: string]: any;
6
+ }
7
+
8
+ type TConfig = {
9
+ host: string;
10
+ }
11
+
12
+ class API extends Core {
13
+ private static instance = new API({
14
+ host: ''
15
+ });
16
+ private _user: TUser;
17
+
18
+ constructor(config: TConfig) {
19
+ super();
20
+ this.setHost(config.host);
21
+ this._user = {};
22
+ this.init();
23
+
24
+ this.setUser = this.setUser.bind(this);
25
+ this.updateUser = this.updateUser.bind(this);
26
+ this.logout = this.logout.bind(this);
27
+ }
28
+
29
+ public get(params: TParams) {
30
+ return this.request('GET', params);
31
+ }
32
+
33
+ public post(params: TParams) {
34
+ return this.request('POST', params);
35
+ }
36
+
37
+ private init(): void {
38
+ const token = localStorage.getItem('token');
39
+ const strUser = localStorage.getItem('user');
40
+ if (strUser) {
41
+ const user: TUser = JSON.parse(strUser);
42
+ this.setUser(user);
43
+ }
44
+ if (token) {
45
+ this.setToken(token);
46
+ }
47
+ }
48
+
49
+ public get uid(): number {
50
+ return 'id' in this._user ? Number(this._user.id) : 0;
51
+ }
52
+
53
+ public get user(): TUser {
54
+ return this._user;
55
+ }
56
+
57
+ public setUser(user: TUser) {
58
+ localStorage.setItem('user', JSON.stringify(user));
59
+ this._user = user;
60
+ this.events.emit('login', user);
61
+ }
62
+
63
+ public updateUser(user: TUser) {
64
+ this.events.emit('update', this.user);
65
+ this.setUser({...this.user, ...user});
66
+ }
67
+
68
+ public logout() {
69
+ this.events.emit('logout', this.user);
70
+ localStorage.removeItem('user');
71
+ localStorage.removeItem('token');
72
+ this.setUser({});
73
+ this.setToken('');
74
+ }
75
+
76
+ public static setHost = this.instance.setHost;
77
+ public static setUser = this.instance.setUser;
78
+ public static updateUser = this.instance.updateUser;
79
+ public static logout = this.instance.logout;
80
+ public static setToken = this.instance.setToken;
81
+ public static addListener = this.instance.addListener;
82
+ }
83
+
84
+ export default API;
package/src/types.ts ADDED
@@ -0,0 +1,30 @@
1
+ export type TCache = {
2
+ [index: string]: any;
3
+ }
4
+
5
+ export type TData = {
6
+ [index: string]: any;
7
+ }
8
+
9
+ export type TMethod = 'GET' | 'POST';
10
+
11
+ export type TParams = {
12
+ path: string;
13
+ data?: TData;
14
+ cache?: string | boolean;
15
+ globalName?: string;
16
+ success?: (...args: any) => void;
17
+ error?: (...args: any) => void;
18
+ fail?: (...args: any) => void;
19
+ complete?: (...args: any) => void;
20
+ }
21
+
22
+ export type TRequestParams = {
23
+ method: TMethod;
24
+ headers?: {
25
+ [index: string]: string;
26
+ };
27
+ body?: string;
28
+ }
29
+
30
+ export type TListenerEvents = 'api-request-fail' | 'api-request-error' | 'api-request-complete' | 'login' | 'logout' | 'user-update';
package/tsconfig.json ADDED
@@ -0,0 +1,109 @@
1
+ {
2
+ "compilerOptions": {
3
+ /* Visit https://aka.ms/tsconfig to read more about this file */
4
+
5
+ /* Projects */
6
+ // "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
7
+ // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
8
+ // "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
9
+ // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
10
+ // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
11
+ // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
12
+
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. */
16
+ // "jsx": "preserve", /* Specify what JSX code is generated. */
17
+ // "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
18
+ // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
19
+ // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
20
+ // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
21
+ // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
22
+ // "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
23
+ // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
24
+ "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
25
+ // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
26
+
27
+ /* Modules */
28
+ "module": "commonjs", /* Specify what module code is generated. */
29
+ // "rootDir": "./", /* Specify the root folder within your source files. */
30
+ // "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */
31
+ // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
32
+ // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
33
+ // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
34
+ // "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
35
+ // "types": [], /* Specify type package names to be included without being referenced in a source file. */
36
+ // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
37
+ // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
38
+ // "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */
39
+ // "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
40
+ // "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
41
+ // "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
42
+ // "resolveJsonModule": true, /* Enable importing .json files. */
43
+ // "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
44
+ // "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
45
+
46
+ /* JavaScript Support */
47
+ // "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
48
+ // "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
49
+ // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
50
+
51
+ /* Emit */
52
+ "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
53
+ // "declarationMap": true, /* Create sourcemaps for d.ts files. */
54
+ // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
55
+ // "sourceMap": true, /* Create source map files for emitted JavaScript files. */
56
+ // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
57
+ // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
58
+ "outDir": "./lib/", /* Specify an output folder for all emitted files. */
59
+ // "removeComments": true, /* Disable emitting comments. */
60
+ // "noEmit": true, /* Disable emitting files from a compilation. */
61
+ // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
62
+ // "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */
63
+ // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
64
+ // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
65
+ // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
66
+ // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
67
+ // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
68
+ // "newLine": "crlf", /* Set the newline character for emitting files. */
69
+ // "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
70
+ // "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
71
+ // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
72
+ // "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
73
+ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */
74
+ // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
75
+
76
+ /* Interop Constraints */
77
+ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
78
+ // "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
79
+ // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
80
+ "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
81
+ // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
82
+ "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
83
+
84
+ /* Type Checking */
85
+ "strict": true, /* Enable all strict type-checking options. */
86
+ // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
87
+ // "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
88
+ // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
89
+ // "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
90
+ // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
91
+ // "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
92
+ // "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
93
+ // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
94
+ // "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
95
+ // "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
96
+ // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
97
+ // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
98
+ // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
99
+ // "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
100
+ // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
101
+ // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
102
+ // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
103
+ // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
104
+
105
+ /* Completeness */
106
+ // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
107
+ "skipLibCheck": true /* Skip type checking all .d.ts files. */
108
+ }
109
+ }