@bitrix24/b24jssdk 0.1.7 → 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.
@@ -383,50 +383,59 @@ declare const Browser: BrowserManager;
383
383
  /**
384
384
  * Interface defining the structure and methods of a Result object.
385
385
  */
386
- interface IResult {
386
+ interface IResult<T = any> {
387
387
  /**
388
388
  * Indicates whether the operation resulted in success (no errors).
389
389
  */
390
- isSuccess: boolean;
390
+ readonly isSuccess: boolean;
391
+ /**
392
+ * Collection of errors
393
+ */
394
+ readonly errors: Map<string, Error>;
391
395
  /**
392
396
  * Sets the data associated with the result.
393
397
  *
394
398
  * @param data The data to be stored in the result.
395
399
  * @returns The current Result object for chaining methods.
396
400
  */
397
- setData: (data: any) => IResult;
401
+ setData: (data: T) => IResult<T>;
398
402
  /**
399
403
  * Retrieves the data associated with the result.
400
404
  *
401
405
  * @returns The data stored in the result, if any.
402
406
  */
403
- getData: () => any;
407
+ getData: () => T | null;
404
408
  /**
405
409
  * Adds an error message or Error object to the result.
406
- *
407
410
  * @param error The error message or Error object to be added.
408
- * @returns The current Result object for chaining methods.
411
+ * @param key Error key. You can leave it blank. Then it will be generated automatically.
412
+ * @returns {IResult} The current Result object for chaining methods.
409
413
  */
410
- addError: (error: Error | string) => IResult;
414
+ addError: (error: Error | string, key?: string) => IResult;
411
415
  /**
412
416
  * Adds multiple errors to the result in a single call.
413
417
  *
414
418
  * @param errors An array of errors or strings that will be converted to errors.
415
- * @returns The current Result object for chaining methods.
419
+ * @returns {IResult} The current Result object for chaining methods.
416
420
  */
417
421
  addErrors: (errors: (Error | string)[]) => IResult;
418
422
  /**
419
423
  * Retrieves an iterator for the errors collected in the result.
420
424
  *
421
- * @returns An iterator over the stored Error objects.
425
+ * @returns {IterableIterator<Error>} An iterator over the stored Error objects.
422
426
  */
423
427
  getErrors: () => IterableIterator<Error>;
424
428
  /**
425
429
  * Retrieves an array of error messages from the collected errors.
426
430
  *
427
- * @returns An array of strings representing the error messages.
431
+ * @returns {string[]} An array of strings representing the error messages.
428
432
  */
429
433
  getErrorMessages: () => string[];
434
+ /**
435
+ * Checks for an error in a collection by key
436
+ * @param key - Error key
437
+ */
438
+ hasError(key: string): boolean;
430
439
  /**
431
440
  * Converts the Result object to a string.
432
441
  *
@@ -439,49 +448,18 @@ interface IResult {
439
448
  * Similar to \Bitrix\Main\Result from Bitrix Framework.
440
449
  * @link https://dev.1c-bitrix.ru/api_d7/bitrix/main/result/index.php
441
450
  */
442
- declare class Result implements IResult {
443
- private _errorCollection;
444
- protected _data: any;
445
- constructor();
446
- /**
447
- * Getter for the `isSuccess` property.
448
- * Checks if the `_errorCollection` is empty to determine success.
449
- *
450
- * @returns Whether the operation resulted in success (no errors).
451
- */
451
+ declare class Result<T = any> implements IResult<T> {
452
+ protected _errors: Map<string, Error>;
453
+ protected _data: T | null;
454
+ constructor(data?: T);
452
455
  get isSuccess(): boolean;
453
- /**
454
- * Sets the data associated with the result.
455
- *
456
- * @param data The data to be stored in the result.
457
- * @returns The current Result object for chaining methods.
458
- */
459
- setData(data: any): Result;
460
- /**
461
- * Retrieves the data associated with the result.
462
- *
463
- * @returns The data stored in the result, if any.
464
- */
465
- getData(): any;
466
- /**
467
- * Adds an error message or Error object to the result.
468
- *
469
- * @param error The error message or Error object to be added.
470
- * @returns The current Result object for chaining methods.
471
- */
472
- addError(error: Error | string): Result;
473
- /**
474
- * Adds multiple errors to the result in a single call.
475
- *
476
- * @param errors An array of errors or strings that will be converted to errors.
477
- * @returns The current Result object for chaining methods.
478
- */
479
- addErrors(errors: (Error | string)[]): Result;
480
- /**
481
- * Retrieves an iterator for the errors collected in the result.
482
- * @returns An iterator over the stored Error objects.
483
- */
456
+ get errors(): Map<string, Error>;
457
+ setData(data: T | null): Result<T>;
458
+ getData(): T | null;
459
+ addError(error: Error | string, key?: string): Result<T>;
460
+ addErrors(errors: (Error | string)[]): Result<T>;
484
461
  getErrors(): IterableIterator<Error>;
462
+ hasError(key: string): boolean;
485
463
  /**
486
464
  * Retrieves an array of error messages from the collected errors.
487
465
  *
@@ -495,6 +473,10 @@ declare class Result implements IResult {
495
473
  * @returns {string} Returns a string representation of the result operation
496
474
  */
497
475
  toString(): string;
476
+ private safeStringify;
477
+ private replacer;
478
+ static ok<U>(data?: U): Result<U>;
479
+ static fail<U>(error: Error | string, key?: string): Result<U>;
498
480
  }
499
481
 
500
482
  type PayloadTime = {
@@ -538,36 +520,52 @@ type BatchPayload<C> = {
538
520
  };
539
521
  type Payload<P> = GetPayload<P> | ListPayload<P> | BatchPayload<P>;
540
522
 
541
- type AjaxQuery = {
523
+ type AjaxQuery = Readonly<{
542
524
  method: string;
543
- params: object;
525
+ params: Readonly<object>;
544
526
  start: number;
545
- };
546
- type AjaxResultParams = {
527
+ }>;
528
+ type AjaxResultParams<T = unknown> = Readonly<{
547
529
  error?: string | {
548
530
  error: string;
549
- error_description: string;
531
+ error_description?: string;
550
532
  };
551
533
  error_description?: string;
552
- result: any;
534
+ result: T;
553
535
  next?: NumberString;
554
536
  total?: NumberString;
555
- };
537
+ time: PayloadTime;
538
+ }>;
539
+ type AjaxResultOptions<T> = Readonly<{
540
+ answer: AjaxResultParams<T>;
541
+ query: AjaxQuery;
542
+ status: number;
543
+ }>;
556
544
  /**
557
545
  * Result of request to Rest Api
558
546
  */
559
- declare class AjaxResult extends Result implements IResult {
547
+ declare class AjaxResult<T = unknown> extends Result<Payload<T>> implements IResult<Payload<T>> {
548
+ #private;
560
549
  private readonly _status;
561
550
  private readonly _query;
562
- protected _data: AjaxResultParams;
563
- constructor(answer: AjaxResultParams, query: AjaxQuery, status: number);
564
- setData(data: any): Result;
565
- getData(): Payload<unknown>;
551
+ protected _data: AjaxResultParams<T>;
552
+ constructor(options: AjaxResultOptions<T>);
553
+ getData(): Payload<T>;
554
+ /**
555
+ * Alias for isMore
556
+ */
557
+ hasMore(): boolean;
566
558
  isMore(): boolean;
567
559
  getTotal(): number;
568
560
  getStatus(): number;
569
- getQuery(): AjaxQuery;
570
- getNext(http: TypeHttp): Promise<false | AjaxResult>;
561
+ getQuery(): Readonly<AjaxQuery>;
562
+ /**
563
+ * Alias for getNext
564
+ * @param http
565
+ */
566
+ fetchNext(http: TypeHttp): Promise<AjaxResult<T> | null>;
567
+ getNext(http: TypeHttp): Promise<AjaxResult<T> | false>;
568
+ setData(): never;
571
569
  }
572
570
 
573
571
  type TypeHttp = {
@@ -1360,18 +1358,81 @@ type AjaxErrorParams = {
1360
1358
  answerError: AnswerError;
1361
1359
  cause?: Error;
1362
1360
  };
1361
+ type ErrorDetails = {
1362
+ code: string;
1363
+ description?: string;
1364
+ status: number;
1365
+ requestInfo?: {
1366
+ method?: string;
1367
+ url?: string;
1368
+ params?: Record<string, unknown> | unknown;
1369
+ };
1370
+ originalError?: unknown;
1371
+ };
1363
1372
  /**
1364
1373
  * Error requesting RestApi
1365
1374
  */
1366
1375
  declare class AjaxError extends Error {
1367
- cause: null | Error;
1376
+ readonly code: string;
1368
1377
  private _status;
1369
- private _answerError;
1370
- constructor(params: AjaxErrorParams);
1378
+ readonly requestInfo?: ErrorDetails['requestInfo'];
1379
+ readonly timestamp: Date;
1380
+ readonly originalError?: unknown;
1381
+ constructor(details: ErrorDetails);
1382
+ /**
1383
+ * @deprecated
1384
+ */
1371
1385
  get answerError(): AnswerError;
1372
1386
  get status(): number;
1387
+ /**
1388
+ * @deprecated
1389
+ */
1373
1390
  set status(status: number);
1391
+ /**
1392
+ * Creates AjaxError from HTTP response
1393
+ */
1394
+ static fromResponse(response: {
1395
+ status: number;
1396
+ data?: {
1397
+ error?: string;
1398
+ error_description?: string;
1399
+ };
1400
+ config?: {
1401
+ method?: string;
1402
+ url?: string;
1403
+ params?: unknown;
1404
+ };
1405
+ }): AjaxError;
1406
+ /**
1407
+ * Creates AjaxError from exception
1408
+ */
1409
+ static fromException(error: unknown, context?: {
1410
+ code?: string;
1411
+ status?: number;
1412
+ requestInfo?: ErrorDetails['requestInfo'];
1413
+ }): AjaxError;
1414
+ /**
1415
+ * Serializes error for logging and debugging
1416
+ */
1417
+ toJSON(): {
1418
+ name: string;
1419
+ code: string;
1420
+ message: string;
1421
+ status: number;
1422
+ timestamp: string;
1423
+ requestInfo: {
1424
+ method?: string;
1425
+ url?: string;
1426
+ params?: Record<string, unknown> | unknown;
1427
+ } | undefined;
1428
+ stack: string | undefined;
1429
+ };
1430
+ /**
1431
+ * Formats error information for human-readable output
1432
+ */
1374
1433
  toString(): string;
1434
+ private static formatErrorMessage;
1435
+ private cleanErrorStack;
1375
1436
  }
1376
1437
 
1377
1438
  declare abstract class AbstractB24 implements TypeB24 {
@@ -383,50 +383,59 @@ declare const Browser: BrowserManager;
383
383
  /**
384
384
  * Interface defining the structure and methods of a Result object.
385
385
  */
386
- interface IResult {
386
+ interface IResult<T = any> {
387
387
  /**
388
388
  * Indicates whether the operation resulted in success (no errors).
389
389
  */
390
- isSuccess: boolean;
390
+ readonly isSuccess: boolean;
391
+ /**
392
+ * Collection of errors
393
+ */
394
+ readonly errors: Map<string, Error>;
391
395
  /**
392
396
  * Sets the data associated with the result.
393
397
  *
394
398
  * @param data The data to be stored in the result.
395
399
  * @returns The current Result object for chaining methods.
396
400
  */
397
- setData: (data: any) => IResult;
401
+ setData: (data: T) => IResult<T>;
398
402
  /**
399
403
  * Retrieves the data associated with the result.
400
404
  *
401
405
  * @returns The data stored in the result, if any.
402
406
  */
403
- getData: () => any;
407
+ getData: () => T | null;
404
408
  /**
405
409
  * Adds an error message or Error object to the result.
406
- *
407
410
  * @param error The error message or Error object to be added.
408
- * @returns The current Result object for chaining methods.
411
+ * @param key Error key. You can leave it blank. Then it will be generated automatically.
412
+ * @returns {IResult} The current Result object for chaining methods.
409
413
  */
410
- addError: (error: Error | string) => IResult;
414
+ addError: (error: Error | string, key?: string) => IResult;
411
415
  /**
412
416
  * Adds multiple errors to the result in a single call.
413
417
  *
414
418
  * @param errors An array of errors or strings that will be converted to errors.
415
- * @returns The current Result object for chaining methods.
419
+ * @returns {IResult} The current Result object for chaining methods.
416
420
  */
417
421
  addErrors: (errors: (Error | string)[]) => IResult;
418
422
  /**
419
423
  * Retrieves an iterator for the errors collected in the result.
420
424
  *
421
- * @returns An iterator over the stored Error objects.
425
+ * @returns {IterableIterator<Error>} An iterator over the stored Error objects.
422
426
  */
423
427
  getErrors: () => IterableIterator<Error>;
424
428
  /**
425
429
  * Retrieves an array of error messages from the collected errors.
426
430
  *
427
- * @returns An array of strings representing the error messages.
431
+ * @returns {string[]} An array of strings representing the error messages.
428
432
  */
429
433
  getErrorMessages: () => string[];
434
+ /**
435
+ * Checks for an error in a collection by key
436
+ * @param key - Error key
437
+ */
438
+ hasError(key: string): boolean;
430
439
  /**
431
440
  * Converts the Result object to a string.
432
441
  *
@@ -439,49 +448,18 @@ interface IResult {
439
448
  * Similar to \Bitrix\Main\Result from Bitrix Framework.
440
449
  * @link https://dev.1c-bitrix.ru/api_d7/bitrix/main/result/index.php
441
450
  */
442
- declare class Result implements IResult {
443
- private _errorCollection;
444
- protected _data: any;
445
- constructor();
446
- /**
447
- * Getter for the `isSuccess` property.
448
- * Checks if the `_errorCollection` is empty to determine success.
449
- *
450
- * @returns Whether the operation resulted in success (no errors).
451
- */
451
+ declare class Result<T = any> implements IResult<T> {
452
+ protected _errors: Map<string, Error>;
453
+ protected _data: T | null;
454
+ constructor(data?: T);
452
455
  get isSuccess(): boolean;
453
- /**
454
- * Sets the data associated with the result.
455
- *
456
- * @param data The data to be stored in the result.
457
- * @returns The current Result object for chaining methods.
458
- */
459
- setData(data: any): Result;
460
- /**
461
- * Retrieves the data associated with the result.
462
- *
463
- * @returns The data stored in the result, if any.
464
- */
465
- getData(): any;
466
- /**
467
- * Adds an error message or Error object to the result.
468
- *
469
- * @param error The error message or Error object to be added.
470
- * @returns The current Result object for chaining methods.
471
- */
472
- addError(error: Error | string): Result;
473
- /**
474
- * Adds multiple errors to the result in a single call.
475
- *
476
- * @param errors An array of errors or strings that will be converted to errors.
477
- * @returns The current Result object for chaining methods.
478
- */
479
- addErrors(errors: (Error | string)[]): Result;
480
- /**
481
- * Retrieves an iterator for the errors collected in the result.
482
- * @returns An iterator over the stored Error objects.
483
- */
456
+ get errors(): Map<string, Error>;
457
+ setData(data: T | null): Result<T>;
458
+ getData(): T | null;
459
+ addError(error: Error | string, key?: string): Result<T>;
460
+ addErrors(errors: (Error | string)[]): Result<T>;
484
461
  getErrors(): IterableIterator<Error>;
462
+ hasError(key: string): boolean;
485
463
  /**
486
464
  * Retrieves an array of error messages from the collected errors.
487
465
  *
@@ -495,6 +473,10 @@ declare class Result implements IResult {
495
473
  * @returns {string} Returns a string representation of the result operation
496
474
  */
497
475
  toString(): string;
476
+ private safeStringify;
477
+ private replacer;
478
+ static ok<U>(data?: U): Result<U>;
479
+ static fail<U>(error: Error | string, key?: string): Result<U>;
498
480
  }
499
481
 
500
482
  type PayloadTime = {
@@ -538,36 +520,52 @@ type BatchPayload<C> = {
538
520
  };
539
521
  type Payload<P> = GetPayload<P> | ListPayload<P> | BatchPayload<P>;
540
522
 
541
- type AjaxQuery = {
523
+ type AjaxQuery = Readonly<{
542
524
  method: string;
543
- params: object;
525
+ params: Readonly<object>;
544
526
  start: number;
545
- };
546
- type AjaxResultParams = {
527
+ }>;
528
+ type AjaxResultParams<T = unknown> = Readonly<{
547
529
  error?: string | {
548
530
  error: string;
549
- error_description: string;
531
+ error_description?: string;
550
532
  };
551
533
  error_description?: string;
552
- result: any;
534
+ result: T;
553
535
  next?: NumberString;
554
536
  total?: NumberString;
555
- };
537
+ time: PayloadTime;
538
+ }>;
539
+ type AjaxResultOptions<T> = Readonly<{
540
+ answer: AjaxResultParams<T>;
541
+ query: AjaxQuery;
542
+ status: number;
543
+ }>;
556
544
  /**
557
545
  * Result of request to Rest Api
558
546
  */
559
- declare class AjaxResult extends Result implements IResult {
547
+ declare class AjaxResult<T = unknown> extends Result<Payload<T>> implements IResult<Payload<T>> {
548
+ #private;
560
549
  private readonly _status;
561
550
  private readonly _query;
562
- protected _data: AjaxResultParams;
563
- constructor(answer: AjaxResultParams, query: AjaxQuery, status: number);
564
- setData(data: any): Result;
565
- getData(): Payload<unknown>;
551
+ protected _data: AjaxResultParams<T>;
552
+ constructor(options: AjaxResultOptions<T>);
553
+ getData(): Payload<T>;
554
+ /**
555
+ * Alias for isMore
556
+ */
557
+ hasMore(): boolean;
566
558
  isMore(): boolean;
567
559
  getTotal(): number;
568
560
  getStatus(): number;
569
- getQuery(): AjaxQuery;
570
- getNext(http: TypeHttp): Promise<false | AjaxResult>;
561
+ getQuery(): Readonly<AjaxQuery>;
562
+ /**
563
+ * Alias for getNext
564
+ * @param http
565
+ */
566
+ fetchNext(http: TypeHttp): Promise<AjaxResult<T> | null>;
567
+ getNext(http: TypeHttp): Promise<AjaxResult<T> | false>;
568
+ setData(): never;
571
569
  }
572
570
 
573
571
  type TypeHttp = {
@@ -1360,18 +1358,81 @@ type AjaxErrorParams = {
1360
1358
  answerError: AnswerError;
1361
1359
  cause?: Error;
1362
1360
  };
1361
+ type ErrorDetails = {
1362
+ code: string;
1363
+ description?: string;
1364
+ status: number;
1365
+ requestInfo?: {
1366
+ method?: string;
1367
+ url?: string;
1368
+ params?: Record<string, unknown> | unknown;
1369
+ };
1370
+ originalError?: unknown;
1371
+ };
1363
1372
  /**
1364
1373
  * Error requesting RestApi
1365
1374
  */
1366
1375
  declare class AjaxError extends Error {
1367
- cause: null | Error;
1376
+ readonly code: string;
1368
1377
  private _status;
1369
- private _answerError;
1370
- constructor(params: AjaxErrorParams);
1378
+ readonly requestInfo?: ErrorDetails['requestInfo'];
1379
+ readonly timestamp: Date;
1380
+ readonly originalError?: unknown;
1381
+ constructor(details: ErrorDetails);
1382
+ /**
1383
+ * @deprecated
1384
+ */
1371
1385
  get answerError(): AnswerError;
1372
1386
  get status(): number;
1387
+ /**
1388
+ * @deprecated
1389
+ */
1373
1390
  set status(status: number);
1391
+ /**
1392
+ * Creates AjaxError from HTTP response
1393
+ */
1394
+ static fromResponse(response: {
1395
+ status: number;
1396
+ data?: {
1397
+ error?: string;
1398
+ error_description?: string;
1399
+ };
1400
+ config?: {
1401
+ method?: string;
1402
+ url?: string;
1403
+ params?: unknown;
1404
+ };
1405
+ }): AjaxError;
1406
+ /**
1407
+ * Creates AjaxError from exception
1408
+ */
1409
+ static fromException(error: unknown, context?: {
1410
+ code?: string;
1411
+ status?: number;
1412
+ requestInfo?: ErrorDetails['requestInfo'];
1413
+ }): AjaxError;
1414
+ /**
1415
+ * Serializes error for logging and debugging
1416
+ */
1417
+ toJSON(): {
1418
+ name: string;
1419
+ code: string;
1420
+ message: string;
1421
+ status: number;
1422
+ timestamp: string;
1423
+ requestInfo: {
1424
+ method?: string;
1425
+ url?: string;
1426
+ params?: Record<string, unknown> | unknown;
1427
+ } | undefined;
1428
+ stack: string | undefined;
1429
+ };
1430
+ /**
1431
+ * Formats error information for human-readable output
1432
+ */
1374
1433
  toString(): string;
1434
+ private static formatErrorMessage;
1435
+ private cleanErrorStack;
1375
1436
  }
1376
1437
 
1377
1438
  declare abstract class AbstractB24 implements TypeB24 {