@dxtmisha/functional-basic 0.1.1 → 0.2.0
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/functions/copyObjectLite.d.ts +9 -0
- package/dist/functions/isDomData.d.ts +6 -0
- package/dist/library.d.ts +2 -0
- package/dist/library.js +414 -413
- package/package.json +1 -3
- package/src/classes/Api.ts +0 -407
- package/src/classes/ApiDefault.ts +0 -83
- package/src/classes/ApiHeaders.ts +0 -52
- package/src/classes/ApiPreparation.ts +0 -114
- package/src/classes/ApiResponse.ts +0 -293
- package/src/classes/ApiStatus.ts +0 -173
- package/src/classes/BroadcastMessage.ts +0 -73
- package/src/classes/Cache.ts +0 -60
- package/src/classes/CacheItem.ts +0 -95
- package/src/classes/CacheStatic.ts +0 -30
- package/src/classes/Cookie.ts +0 -135
- package/src/classes/CookieBlock.ts +0 -31
- package/src/classes/DataStorage.ts +0 -194
- package/src/classes/Datetime.ts +0 -891
- package/src/classes/EventItem.ts +0 -373
- package/src/classes/Geo.ts +0 -320
- package/src/classes/GeoFlag.ts +0 -386
- package/src/classes/GeoIntl.ts +0 -839
- package/src/classes/GeoPhone.ts +0 -272
- package/src/classes/Global.ts +0 -32
- package/src/classes/Hash.ts +0 -142
- package/src/classes/Icons.ts +0 -165
- package/src/classes/Loading.ts +0 -90
- package/src/classes/Meta.ts +0 -284
- package/src/classes/MetaManager.ts +0 -200
- package/src/classes/MetaOg.ts +0 -147
- package/src/classes/MetaTwitter.ts +0 -154
- package/src/classes/ScrollbarWidth.ts +0 -86
- package/src/classes/Translate.ts +0 -293
- package/src/classes/__tests__/Api.test.ts +0 -728
- package/src/classes/__tests__/ApiDefault.test.ts +0 -222
- package/src/classes/__tests__/ApiHeaders.test.ts +0 -447
- package/src/classes/__tests__/ApiPreparation.test.ts +0 -257
- package/src/classes/__tests__/ApiResponse.test.ts +0 -547
- package/src/classes/__tests__/ApiStatus.test.ts +0 -403
- package/src/classes/__tests__/Meta.test.ts +0 -629
- package/src/classes/__tests__/MetaManager.test.ts +0 -836
- package/src/classes/__tests__/MetaOg.test.ts +0 -677
- package/src/classes/__tests__/MetaTwitter.test.ts +0 -423
- package/src/functions/anyToString.ts +0 -36
- package/src/functions/applyTemplate.ts +0 -63
- package/src/functions/arrFill.ts +0 -10
- package/src/functions/copyObject.ts +0 -10
- package/src/functions/createElement.ts +0 -40
- package/src/functions/domQuerySelector.ts +0 -15
- package/src/functions/domQuerySelectorAll.ts +0 -15
- package/src/functions/encodeAttribute.ts +0 -15
- package/src/functions/eventStopPropagation.ts +0 -10
- package/src/functions/executeFunction.ts +0 -13
- package/src/functions/executePromise.ts +0 -19
- package/src/functions/forEach.ts +0 -39
- package/src/functions/frame.ts +0 -38
- package/src/functions/getAttributes.ts +0 -27
- package/src/functions/getClipboardData.ts +0 -13
- package/src/functions/getColumn.ts +0 -18
- package/src/functions/getElement.ts +0 -35
- package/src/functions/getElementId.ts +0 -39
- package/src/functions/getElementItem.ts +0 -27
- package/src/functions/getElementOrWindow.ts +0 -23
- package/src/functions/getExp.ts +0 -21
- package/src/functions/getItemByPath.ts +0 -24
- package/src/functions/getKey.ts +0 -9
- package/src/functions/getLengthOfAllArray.ts +0 -13
- package/src/functions/getMaxLengthAllArray.ts +0 -13
- package/src/functions/getMinLengthAllArray.ts +0 -13
- package/src/functions/getMouseClient.ts +0 -17
- package/src/functions/getMouseClientX.ts +0 -9
- package/src/functions/getMouseClientY.ts +0 -9
- package/src/functions/getObjectByKeys.ts +0 -24
- package/src/functions/getObjectNoUndefined.ts +0 -23
- package/src/functions/getObjectOrNone.ts +0 -11
- package/src/functions/getRandomText.ts +0 -29
- package/src/functions/getRequestString.ts +0 -21
- package/src/functions/getStepPercent.ts +0 -19
- package/src/functions/getStepValue.ts +0 -19
- package/src/functions/goScroll.ts +0 -40
- package/src/functions/inArray.ts +0 -10
- package/src/functions/initScrollbarOffset.ts +0 -14
- package/src/functions/intersectKey.ts +0 -34
- package/src/functions/isArray.ts +0 -9
- package/src/functions/isDifferent.ts +0 -27
- package/src/functions/isDomRuntime.ts +0 -12
- package/src/functions/isFilled.ts +0 -49
- package/src/functions/isFloat.ts +0 -16
- package/src/functions/isFunction.ts +0 -11
- package/src/functions/isInDom.ts +0 -15
- package/src/functions/isIntegerBetween.ts +0 -11
- package/src/functions/isNull.ts +0 -11
- package/src/functions/isNumber.ts +0 -16
- package/src/functions/isObject.ts +0 -9
- package/src/functions/isObjectNotArray.ts +0 -11
- package/src/functions/isSelected.ts +0 -32
- package/src/functions/isSelectedByList.ts +0 -19
- package/src/functions/isString.ts +0 -9
- package/src/functions/isWindow.ts +0 -11
- package/src/functions/random.ts +0 -10
- package/src/functions/replaceRecursive.ts +0 -60
- package/src/functions/replaceTemplate.ts +0 -22
- package/src/functions/secondToTime.ts +0 -20
- package/src/functions/setElementItem.ts +0 -56
- package/src/functions/setValues.ts +0 -59
- package/src/functions/splice.ts +0 -59
- package/src/functions/strFill.ts +0 -12
- package/src/functions/toArray.ts +0 -19
- package/src/functions/toCamelCase.ts +0 -16
- package/src/functions/toCamelCaseFirst.ts +0 -12
- package/src/functions/toDate.ts +0 -44
- package/src/functions/toKebabCase.ts +0 -25
- package/src/functions/toNumber.ts +0 -35
- package/src/functions/toNumberByMax.ts +0 -33
- package/src/functions/toPercent.ts +0 -10
- package/src/functions/toPercentBy100.ts +0 -12
- package/src/functions/transformation.ts +0 -59
- package/src/functions/uniqueArray.ts +0 -9
- package/src/functions/writeClipboardData.ts +0 -17
- package/src/library.ts +0 -116
- package/src/types/apiTypes.ts +0 -143
- package/src/types/basicTypes.ts +0 -155
- package/src/types/geoTypes.ts +0 -109
- package/src/types/metaTypes.ts +0 -764
- package/src/vite-env.d.ts +0 -1
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxtmisha/functional-basic",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.2.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Core functional utility library for modern web development without framework dependencies",
|
|
7
7
|
"keywords": [
|
|
@@ -40,7 +40,6 @@
|
|
|
40
40
|
},
|
|
41
41
|
"files": [
|
|
42
42
|
"dist",
|
|
43
|
-
"src",
|
|
44
43
|
"README.md",
|
|
45
44
|
"package.json",
|
|
46
45
|
"LICENSE"
|
|
@@ -56,7 +55,6 @@
|
|
|
56
55
|
],
|
|
57
56
|
"types": "./dist/library.d.ts"
|
|
58
57
|
},
|
|
59
|
-
"./main": "./src/library.ts",
|
|
60
58
|
"./style": "./dist/style.css",
|
|
61
59
|
"./types/*": "./dist/*",
|
|
62
60
|
"./types/**/*.d.ts": "./dist/**/*.d.ts"
|
package/src/classes/Api.ts
DELETED
|
@@ -1,407 +0,0 @@
|
|
|
1
|
-
import { getRequestString } from '../functions/getRequestString'
|
|
2
|
-
import { isFilled } from '../functions/isFilled'
|
|
3
|
-
import { isObjectNotArray } from '../functions/isObjectNotArray'
|
|
4
|
-
import { isString } from '../functions/isString'
|
|
5
|
-
|
|
6
|
-
import { Geo } from './Geo'
|
|
7
|
-
import { Loading } from './Loading'
|
|
8
|
-
|
|
9
|
-
import { ApiHeaders } from './ApiHeaders'
|
|
10
|
-
import { ApiDefault } from './ApiDefault'
|
|
11
|
-
import { ApiStatus } from './ApiStatus'
|
|
12
|
-
import { ApiResponse } from './ApiResponse'
|
|
13
|
-
import { ApiPreparation } from './ApiPreparation'
|
|
14
|
-
|
|
15
|
-
import {
|
|
16
|
-
type ApiData,
|
|
17
|
-
type ApiFetch,
|
|
18
|
-
ApiMethodItem,
|
|
19
|
-
type ApiPreparationEnd
|
|
20
|
-
} from '../types/apiTypes'
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Class for working with requests.
|
|
24
|
-
*
|
|
25
|
-
* Класс для работы с запросами.
|
|
26
|
-
*/
|
|
27
|
-
export class Api {
|
|
28
|
-
protected static url = '/api/'
|
|
29
|
-
|
|
30
|
-
protected static headers = new ApiHeaders()
|
|
31
|
-
protected static requestDefault: ApiDefault = new ApiDefault()
|
|
32
|
-
protected static status = new ApiStatus()
|
|
33
|
-
protected static response = new ApiResponse(this.requestDefault)
|
|
34
|
-
protected static preparation = new ApiPreparation()
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Is the server local.
|
|
38
|
-
*
|
|
39
|
-
* Является ли сервер локальный.
|
|
40
|
-
*/
|
|
41
|
-
static isLocalhost(): boolean {
|
|
42
|
-
return typeof location === 'undefined' || location.hostname === 'localhost'
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Returns the status of the last request.
|
|
47
|
-
*
|
|
48
|
-
* Возвращает статус последнего запроса.
|
|
49
|
-
*/
|
|
50
|
-
static getStatus() {
|
|
51
|
-
return this.status
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Getting the response handler.
|
|
56
|
-
*
|
|
57
|
-
* Получение обработчика ответа.
|
|
58
|
-
*/
|
|
59
|
-
static getResponse() {
|
|
60
|
-
return this.response
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Getting the full path to the request script.
|
|
65
|
-
*
|
|
66
|
-
* Получение полного пути к скрипту запроса.
|
|
67
|
-
* @param path path to the script/ путь к скрипту
|
|
68
|
-
* @param api adding a path to the site’s API/ добавление пути к API сайта
|
|
69
|
-
*/
|
|
70
|
-
static getUrl(path: string, api: boolean = true): string {
|
|
71
|
-
return `${api ? this.url : ''}${path}`
|
|
72
|
-
.replace('{locale}', Geo.getLocation())
|
|
73
|
-
.replace('{country}', Geo.getCountry())
|
|
74
|
-
.replace('{language}', Geo.getLanguage())
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Getting data for the body.
|
|
79
|
-
*
|
|
80
|
-
* Получение данных для тела.
|
|
81
|
-
* @param request this request/ данный запрос
|
|
82
|
-
* @param method method for request/ метод запрос
|
|
83
|
-
*/
|
|
84
|
-
static getBody(
|
|
85
|
-
request: ApiFetch['request'] = {},
|
|
86
|
-
method = ApiMethodItem.get
|
|
87
|
-
): string | FormData | undefined {
|
|
88
|
-
if (request instanceof FormData) {
|
|
89
|
-
return request
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
if (
|
|
93
|
-
method !== ApiMethodItem.get
|
|
94
|
-
&& isFilled(request)
|
|
95
|
-
) {
|
|
96
|
-
if (isString(request)) {
|
|
97
|
-
return request
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
return JSON.stringify(request)
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return undefined
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Getting data for the body of the get method.
|
|
108
|
-
*
|
|
109
|
-
* Получение данных для тела метода get.
|
|
110
|
-
* @param request this request/ данный запрос
|
|
111
|
-
* @param path path to request/ путь к запрос
|
|
112
|
-
* @param method method for request/ метод запрос
|
|
113
|
-
*/
|
|
114
|
-
static getBodyForGet(
|
|
115
|
-
request: ApiFetch['request'],
|
|
116
|
-
path: string = '',
|
|
117
|
-
method = ApiMethodItem.get
|
|
118
|
-
): string {
|
|
119
|
-
if (method === ApiMethodItem.get) {
|
|
120
|
-
const type = path.match(/\?/) ? '&' : '?'
|
|
121
|
-
const body = typeof request === 'object' ? getRequestString(request) : request
|
|
122
|
-
|
|
123
|
-
if (isFilled(body)) {
|
|
124
|
-
return `${type}${body}`
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
return ''
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Modifies the default header data.
|
|
133
|
-
*
|
|
134
|
-
* Изменяет данные заголовка по умолчанию.
|
|
135
|
-
*/
|
|
136
|
-
static setHeaders(headers: Record<string, string>): Api {
|
|
137
|
-
this.headers.set(headers)
|
|
138
|
-
return Api
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* Modifies the default request data.
|
|
143
|
-
*
|
|
144
|
-
* Изменяет данные запроса по умолчанию.
|
|
145
|
-
*/
|
|
146
|
-
static setRequestDefault(request: Record<string, any>): Api {
|
|
147
|
-
this.requestDefault.set(request)
|
|
148
|
-
return Api
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Change the base path to the script.
|
|
153
|
-
*
|
|
154
|
-
* Изменить базовый путь к скрипту.
|
|
155
|
-
* @param url path to the script/ путь к скрипту
|
|
156
|
-
*/
|
|
157
|
-
static setUrl(url: string): Api {
|
|
158
|
-
this.url = url
|
|
159
|
-
return Api
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* The function is modified for a call before the request.
|
|
164
|
-
*
|
|
165
|
-
* Изменить функцию перед запросом.
|
|
166
|
-
* @param callback function for call/ функция для вызова
|
|
167
|
-
*/
|
|
168
|
-
static setPreparation(callback: () => Promise<void>): Api {
|
|
169
|
-
this.preparation.set(callback)
|
|
170
|
-
return Api
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
* Modify the function after the request.
|
|
175
|
-
*
|
|
176
|
-
* Изменить функцию после запроса.
|
|
177
|
-
* @param callback function for call/ функция для вызова
|
|
178
|
-
*/
|
|
179
|
-
static setEnd(callback: (query: Response) => Promise<ApiPreparationEnd>): Api {
|
|
180
|
-
this.preparation.setEnd(callback)
|
|
181
|
-
return Api
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
/**
|
|
185
|
-
* To execute a request.
|
|
186
|
-
*
|
|
187
|
-
* Выполнить запрос.
|
|
188
|
-
* @param pathRequest query string or list of parameters/ строка запроса или список параметров
|
|
189
|
-
*/
|
|
190
|
-
static async request<T>(pathRequest: string | ApiFetch): Promise<T> {
|
|
191
|
-
if (isString(pathRequest)) {
|
|
192
|
-
return await this.fetch<T>({
|
|
193
|
-
path: pathRequest
|
|
194
|
-
})
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
return await this.fetch<T>(pathRequest)
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* Sends a get method request.
|
|
202
|
-
*
|
|
203
|
-
* Отправляет запрос метода get.
|
|
204
|
-
* @param request list of parameters/ список параметров
|
|
205
|
-
*/
|
|
206
|
-
static get<T>(request: ApiFetch): Promise<T> {
|
|
207
|
-
return this.request({
|
|
208
|
-
...request,
|
|
209
|
-
method: ApiMethodItem.get
|
|
210
|
-
})
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* Sends a post method request.
|
|
215
|
-
*
|
|
216
|
-
* Отправляет запрос метода post.
|
|
217
|
-
* @param request list of parameters/ список параметров
|
|
218
|
-
*/
|
|
219
|
-
static post<T>(request: ApiFetch): Promise<T> {
|
|
220
|
-
return this.request({
|
|
221
|
-
...request,
|
|
222
|
-
method: ApiMethodItem.post
|
|
223
|
-
})
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
/**
|
|
227
|
-
* Sends a put method request.
|
|
228
|
-
*
|
|
229
|
-
* Отправляет запрос метода put.
|
|
230
|
-
* @param request list of parameters/ список параметров
|
|
231
|
-
*/
|
|
232
|
-
static put<T>(request: ApiFetch): Promise<T> {
|
|
233
|
-
return this.request({
|
|
234
|
-
...request,
|
|
235
|
-
method: ApiMethodItem.put
|
|
236
|
-
})
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* Sends a delete method request.
|
|
241
|
-
*
|
|
242
|
-
* Отправляет запрос метода delete.
|
|
243
|
-
* @param request list of parameters/ список параметров
|
|
244
|
-
*/
|
|
245
|
-
static delete<T>(request: ApiFetch): Promise<T> {
|
|
246
|
-
return this.request({
|
|
247
|
-
...request,
|
|
248
|
-
method: ApiMethodItem.delete
|
|
249
|
-
})
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
/**
|
|
253
|
-
* To execute a request.
|
|
254
|
-
*
|
|
255
|
-
* Выполнить запрос.
|
|
256
|
-
* @param apiFetch property of the request/ свойство запроса
|
|
257
|
-
*/
|
|
258
|
-
protected static async fetch<T>(apiFetch: ApiFetch): Promise<T> {
|
|
259
|
-
const {
|
|
260
|
-
toData = true,
|
|
261
|
-
hideError = false,
|
|
262
|
-
queryReturn = undefined,
|
|
263
|
-
globalPreparation = true,
|
|
264
|
-
globalEnd = true
|
|
265
|
-
} = apiFetch
|
|
266
|
-
|
|
267
|
-
const emulator = await this.response.emulator<T>(apiFetch)
|
|
268
|
-
|
|
269
|
-
if (emulator) {
|
|
270
|
-
return emulator
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
let data: ApiData<T> = {} as ApiData<T>
|
|
274
|
-
|
|
275
|
-
Loading.show()
|
|
276
|
-
|
|
277
|
-
try {
|
|
278
|
-
await this.preparation.make(globalPreparation)
|
|
279
|
-
|
|
280
|
-
const query = await this.makeQuery(apiFetch)
|
|
281
|
-
const end = await this.preparation.makeEnd(globalEnd, query)
|
|
282
|
-
|
|
283
|
-
this.status.setStatus(
|
|
284
|
-
query.status,
|
|
285
|
-
query.statusText
|
|
286
|
-
)
|
|
287
|
-
|
|
288
|
-
if (end?.reset) {
|
|
289
|
-
Loading.hide()
|
|
290
|
-
return await this.fetch(apiFetch)
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
data = await this.readData(
|
|
294
|
-
query,
|
|
295
|
-
queryReturn,
|
|
296
|
-
end
|
|
297
|
-
)
|
|
298
|
-
} catch (e) {
|
|
299
|
-
if (!hideError) {
|
|
300
|
-
console.error('Api: ', e)
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
this.status.setError(String(e))
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
Loading.hide()
|
|
307
|
-
|
|
308
|
-
return this.makeData(data, toData)
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
/**
|
|
312
|
-
* Reading data from the response.
|
|
313
|
-
*
|
|
314
|
-
* Чтение данных из ответа.
|
|
315
|
-
* @param query response from the server/ ответ от сервера
|
|
316
|
-
* @param queryReturn custom function for reading data/ кастомная функция для чтения данных
|
|
317
|
-
* @param end finalization data/ данные финализации
|
|
318
|
-
*/
|
|
319
|
-
protected static async readData(
|
|
320
|
-
query: Response,
|
|
321
|
-
queryReturn: ApiFetch['queryReturn'],
|
|
322
|
-
end: ApiPreparationEnd
|
|
323
|
-
) {
|
|
324
|
-
if (queryReturn) {
|
|
325
|
-
return await queryReturn(query)
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
if ('data' in end) {
|
|
329
|
-
return end.data
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
if ((query.headers.get('Content-Type') ?? '').match('application/json')) {
|
|
333
|
-
return await query.json()
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
return { data: await query.text() } as any
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
/**
|
|
340
|
-
* Executing the request.
|
|
341
|
-
*
|
|
342
|
-
* Выполнение запроса.
|
|
343
|
-
* @param apiFetch property of the request/ свойство запроса
|
|
344
|
-
*/
|
|
345
|
-
protected static async makeQuery(apiFetch: ApiFetch): Promise<Response> {
|
|
346
|
-
const request: ApiFetch['request'] = this.requestDefault.request(apiFetch.request)
|
|
347
|
-
const {
|
|
348
|
-
api = true,
|
|
349
|
-
path = '',
|
|
350
|
-
pathFull = undefined,
|
|
351
|
-
method = ApiMethodItem.get,
|
|
352
|
-
headers = {},
|
|
353
|
-
type = 'application/json;charset=UTF-8',
|
|
354
|
-
init = {}
|
|
355
|
-
} = apiFetch
|
|
356
|
-
|
|
357
|
-
const pathFinal = pathFull ?? this.getUrl(path, api)
|
|
358
|
-
const url = `${pathFinal}${this.getBodyForGet(request, pathFinal, method)}`
|
|
359
|
-
const fetchHeaders = this.headers.get(headers, type)
|
|
360
|
-
const fetchInit = {
|
|
361
|
-
...init,
|
|
362
|
-
method,
|
|
363
|
-
body: this.getBody(request, method)
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
if (fetchHeaders) {
|
|
367
|
-
fetchInit.headers = fetchHeaders
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
return await fetch(url, fetchInit)
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
/**
|
|
374
|
-
* Transforms data if needed.
|
|
375
|
-
*
|
|
376
|
-
* Преобразует данные, если нужно.
|
|
377
|
-
* @param data data for transformation/ данные для преобразования
|
|
378
|
-
* @param toData is it necessary to process the data/ нужно ли обрабатывать данные
|
|
379
|
-
*/
|
|
380
|
-
protected static makeData<T>(
|
|
381
|
-
data: ApiData<T>,
|
|
382
|
-
toData: boolean
|
|
383
|
-
): T {
|
|
384
|
-
this.status.setLastResponse(data)
|
|
385
|
-
|
|
386
|
-
if (
|
|
387
|
-
data
|
|
388
|
-
&& toData
|
|
389
|
-
&& isObjectNotArray(data)
|
|
390
|
-
&& 'data' in data
|
|
391
|
-
) {
|
|
392
|
-
if (isObjectNotArray(data.data)) {
|
|
393
|
-
const item: ApiData<T> = { ...data.data }
|
|
394
|
-
|
|
395
|
-
if ('success' in data) {
|
|
396
|
-
item.success = data.success
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
return item
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
return data.data as T
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
return data
|
|
406
|
-
}
|
|
407
|
-
}
|
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
import { isObjectNotArray } from '../functions/isObjectNotArray'
|
|
2
|
-
|
|
3
|
-
import { type ApiDefaultValue, type ApiFetch } from '../types/apiTypes'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Class for working with default API request data.
|
|
7
|
-
*
|
|
8
|
-
* Класс для работы с данными запроса API по умолчанию.
|
|
9
|
-
*/
|
|
10
|
-
export class ApiDefault {
|
|
11
|
-
/** Default request data/ Данные запроса по умолчанию */
|
|
12
|
-
protected value?: ApiDefaultValue
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Checks if default request data exists.
|
|
16
|
-
*
|
|
17
|
-
* Проверяет, существуют ли данные запроса по умолчанию.
|
|
18
|
-
*/
|
|
19
|
-
is(): boolean {
|
|
20
|
-
return Boolean(this.value)
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Gets the default request data.
|
|
25
|
-
*
|
|
26
|
-
* Получает данные запроса по умолчанию.
|
|
27
|
-
*/
|
|
28
|
-
get(): ApiDefaultValue {
|
|
29
|
-
return this.value as ApiDefaultValue
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Adds default data to the request.
|
|
34
|
-
*
|
|
35
|
-
* Добавляет данные по умолчанию к запросу.
|
|
36
|
-
* @param request request data/ данные запроса
|
|
37
|
-
*/
|
|
38
|
-
request(
|
|
39
|
-
request: ApiFetch['request']
|
|
40
|
-
): ApiFetch['request'] {
|
|
41
|
-
const value = this.get()
|
|
42
|
-
|
|
43
|
-
if (value) {
|
|
44
|
-
if (request instanceof FormData) {
|
|
45
|
-
this.addByFormData(request, value)
|
|
46
|
-
} else if (isObjectNotArray(request)) {
|
|
47
|
-
return {
|
|
48
|
-
...value,
|
|
49
|
-
...request
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
return request
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Modifies the default request data.
|
|
59
|
-
*
|
|
60
|
-
* Изменяет данные запроса по умолчанию.
|
|
61
|
-
*/
|
|
62
|
-
set(request: ApiDefaultValue) {
|
|
63
|
-
this.value = request
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Adds default data to FormData request.
|
|
68
|
-
*
|
|
69
|
-
* Добавляет данные по умолчанию к запросу FormData.
|
|
70
|
-
* @param request FormData request/ запрос FormData
|
|
71
|
-
* @param value default values/ значения по умолчанию
|
|
72
|
-
*/
|
|
73
|
-
protected addByFormData(
|
|
74
|
-
request: FormData,
|
|
75
|
-
value: ApiDefaultValue
|
|
76
|
-
) {
|
|
77
|
-
for (const name in value) {
|
|
78
|
-
if (!request.has(name)) {
|
|
79
|
-
request.set(name, value[name])
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { isFilled } from '../functions/isFilled'
|
|
2
|
-
import { isObjectNotArray } from '../functions/isObjectNotArray'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Class for managing HTTP request headers.
|
|
6
|
-
*
|
|
7
|
-
* Класс для управления заголовками HTTP-запросов.
|
|
8
|
-
*/
|
|
9
|
-
export class ApiHeaders {
|
|
10
|
-
/** Default headers/ Заголовки по умолчанию */
|
|
11
|
-
protected headers: Record<string, string> = {}
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Getting the header for the request.
|
|
15
|
-
*
|
|
16
|
-
* Получение заголовка для запроса.
|
|
17
|
-
* @param value list of headers/ список заголовков
|
|
18
|
-
* @param type type of request/ тип запроса
|
|
19
|
-
*/
|
|
20
|
-
get(
|
|
21
|
-
value?: Record<string, string> | null,
|
|
22
|
-
type = 'application/json;charset=UTF-8'
|
|
23
|
-
): Record<string, string> | undefined {
|
|
24
|
-
if (value !== null) {
|
|
25
|
-
const headers = {
|
|
26
|
-
...this.headers,
|
|
27
|
-
...(value || {})
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
if (isFilled(type)) {
|
|
31
|
-
headers['Content-Type'] = type
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return headers
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
return undefined
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Modifies the default header data.
|
|
42
|
-
*
|
|
43
|
-
* Изменяет данные заголовка по умолчанию.
|
|
44
|
-
*/
|
|
45
|
-
set(headers: Record<string, string>): this {
|
|
46
|
-
if (isObjectNotArray(headers)) {
|
|
47
|
-
this.headers = headers
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
return this
|
|
51
|
-
}
|
|
52
|
-
}
|
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
import type { ApiPreparationEnd } from '../types/apiTypes'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Class for preparing requests.
|
|
5
|
-
*
|
|
6
|
-
* Класс для подготовки запросов.
|
|
7
|
-
*/
|
|
8
|
-
export class ApiPreparation {
|
|
9
|
-
/** Function for call before the request/ Функция для вызова перед запросом */
|
|
10
|
-
protected callback?: () => Promise<void>
|
|
11
|
-
|
|
12
|
-
/** Function for call after the request/ Функция для вызова после запроса */
|
|
13
|
-
protected callbackEnd?: (query: Response) => Promise<ApiPreparationEnd>
|
|
14
|
-
|
|
15
|
-
/** Is the preparation in progress/ Идет ли подготовка */
|
|
16
|
-
protected loading = false
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Preparation before executing the request.
|
|
20
|
-
*
|
|
21
|
-
* Подготовка перед выполнением запроса.
|
|
22
|
-
* @param active is preparation active/ активна ли подготовка
|
|
23
|
-
*/
|
|
24
|
-
async make(active: boolean) {
|
|
25
|
-
if (
|
|
26
|
-
active
|
|
27
|
-
&& this.callback
|
|
28
|
-
) {
|
|
29
|
-
return this.go()
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Analysis of the request after execution.
|
|
35
|
-
*
|
|
36
|
-
* Анализ запроса после выполнения.
|
|
37
|
-
* @param active is preparation active/ активна ли подготовка
|
|
38
|
-
* @param query data received in the request/ данные, полученные в запросе
|
|
39
|
-
*/
|
|
40
|
-
async makeEnd(
|
|
41
|
-
active: boolean,
|
|
42
|
-
query: Response
|
|
43
|
-
): Promise<ApiPreparationEnd> {
|
|
44
|
-
let data: ApiPreparationEnd = {}
|
|
45
|
-
|
|
46
|
-
if (
|
|
47
|
-
active
|
|
48
|
-
&& this.callbackEnd
|
|
49
|
-
) {
|
|
50
|
-
data = await this.callbackEnd(query)
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
return data
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* The function is modified for a call before the request.
|
|
58
|
-
*
|
|
59
|
-
* Изменить функцию перед запросом.
|
|
60
|
-
* @param callback function for call/ функция для вызова
|
|
61
|
-
*/
|
|
62
|
-
set(callback: () => Promise<void>): this {
|
|
63
|
-
this.callback = callback
|
|
64
|
-
return this
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Modify the function after the request.
|
|
69
|
-
*
|
|
70
|
-
* Изменить функцию после запроса.
|
|
71
|
-
* @param callback function for call/ функция для вызова
|
|
72
|
-
*/
|
|
73
|
-
setEnd(callback: (query: Response) => Promise<ApiPreparationEnd>): this {
|
|
74
|
-
this.callbackEnd = callback
|
|
75
|
-
return this
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* To execute preparation.
|
|
80
|
-
*
|
|
81
|
-
* Выполнить подготовку.
|
|
82
|
-
*/
|
|
83
|
-
protected async go() {
|
|
84
|
-
return new Promise<void>((resolve) => {
|
|
85
|
-
if (this.loading) {
|
|
86
|
-
setTimeout(() => this.go().then(resolve), 160)
|
|
87
|
-
} else if (this.callback) {
|
|
88
|
-
this.loading = true
|
|
89
|
-
this.callback().then(() => {
|
|
90
|
-
this.loading = false
|
|
91
|
-
resolve()
|
|
92
|
-
})
|
|
93
|
-
} else {
|
|
94
|
-
resolve()
|
|
95
|
-
}
|
|
96
|
-
})
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Analysis of the request after execution.
|
|
101
|
-
*
|
|
102
|
-
* Анализ запроса после выполнения.
|
|
103
|
-
* @param query data received in the request/ данные, полученные в запросе
|
|
104
|
-
*/
|
|
105
|
-
protected async end(query: Response) {
|
|
106
|
-
let data: ApiPreparationEnd = {}
|
|
107
|
-
|
|
108
|
-
if (this.callbackEnd) {
|
|
109
|
-
data = await this.callbackEnd(query)
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
return data
|
|
113
|
-
}
|
|
114
|
-
}
|