@blockchaincommons/envelope - v1.0.0-beta.4
    Preparing search index...

    Class Response

    A Response represents a reply to a Request containing either a successful result or an error.

    import { Response, ARID } from '@blockchaincommons/envelope';

    // Create a request ID (normally this would come from the original request)
    const requestId = ARID.new();

    // Create a successful response
    const successResponse = Response.success(requestId)
    .withResult("Transaction completed");

    // Create an error response
    const errorResponse = Response.failure(requestId)
    .withError("Insufficient funds");

    // Convert to envelopes
    const successEnvelope = successResponse.toEnvelope();
    const errorEnvelope = errorResponse.toEnvelope();

    Implements

    Index
    • get id(): ARID | undefined

      Returns the ID of the request this response corresponds to, if known.

      Returns ARID | undefined

    • Creates a new successful response with the specified request ID.

      By default, the result will be the 'OK' known value. Use withResult to set a specific result value.

      Parameters

      • id: ARID

      Returns Response

    • Creates a new failure response with the specified request ID.

      By default, the error will be the 'Unknown' known value. Use withError to set a specific error message.

      Parameters

      • id: ARID

      Returns Response

    • Creates a new early failure response without a request ID.

      An early failure occurs when the error happens before the request has been fully processed, so the request ID is not known.

      Returns Response

    • The id; General (Expected an ID, the reference's panic text) when the response has none.

      Returns ARID

      EnvelopeError with code General.

    • Converts the response to an envelope.

      Successful responses have the request ID as the subject and a 'result' assertion. Failure responses have the request ID (or 'Unknown' if not known) as the subject and an 'error' assertion.

      Returns Envelope

    • Reads a response from an envelope (the reference's Response::try_from(envelope)): exactly one of 'result' and 'error' must be present, then the subject is TAG_RESPONSE(ARID), or TAG_RESPONSE('Unknown') for a failure without an id.

      Parameters

      Returns Response

      EnvelopeError InvalidResponse when neither or both of 'result' and 'error' are present (or either is present more than once), or a failure's known-value id is not 'Unknown'; NotLeaf / Cbor (dcbor error: <Display>) when the subject is not the tagged id

    • Checks equality with another response: the same outcome, the same id (or none on both) and an equal result or error envelope (the reference's derived PartialEq).

      Parameters

      Returns boolean