jmap-kit
    Preparing search index...

    JMAP Client for interacting with a JMAP server.

    This class manages the connection lifecycle, session state, and provides methods for interacting with the JMAP API, including capability checks, file downloads, and invocation requests and responses.

    Implements

    Index

    Constructors

    • Create a new JMAPClient instance.

      The provided Transport is responsible for all HTTP request concerns, including authentication, network errors, and any custom headers or request logic required by the server. The client itself does not handle authentication or low-level HTTP details.

      If the port is not specified, it defaults to 443.

      Parameters

      • transport: Transport

        The transport implementation for HTTP requests, including authentication.

      • options: JMAPClientOptions

        JMAP client options including hostname, port, headers, logger, and emitter.

      Returns JMAPClient

    Accessors

    Methods

    • Connect to the JMAP server and fetch the session object.

      This method is idempotent: if called multiple times while a connection is already in progress, each call will return a new Promise that resolves or rejects with the same result as the in-progress connection. Only one connection attempt will be made at a time; concurrent calls will not trigger multiple connections.

      Parameters

      • Optionalsignal: AbortSignal

      Returns Promise<void>

      Error if hostname is not set or connection fails.

    • Disconnect from the JMAP server and clear session state.

      This method is asynchronous and will not set the status to 'disconnected' until all in-flight requests have settled (resolved or rejected) after being aborted. It is idempotent: if already disconnecting, it returns the same promise.

      Returns Promise<void>

    • Download a file from the server.

      Parameters

      • accountId: Id

        The id of the account to which the record with the blobId belongs.

      • blobId: Id

        The blobId representing the data of the file to download.

      • name: string

        The name for the file; the server MUST return this as the filename if it sets a Content-Disposition header.

      • type: string

        The type for the server to set in the Content-Type header of the response; the blobId only represents the binary data and does not have a content-type innately associated with it.

      • Optionalsignal: AbortSignal

        Optional AbortSignal to cancel the request.

      Returns Promise<Blob>

      A promise that resolves to a Blob containing the data of the file.

      Error if the client is not connected, downloadUrl is missing from session, accountId is not listed in the session's accounts.

      JMAPRequestError for JMAP protocol errors (non-200 status codes with RFC 7807 Problem Details).

      TypeError If parsing the response fails.

      If a network error, timeout, or other transport failure occurs.

    • Get the parsed download URL for a file.

      Parameters

      • accountId: Id

        The id of the account to which the record with the blobId belongs.

      • blobId: Id

        The blobId representing the data of the file to download.

      • name: string

        The name for the file; the server MUST return this as the filename if it sets a Content-Disposition header.

      • type: string

        The type for the server to set in the Content-Type header of the response; the blobId only represents the binary data and does not have a content-type innately associated with it.

      Returns URL

      the download URL with variables expanded

      Error if the Client is not connected to a JMAP server

      Error if downloadUrl is missing from session

      Error if accountId is not listed in the session's accounts

    • Get the parsed event source URL.

      Parameters

      • types: string[] | "*"

        Either an array of event types to listen for, or "*" to listen for all types.

      • closeafter: "state" | "no"

        Either "state" (end response after pushing a state event) or "no" (persist connection).

      • ping: number

        Positive integer value in seconds. If non-zero, the server will send a ping event after this time elapses since the previous event.

      Returns URL

      the event source URL with variables expanded

      Error if the Client is not connected to a JMAP server

      Error if eventSourceUrl is missing from session

      Error if ping is negative

    • Get the parsed upload URL for a file.

      Parameters

      • accountId: Id

        The id of the account to upload the file to.

      Returns URL

      the upload URL with variables expanded

      Error if the Client is not connected to a JMAP server

      Error if uploadUrl is missing from session

      Error if accountId is not listed in the session's accounts

    • Register one or more capabilities with the client.

      When called after the client is connected, each capability's schema is validated against the session data. Capabilities that fail validation are rejected and not registered. When called before connecting, capabilities are registered without validation.

      If called while the client is in the process of connecting, registration waits for the connection to complete before validating. If the connection fails, capabilities are registered without validation (no session to validate against).

      Parameters

      Returns Promise<CapabilityRegistrationResult>

      A result object containing arrays of validation failures (empty arrays if all succeeded)

    • Send an API request to the JMAP server.

      Parameters

      • jmapRequest: RequestBuilder

        The request builder instance.

      • Optionalsignal: AbortSignal

        Optional AbortSignal to cancel the request.

      Returns Promise<
          {
              createdIds: IdMap;
              methodResponses: InvocationList<BaseInvocationArgs>;
              sessionState: string;
          },
      >

      The parsed response from the server.

      Error if the client is not connected, disconnecting, or failed to connect.

      JMAPRequestError for JMAP protocol errors (non-200 status codes with RFC 7807 Problem Details).

      TypeError If parsing the response fails.

      If a network error, timeout, or other transport failure occurs.

    • Upload a file to the server.

      Parameters

      • accountId: Id

        The id of the account to upload to.

      • file: Blob | ArrayBuffer | File

        The file data to upload. Can be a Blob, ArrayBuffer, or File.

      • Optionalsignal: AbortSignal

        Optional AbortSignal to cancel the request.

      Returns Promise<JMAPUploadResponse>

      A promise that resolves to the upload response from the server.

      Error if the file size exceeds the maxSizeUpload limit from server capabilities.

      Error if the client is not connected, uploadUrl is missing from session, or accountId is not listed in the session's accounts.

      JMAPRequestError for JMAP protocol errors (non-200 status codes with RFC 7807 Problem Details).

      TypeError If parsing the response fails.

      If a network error, timeout, or other transport failure occurs.