@authenty/authapi-types 1.0.12 → 1.0.13
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.d.ts +623 -4
- package/dist/index.js +419 -18
- package/dist/tools.d.ts +1 -0
- package/dist/tools.js +16 -0
- package/package.json +1 -1
- package/src/index.ts +1 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,623 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Namespace principal da API de autenticação
|
|
3
|
+
* Acesso aos tipos: AuthApi.objects.User, AuthApi.responses.auth.login.post, etc.
|
|
4
|
+
*/
|
|
5
|
+
export declare namespace AuthApi {
|
|
6
|
+
/**
|
|
7
|
+
* Objetos de domínio da aplicação
|
|
8
|
+
*/
|
|
9
|
+
namespace objects {
|
|
10
|
+
enum BundleType {
|
|
11
|
+
BUNDLE = 0,
|
|
12
|
+
SYMBOLIC_PRODUCT = 1,
|
|
13
|
+
SYMBOLIC_COURSE = 2,
|
|
14
|
+
SYMBOLIC_CONSULTANCY = 3
|
|
15
|
+
}
|
|
16
|
+
type Product = {
|
|
17
|
+
id: number;
|
|
18
|
+
title: string;
|
|
19
|
+
short: string;
|
|
20
|
+
resume: string;
|
|
21
|
+
video: string;
|
|
22
|
+
imgUrls: string | null;
|
|
23
|
+
logo: string;
|
|
24
|
+
themeImage: string;
|
|
25
|
+
themeColor: string;
|
|
26
|
+
themeVideo: string;
|
|
27
|
+
url: string;
|
|
28
|
+
type: number;
|
|
29
|
+
period_test: number;
|
|
30
|
+
created: string;
|
|
31
|
+
deletedAt?: string | null;
|
|
32
|
+
Levels?: ProductLevel[];
|
|
33
|
+
};
|
|
34
|
+
type ProductLevel = {
|
|
35
|
+
id: number;
|
|
36
|
+
productID: number;
|
|
37
|
+
level: number;
|
|
38
|
+
short: string;
|
|
39
|
+
Product?: Product;
|
|
40
|
+
store_bundles_levels?: {
|
|
41
|
+
id: number;
|
|
42
|
+
bundleID: number;
|
|
43
|
+
levelID: number;
|
|
44
|
+
quantity: number;
|
|
45
|
+
Bundle?: Bundle;
|
|
46
|
+
}[];
|
|
47
|
+
};
|
|
48
|
+
type Course = {
|
|
49
|
+
id: number;
|
|
50
|
+
type: number;
|
|
51
|
+
name: string;
|
|
52
|
+
short: string;
|
|
53
|
+
resume: string;
|
|
54
|
+
img_landscape: string;
|
|
55
|
+
img_square: string;
|
|
56
|
+
workload: number;
|
|
57
|
+
langID: number;
|
|
58
|
+
num_classes?: number;
|
|
59
|
+
seats: number;
|
|
60
|
+
url?: string;
|
|
61
|
+
dates: string;
|
|
62
|
+
local: string;
|
|
63
|
+
local_url: string;
|
|
64
|
+
date_mat_s?: string;
|
|
65
|
+
date_mat_e?: string;
|
|
66
|
+
date_ends?: string;
|
|
67
|
+
email_enrollment?: string;
|
|
68
|
+
email_before?: string;
|
|
69
|
+
email_after?: string;
|
|
70
|
+
date_c: string;
|
|
71
|
+
date_r?: string | null;
|
|
72
|
+
};
|
|
73
|
+
type Consultancy = {
|
|
74
|
+
id: number;
|
|
75
|
+
teacherID: number;
|
|
76
|
+
type: number;
|
|
77
|
+
name: string;
|
|
78
|
+
short: string;
|
|
79
|
+
resume: string;
|
|
80
|
+
img_landscape: string;
|
|
81
|
+
img_square: string;
|
|
82
|
+
workload: number;
|
|
83
|
+
langID: number;
|
|
84
|
+
url?: string;
|
|
85
|
+
datetime: string;
|
|
86
|
+
date_mat_s?: string;
|
|
87
|
+
date_mat_e?: string;
|
|
88
|
+
date_ends?: string;
|
|
89
|
+
email_enrollment: string;
|
|
90
|
+
email_before: string;
|
|
91
|
+
email_after: string;
|
|
92
|
+
date_c: string;
|
|
93
|
+
date_r?: string | null;
|
|
94
|
+
};
|
|
95
|
+
type Bundle = {
|
|
96
|
+
id: number;
|
|
97
|
+
type: BundleType;
|
|
98
|
+
title?: string;
|
|
99
|
+
short?: string;
|
|
100
|
+
resume?: string;
|
|
101
|
+
img: string;
|
|
102
|
+
themeImage: string;
|
|
103
|
+
themeColor: string;
|
|
104
|
+
shareLimit: number;
|
|
105
|
+
created: string;
|
|
106
|
+
active: boolean;
|
|
107
|
+
showInSite: boolean;
|
|
108
|
+
defaultMetaArray: string;
|
|
109
|
+
deletedAt?: string | null;
|
|
110
|
+
Levels?: {
|
|
111
|
+
id: number;
|
|
112
|
+
bundleID: number;
|
|
113
|
+
levelID: number;
|
|
114
|
+
quantity: number;
|
|
115
|
+
Level: ProductLevel;
|
|
116
|
+
}[];
|
|
117
|
+
Courses?: {
|
|
118
|
+
id: number;
|
|
119
|
+
bundleID: number;
|
|
120
|
+
courseID: number;
|
|
121
|
+
quantity: number;
|
|
122
|
+
Course: Course;
|
|
123
|
+
}[];
|
|
124
|
+
Consultancies?: {
|
|
125
|
+
id: number;
|
|
126
|
+
bundleID: number;
|
|
127
|
+
consultancyID: number;
|
|
128
|
+
quantity: number;
|
|
129
|
+
Consultancy: Consultancy;
|
|
130
|
+
}[];
|
|
131
|
+
store_bundles_paymethod: PaymentMethod[];
|
|
132
|
+
};
|
|
133
|
+
type PaymentMethod = {
|
|
134
|
+
id: number;
|
|
135
|
+
bundleID: number;
|
|
136
|
+
price: number;
|
|
137
|
+
fromprice: number;
|
|
138
|
+
period_renew: number;
|
|
139
|
+
period_grace: number;
|
|
140
|
+
gatewayHash: string;
|
|
141
|
+
allowBoleto: boolean;
|
|
142
|
+
gatewayID: number;
|
|
143
|
+
Bundle?: Bundle;
|
|
144
|
+
};
|
|
145
|
+
type Purchase = {
|
|
146
|
+
paghash: string;
|
|
147
|
+
id: number;
|
|
148
|
+
price: number;
|
|
149
|
+
licenseStatus: PaymentSystem.PG_PurchaseLicenseStatus;
|
|
150
|
+
paymentStatus: PaymentSystem.PG_PurchasePaymentStatus;
|
|
151
|
+
type_step: PaymentSystem.PG_PurchaseType;
|
|
152
|
+
PaymentMethod: PaymentMethod;
|
|
153
|
+
Wallet?: Wallet;
|
|
154
|
+
walletID: number;
|
|
155
|
+
createdAt: string;
|
|
156
|
+
paymentDueAt: string | null;
|
|
157
|
+
owner: string;
|
|
158
|
+
invoiceUrl?: string;
|
|
159
|
+
nfUrl?: string;
|
|
160
|
+
isAdmin?: boolean;
|
|
161
|
+
meta_data?: Record<string, any>;
|
|
162
|
+
PurchaseShares?: PurchaseShare[];
|
|
163
|
+
Levels?: {
|
|
164
|
+
Level: ProductLevel;
|
|
165
|
+
quant: number;
|
|
166
|
+
}[];
|
|
167
|
+
};
|
|
168
|
+
type Wallet = {
|
|
169
|
+
id: number;
|
|
170
|
+
userID: number;
|
|
171
|
+
balance?: number;
|
|
172
|
+
Purchases?: Purchase[];
|
|
173
|
+
};
|
|
174
|
+
type PurchaseShare = {
|
|
175
|
+
id: number;
|
|
176
|
+
purchaseID: number;
|
|
177
|
+
userID: number;
|
|
178
|
+
Purchase: Purchase;
|
|
179
|
+
};
|
|
180
|
+
type LocalUser = {
|
|
181
|
+
id: number;
|
|
182
|
+
fullname: string;
|
|
183
|
+
email: string;
|
|
184
|
+
lang: number;
|
|
185
|
+
avatar: string;
|
|
186
|
+
end_city?: number;
|
|
187
|
+
end_comp?: string;
|
|
188
|
+
end_country: number;
|
|
189
|
+
end_district?: string;
|
|
190
|
+
end_n?: string;
|
|
191
|
+
end_state?: number;
|
|
192
|
+
end_street?: string;
|
|
193
|
+
end_zip?: string;
|
|
194
|
+
email_verif: boolean;
|
|
195
|
+
fiscal_verif: boolean;
|
|
196
|
+
terms: boolean;
|
|
197
|
+
cpf?: string;
|
|
198
|
+
newslatter?: boolean;
|
|
199
|
+
allow_purchase?: boolean;
|
|
200
|
+
allow_license?: boolean;
|
|
201
|
+
date_c?: Date | string;
|
|
202
|
+
date_r?: Date | string | null;
|
|
203
|
+
Wallets?: Wallet[];
|
|
204
|
+
PurchasesShares?: PurchaseShare[];
|
|
205
|
+
Sys_admUser_config?: AdminUserConfig;
|
|
206
|
+
Sys_adminUser?: auxiliarTypes.PrivilegeTable;
|
|
207
|
+
};
|
|
208
|
+
type AdminUserConfig = {
|
|
209
|
+
campaigns_blacklist: string;
|
|
210
|
+
deletedAt: Date | null;
|
|
211
|
+
userID: number;
|
|
212
|
+
};
|
|
213
|
+
type LogEntry = {
|
|
214
|
+
appId: string;
|
|
215
|
+
logCode: number;
|
|
216
|
+
content: string;
|
|
217
|
+
timestamp?: string;
|
|
218
|
+
metadata?: {
|
|
219
|
+
requestId: string;
|
|
220
|
+
endpoint: string;
|
|
221
|
+
method: string;
|
|
222
|
+
userId?: number;
|
|
223
|
+
ip?: string;
|
|
224
|
+
browser?: string;
|
|
225
|
+
system?: string;
|
|
226
|
+
referer?: string;
|
|
227
|
+
};
|
|
228
|
+
};
|
|
229
|
+
namespace PaymentSystem {
|
|
230
|
+
type PG_Customer = {
|
|
231
|
+
id: string;
|
|
232
|
+
email: string;
|
|
233
|
+
name: string;
|
|
234
|
+
};
|
|
235
|
+
enum PG_PurchaseLicenseStatus {
|
|
236
|
+
UNKNOWN = 0,//Nunca será atualizado automaticamente!!
|
|
237
|
+
OPEN = 3,//ABERTA - Aguardando pagamento
|
|
238
|
+
EXPIRED = 1,//VENCEU - Era PAGAMENTO, e acabou prazo pra uso acabou
|
|
239
|
+
PAID = 5,//PAGA - Pagamento realizado com sucesso - é pagamento único
|
|
240
|
+
ACTIVE = 6,//ATIVO - Assinatura ativa e em dia
|
|
241
|
+
RECYCLING = 4,//RETENTANDO - Tentativa de renovação automática falhou, mas ainda está no prazo para pagar manualmente
|
|
242
|
+
CANCELED = 2
|
|
243
|
+
}
|
|
244
|
+
const PG_PurchaseLicenseStatus_GroupActive: PG_PurchaseLicenseStatus[];
|
|
245
|
+
enum PG_PurchasePaymentStatus {
|
|
246
|
+
UNKNOWN = 0,//pendent or unknown
|
|
247
|
+
ACTIVE = 1,
|
|
248
|
+
CANCELED = 2,
|
|
249
|
+
REFUNDED = 3,
|
|
250
|
+
REMOVED = 4,// REMOVIDO DO GATEWAY - Não há mais registro do pagamento no gateway
|
|
251
|
+
PENDING = 5
|
|
252
|
+
}
|
|
253
|
+
enum PG_PurchaseType {
|
|
254
|
+
SESSION = 0,//o registro se trata de uma sessão de compra (ex: intenção de compra preparada para redirecionar o usuário ao gateway. Ainda não é um pagamento nem uma assinatura ativa)
|
|
255
|
+
STANDARD = 1,//Pagamento único
|
|
256
|
+
RECURRENT = 2
|
|
257
|
+
}
|
|
258
|
+
type PG_Purchase = {
|
|
259
|
+
id: string;
|
|
260
|
+
amount: number;
|
|
261
|
+
recurrent: boolean;
|
|
262
|
+
type: PG_PurchaseType;
|
|
263
|
+
paymentStatus: PG_PurchasePaymentStatus;
|
|
264
|
+
url: string | null;
|
|
265
|
+
dueTime: Date | null;
|
|
266
|
+
};
|
|
267
|
+
enum PG_RecurrenceType {
|
|
268
|
+
NONE = 0,
|
|
269
|
+
DAILY = 1,
|
|
270
|
+
WEEKLY = 2,
|
|
271
|
+
MONTHLY = 3,
|
|
272
|
+
YEARLY = 4
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
namespace auxiliarTypes {
|
|
277
|
+
type availableSoftware = {
|
|
278
|
+
Level: objects.ProductLevel;
|
|
279
|
+
quantity: number;
|
|
280
|
+
purchaseId: number | null;
|
|
281
|
+
type: 'free' | 'own' | 'shared';
|
|
282
|
+
};
|
|
283
|
+
enum Privilege {
|
|
284
|
+
NONE = 0,
|
|
285
|
+
READ = 1,
|
|
286
|
+
WRITE = 2
|
|
287
|
+
}
|
|
288
|
+
type PrivilegeTable = {
|
|
289
|
+
users: Privilege;
|
|
290
|
+
products: Privilege;
|
|
291
|
+
courses: Privilege;
|
|
292
|
+
consultancies: Privilege;
|
|
293
|
+
bundles: Privilege;
|
|
294
|
+
logs: Privilege;
|
|
295
|
+
support: Privilege;
|
|
296
|
+
leads: Privilege;
|
|
297
|
+
paymentSystem: Privilege;
|
|
298
|
+
adminMaster: Privilege;
|
|
299
|
+
activityTrakking: Privilege;
|
|
300
|
+
financial: Privilege;
|
|
301
|
+
};
|
|
302
|
+
type CronJob = {
|
|
303
|
+
startAt: Date | null;
|
|
304
|
+
latestRunErrorCount: number;
|
|
305
|
+
latestCompletedAt: Date | null;
|
|
306
|
+
};
|
|
307
|
+
type CronjobsStatuses = {
|
|
308
|
+
purchasesStatusUpdate: CronJob;
|
|
309
|
+
};
|
|
310
|
+
type LogFilters = {
|
|
311
|
+
page: number;
|
|
312
|
+
itemsPerPage: number;
|
|
313
|
+
search: string;
|
|
314
|
+
logCode?: number;
|
|
315
|
+
appId?: string;
|
|
316
|
+
userId?: number;
|
|
317
|
+
method?: string;
|
|
318
|
+
startDate?: string;
|
|
319
|
+
endDate?: string;
|
|
320
|
+
};
|
|
321
|
+
type PaginationInfo = {
|
|
322
|
+
total: number;
|
|
323
|
+
page: number;
|
|
324
|
+
itemsPerPage: number;
|
|
325
|
+
totalPages: number;
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Tipos de resposta padrão da API
|
|
330
|
+
*/
|
|
331
|
+
namespace responses {
|
|
332
|
+
type ApiResponse<T = any> = {
|
|
333
|
+
conex: boolean;
|
|
334
|
+
reqStat: number;
|
|
335
|
+
success: boolean;
|
|
336
|
+
data: T;
|
|
337
|
+
msg: string;
|
|
338
|
+
};
|
|
339
|
+
namespace auth {
|
|
340
|
+
namespace login {
|
|
341
|
+
type POST = ApiResponse<{
|
|
342
|
+
user: objects.LocalUser;
|
|
343
|
+
msg?: string;
|
|
344
|
+
}>;
|
|
345
|
+
}
|
|
346
|
+
namespace logout {
|
|
347
|
+
type GET = ApiResponse<{}>;
|
|
348
|
+
}
|
|
349
|
+
namespace checkSession {
|
|
350
|
+
type GET = ApiResponse<{
|
|
351
|
+
user?: objects.LocalUser;
|
|
352
|
+
msg?: string;
|
|
353
|
+
}>;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
namespace users {
|
|
357
|
+
type GET = ApiResponse<objects.LocalUser[]>;
|
|
358
|
+
}
|
|
359
|
+
namespace user {
|
|
360
|
+
type GET = ApiResponse<objects.LocalUser & {
|
|
361
|
+
Wallets?: objects.Wallet[];
|
|
362
|
+
PurchasesShares?: objects.PurchaseShare[];
|
|
363
|
+
}>;
|
|
364
|
+
type PUT = ApiResponse<objects.LocalUser>;
|
|
365
|
+
namespace updateConfig {
|
|
366
|
+
type PUT = ApiResponse<{
|
|
367
|
+
msg?: string;
|
|
368
|
+
}>;
|
|
369
|
+
}
|
|
370
|
+
namespace updateAdmin {
|
|
371
|
+
type PUT = ApiResponse<objects.LocalUser>;
|
|
372
|
+
}
|
|
373
|
+
namespace license {
|
|
374
|
+
type PUT = ApiResponse<{
|
|
375
|
+
availableLicenses: number;
|
|
376
|
+
heldLicenses: number;
|
|
377
|
+
usedLicenses: number;
|
|
378
|
+
msg?: string;
|
|
379
|
+
}>;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
namespace purchase {
|
|
383
|
+
namespace create {
|
|
384
|
+
type POST = ApiResponse<{
|
|
385
|
+
msg?: string;
|
|
386
|
+
purchase: objects.Purchase;
|
|
387
|
+
url?: string;
|
|
388
|
+
}>;
|
|
389
|
+
}
|
|
390
|
+
namespace id {
|
|
391
|
+
namespace sync {
|
|
392
|
+
type POST = ApiResponse<objects.Purchase & {
|
|
393
|
+
msg?: string;
|
|
394
|
+
}>;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
type PUT = ApiResponse<objects.Purchase | {
|
|
398
|
+
msg?: string;
|
|
399
|
+
}>;
|
|
400
|
+
namespace cancel {
|
|
401
|
+
type PUT = ApiResponse<objects.Purchase>;
|
|
402
|
+
}
|
|
403
|
+
type DELETE = ApiResponse<{
|
|
404
|
+
msg?: string;
|
|
405
|
+
}>;
|
|
406
|
+
namespace getFromGateway {
|
|
407
|
+
type GET = ApiResponse<objects.PaymentSystem.PG_Purchase>;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
namespace purchases {
|
|
411
|
+
type post_list = ApiResponse<{
|
|
412
|
+
purchases: objects.Purchase[];
|
|
413
|
+
pagination: {
|
|
414
|
+
page: number;
|
|
415
|
+
pageSize: number;
|
|
416
|
+
count: number;
|
|
417
|
+
};
|
|
418
|
+
}>;
|
|
419
|
+
}
|
|
420
|
+
namespace product {
|
|
421
|
+
type GET = ApiResponse<objects.Product>;
|
|
422
|
+
type PUT = ApiResponse<objects.Product>;
|
|
423
|
+
}
|
|
424
|
+
namespace products {
|
|
425
|
+
type GET = ApiResponse<objects.Product[]>;
|
|
426
|
+
}
|
|
427
|
+
namespace paymentMethod {
|
|
428
|
+
type POST = ApiResponse<objects.PaymentMethod>;
|
|
429
|
+
type PUT = ApiResponse<objects.PaymentMethod>;
|
|
430
|
+
type DELETE = ApiResponse<{
|
|
431
|
+
msg?: string;
|
|
432
|
+
}>;
|
|
433
|
+
}
|
|
434
|
+
namespace logs {
|
|
435
|
+
type POST_SEARCH = ApiResponse<{
|
|
436
|
+
data: {
|
|
437
|
+
logs: objects.LogEntry[];
|
|
438
|
+
pagination: auxiliarTypes.PaginationInfo;
|
|
439
|
+
available: boolean;
|
|
440
|
+
};
|
|
441
|
+
}>;
|
|
442
|
+
namespace status {
|
|
443
|
+
type GET = ApiResponse<{
|
|
444
|
+
saveToFile: boolean;
|
|
445
|
+
logPretty: boolean;
|
|
446
|
+
fileExists: boolean;
|
|
447
|
+
filePath: string;
|
|
448
|
+
logLevel: string;
|
|
449
|
+
canListLogs: boolean;
|
|
450
|
+
}>;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
namespace cronJobs {
|
|
454
|
+
namespace status {
|
|
455
|
+
type GET = ApiResponse<auxiliarTypes.CronjobsStatuses>;
|
|
456
|
+
}
|
|
457
|
+
namespace run {
|
|
458
|
+
type POST = ApiResponse<{
|
|
459
|
+
msg: string;
|
|
460
|
+
}>;
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
namespace aux {
|
|
464
|
+
namespace countries {
|
|
465
|
+
type GET = ApiResponse<Array<{
|
|
466
|
+
id: number;
|
|
467
|
+
nome: string;
|
|
468
|
+
nome_pt: string;
|
|
469
|
+
}>>;
|
|
470
|
+
}
|
|
471
|
+
namespace states {
|
|
472
|
+
type GET = ApiResponse<Array<{
|
|
473
|
+
id: number;
|
|
474
|
+
nome: string;
|
|
475
|
+
uf: string;
|
|
476
|
+
ibge: number;
|
|
477
|
+
pais?: number;
|
|
478
|
+
ddd?: string;
|
|
479
|
+
}>>;
|
|
480
|
+
}
|
|
481
|
+
namespace cities {
|
|
482
|
+
type GET = ApiResponse<Array<{
|
|
483
|
+
id: number;
|
|
484
|
+
nome: string;
|
|
485
|
+
uf: number;
|
|
486
|
+
ibge: number;
|
|
487
|
+
}>>;
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
namespace report {
|
|
491
|
+
type POST = ApiResponse<{
|
|
492
|
+
msg?: string;
|
|
493
|
+
}>;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
/**
|
|
497
|
+
* Configuração do Client
|
|
498
|
+
*/
|
|
499
|
+
type ClientConfig = {
|
|
500
|
+
baseURL: string;
|
|
501
|
+
tokenStorage?: {
|
|
502
|
+
getToken: () => string | null;
|
|
503
|
+
setToken: (token: string) => void;
|
|
504
|
+
getPin1: () => string | null;
|
|
505
|
+
setPin1: (pin1: string) => void;
|
|
506
|
+
};
|
|
507
|
+
onTokenUpdate?: (token: string) => void;
|
|
508
|
+
onPin1Update?: (pin1: string) => void;
|
|
509
|
+
retryAttempts?: number;
|
|
510
|
+
retryDelay?: number;
|
|
511
|
+
};
|
|
512
|
+
/**
|
|
513
|
+
* Client para comunicação com a API de autenticação
|
|
514
|
+
*/
|
|
515
|
+
class Client {
|
|
516
|
+
private baseURL;
|
|
517
|
+
private bearer;
|
|
518
|
+
private pin1;
|
|
519
|
+
private tokenStorage?;
|
|
520
|
+
private onTokenUpdate?;
|
|
521
|
+
private onPin1Update?;
|
|
522
|
+
private retryAttempts;
|
|
523
|
+
private retryDelay;
|
|
524
|
+
private isHealth;
|
|
525
|
+
constructor(config: ClientConfig);
|
|
526
|
+
/**
|
|
527
|
+
* Define o token de autenticação
|
|
528
|
+
*/
|
|
529
|
+
setToken(token: string): void;
|
|
530
|
+
/**
|
|
531
|
+
* Define o PIN1
|
|
532
|
+
*/
|
|
533
|
+
setPin1(pin1: string): void;
|
|
534
|
+
/**
|
|
535
|
+
* Obtém o token atual
|
|
536
|
+
*/
|
|
537
|
+
getToken(): string;
|
|
538
|
+
/**
|
|
539
|
+
* Obtém o PIN1 atual
|
|
540
|
+
*/
|
|
541
|
+
getPin1(): string;
|
|
542
|
+
/**
|
|
543
|
+
* Verifica se o servidor está saudável
|
|
544
|
+
*/
|
|
545
|
+
isHealthy(): Promise<boolean>;
|
|
546
|
+
/**
|
|
547
|
+
* Requisição base para a API
|
|
548
|
+
*/
|
|
549
|
+
private request;
|
|
550
|
+
/**
|
|
551
|
+
* Constrói query string a partir de um objeto
|
|
552
|
+
*/
|
|
553
|
+
private buildQueryString;
|
|
554
|
+
auth: {
|
|
555
|
+
/**
|
|
556
|
+
* Faz login na API
|
|
557
|
+
*/
|
|
558
|
+
login: (email: string, password: string) => Promise<responses.auth.login.POST>;
|
|
559
|
+
/**
|
|
560
|
+
* Faz logout da API
|
|
561
|
+
*/
|
|
562
|
+
logout: () => Promise<responses.auth.logout.GET>;
|
|
563
|
+
/**
|
|
564
|
+
* Verifica a sessão atual
|
|
565
|
+
*/
|
|
566
|
+
checkSession: () => Promise<responses.auth.checkSession.GET>;
|
|
567
|
+
};
|
|
568
|
+
checkHealth(): Promise<boolean>;
|
|
569
|
+
user: {
|
|
570
|
+
list: () => Promise<responses.ApiResponse<responses.users.GET>>;
|
|
571
|
+
get: (userId: number, includePurchases?: boolean) => Promise<responses.ApiResponse<responses.user.GET>>;
|
|
572
|
+
update: (userId: number, userData: Partial<objects.LocalUser>) => Promise<responses.ApiResponse<responses.user.PUT>>;
|
|
573
|
+
updateConfig: (obj: {
|
|
574
|
+
id: number;
|
|
575
|
+
config_ihm_showRates?: boolean;
|
|
576
|
+
}) => Promise<responses.ApiResponse<responses.user.updateConfig.PUT>>;
|
|
577
|
+
updateAdmin: (userId: number, isAdmin: boolean) => Promise<responses.ApiResponse<responses.user.updateAdmin.PUT>>;
|
|
578
|
+
license: (userId: number) => Promise<responses.ApiResponse<responses.user.license.PUT>>;
|
|
579
|
+
};
|
|
580
|
+
purchase: {
|
|
581
|
+
new: (paymentMethodId: number, userId: number) => Promise<responses.ApiResponse<responses.purchase.create.POST>>;
|
|
582
|
+
sync: (purchaseId: number, userId: number, async?: boolean) => Promise<responses.ApiResponse<responses.purchase.id.sync.POST>>;
|
|
583
|
+
update: (userId: number, purchase: objects.Purchase) => Promise<responses.ApiResponse<responses.purchase.PUT>>;
|
|
584
|
+
cancel: (purchaseId: number, userId: number) => Promise<responses.ApiResponse<responses.purchase.cancel.PUT>>;
|
|
585
|
+
delete: (purchaseId: number, userId: number) => Promise<responses.ApiResponse<responses.purchase.DELETE>>;
|
|
586
|
+
list: (page?: number, pageSize?: number, userId?: number) => Promise<responses.ApiResponse<responses.purchases.post_list>>;
|
|
587
|
+
getFromGateway: (purchaseId: number, userId: number, async?: boolean) => Promise<responses.ApiResponse<responses.purchase.getFromGateway.GET>>;
|
|
588
|
+
};
|
|
589
|
+
products: {
|
|
590
|
+
list: () => Promise<responses.ApiResponse<responses.products.GET>>;
|
|
591
|
+
get: (productId: number) => Promise<responses.ApiResponse<responses.product.GET>>;
|
|
592
|
+
update: (productId: number, productData: any) => Promise<responses.ApiResponse<responses.product.PUT>>;
|
|
593
|
+
};
|
|
594
|
+
paymentMethods: {
|
|
595
|
+
create: (id: number, paymentMethodData: objects.PaymentMethod) => Promise<responses.ApiResponse<responses.paymentMethod.POST>>;
|
|
596
|
+
update: (id: number, paymentMethodId: number, paymentMethodData: objects.PaymentMethod) => Promise<responses.ApiResponse<responses.paymentMethod.PUT>>;
|
|
597
|
+
delete: (id: number, paymentMethodId: number) => Promise<responses.ApiResponse<responses.paymentMethod.DELETE>>;
|
|
598
|
+
};
|
|
599
|
+
aux: {
|
|
600
|
+
countries: () => Promise<responses.ApiResponse<responses.aux.countries.GET>>;
|
|
601
|
+
states: (countryId: number) => Promise<responses.ApiResponse<responses.aux.states.GET>>;
|
|
602
|
+
cities: (countryId: number, stateId: number) => Promise<responses.ApiResponse<responses.aux.cities.GET>>;
|
|
603
|
+
};
|
|
604
|
+
admin: {
|
|
605
|
+
logs: {
|
|
606
|
+
/**
|
|
607
|
+
* @param filters
|
|
608
|
+
* @returns
|
|
609
|
+
*/
|
|
610
|
+
list: (filters?: Partial<auxiliarTypes.LogFilters>) => Promise<responses.ApiResponse<responses.logs.POST_SEARCH>>;
|
|
611
|
+
status: () => Promise<responses.ApiResponse<responses.logs.status.GET>>;
|
|
612
|
+
};
|
|
613
|
+
cronJobs: {
|
|
614
|
+
status: () => Promise<responses.ApiResponse<responses.cronJobs.status.GET>>;
|
|
615
|
+
run: (name: string) => Promise<responses.ApiResponse<responses.cronJobs.run.POST>>;
|
|
616
|
+
};
|
|
617
|
+
};
|
|
618
|
+
report: {
|
|
619
|
+
new: (id: string, data: any) => Promise<responses.ApiResponse<responses.report.POST>>;
|
|
620
|
+
};
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
export default AuthApi;
|
package/dist/index.js
CHANGED
|
@@ -1,20 +1,421 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
3
|
+
exports.AuthApi = void 0;
|
|
4
|
+
const tools_1 = require("./tools");
|
|
5
|
+
/**
|
|
6
|
+
* Namespace principal da API de autenticação
|
|
7
|
+
* Acesso aos tipos: AuthApi.objects.User, AuthApi.responses.auth.login.post, etc.
|
|
8
|
+
*/
|
|
9
|
+
var AuthApi;
|
|
10
|
+
(function (AuthApi) {
|
|
11
|
+
/**
|
|
12
|
+
* Objetos de domínio da aplicação
|
|
13
|
+
*/
|
|
14
|
+
let objects;
|
|
15
|
+
(function (objects) {
|
|
16
|
+
let BundleType;
|
|
17
|
+
(function (BundleType) {
|
|
18
|
+
BundleType[BundleType["BUNDLE"] = 0] = "BUNDLE";
|
|
19
|
+
BundleType[BundleType["SYMBOLIC_PRODUCT"] = 1] = "SYMBOLIC_PRODUCT";
|
|
20
|
+
BundleType[BundleType["SYMBOLIC_COURSE"] = 2] = "SYMBOLIC_COURSE";
|
|
21
|
+
BundleType[BundleType["SYMBOLIC_CONSULTANCY"] = 3] = "SYMBOLIC_CONSULTANCY";
|
|
22
|
+
})(BundleType = objects.BundleType || (objects.BundleType = {}));
|
|
23
|
+
let PaymentSystem;
|
|
24
|
+
(function (PaymentSystem) {
|
|
25
|
+
let PG_PurchaseLicenseStatus;
|
|
26
|
+
(function (PG_PurchaseLicenseStatus) {
|
|
27
|
+
// Pagamentos ou Assinaturas
|
|
28
|
+
PG_PurchaseLicenseStatus[PG_PurchaseLicenseStatus["UNKNOWN"] = 0] = "UNKNOWN";
|
|
29
|
+
PG_PurchaseLicenseStatus[PG_PurchaseLicenseStatus["OPEN"] = 3] = "OPEN";
|
|
30
|
+
// Pagamentos
|
|
31
|
+
PG_PurchaseLicenseStatus[PG_PurchaseLicenseStatus["EXPIRED"] = 1] = "EXPIRED";
|
|
32
|
+
PG_PurchaseLicenseStatus[PG_PurchaseLicenseStatus["PAID"] = 5] = "PAID";
|
|
33
|
+
// Assinaturas
|
|
34
|
+
PG_PurchaseLicenseStatus[PG_PurchaseLicenseStatus["ACTIVE"] = 6] = "ACTIVE";
|
|
35
|
+
PG_PurchaseLicenseStatus[PG_PurchaseLicenseStatus["RECYCLING"] = 4] = "RECYCLING";
|
|
36
|
+
PG_PurchaseLicenseStatus[PG_PurchaseLicenseStatus["CANCELED"] = 2] = "CANCELED";
|
|
37
|
+
})(PG_PurchaseLicenseStatus = PaymentSystem.PG_PurchaseLicenseStatus || (PaymentSystem.PG_PurchaseLicenseStatus = {}));
|
|
38
|
+
PaymentSystem.PG_PurchaseLicenseStatus_GroupActive = [
|
|
39
|
+
PG_PurchaseLicenseStatus.PAID,
|
|
40
|
+
PG_PurchaseLicenseStatus.ACTIVE,
|
|
41
|
+
PG_PurchaseLicenseStatus.RECYCLING,
|
|
42
|
+
];
|
|
43
|
+
let PG_PurchasePaymentStatus;
|
|
44
|
+
(function (PG_PurchasePaymentStatus) {
|
|
45
|
+
PG_PurchasePaymentStatus[PG_PurchasePaymentStatus["UNKNOWN"] = 0] = "UNKNOWN";
|
|
46
|
+
PG_PurchasePaymentStatus[PG_PurchasePaymentStatus["ACTIVE"] = 1] = "ACTIVE";
|
|
47
|
+
PG_PurchasePaymentStatus[PG_PurchasePaymentStatus["CANCELED"] = 2] = "CANCELED";
|
|
48
|
+
PG_PurchasePaymentStatus[PG_PurchasePaymentStatus["REFUNDED"] = 3] = "REFUNDED";
|
|
49
|
+
PG_PurchasePaymentStatus[PG_PurchasePaymentStatus["REMOVED"] = 4] = "REMOVED";
|
|
50
|
+
PG_PurchasePaymentStatus[PG_PurchasePaymentStatus["PENDING"] = 5] = "PENDING";
|
|
51
|
+
})(PG_PurchasePaymentStatus = PaymentSystem.PG_PurchasePaymentStatus || (PaymentSystem.PG_PurchasePaymentStatus = {}));
|
|
52
|
+
let PG_PurchaseType;
|
|
53
|
+
(function (PG_PurchaseType) {
|
|
54
|
+
PG_PurchaseType[PG_PurchaseType["SESSION"] = 0] = "SESSION";
|
|
55
|
+
PG_PurchaseType[PG_PurchaseType["STANDARD"] = 1] = "STANDARD";
|
|
56
|
+
PG_PurchaseType[PG_PurchaseType["RECURRENT"] = 2] = "RECURRENT";
|
|
57
|
+
})(PG_PurchaseType = PaymentSystem.PG_PurchaseType || (PaymentSystem.PG_PurchaseType = {}));
|
|
58
|
+
let PG_RecurrenceType;
|
|
59
|
+
(function (PG_RecurrenceType) {
|
|
60
|
+
PG_RecurrenceType[PG_RecurrenceType["NONE"] = 0] = "NONE";
|
|
61
|
+
PG_RecurrenceType[PG_RecurrenceType["DAILY"] = 1] = "DAILY";
|
|
62
|
+
PG_RecurrenceType[PG_RecurrenceType["WEEKLY"] = 2] = "WEEKLY";
|
|
63
|
+
PG_RecurrenceType[PG_RecurrenceType["MONTHLY"] = 3] = "MONTHLY";
|
|
64
|
+
PG_RecurrenceType[PG_RecurrenceType["YEARLY"] = 4] = "YEARLY";
|
|
65
|
+
})(PG_RecurrenceType = PaymentSystem.PG_RecurrenceType || (PaymentSystem.PG_RecurrenceType = {}));
|
|
66
|
+
})(PaymentSystem = objects.PaymentSystem || (objects.PaymentSystem = {}));
|
|
67
|
+
})(objects = AuthApi.objects || (AuthApi.objects = {}));
|
|
68
|
+
let auxiliarTypes;
|
|
69
|
+
(function (auxiliarTypes) {
|
|
70
|
+
let Privilege;
|
|
71
|
+
(function (Privilege) {
|
|
72
|
+
Privilege[Privilege["NONE"] = 0] = "NONE";
|
|
73
|
+
Privilege[Privilege["READ"] = 1] = "READ";
|
|
74
|
+
Privilege[Privilege["WRITE"] = 2] = "WRITE";
|
|
75
|
+
})(Privilege = auxiliarTypes.Privilege || (auxiliarTypes.Privilege = {}));
|
|
76
|
+
})(auxiliarTypes = AuthApi.auxiliarTypes || (AuthApi.auxiliarTypes = {}));
|
|
77
|
+
/**
|
|
78
|
+
* Client para comunicação com a API de autenticação
|
|
79
|
+
*/
|
|
80
|
+
class Client {
|
|
81
|
+
constructor(config) {
|
|
82
|
+
this.bearer = '';
|
|
83
|
+
this.pin1 = '';
|
|
84
|
+
this.retryAttempts = 0;
|
|
85
|
+
this.retryDelay = 500;
|
|
86
|
+
this.isHealth = true;
|
|
87
|
+
// ==================== MÉTODOS DE AUTENTICAÇÃO ====================
|
|
88
|
+
this.auth = {
|
|
89
|
+
/**
|
|
90
|
+
* Faz login na API
|
|
91
|
+
*/
|
|
92
|
+
login: async (email, password) => {
|
|
93
|
+
return this.request('POST', 'auth/login', { email, password });
|
|
94
|
+
},
|
|
95
|
+
/**
|
|
96
|
+
* Faz logout da API
|
|
97
|
+
*/
|
|
98
|
+
logout: async () => {
|
|
99
|
+
const result = await this.request('GET', 'auth/logout', {});
|
|
100
|
+
this.setToken('');
|
|
101
|
+
this.setPin1('');
|
|
102
|
+
return result;
|
|
103
|
+
},
|
|
104
|
+
/**
|
|
105
|
+
* Verifica a sessão atual
|
|
106
|
+
*/
|
|
107
|
+
checkSession: async () => {
|
|
108
|
+
if (!this.bearer) {
|
|
109
|
+
return {
|
|
110
|
+
conex: true,
|
|
111
|
+
reqStat: 401,
|
|
112
|
+
success: false,
|
|
113
|
+
data: {},
|
|
114
|
+
msg: 'Token not found'
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
return this.request('GET', 'auth', {}, { retryAttempts: 2, retryDelay: 1000 });
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
this.user = {
|
|
121
|
+
list: async () => {
|
|
122
|
+
return await this.request('GET', 'users', {});
|
|
123
|
+
},
|
|
124
|
+
get: async (userId, includePurchases = true) => {
|
|
125
|
+
return await this.request('GET', `user/${userId}?includePurchases=${includePurchases}`, {});
|
|
126
|
+
},
|
|
127
|
+
update: async (userId, userData) => {
|
|
128
|
+
return await this.request('PUT', `user/${userId}`, userData);
|
|
129
|
+
},
|
|
130
|
+
updateConfig: async (obj) => {
|
|
131
|
+
return await this.request('PUT', `user/${obj.id}/config`, obj);
|
|
132
|
+
},
|
|
133
|
+
updateAdmin: async (userId, isAdmin) => {
|
|
134
|
+
return await this.request('PUT', `user/${userId}/admin`, { isAdmin });
|
|
135
|
+
},
|
|
136
|
+
license: async (userId) => {
|
|
137
|
+
return await this.request('GET', `user/${userId}/license`, {});
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
this.purchase = {
|
|
141
|
+
new: async (paymentMethodId, userId) => {
|
|
142
|
+
return await this.request('POST', `user/${userId}/purchase/new/${paymentMethodId}`, {});
|
|
143
|
+
},
|
|
144
|
+
sync: async (purchaseId, userId, async = false) => {
|
|
145
|
+
return await this.request('POST', `user/${userId}/purchase/${purchaseId}/sync`, { async });
|
|
146
|
+
},
|
|
147
|
+
update: async (userId, purchase) => {
|
|
148
|
+
return await this.request('PUT', `user/${userId}/purchase/${purchase.id}`, purchase);
|
|
149
|
+
},
|
|
150
|
+
cancel: async (purchaseId, userId) => {
|
|
151
|
+
return await this.request('PUT', `user/${userId}/purchase/${purchaseId}/cancel`, {});
|
|
152
|
+
},
|
|
153
|
+
delete: async (purchaseId, userId) => {
|
|
154
|
+
return await this.request('DELETE', `user/${userId}/purchase/${purchaseId}`, {});
|
|
155
|
+
},
|
|
156
|
+
list: async (page = 1, pageSize, userId) => {
|
|
157
|
+
return await this.request('POST', `admin/purchases/search`, { page, pageSize, userId });
|
|
158
|
+
},
|
|
159
|
+
getFromGateway: async (purchaseId, userId, async = false) => {
|
|
160
|
+
return await this.request('GET', `user/${userId}/purchase/${purchaseId}/getFromGateway`, {});
|
|
161
|
+
},
|
|
162
|
+
};
|
|
163
|
+
this.products = {
|
|
164
|
+
list: async () => {
|
|
165
|
+
return await this.request('GET', 'products', {});
|
|
166
|
+
},
|
|
167
|
+
get: async (productId) => {
|
|
168
|
+
return await this.request('GET', `product/${productId}`, { mode: 'admin' });
|
|
169
|
+
},
|
|
170
|
+
update: async (productId, productData) => {
|
|
171
|
+
return await this.request('PUT', `product/${productId}`, productData);
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
this.paymentMethods = {
|
|
175
|
+
create: async (id, paymentMethodData) => {
|
|
176
|
+
return await this.request('POST', `bundle/${id}/payment-method`, paymentMethodData);
|
|
177
|
+
},
|
|
178
|
+
update: async (id, paymentMethodId, paymentMethodData) => {
|
|
179
|
+
return await this.request('PUT', `bundle/${id}/payment-method/${paymentMethodId}`, paymentMethodData);
|
|
180
|
+
},
|
|
181
|
+
delete: async (id, paymentMethodId) => {
|
|
182
|
+
return await this.request('DELETE', `bundle/${id}/payment-method/${paymentMethodId}`, {});
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
this.aux = {
|
|
186
|
+
countries: async () => {
|
|
187
|
+
return await this.request('GET', 'aux/countries', {});
|
|
188
|
+
},
|
|
189
|
+
states: async (countryId) => {
|
|
190
|
+
return await this.request('GET', `aux/country/${countryId}/states`, {});
|
|
191
|
+
},
|
|
192
|
+
cities: async (countryId, stateId) => {
|
|
193
|
+
return await this.request('GET', `aux/country/${countryId}/state/${stateId}/cities`, {});
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
this.admin = {
|
|
197
|
+
logs: {
|
|
198
|
+
/**
|
|
199
|
+
* @param filters
|
|
200
|
+
* @returns
|
|
201
|
+
*/
|
|
202
|
+
list: async (filters = {}) => {
|
|
203
|
+
const defaultFilters = {
|
|
204
|
+
page: 1,
|
|
205
|
+
itemsPerPage: 50,
|
|
206
|
+
search: '',
|
|
207
|
+
...filters
|
|
208
|
+
};
|
|
209
|
+
return await this.request('POST', 'logs', defaultFilters);
|
|
210
|
+
},
|
|
211
|
+
status: async () => {
|
|
212
|
+
return await this.request('GET', 'logs/status', {});
|
|
213
|
+
}
|
|
214
|
+
},
|
|
215
|
+
cronJobs: {
|
|
216
|
+
status: async () => {
|
|
217
|
+
return await this.request('GET', 'admin/cronjobs/status', {});
|
|
218
|
+
},
|
|
219
|
+
run: async (name) => {
|
|
220
|
+
return await this.request('POST', `admin/cronjobs/run/${name}`, {});
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
this.report = {
|
|
225
|
+
new: async (id, data) => {
|
|
226
|
+
return await this.request('POST', 'report', {
|
|
227
|
+
id,
|
|
228
|
+
data: (0, tools_1.safeStringify)(data),
|
|
229
|
+
appVersion: 'unknown'
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
this.baseURL = config.baseURL;
|
|
234
|
+
this.tokenStorage = config.tokenStorage;
|
|
235
|
+
this.onTokenUpdate = config.onTokenUpdate;
|
|
236
|
+
this.onPin1Update = config.onPin1Update;
|
|
237
|
+
this.retryAttempts = config.retryAttempts ?? 0;
|
|
238
|
+
this.retryDelay = config.retryDelay ?? 500;
|
|
239
|
+
// Carrega tokens do storage se disponível
|
|
240
|
+
if (this.tokenStorage) {
|
|
241
|
+
this.bearer = this.tokenStorage.getToken() ?? '';
|
|
242
|
+
this.pin1 = this.tokenStorage.getPin1() ?? '';
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Define o token de autenticação
|
|
247
|
+
*/
|
|
248
|
+
setToken(token) {
|
|
249
|
+
this.bearer = token;
|
|
250
|
+
if (this.tokenStorage) {
|
|
251
|
+
this.tokenStorage.setToken(token);
|
|
252
|
+
}
|
|
253
|
+
if (this.onTokenUpdate) {
|
|
254
|
+
this.onTokenUpdate(token);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Define o PIN1
|
|
259
|
+
*/
|
|
260
|
+
setPin1(pin1) {
|
|
261
|
+
this.pin1 = pin1;
|
|
262
|
+
if (this.tokenStorage) {
|
|
263
|
+
this.tokenStorage.setPin1(pin1);
|
|
264
|
+
}
|
|
265
|
+
if (this.onPin1Update) {
|
|
266
|
+
this.onPin1Update(pin1);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Obtém o token atual
|
|
271
|
+
*/
|
|
272
|
+
getToken() {
|
|
273
|
+
return this.bearer;
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Obtém o PIN1 atual
|
|
277
|
+
*/
|
|
278
|
+
getPin1() {
|
|
279
|
+
return this.pin1;
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Verifica se o servidor está saudável
|
|
283
|
+
*/
|
|
284
|
+
async isHealthy() {
|
|
285
|
+
if (!this.isHealth) {
|
|
286
|
+
try {
|
|
287
|
+
const r = await this.request('GET', '', {});
|
|
288
|
+
if (r.success) {
|
|
289
|
+
this.isHealth = true;
|
|
290
|
+
return true;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
catch { }
|
|
294
|
+
return false;
|
|
295
|
+
}
|
|
296
|
+
return true;
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Requisição base para a API
|
|
300
|
+
*/
|
|
301
|
+
async request(method, endpoint, params = {}, options) {
|
|
302
|
+
const result = {
|
|
303
|
+
conex: true,
|
|
304
|
+
reqStat: 0,
|
|
305
|
+
success: false,
|
|
306
|
+
data: {},
|
|
307
|
+
msg: ''
|
|
308
|
+
};
|
|
309
|
+
const headers = {};
|
|
310
|
+
if (this.bearer)
|
|
311
|
+
headers['authorization'] = `Bearer ${this.bearer}`;
|
|
312
|
+
if (this.pin1)
|
|
313
|
+
headers['pin1'] = this.pin1;
|
|
314
|
+
const maxAttempts = 1 + (options?.retryAttempts ?? this.retryAttempts);
|
|
315
|
+
const retryDelay = options?.retryDelay ?? this.retryDelay;
|
|
316
|
+
let attempts = 0;
|
|
317
|
+
let response = null;
|
|
318
|
+
while (attempts < maxAttempts) {
|
|
319
|
+
attempts++;
|
|
320
|
+
try {
|
|
321
|
+
const url = `${this.baseURL}/${endpoint}${method === 'GET' || method === 'DELETE' ? this.buildQueryString(params) : ''}`;
|
|
322
|
+
const fetchOptions = {
|
|
323
|
+
method,
|
|
324
|
+
headers: {
|
|
325
|
+
'Content-Type': 'application/json',
|
|
326
|
+
...headers
|
|
327
|
+
},
|
|
328
|
+
credentials: 'include'
|
|
329
|
+
};
|
|
330
|
+
if (method === 'POST' || method === 'PUT') {
|
|
331
|
+
fetchOptions.body = JSON.stringify(params);
|
|
332
|
+
}
|
|
333
|
+
response = await fetch(url, fetchOptions);
|
|
334
|
+
// Atualiza tokens se presentes nos headers
|
|
335
|
+
const newToken = response.headers.get('authorization');
|
|
336
|
+
if (newToken) {
|
|
337
|
+
this.setToken(newToken.replace('Bearer ', ''));
|
|
338
|
+
}
|
|
339
|
+
const newPin1 = response.headers.get('pin1');
|
|
340
|
+
if (newPin1) {
|
|
341
|
+
this.setPin1(newPin1);
|
|
342
|
+
}
|
|
343
|
+
// Se receber 503, marca como unhealthy e tenta novamente
|
|
344
|
+
if (response.status === 503) {
|
|
345
|
+
this.isHealth = false;
|
|
346
|
+
if (attempts >= maxAttempts)
|
|
347
|
+
break;
|
|
348
|
+
await new Promise(res => setTimeout(res, retryDelay));
|
|
349
|
+
continue;
|
|
350
|
+
}
|
|
351
|
+
this.isHealth = true;
|
|
352
|
+
result.reqStat = response.status;
|
|
353
|
+
result.success = response.status >= 200 && response.status < 300;
|
|
354
|
+
try {
|
|
355
|
+
const jsonData = await response.json();
|
|
356
|
+
result.data = jsonData;
|
|
357
|
+
result.msg = jsonData.msg ?? '';
|
|
358
|
+
}
|
|
359
|
+
catch {
|
|
360
|
+
result.msg = `stat_${response.status}`;
|
|
361
|
+
}
|
|
362
|
+
break;
|
|
363
|
+
}
|
|
364
|
+
catch (e) {
|
|
365
|
+
this.isHealth = false;
|
|
366
|
+
if (attempts >= maxAttempts) {
|
|
367
|
+
result.conex = false;
|
|
368
|
+
result.msg = `connectionError_${e.code ?? 'unknown'}`;
|
|
369
|
+
}
|
|
370
|
+
else {
|
|
371
|
+
await new Promise(res => setTimeout(res, retryDelay));
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
if (!result.conex) {
|
|
376
|
+
result.msg = result.msg || 'generic connection error';
|
|
377
|
+
}
|
|
378
|
+
return result;
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* Constrói query string a partir de um objeto
|
|
382
|
+
*/
|
|
383
|
+
buildQueryString(params) {
|
|
384
|
+
if (!params || Object.keys(params).length === 0)
|
|
385
|
+
return '';
|
|
386
|
+
const queryParams = new URLSearchParams();
|
|
387
|
+
Object.keys(params).forEach(key => {
|
|
388
|
+
const value = params[key];
|
|
389
|
+
if (value !== undefined && value !== null) {
|
|
390
|
+
if (Array.isArray(value)) {
|
|
391
|
+
queryParams.append(key, JSON.stringify(value));
|
|
392
|
+
}
|
|
393
|
+
else if (typeof value === 'object') {
|
|
394
|
+
queryParams.append(key, JSON.stringify(value));
|
|
395
|
+
}
|
|
396
|
+
else {
|
|
397
|
+
queryParams.append(key, String(value));
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
});
|
|
401
|
+
return `?${queryParams.toString()}`;
|
|
402
|
+
}
|
|
403
|
+
async checkHealth() {
|
|
404
|
+
if (!this.isHealth) {
|
|
405
|
+
try {
|
|
406
|
+
const r = await this.request('GET', '', {});
|
|
407
|
+
if (r.success) {
|
|
408
|
+
this.isHealth = true;
|
|
409
|
+
return true;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
catch { }
|
|
413
|
+
return false;
|
|
414
|
+
}
|
|
415
|
+
return true;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
AuthApi.Client = Client;
|
|
419
|
+
})(AuthApi || (exports.AuthApi = AuthApi = {}));
|
|
420
|
+
// Exporta o namespace como default também
|
|
421
|
+
exports.default = AuthApi;
|
package/dist/tools.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function safeStringify(obj: any): string;
|
package/dist/tools.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.safeStringify = safeStringify;
|
|
4
|
+
function safeStringify(obj) {
|
|
5
|
+
const seen = new WeakSet();
|
|
6
|
+
return JSON.stringify(obj, (key, val) => {
|
|
7
|
+
if (val != null && typeof val === "object") {
|
|
8
|
+
if (seen.has(val)) {
|
|
9
|
+
return '[Circular]';
|
|
10
|
+
}
|
|
11
|
+
seen.add(val);
|
|
12
|
+
}
|
|
13
|
+
return val;
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
;
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import packageJson from '../package.json';
|
|
2
1
|
import { safeStringify } from "./tools";
|
|
3
2
|
|
|
4
3
|
/**
|
|
@@ -909,7 +908,7 @@ export namespace AuthApi {
|
|
|
909
908
|
return await this.request<responses.report.POST>('POST', 'report', {
|
|
910
909
|
id,
|
|
911
910
|
data: safeStringify(data),
|
|
912
|
-
appVersion:
|
|
911
|
+
appVersion: 'unknown'
|
|
913
912
|
});
|
|
914
913
|
}
|
|
915
914
|
}
|