v2.0.0

IResult

Generic operation-result interface. Implemented by Result<T> and (transitively) by AjaxResult<T>.

IResult<T> captures the success-flag-plus-data-plus-error-bag pattern used throughout the SDK. Concrete classes:

Shape

interface IResult<T = any> {
  readonly isSuccess: boolean
  readonly errors: Map<string, Error>

  setData(data: T | null | undefined): IResult<T>
  getData(): T | null | undefined

  addError(error: Error | string, key?: string): IResult
  addErrors(errors: (Error | string)[]): IResult
  getErrors(): IterableIterator<Error>
  getErrorMessages(): string[]
  getErrorsByKey(): Record<string, Error>
  getErrorMessagesByKey(): Record<string, string>
  hasError(key: string): boolean

  toString(): string
}

AjaxResult<T> narrows setData to never (immutable response), and overrides getData() to return SuccessPayload<T> when isSuccess.

Typical Usage Pattern

import type { IResult } from '@bitrix24/b24jssdk'

function consume<T>(result: IResult<T>) {
  if (!result.isSuccess) {
    for (const message of result.getErrorMessages()) {
      console.error(message)
    }
    return
  }
  const data = result.getData()
  // ...
}