larapi 0.2.13 → 0.2.15
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 +37 -0
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/lib/Core.d.ts +5 -3
- package/lib/Http.d.ts +6 -4
- package/lib/Queue.d.ts +7 -9
- package/lib/index.d.ts +32 -34
- package/lib/types.d.ts +36 -8
- package/package.json +1 -1
- package/src/Core.ts +62 -6
- package/src/Http.ts +13 -10
- package/src/Queue.ts +13 -15
- package/src/index.ts +30 -29
- package/src/types.ts +43 -9
package/lib/Http.d.ts
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
import Core from "./Core";
|
|
2
|
+
import Queue from './Queue';
|
|
2
3
|
import { Event, Events } from "easy-event-emitter";
|
|
3
|
-
import type { TMethod, TParams, TData, TRequestParams, TListenerEvents, TRequestProgress } from "./types";
|
|
4
|
-
declare class Http extends Core {
|
|
4
|
+
import type { TMethod, TParams, TData, TRequestParams, TListenerEvents, TRequestProgress, TResponseData } from "./types";
|
|
5
|
+
declare class Http<D extends TResponseData = TResponseData, PATH = any, DATA extends ((...args: any) => any) = any> extends Core {
|
|
5
6
|
private cacheIndex;
|
|
6
7
|
currentCache?: TData;
|
|
7
8
|
currentEvents: Events;
|
|
8
9
|
requestParams: TRequestParams;
|
|
9
10
|
method: TMethod;
|
|
10
|
-
params: TParams
|
|
11
|
+
params: TParams<PATH, DATA>;
|
|
11
12
|
path: string;
|
|
13
|
+
queue: Queue<D>;
|
|
12
14
|
queueName: string;
|
|
13
|
-
constructor(method: TMethod, params: TParams, context?: Core);
|
|
15
|
+
constructor(method: TMethod, params: TParams<PATH, DATA>, context?: Core);
|
|
14
16
|
private initRequest;
|
|
15
17
|
private initCache;
|
|
16
18
|
private initQueue;
|
package/lib/Queue.d.ts
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
import Http from "./Http";
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
import type { TResponseData } from "./types";
|
|
3
|
+
declare class Queue<D extends TResponseData = TResponseData> {
|
|
4
|
+
private static _Queue;
|
|
4
5
|
private queue;
|
|
5
|
-
|
|
6
|
+
http: Http<D>;
|
|
7
|
+
constructor(http: Http<D>);
|
|
6
8
|
has(request: string): boolean;
|
|
7
|
-
get(request: string): Http | undefined;
|
|
9
|
+
get(request: string): Http<TResponseData, any, any> | undefined;
|
|
8
10
|
clear(request: string): void;
|
|
9
|
-
push(request: string
|
|
10
|
-
static has: (request: string) => boolean;
|
|
11
|
-
static get: (request: string) => Http | undefined;
|
|
12
|
-
static clear: (request: string) => void;
|
|
13
|
-
static push: (request: string, instance: Http) => boolean;
|
|
11
|
+
push(request: string): boolean;
|
|
14
12
|
}
|
|
15
13
|
export default Queue;
|
package/lib/index.d.ts
CHANGED
|
@@ -1,50 +1,44 @@
|
|
|
1
1
|
import Core from "./Core";
|
|
2
|
-
import type { TMethod, TParams, TListenerEvents } from "./types";
|
|
2
|
+
import type { TUser, TConfig, TMethod, TParams, TResponseData, TListenerEvents } from "./types";
|
|
3
3
|
import type { Event } from "easy-event-emitter";
|
|
4
4
|
import Http from "./Http";
|
|
5
|
-
type TUser = {
|
|
6
|
-
id: number;
|
|
7
|
-
[index: string]: any;
|
|
8
|
-
};
|
|
9
|
-
type TConfig = {
|
|
10
|
-
host: string;
|
|
11
|
-
};
|
|
12
5
|
/** Laravel API Client */
|
|
13
|
-
declare class API<
|
|
6
|
+
declare class API<D extends TResponseData = TResponseData, U extends TUser = TUser, S extends D['success'] = D['success']> extends Core {
|
|
14
7
|
private static instance;
|
|
15
8
|
private _user;
|
|
16
9
|
token?: string;
|
|
17
10
|
constructor(config: TConfig);
|
|
18
|
-
http(method: TMethod, params: TParams): Http
|
|
19
|
-
get(params: TParams): Http
|
|
20
|
-
head(params: TParams): Http
|
|
21
|
-
post(params: TParams): Http
|
|
22
|
-
put(params: TParams): Http
|
|
23
|
-
patch(params: TParams): Http
|
|
24
|
-
delete(params: TParams): Http
|
|
25
|
-
options(params: TParams): Http
|
|
26
|
-
connect(params: TParams): Http
|
|
27
|
-
trace(params: TParams): Http
|
|
11
|
+
http<PATH extends keyof S, DATA extends S[PATH]>(method: TMethod, params: TParams<PATH, DATA>): Http<D, PATH, DATA>;
|
|
12
|
+
get<PATH extends keyof S, DATA extends S[PATH]>(params: TParams<PATH, DATA, 'GET'>): Http<D, PATH, DATA>;
|
|
13
|
+
head<PATH extends keyof S, DATA extends S[PATH]>(params: TParams<PATH, DATA>): Http<D, PATH, DATA>;
|
|
14
|
+
post<PATH extends keyof S, DATA extends S[PATH]>(params: TParams<PATH, DATA>): Http<D, PATH, DATA>;
|
|
15
|
+
put<PATH extends keyof S, DATA extends S[PATH]>(params: TParams<PATH, DATA>): Http<D, PATH, DATA>;
|
|
16
|
+
patch<PATH extends keyof S, DATA extends S[PATH]>(params: TParams<PATH, DATA>): Http<D, PATH, DATA>;
|
|
17
|
+
delete<PATH extends keyof S, DATA extends S[PATH]>(params: TParams<PATH, DATA>): Http<D, PATH, DATA>;
|
|
18
|
+
options<PATH extends keyof S, DATA extends S[PATH]>(params: TParams<PATH, DATA>): Http<D, PATH, DATA>;
|
|
19
|
+
connect<PATH extends keyof S, DATA extends S[PATH]>(params: TParams<PATH, DATA>): Http<D, PATH, DATA>;
|
|
20
|
+
trace<PATH extends keyof S, DATA extends S[PATH]>(params: TParams<PATH, DATA>): Http<D, PATH, DATA>;
|
|
28
21
|
addListener(event: TListenerEvents, callback: (data: any) => void): Event;
|
|
29
22
|
setToken(token: string): Promise<void>;
|
|
30
23
|
getToken(): string | undefined;
|
|
31
24
|
getUid(): number;
|
|
32
|
-
getUser():
|
|
25
|
+
getUser(): U;
|
|
33
26
|
get uid(): number;
|
|
34
|
-
get user():
|
|
35
|
-
setUser(user:
|
|
36
|
-
updateUser(user:
|
|
27
|
+
get user(): U;
|
|
28
|
+
setUser(user: U): Promise<void>;
|
|
29
|
+
updateUser(user: U): void;
|
|
37
30
|
logout(): Promise<void>;
|
|
38
|
-
static http: (method: TMethod, params: TParams) => Http
|
|
39
|
-
static get: (params: TParams) => Http
|
|
40
|
-
static head: (params: TParams) => Http
|
|
41
|
-
static post: (params: TParams) => Http
|
|
42
|
-
static put: (params: TParams) => Http
|
|
43
|
-
static patch: (params: TParams) => Http
|
|
44
|
-
static delete: (params: TParams) => Http
|
|
45
|
-
static options: (params: TParams) => Http
|
|
46
|
-
static connect: (params: TParams) => Http
|
|
47
|
-
static trace: (params: TParams) => Http
|
|
31
|
+
static http: <PATH extends string | number, DATA extends import("./types").TData[PATH]>(method: TMethod, params: TParams<PATH, DATA>) => Http<TResponseData, PATH, DATA>;
|
|
32
|
+
static get: <PATH extends string | number, DATA extends import("./types").TData[PATH]>(params: TParams<PATH, DATA, "GET">) => Http<TResponseData, PATH, DATA>;
|
|
33
|
+
static head: <PATH extends string | number, DATA extends import("./types").TData[PATH]>(params: TParams<PATH, DATA>) => Http<TResponseData, PATH, DATA>;
|
|
34
|
+
static post: <PATH extends string | number, DATA extends import("./types").TData[PATH]>(params: TParams<PATH, DATA>) => Http<TResponseData, PATH, DATA>;
|
|
35
|
+
static put: <PATH extends string | number, DATA extends import("./types").TData[PATH]>(params: TParams<PATH, DATA>) => Http<TResponseData, PATH, DATA>;
|
|
36
|
+
static patch: <PATH extends string | number, DATA extends import("./types").TData[PATH]>(params: TParams<PATH, DATA>) => Http<TResponseData, PATH, DATA>;
|
|
37
|
+
static delete: <PATH extends string | number, DATA extends import("./types").TData[PATH]>(params: TParams<PATH, DATA>) => Http<TResponseData, PATH, DATA>;
|
|
38
|
+
static options: <PATH extends string | number, DATA extends import("./types").TData[PATH]>(params: TParams<PATH, DATA>) => Http<TResponseData, PATH, DATA>;
|
|
39
|
+
static connect: <PATH extends string | number, DATA extends import("./types").TData[PATH]>(params: TParams<PATH, DATA>) => Http<TResponseData, PATH, DATA>;
|
|
40
|
+
static trace: <PATH extends string | number, DATA extends import("./types").TData[PATH]>(params: TParams<PATH, DATA>) => Http<TResponseData, PATH, DATA>;
|
|
41
|
+
static setConfig: (config: TConfig) => void;
|
|
48
42
|
static setHost: (host: string) => void;
|
|
49
43
|
static setUser: (user: TUser) => Promise<void>;
|
|
50
44
|
static updateUser: (user: TUser) => void;
|
|
@@ -65,5 +59,9 @@ declare class API<USER extends TUser = TUser> extends Core {
|
|
|
65
59
|
*/
|
|
66
60
|
static deleteCacheGroup: (groups: string | string[], data?: import("./types").TData | undefined, fieldKey?: string | null) => void;
|
|
67
61
|
}
|
|
68
|
-
export type {
|
|
62
|
+
export type {
|
|
63
|
+
/**
|
|
64
|
+
* @deprecated The type should not be used
|
|
65
|
+
*/
|
|
66
|
+
Event, Event as EventListener, TResponseData };
|
|
69
67
|
export default API;
|
package/lib/types.d.ts
CHANGED
|
@@ -3,11 +3,18 @@ export type TCacheBody = {
|
|
|
3
3
|
group?: string;
|
|
4
4
|
time: number;
|
|
5
5
|
};
|
|
6
|
+
export type TUser = {
|
|
7
|
+
id: number;
|
|
8
|
+
[index: string]: any;
|
|
9
|
+
};
|
|
6
10
|
export type TCache = {
|
|
7
11
|
[index: string]: TCacheBody;
|
|
8
12
|
};
|
|
13
|
+
export type TError = {
|
|
14
|
+
message?: string;
|
|
15
|
+
};
|
|
9
16
|
export type TData = {
|
|
10
|
-
[index: string]: any;
|
|
17
|
+
[index: string]: any | ((...args: any) => any);
|
|
11
18
|
};
|
|
12
19
|
export type TGroupsData = {
|
|
13
20
|
[index: string]: TData;
|
|
@@ -22,18 +29,39 @@ export type TRequestProgress = {
|
|
|
22
29
|
total: number;
|
|
23
30
|
loaded: number;
|
|
24
31
|
};
|
|
25
|
-
export type
|
|
26
|
-
|
|
27
|
-
|
|
32
|
+
export type TCoreRviverObject = {
|
|
33
|
+
and?: {
|
|
34
|
+
[index: string]: string[];
|
|
35
|
+
};
|
|
36
|
+
or?: {
|
|
37
|
+
[index: string]: string[];
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
export type TConfigDataReviver = {
|
|
41
|
+
[index: string]: (value: any) => any;
|
|
42
|
+
};
|
|
43
|
+
export type TConfig = {
|
|
44
|
+
host: string;
|
|
45
|
+
reviver?: (this: any, key: string, value: any) => any;
|
|
46
|
+
dataReviver?: TConfigDataReviver;
|
|
47
|
+
};
|
|
48
|
+
export type TResponseData = {
|
|
49
|
+
success: TData;
|
|
50
|
+
error?: TError;
|
|
51
|
+
fail?: TError;
|
|
52
|
+
};
|
|
53
|
+
export type TParams<P = string | keyof TResponseData['success'], D extends ((...args: any) => any) = TResponseData['success']['?'] | ((...args: any) => any), M extends TMethod = TMethod, E extends TError = TError, F extends TError = TError> = {
|
|
54
|
+
path: P;
|
|
55
|
+
data?: Parameters<D>[0];
|
|
28
56
|
cache?: string | boolean;
|
|
29
57
|
cacheUpdate?: TCacheControll | TCacheControll[];
|
|
30
58
|
cacheClear?: TCacheControll | TCacheControll[];
|
|
31
59
|
globalName?: string;
|
|
32
60
|
queueThrottling?: boolean;
|
|
33
|
-
success?: (data:
|
|
34
|
-
error?: (
|
|
35
|
-
fail?: (
|
|
36
|
-
complete?: (
|
|
61
|
+
success?: (data: ReturnType<D>) => void;
|
|
62
|
+
error?: (error: E) => void;
|
|
63
|
+
fail?: (fail: F) => void;
|
|
64
|
+
complete?: (complete: any) => void;
|
|
37
65
|
progress?: (progress: TRequestProgress) => void;
|
|
38
66
|
};
|
|
39
67
|
export type TRequestParams = {
|
package/package.json
CHANGED
package/src/Core.ts
CHANGED
|
@@ -2,25 +2,30 @@ import EventEmitter,{
|
|
|
2
2
|
Events
|
|
3
3
|
} from 'easy-event-emitter';
|
|
4
4
|
import type {
|
|
5
|
+
TConfig,
|
|
5
6
|
TCache,
|
|
6
7
|
TCacheBody,
|
|
7
8
|
TData,
|
|
8
|
-
TGroupsData
|
|
9
|
+
TGroupsData,
|
|
10
|
+
TCoreRviverObject
|
|
9
11
|
} from './types';
|
|
10
12
|
|
|
11
13
|
abstract class Core {
|
|
12
14
|
private cache: TCache;
|
|
13
15
|
initData: TGroupsData;
|
|
14
|
-
protected events: Events;
|
|
15
|
-
|
|
16
|
+
protected readonly events: Events;
|
|
17
|
+
config: TConfig;
|
|
16
18
|
|
|
17
19
|
|
|
18
20
|
constructor(context?: Core) {
|
|
19
21
|
this.events = new EventEmitter();
|
|
20
|
-
this.
|
|
22
|
+
this.config = {
|
|
23
|
+
host: 'http://127.0.0.1/'
|
|
24
|
+
};
|
|
21
25
|
this.cache = {};
|
|
22
26
|
this.initData = {};
|
|
23
27
|
|
|
28
|
+
this.setConfig = this.setConfig.bind(this);
|
|
24
29
|
this.setHost = this.setHost.bind(this);
|
|
25
30
|
this.setInitData = this.setInitData.bind(this);
|
|
26
31
|
this.triggerByCacheGroup = this.triggerByCacheGroup.bind(this);
|
|
@@ -28,7 +33,7 @@ abstract class Core {
|
|
|
28
33
|
this.updateCacheGroup = this.updateCacheGroup.bind(this);
|
|
29
34
|
if (context) {
|
|
30
35
|
Object.assign(this, {
|
|
31
|
-
|
|
36
|
+
config: context?.config,
|
|
32
37
|
events: context?.events,
|
|
33
38
|
cache: context?.cache,
|
|
34
39
|
initData: context?.initData
|
|
@@ -36,6 +41,10 @@ abstract class Core {
|
|
|
36
41
|
}
|
|
37
42
|
}
|
|
38
43
|
|
|
44
|
+
protected setConfig(config: TConfig) {
|
|
45
|
+
this.config = config;
|
|
46
|
+
}
|
|
47
|
+
|
|
39
48
|
protected getCache(key: string): TData | undefined {
|
|
40
49
|
if (this.cache[key]) {
|
|
41
50
|
if ((new Date()).getTime() <= (this.cache[key].time + 3600000000)) {
|
|
@@ -55,8 +64,55 @@ abstract class Core {
|
|
|
55
64
|
return true;
|
|
56
65
|
}
|
|
57
66
|
|
|
67
|
+
protected jsonParse(str: string) {
|
|
68
|
+
if (typeof this.config.reviver === 'function') {
|
|
69
|
+
return JSON.parse(str, this.config.reviver);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (this.config.dataReviver) {
|
|
73
|
+
const replace = this.config.dataReviver;
|
|
74
|
+
const dataKeys = Object.keys(replace);
|
|
75
|
+
const ifKeys: TCoreRviverObject = {};
|
|
76
|
+
|
|
77
|
+
dataKeys.forEach((key) => {
|
|
78
|
+
const and = key.split('&');
|
|
79
|
+
if (and.length > 1) {
|
|
80
|
+
ifKeys.and = ifKeys.and ?? {};
|
|
81
|
+
if (key in ifKeys.and) {
|
|
82
|
+
ifKeys.and[key].push(...and);
|
|
83
|
+
}else{
|
|
84
|
+
ifKeys.and[key] = and;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
const data = JSON.parse(str, (key, value) => {
|
|
90
|
+
if ((key in replace && typeof replace[key] === 'function')) {
|
|
91
|
+
return replace[key](value);
|
|
92
|
+
}
|
|
93
|
+
return value;
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
if (ifKeys.and && data) {
|
|
97
|
+
const dataKeys = Object.keys(data);
|
|
98
|
+
|
|
99
|
+
if (ifKeys.and) {
|
|
100
|
+
for (const [key, keys] of Object.entries(ifKeys.and)) {
|
|
101
|
+
if (keys.length === dataKeys.length && keys.every((key) => dataKeys.includes(key))) {
|
|
102
|
+
return replace[key](data);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return data;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return JSON.parse(str);
|
|
112
|
+
}
|
|
113
|
+
|
|
58
114
|
public setHost(host: string): void {
|
|
59
|
-
this.host = host;
|
|
115
|
+
this.config.host = host;
|
|
60
116
|
}
|
|
61
117
|
|
|
62
118
|
protected setCache(key: string, data: TData, group?: string): void {
|
package/src/Http.ts
CHANGED
|
@@ -12,27 +12,30 @@ import type {
|
|
|
12
12
|
TRequestParams,
|
|
13
13
|
TListenerEvents,
|
|
14
14
|
TCacheControll,
|
|
15
|
-
TRequestProgress
|
|
15
|
+
TRequestProgress,
|
|
16
|
+
TResponseData
|
|
16
17
|
} from "./types";
|
|
17
18
|
|
|
18
|
-
class Http extends Core {
|
|
19
|
+
class Http<D extends TResponseData = TResponseData, PATH = any, DATA extends ((...args: any) => any) = any> extends Core {
|
|
19
20
|
private cacheIndex: string;
|
|
20
21
|
currentCache?: TData;
|
|
21
22
|
currentEvents: Events
|
|
22
23
|
requestParams: TRequestParams;
|
|
23
24
|
method: TMethod;
|
|
24
|
-
params: TParams
|
|
25
|
+
params: TParams<PATH, DATA>;
|
|
25
26
|
path: string;
|
|
27
|
+
queue: Queue<D>;
|
|
26
28
|
queueName: string;
|
|
27
29
|
|
|
28
|
-
constructor(method: TMethod, params: TParams, context?: Core) {
|
|
30
|
+
constructor(method: TMethod, params: TParams<PATH, DATA>, context?: Core) {
|
|
29
31
|
super(context);
|
|
30
32
|
this.currentEvents = new EventEmitter();
|
|
33
|
+
this.queue = new Queue<D>(this);
|
|
31
34
|
this.cacheIndex = '';
|
|
32
35
|
this.queueName = '';
|
|
33
36
|
this.method = method;
|
|
34
37
|
this.params = params;
|
|
35
|
-
this.path = this.params.path;
|
|
38
|
+
this.path = this.params.path as string;
|
|
36
39
|
this.requestParams = {
|
|
37
40
|
method: this.method
|
|
38
41
|
};
|
|
@@ -62,7 +65,7 @@ class Http extends Core {
|
|
|
62
65
|
|
|
63
66
|
if ((['GET', 'HEAD'] as TMethod[]).includes(this.method)) {
|
|
64
67
|
if (this.params.data)
|
|
65
|
-
this.path = this.params.path + '?' + this.encodeUrlParams(this.params.data);
|
|
68
|
+
this.path = (this.params.path as string) + '?' + this.encodeUrlParams(this.params.data);
|
|
66
69
|
|
|
67
70
|
if (this.method === 'HEAD') {
|
|
68
71
|
this.requestParams.withoutResponse = true;
|
|
@@ -189,12 +192,12 @@ class Http extends Core {
|
|
|
189
192
|
}
|
|
190
193
|
|
|
191
194
|
public request(): this {
|
|
192
|
-
if (typeof XMLHttpRequest === 'undefined' || this.currentCache ||
|
|
195
|
+
if (typeof XMLHttpRequest === 'undefined' || this.currentCache || this.queue.push(this.queueName)) return this;
|
|
193
196
|
|
|
194
197
|
try {
|
|
195
198
|
const xhr = new XMLHttpRequest();
|
|
196
199
|
|
|
197
|
-
xhr.open(this.method, this.host + this.path, true);
|
|
200
|
+
xhr.open(this.method, this.config.host + this.path, true);
|
|
198
201
|
for(const header in this.requestParams.headers) {
|
|
199
202
|
xhr.setRequestHeader(header, this.requestParams.headers[header]);
|
|
200
203
|
}
|
|
@@ -226,7 +229,7 @@ class Http extends Core {
|
|
|
226
229
|
xhr.onreadystatechange = () => {
|
|
227
230
|
if (xhr.readyState !== 4) return;
|
|
228
231
|
|
|
229
|
-
const result = (!this.requestParams.withoutResponse && this.isJsonString(xhr.responseText)) ?
|
|
232
|
+
const result = (!this.requestParams.withoutResponse && this.isJsonString(xhr.responseText)) ? this.jsonParse(xhr.responseText) : {};
|
|
230
233
|
|
|
231
234
|
if (xhr.status >= 200 && xhr.status <= 299) {
|
|
232
235
|
if (this.params.cache) {
|
|
@@ -242,7 +245,7 @@ class Http extends Core {
|
|
|
242
245
|
console.warn(result);
|
|
243
246
|
}
|
|
244
247
|
this.complete(result);
|
|
245
|
-
|
|
248
|
+
this.queue.clear(this.queueName);
|
|
246
249
|
}
|
|
247
250
|
xhr.send(this.requestParams.body);
|
|
248
251
|
}catch(e) {
|
package/src/Queue.ts
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
import Http from "./Http";
|
|
2
|
+
import type { TResponseData } from "./types";
|
|
2
3
|
|
|
3
4
|
type TQueue = {
|
|
4
5
|
[index: string]: Http;
|
|
5
6
|
};
|
|
6
7
|
|
|
7
|
-
class Queue {
|
|
8
|
-
private static
|
|
9
|
-
private queue
|
|
8
|
+
class Queue<D extends TResponseData = TResponseData> {
|
|
9
|
+
private static _Queue: TQueue = {};
|
|
10
|
+
private queue = Queue._Queue;
|
|
11
|
+
http: Http<D>;
|
|
10
12
|
|
|
11
|
-
constructor() {
|
|
13
|
+
constructor(http: Http<D>) {
|
|
14
|
+
this.http = http;
|
|
12
15
|
this.has = this.has.bind(this);
|
|
13
16
|
this.get = this.get.bind(this);
|
|
14
17
|
this.clear = this.clear.bind(this);
|
|
@@ -29,23 +32,18 @@ class Queue {
|
|
|
29
32
|
}
|
|
30
33
|
}
|
|
31
34
|
|
|
32
|
-
public push(request: string
|
|
35
|
+
public push(request: string): boolean {
|
|
33
36
|
const refInstance = this.get(request);
|
|
34
37
|
if (!refInstance) {
|
|
35
|
-
this.queue[request] =
|
|
38
|
+
(this.queue[request] as any) = this.http;
|
|
36
39
|
}else{
|
|
37
|
-
refInstance?.addListener('api-request-success',
|
|
38
|
-
refInstance?.addListener('api-request-error',
|
|
39
|
-
refInstance?.addListener('api-request-fail',
|
|
40
|
-
refInstance?.addListener('api-request-complete',
|
|
40
|
+
refInstance?.addListener('api-request-success', this.http.success);
|
|
41
|
+
refInstance?.addListener('api-request-error', this.http.error);
|
|
42
|
+
refInstance?.addListener('api-request-fail', this.http.fail);
|
|
43
|
+
refInstance?.addListener('api-request-complete', this.http.complete);
|
|
41
44
|
}
|
|
42
45
|
return refInstance instanceof Http;
|
|
43
46
|
}
|
|
44
|
-
|
|
45
|
-
public static has = this.instance.has;
|
|
46
|
-
public static get = this.instance.get;
|
|
47
|
-
public static clear = this.instance.clear;
|
|
48
|
-
public static push = this.instance.push;
|
|
49
47
|
}
|
|
50
48
|
|
|
51
49
|
export default Queue;
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import Core from "./Core";
|
|
2
2
|
import type {
|
|
3
|
+
TUser,
|
|
4
|
+
TConfig,
|
|
3
5
|
TMethod,
|
|
4
6
|
TParams,
|
|
7
|
+
TResponseData,
|
|
5
8
|
TListenerEvents
|
|
6
9
|
} from "./types";
|
|
7
10
|
import type {
|
|
@@ -9,27 +12,19 @@ import type {
|
|
|
9
12
|
} from "easy-event-emitter";
|
|
10
13
|
import Http from "./Http";
|
|
11
14
|
|
|
12
|
-
type TUser = {
|
|
13
|
-
id: number;
|
|
14
|
-
[index: string]: any;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
type TConfig = {
|
|
18
|
-
host: string;
|
|
19
|
-
};
|
|
20
15
|
|
|
21
16
|
/** Laravel API Client */
|
|
22
|
-
class API<
|
|
17
|
+
class API<D extends TResponseData = TResponseData, U extends TUser = TUser, S extends D['success'] = D['success']> extends Core {
|
|
23
18
|
private static instance = new API({
|
|
24
19
|
host: ''
|
|
25
20
|
});
|
|
26
|
-
private _user:
|
|
21
|
+
private _user: U;
|
|
27
22
|
token?: string;
|
|
28
23
|
|
|
29
24
|
constructor(config: TConfig) {
|
|
30
25
|
super();
|
|
31
|
-
this.
|
|
32
|
-
this._user = {} as
|
|
26
|
+
this.setConfig(config);
|
|
27
|
+
this._user = {} as U;
|
|
33
28
|
|
|
34
29
|
this.setToken = this.setToken.bind(this);
|
|
35
30
|
this.setUser = this.setUser.bind(this);
|
|
@@ -51,47 +46,47 @@ class API<USER extends TUser = TUser> extends Core {
|
|
|
51
46
|
this.addListener = this.addListener.bind(this);
|
|
52
47
|
}
|
|
53
48
|
|
|
54
|
-
public http(method: TMethod, params: TParams)
|
|
55
|
-
const http = new Http(method, params, this);
|
|
49
|
+
public http<PATH extends keyof S, DATA extends S[PATH]>(method: TMethod, params: TParams<PATH, DATA>) {
|
|
50
|
+
const http = new Http<D, PATH, DATA>(method, params, this);
|
|
56
51
|
if (this.token) {
|
|
57
52
|
http.addHeader('Authorization', 'Bearer ' + this.token);
|
|
58
53
|
}
|
|
59
54
|
return http;
|
|
60
55
|
}
|
|
61
56
|
|
|
62
|
-
public get(params: TParams)
|
|
57
|
+
public get<PATH extends keyof S, DATA extends S[PATH]>(params: TParams<PATH, DATA, 'GET'>) {
|
|
63
58
|
return this.http('GET', params).request();
|
|
64
59
|
}
|
|
65
60
|
|
|
66
|
-
public head(params: TParams)
|
|
61
|
+
public head<PATH extends keyof S, DATA extends S[PATH]>(params: TParams<PATH, DATA>) {
|
|
67
62
|
return this.http('HEAD', params).request();
|
|
68
63
|
}
|
|
69
64
|
|
|
70
|
-
public post(params: TParams)
|
|
65
|
+
public post<PATH extends keyof S, DATA extends S[PATH]>(params: TParams<PATH, DATA>) {
|
|
71
66
|
return this.http('POST', params).request();
|
|
72
67
|
}
|
|
73
68
|
|
|
74
|
-
public put(params: TParams)
|
|
69
|
+
public put<PATH extends keyof S, DATA extends S[PATH]>(params: TParams<PATH, DATA>) {
|
|
75
70
|
return this.http('PUT', params).request();
|
|
76
71
|
}
|
|
77
72
|
|
|
78
|
-
public patch(params: TParams)
|
|
73
|
+
public patch<PATH extends keyof S, DATA extends S[PATH]>(params: TParams<PATH, DATA>) {
|
|
79
74
|
return this.http('PATCH', params).request();
|
|
80
75
|
}
|
|
81
76
|
|
|
82
|
-
public delete(params: TParams)
|
|
77
|
+
public delete<PATH extends keyof S, DATA extends S[PATH]>(params: TParams<PATH, DATA>) {
|
|
83
78
|
return this.http('DELETE', params).request();
|
|
84
79
|
}
|
|
85
80
|
|
|
86
|
-
public options(params: TParams)
|
|
81
|
+
public options<PATH extends keyof S, DATA extends S[PATH]>(params: TParams<PATH, DATA>) {
|
|
87
82
|
return this.http('OPTIONS', params).request();
|
|
88
83
|
}
|
|
89
84
|
|
|
90
|
-
public connect(params: TParams)
|
|
85
|
+
public connect<PATH extends keyof S, DATA extends S[PATH]>(params: TParams<PATH, DATA>) {
|
|
91
86
|
return this.http('CONNECT', params).request();
|
|
92
87
|
}
|
|
93
88
|
|
|
94
|
-
public trace(params: TParams)
|
|
89
|
+
public trace<PATH extends keyof S, DATA extends S[PATH]>(params: TParams<PATH, DATA>) {
|
|
95
90
|
return this.http('TRACE', params).request();
|
|
96
91
|
}
|
|
97
92
|
|
|
@@ -111,7 +106,7 @@ class API<USER extends TUser = TUser> extends Core {
|
|
|
111
106
|
return (typeof this._user === 'object' && 'id' in this._user) ? Number(this._user.id) : 0;
|
|
112
107
|
}
|
|
113
108
|
|
|
114
|
-
public getUser():
|
|
109
|
+
public getUser(): U {
|
|
115
110
|
return this._user;
|
|
116
111
|
}
|
|
117
112
|
|
|
@@ -119,16 +114,16 @@ class API<USER extends TUser = TUser> extends Core {
|
|
|
119
114
|
return this.getUid();
|
|
120
115
|
}
|
|
121
116
|
|
|
122
|
-
public get user():
|
|
117
|
+
public get user(): U {
|
|
123
118
|
return this.getUser();
|
|
124
119
|
}
|
|
125
120
|
|
|
126
|
-
public async setUser(user:
|
|
121
|
+
public async setUser(user: U) {
|
|
127
122
|
this._user = user;
|
|
128
123
|
this.events.emit('login', user);
|
|
129
124
|
}
|
|
130
125
|
|
|
131
|
-
public updateUser(user:
|
|
126
|
+
public updateUser(user: U) {
|
|
132
127
|
const updatedUser = {...this.user, ...user};
|
|
133
128
|
this.setUser(updatedUser);
|
|
134
129
|
this.events.emit('user-update', updatedUser);
|
|
@@ -136,7 +131,7 @@ class API<USER extends TUser = TUser> extends Core {
|
|
|
136
131
|
|
|
137
132
|
public async logout() {
|
|
138
133
|
const user = this.user;
|
|
139
|
-
this.setUser({} as
|
|
134
|
+
this.setUser({} as U);
|
|
140
135
|
this.setToken('');
|
|
141
136
|
this.events.emit('logout', user);
|
|
142
137
|
}
|
|
@@ -151,6 +146,7 @@ class API<USER extends TUser = TUser> extends Core {
|
|
|
151
146
|
public static options = this.instance.options;
|
|
152
147
|
public static connect = this.instance.connect;
|
|
153
148
|
public static trace = this.instance.trace;
|
|
149
|
+
public static setConfig = this.instance.setConfig;
|
|
154
150
|
public static setHost = this.instance.setHost;
|
|
155
151
|
public static setUser = this.instance.setUser;
|
|
156
152
|
public static updateUser = this.instance.updateUser;
|
|
@@ -175,6 +171,11 @@ class API<USER extends TUser = TUser> extends Core {
|
|
|
175
171
|
|
|
176
172
|
(globalThis as any).apiSetInitData = API.setInitData;
|
|
177
173
|
export type {
|
|
178
|
-
|
|
174
|
+
/**
|
|
175
|
+
* @deprecated The type should not be used
|
|
176
|
+
*/
|
|
177
|
+
Event,
|
|
178
|
+
Event as EventListener,
|
|
179
|
+
TResponseData
|
|
179
180
|
};
|
|
180
181
|
export default API;
|