larapi 0.2.28 → 0.2.30
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/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/lib/types.d.ts +2 -0
- package/package.json +8 -3
- package/__tests__/connect.test.js +0 -5
- package/babel.config.json +0 -3
- package/global.d.ts +0 -5
- package/src/Core.ts +0 -261
- package/src/Http.ts +0 -402
- package/src/Queue.ts +0 -50
- package/src/index.ts +0 -205
- package/src/types.ts +0 -125
- package/tsconfig.json +0 -109
- package/webpack.config.js +0 -32
package/src/Core.ts
DELETED
|
@@ -1,261 +0,0 @@
|
|
|
1
|
-
import EventEmitter,{
|
|
2
|
-
Events
|
|
3
|
-
} from 'easy-event-emitter';
|
|
4
|
-
import type {
|
|
5
|
-
TConfig,
|
|
6
|
-
TCache,
|
|
7
|
-
TCacheBody,
|
|
8
|
-
TData,
|
|
9
|
-
TGroupsData,
|
|
10
|
-
TCoreRviverObject
|
|
11
|
-
} from './types';
|
|
12
|
-
|
|
13
|
-
abstract class Core {
|
|
14
|
-
private cache: TCache;
|
|
15
|
-
initData: TGroupsData;
|
|
16
|
-
protected readonly events: EventEmitter;
|
|
17
|
-
config: TConfig;
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
constructor(context?: Core) {
|
|
21
|
-
this.events = new EventEmitter();
|
|
22
|
-
this.config = {
|
|
23
|
-
host: 'http://127.0.0.1/'
|
|
24
|
-
};
|
|
25
|
-
this.cache = {};
|
|
26
|
-
this.initData = {};
|
|
27
|
-
|
|
28
|
-
this.setConfig = this.setConfig.bind(this);
|
|
29
|
-
this.setHost = this.setHost.bind(this);
|
|
30
|
-
this.setInitData = this.setInitData.bind(this);
|
|
31
|
-
this.triggerByCacheGroup = this.triggerByCacheGroup.bind(this);
|
|
32
|
-
this.clearCacheGroup = this.clearCacheGroup.bind(this);
|
|
33
|
-
this.updateCacheGroup = this.updateCacheGroup.bind(this);
|
|
34
|
-
if (context) {
|
|
35
|
-
Object.assign(this, {
|
|
36
|
-
config: context?.config,
|
|
37
|
-
events: context?.events,
|
|
38
|
-
cache: context?.cache,
|
|
39
|
-
initData: context?.initData
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
protected setConfig(config: TConfig) {
|
|
45
|
-
this.config = config;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
protected getCache(key: string): TData | undefined {
|
|
49
|
-
if (this.cache[key]) {
|
|
50
|
-
if ((new Date()).getTime() <= (this.cache[key].time + 3600000000)) {
|
|
51
|
-
return this.cache[key].data;
|
|
52
|
-
}else{
|
|
53
|
-
delete this.cache[key];
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
protected isJsonString(str: string) {
|
|
59
|
-
try {
|
|
60
|
-
JSON.parse(str);
|
|
61
|
-
} catch (e) {
|
|
62
|
-
return false;
|
|
63
|
-
}
|
|
64
|
-
return true;
|
|
65
|
-
}
|
|
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
|
-
|
|
114
|
-
public setHost(host: string): void {
|
|
115
|
-
this.config.host = host;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
protected setCache(key: string, data: TData, group?: string): void {
|
|
119
|
-
this.cache[key] = {
|
|
120
|
-
time: (new Date()).getTime(),
|
|
121
|
-
data: data,
|
|
122
|
-
group: group
|
|
123
|
-
};
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
protected groupFromArray<T = string>(groups: T | T[], callback: (group: T, params?: string[]) => void, withoutSplit: boolean = false): void {
|
|
127
|
-
const currentCallback = (group: T) => {
|
|
128
|
-
if (typeof group === 'string' && !withoutSplit) {
|
|
129
|
-
const params = group.split('.');
|
|
130
|
-
const current = params.shift() as T;
|
|
131
|
-
callback(current, params);
|
|
132
|
-
}else{
|
|
133
|
-
callback(group);
|
|
134
|
-
}
|
|
135
|
-
};
|
|
136
|
-
|
|
137
|
-
if (Array.isArray(groups)) {
|
|
138
|
-
groups.map(currentCallback);
|
|
139
|
-
}else if (typeof groups === 'string') {
|
|
140
|
-
currentCallback(groups);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
public triggerByCacheGroup(groups: string | string[]): void {
|
|
145
|
-
this.groupFromArray(groups, (group) => this.events.emit('trigger-request-by-cache-' + group, undefined));
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
public clearCacheGroup(groups: string | string[], data?: TData, fieldKey: string | null = 'id'): void {
|
|
149
|
-
this.groupFromArray(groups, (group) => {
|
|
150
|
-
if (data) {
|
|
151
|
-
|
|
152
|
-
const clear = (cacheData: any) => {
|
|
153
|
-
if ((fieldKey && cacheData?.[fieldKey] === data?.[fieldKey]) || fieldKey === null) {
|
|
154
|
-
for(const key in cacheData) {
|
|
155
|
-
if (key in data) {
|
|
156
|
-
return false
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
return true;
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
for(const key in this.cache) {
|
|
165
|
-
if (this.cache[key].group === group) {
|
|
166
|
-
if (Array.isArray(this.cache[key].data)) {
|
|
167
|
-
this.cache[key].data = Object.values(this.cache[key].data).filter(clear);
|
|
168
|
-
}else{
|
|
169
|
-
if (!clear(this.cache[key].data))
|
|
170
|
-
delete this.cache[key];
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
}else{
|
|
175
|
-
for(const key in this.cache) {
|
|
176
|
-
if (this.cache[key].group === group) {
|
|
177
|
-
delete this.cache[key];
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
});
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
private setDataTree(obj: any, path: string, val: any) {
|
|
185
|
-
const keys = path.split('.');
|
|
186
|
-
const lastKey = keys.pop();
|
|
187
|
-
const lastObj = keys.reduce((obj, key) =>
|
|
188
|
-
obj[key] = obj[key] || {},
|
|
189
|
-
obj);
|
|
190
|
-
if (typeof lastKey === 'string') {
|
|
191
|
-
lastObj[lastKey] = val;
|
|
192
|
-
}
|
|
193
|
-
return obj;
|
|
194
|
-
};
|
|
195
|
-
|
|
196
|
-
public updateCacheGroup(groups: string | string[], data: TData, fieldKey: string | null = 'id'): void {
|
|
197
|
-
const update = (cacheData: any) => {
|
|
198
|
-
const retData = cacheData;
|
|
199
|
-
|
|
200
|
-
const fieldList = fieldKey!.split('.');
|
|
201
|
-
if (fieldList.length > 1) {
|
|
202
|
-
for(const key of fieldList) {
|
|
203
|
-
cacheData = cacheData[key];
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
if ((fieldKey && cacheData[fieldKey] === data[fieldKey]) || fieldKey === null) {
|
|
208
|
-
for(const key in cacheData) {
|
|
209
|
-
if (key in data) {
|
|
210
|
-
cacheData[key] = data[key];
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
return retData;
|
|
215
|
-
};
|
|
216
|
-
|
|
217
|
-
this.groupFromArray(groups, (group, params) => {
|
|
218
|
-
for(const key in this.cache) {
|
|
219
|
-
if (this.cache[key].group === group) {
|
|
220
|
-
if (Array.isArray(this.cache[key].data)) {
|
|
221
|
-
this.cache[key].data = Object.values(this.cache[key].data).map(update);
|
|
222
|
-
}else{
|
|
223
|
-
this.cache[key].data = update(this.cache[key].data);
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
});
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
public appendCacheGroup(group: string, data: TData): void {
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
// for(const key in this.cache) {
|
|
234
|
-
// if (this.cache[key].group === group) {
|
|
235
|
-
// console.log(this.setDataTree(this.cache[key].data, 'data.test', 'tttttt'));
|
|
236
|
-
// }
|
|
237
|
-
// }
|
|
238
|
-
//console.log(this.cache);
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
protected getCacheByIndex(index: string): TCacheBody {
|
|
242
|
-
return this.cache[index];
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
protected deleteCache(key: string): void {
|
|
246
|
-
if (!this.cache[key])
|
|
247
|
-
return;
|
|
248
|
-
|
|
249
|
-
const group = this.cache[key].group;
|
|
250
|
-
if (group) {
|
|
251
|
-
this.clearCacheGroup(group);
|
|
252
|
-
}
|
|
253
|
-
delete this.cache[key];
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
public setInitData(data: TGroupsData): void {
|
|
257
|
-
this.initData = data;
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
export default Core;
|
package/src/Http.ts
DELETED
|
@@ -1,402 +0,0 @@
|
|
|
1
|
-
import md5 from 'md5';
|
|
2
|
-
import Core from "./Core";
|
|
3
|
-
import Queue from './Queue';
|
|
4
|
-
import EventEmitter,{
|
|
5
|
-
EventListener
|
|
6
|
-
} from "easy-event-emitter";
|
|
7
|
-
import type {
|
|
8
|
-
TMethod,
|
|
9
|
-
TParams,
|
|
10
|
-
TData,
|
|
11
|
-
TRequestParams,
|
|
12
|
-
TListenerEvents,
|
|
13
|
-
TCacheControll,
|
|
14
|
-
TRequestProgress,
|
|
15
|
-
TResponseData,
|
|
16
|
-
TRequestHeaders
|
|
17
|
-
} from "./types";
|
|
18
|
-
|
|
19
|
-
class Http<D extends TResponseData = TResponseData, PATH = any, DATA extends ((...args: any) => any) = any> extends Core {
|
|
20
|
-
private cacheIndex: string;
|
|
21
|
-
private currentCache?: TData;
|
|
22
|
-
private currentEvents: EventEmitter
|
|
23
|
-
private requestParams: TRequestParams;
|
|
24
|
-
private method: TMethod;
|
|
25
|
-
private params: TParams<PATH, DATA>;
|
|
26
|
-
private path: string;
|
|
27
|
-
private queue: Queue<D>;
|
|
28
|
-
private queueName: string;
|
|
29
|
-
private _promise?: Promise<DATA>;
|
|
30
|
-
private xhr?: XMLHttpRequest;
|
|
31
|
-
|
|
32
|
-
constructor(method: TMethod, params: TParams<PATH, DATA>, context?: Core) {
|
|
33
|
-
super(context);
|
|
34
|
-
this.currentEvents = new EventEmitter();
|
|
35
|
-
this.queue = new Queue<D>(this);
|
|
36
|
-
this.cacheIndex = '';
|
|
37
|
-
this.queueName = '';
|
|
38
|
-
this.method = method;
|
|
39
|
-
this.params = params;
|
|
40
|
-
this.path = this.params.path as string;
|
|
41
|
-
this.requestParams = {
|
|
42
|
-
method: this.method
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
this.requestParams.headers = {
|
|
46
|
-
...this.params.headers
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
this.handleSuccess = this.handleSuccess.bind(this);
|
|
50
|
-
this.handleAbort = this.handleAbort.bind(this);
|
|
51
|
-
this.handleFail = this.handleFail.bind(this);
|
|
52
|
-
this.handleError = this.handleError.bind(this);
|
|
53
|
-
this.handleComplete = this.handleComplete.bind(this);
|
|
54
|
-
this.handleProgress = this.handleProgress.bind(this);
|
|
55
|
-
this.setEmit = this.setEmit.bind(this);
|
|
56
|
-
this.request = this.request.bind(this);
|
|
57
|
-
this.addHeader = this.addHeader.bind(this);
|
|
58
|
-
this.deleteHeader = this.deleteHeader.bind(this);
|
|
59
|
-
|
|
60
|
-
this.initRequest();
|
|
61
|
-
this.initCache();
|
|
62
|
-
this.initQueue();
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
private initRequest(): void {
|
|
66
|
-
|
|
67
|
-
if (typeof XMLHttpRequest !== 'undefined') {
|
|
68
|
-
this.xhr = new XMLHttpRequest();
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
this.params.data = this.params.data ? this.cuteUndifinedParams(this.params.data) : this.params.data;
|
|
72
|
-
|
|
73
|
-
if ((['GET', 'HEAD'] as TMethod[]).includes(this.method)) {
|
|
74
|
-
if (this.params.data)
|
|
75
|
-
this.path = (this.params.path as string) + '?' + this.encodeUrlParams(this.params.data);
|
|
76
|
-
|
|
77
|
-
if (this.method === 'HEAD') {
|
|
78
|
-
this.requestParams.withoutResponse = true;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if ((['POST', 'PUT', 'CONNECT', 'PATH'] as TMethod[]).includes(this.method)) {
|
|
83
|
-
this.requestParams.body = this.params.body ?? JSON.stringify(this.params.data);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
if ((['PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE'] as TMethod[]).includes(this.method)) {
|
|
87
|
-
this.params.cache = false;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
private initCache(): void {
|
|
92
|
-
this.cacheIndex = md5(this.path + JSON.stringify(this.params.data));
|
|
93
|
-
if (this.params.globalName) {
|
|
94
|
-
if (this.initData[this.params.globalName]) {
|
|
95
|
-
this.setCache(this.cacheIndex, this.initData[this.params.globalName], (typeof this.params.cache === 'boolean' ? undefined : this.params.cache));
|
|
96
|
-
this.params.cache = this.params.cache ?? true;
|
|
97
|
-
delete this.initData[this.params.globalName];
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
if (this.params.cache) {
|
|
101
|
-
if (typeof this.params.cache === 'string') {
|
|
102
|
-
this.events.addListener('trigger-request-by-cache-' + this.params.cache, this.request);
|
|
103
|
-
}
|
|
104
|
-
if (this.currentCache = this.getCache(this.cacheIndex)) {
|
|
105
|
-
this.handleSuccess(this.currentCache);
|
|
106
|
-
this.handleComplete(this.currentCache);
|
|
107
|
-
}
|
|
108
|
-
}else{
|
|
109
|
-
this.deleteCache(this.cacheIndex);
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
public get promise() {
|
|
114
|
-
return this._promise;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
private initQueue(): void {
|
|
118
|
-
this.queueName = this.params.queueThrottling ? md5(this.path) : this.cacheIndex;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
private setEmit<EVENT extends keyof TListenerEvents, DATA extends TListenerEvents[EVENT]>(event: EVENT, data: DATA) {
|
|
122
|
-
this.currentEvents.emit(event, data);
|
|
123
|
-
this.events.emit(event, {
|
|
124
|
-
cacheIndex: this.cacheIndex,
|
|
125
|
-
data
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
//Заголовки от 200 до 299
|
|
130
|
-
public handleSuccess(data: any) {
|
|
131
|
-
if (typeof this.params.success === 'function' && this.xhr)
|
|
132
|
-
this.params.success(data, this.xhr);
|
|
133
|
-
if (this.params.cacheUpdate && this.params.data) {
|
|
134
|
-
this.groupFromArray<TCacheControll>(this.params.cacheUpdate, (cacheUpdate, params) => {
|
|
135
|
-
this.updateCacheGroup(
|
|
136
|
-
typeof cacheUpdate === 'string' ? cacheUpdate : cacheUpdate.group,
|
|
137
|
-
this.params.data ?? {},
|
|
138
|
-
typeof cacheUpdate === 'string' ? undefined : cacheUpdate.fieldKey);
|
|
139
|
-
}, true);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
if (this.params.cacheClear && this.params.data) {
|
|
143
|
-
this.groupFromArray<TCacheControll>(this.params.cacheClear, (cacheClear) => {
|
|
144
|
-
this.clearCacheGroup(
|
|
145
|
-
typeof cacheClear === 'string' ? cacheClear : cacheClear.group,
|
|
146
|
-
this.params.data,
|
|
147
|
-
typeof cacheClear === 'string' ? undefined : cacheClear.fieldKey);
|
|
148
|
-
});
|
|
149
|
-
}
|
|
150
|
-
this.setEmit('api-request-success', data);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
public handleAbort(data: any) {
|
|
154
|
-
if (typeof this.params.abort === 'function') {
|
|
155
|
-
this.params.abort(data);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
this.setEmit('api-request-abort', data);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
//Заголовки от 400 до 499
|
|
162
|
-
public handleFail(data: any) {
|
|
163
|
-
if (typeof this.params.fail === 'function') {
|
|
164
|
-
this.params.fail(data);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
this.setEmit('api-request-fail', data);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
//Все остальные
|
|
171
|
-
public handleError(data: any) {
|
|
172
|
-
if (typeof this.params.error === 'function') {
|
|
173
|
-
this.params.error(data);
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
this.setEmit('api-request-error', data);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
public handleComplete(data: any) {
|
|
180
|
-
if (typeof this.params.complete === 'function') {
|
|
181
|
-
this.params.complete(data);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
this.setEmit('api-request-complete', data);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
public handleProgress(progress: TRequestProgress) {
|
|
188
|
-
if (typeof this.params.progress === 'function') {
|
|
189
|
-
this.params.progress(progress);
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
this.setEmit('api-request-progress', progress);
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
private encodeUrlParams(data: TData) {
|
|
196
|
-
return Object.entries(data).map(([key, value]) => {
|
|
197
|
-
if (Array.isArray(value)) {
|
|
198
|
-
return value.map((val) => encodeURIComponent(key) + "[]=" + encodeURIComponent(val)).join('&');
|
|
199
|
-
}
|
|
200
|
-
if (typeof value === 'object') {
|
|
201
|
-
value = JSON.stringify(value);
|
|
202
|
-
}
|
|
203
|
-
return encodeURIComponent(key) + "=" + encodeURIComponent(value);
|
|
204
|
-
}).join('&');
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
private cuteUndifinedParams(data: TData) {
|
|
208
|
-
if (typeof data !== 'object') {
|
|
209
|
-
return;
|
|
210
|
-
}
|
|
211
|
-
for(const key in data) {
|
|
212
|
-
if (!data[key]) {
|
|
213
|
-
delete data[key];
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
return data;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
public request(force?: boolean): this {
|
|
220
|
-
|
|
221
|
-
if (!force && (this.currentCache || this.queue.push(this.queueName))) return this;
|
|
222
|
-
this._promise = new Promise(async (resolve, reject) => {
|
|
223
|
-
if (this.params.stream) {
|
|
224
|
-
fetch(this.config.host + this.path, {
|
|
225
|
-
method: this.method,
|
|
226
|
-
headers: this.requestParams.headers,
|
|
227
|
-
body: this.requestParams.body,
|
|
228
|
-
}).then((response) => {
|
|
229
|
-
this.handleSuccess(response);
|
|
230
|
-
//@ts-expect-error Skip
|
|
231
|
-
resolve(response);
|
|
232
|
-
}).catch((e) => {
|
|
233
|
-
this.handleFail(e);
|
|
234
|
-
reject(e)
|
|
235
|
-
});
|
|
236
|
-
return;
|
|
237
|
-
}
|
|
238
|
-
if (typeof this.xhr === 'undefined') return;
|
|
239
|
-
try {
|
|
240
|
-
if (this.params.file) {
|
|
241
|
-
const data = this.params.file;
|
|
242
|
-
const chunkSize = this.params.fileSizeChunk ? this.params.fileSizeChunk * 1024 * 1024 : this.params.file.byteLength;
|
|
243
|
-
const totalChunks = Math.ceil(this.params.file.byteLength / chunkSize);
|
|
244
|
-
const fileId = `file-${new Date().getTime()}`;
|
|
245
|
-
|
|
246
|
-
for (let i = 0; i < totalChunks; i++) {
|
|
247
|
-
|
|
248
|
-
const start = i * chunkSize;
|
|
249
|
-
const end = Math.min(start + chunkSize, data.byteLength);
|
|
250
|
-
const chunk = data.slice(start, end);
|
|
251
|
-
|
|
252
|
-
const formData = new FormData();
|
|
253
|
-
formData.append("file", new Blob([chunk]));
|
|
254
|
-
formData.append("file_id", fileId);
|
|
255
|
-
formData.append("chunk_index", String(i));
|
|
256
|
-
formData.append("total_chunks", String(totalChunks));
|
|
257
|
-
const headers: TRequestHeaders = {
|
|
258
|
-
...this.requestParams.headers
|
|
259
|
-
};
|
|
260
|
-
|
|
261
|
-
delete headers['Accept'];
|
|
262
|
-
delete headers['Content-Type'];
|
|
263
|
-
|
|
264
|
-
const http = new Http(this.method, {
|
|
265
|
-
...this.params,
|
|
266
|
-
file: undefined,
|
|
267
|
-
headers,
|
|
268
|
-
data: undefined,
|
|
269
|
-
fileSizeChunk: undefined,
|
|
270
|
-
body: formData,
|
|
271
|
-
success: (totalChunks - 1 > i) ? undefined : (data) => {
|
|
272
|
-
resolve(data);
|
|
273
|
-
this.handleSuccess(data);
|
|
274
|
-
},
|
|
275
|
-
progress: (progres) => {
|
|
276
|
-
const loaded = data.byteLength + progres.loaded;
|
|
277
|
-
this.handleProgress({
|
|
278
|
-
percent: (start / data.byteLength * 100),
|
|
279
|
-
total: data.byteLength,
|
|
280
|
-
loaded
|
|
281
|
-
});
|
|
282
|
-
},
|
|
283
|
-
fail: reject,
|
|
284
|
-
error: reject
|
|
285
|
-
}, this);
|
|
286
|
-
|
|
287
|
-
this.xhr = http.xhr;
|
|
288
|
-
|
|
289
|
-
await http.request(true).promise;
|
|
290
|
-
}
|
|
291
|
-
return;
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
this.xhr.open(this.method, this.config.host + this.path, true);
|
|
295
|
-
for(const header in this.requestParams.headers) {
|
|
296
|
-
this.xhr.setRequestHeader(header, this.requestParams.headers[header]);
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
if (this.xhr.upload) {
|
|
300
|
-
this.xhr.upload.onprogress = ({lengthComputable, loaded, total}) => {
|
|
301
|
-
if (lengthComputable) {
|
|
302
|
-
this.handleProgress({
|
|
303
|
-
percent: (loaded / total * 100),
|
|
304
|
-
total,
|
|
305
|
-
loaded
|
|
306
|
-
});
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
this.xhr.onabort = () => {
|
|
312
|
-
reject({
|
|
313
|
-
type: 'abort',
|
|
314
|
-
message: 'request abort'
|
|
315
|
-
});
|
|
316
|
-
this.handleAbort({
|
|
317
|
-
message: 'request abort'
|
|
318
|
-
});
|
|
319
|
-
};
|
|
320
|
-
|
|
321
|
-
this.xhr.onerror = () => {
|
|
322
|
-
reject({
|
|
323
|
-
type: 'error',
|
|
324
|
-
message: 'request error'
|
|
325
|
-
});
|
|
326
|
-
this.handleError({
|
|
327
|
-
message: 'request error'
|
|
328
|
-
});
|
|
329
|
-
};
|
|
330
|
-
|
|
331
|
-
this.xhr.onreadystatechange = () => {
|
|
332
|
-
if (this.xhr?.readyState !== XMLHttpRequest.DONE) return;
|
|
333
|
-
|
|
334
|
-
const result = (!this.requestParams.withoutResponse && this.isJsonString(this.xhr.responseText)) ? this.jsonParse(this.xhr.responseText) : {};
|
|
335
|
-
|
|
336
|
-
if (this.xhr?.status >= 200 && this.xhr.status <= 299) {
|
|
337
|
-
if (this.params.cache) {
|
|
338
|
-
this.setCache(this.cacheIndex, result, (typeof this.params.cache === 'boolean' ? undefined : this.params.cache));
|
|
339
|
-
}
|
|
340
|
-
resolve(result);
|
|
341
|
-
this.handleSuccess(result);
|
|
342
|
-
}else{
|
|
343
|
-
if (this.xhr.status >= 400 && this.xhr.status <= 499) {
|
|
344
|
-
reject(result);
|
|
345
|
-
this.handleFail(result);
|
|
346
|
-
}else{
|
|
347
|
-
reject(result);
|
|
348
|
-
this.handleError(result);
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
this.handleComplete(result);
|
|
352
|
-
this.queue.clear(this.queueName);
|
|
353
|
-
}
|
|
354
|
-
this.xhr.send(this.requestParams.body);
|
|
355
|
-
}catch(e) {
|
|
356
|
-
reject(e);
|
|
357
|
-
}
|
|
358
|
-
});
|
|
359
|
-
|
|
360
|
-
return this
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
public abort() {
|
|
364
|
-
this.xhr?.abort();
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
public addHeader(key: string, value: string): this {
|
|
368
|
-
this.requestParams.headers = {...this.requestParams.headers, [key]: value};
|
|
369
|
-
return this;
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
public addListener<EVENT extends keyof TListenerEvents, DATA extends TListenerEvents[EVENT]>(event: EVENT, callback: (data: DATA) => void): EventListener {
|
|
373
|
-
return this.currentEvents.addListener(event, callback);
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
public deleteHeader(key: string): this {
|
|
377
|
-
if (this.requestParams.headers && (key in this.requestParams.headers)) {
|
|
378
|
-
delete this.requestParams.headers[key];
|
|
379
|
-
}
|
|
380
|
-
return this;
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
public updateCache(data: TData, fieldKey: string | null = 'id'): void {
|
|
384
|
-
const cache = this.getCacheByIndex(this.cacheIndex);
|
|
385
|
-
if (cache && cache.group) {
|
|
386
|
-
this.updateCacheGroup(cache.group, data, fieldKey);
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
public clearCache(data?: TData, fieldKey: string | null = 'id'): void {
|
|
391
|
-
if (data) {
|
|
392
|
-
const cache = this.getCacheByIndex(this.cacheIndex);
|
|
393
|
-
if (cache && cache.group) {
|
|
394
|
-
this.clearCacheGroup(cache.group, data, fieldKey);
|
|
395
|
-
}
|
|
396
|
-
}else{
|
|
397
|
-
this.deleteCache(this.cacheIndex);
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
export default Http;
|
package/src/Queue.ts
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import Http from "./Http";
|
|
2
|
-
import type { TResponseData } from "./types";
|
|
3
|
-
|
|
4
|
-
type TQueue = {
|
|
5
|
-
[index: string]: Http;
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
class Queue<D extends TResponseData = TResponseData> {
|
|
9
|
-
private static _Queue: TQueue = {};
|
|
10
|
-
private queue = Queue._Queue;
|
|
11
|
-
http: Http<D>;
|
|
12
|
-
|
|
13
|
-
constructor(http: Http<D>) {
|
|
14
|
-
this.http = http;
|
|
15
|
-
this.has = this.has.bind(this);
|
|
16
|
-
this.get = this.get.bind(this);
|
|
17
|
-
this.clear = this.clear.bind(this);
|
|
18
|
-
this.push = this.push.bind(this);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
public has(request: string) {
|
|
22
|
-
return request in this.queue;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
public get(request: string) {
|
|
26
|
-
return this.has(request) ? this.queue[request] : undefined;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
public clear(request: string) {
|
|
30
|
-
if (this.has(request)) {
|
|
31
|
-
delete this.queue[request];
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
public push(request: string): boolean {
|
|
36
|
-
const refInstance = this.get(request);
|
|
37
|
-
if (!refInstance) {
|
|
38
|
-
(this.queue[request] as any) = this.http;
|
|
39
|
-
}else{
|
|
40
|
-
refInstance?.addListener('api-request-success', this.http.handleSuccess);
|
|
41
|
-
refInstance?.addListener('api-request-error', this.http.handleError);
|
|
42
|
-
refInstance?.addListener('api-request-fail', this.http.handleFail);
|
|
43
|
-
refInstance?.addListener('api-request-complete', this.http.handleComplete);
|
|
44
|
-
refInstance?.addListener('api-request-abort', this.http.handleAbort);
|
|
45
|
-
}
|
|
46
|
-
return refInstance instanceof Http;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export default Queue;
|