Skip to content
This repository was archived by the owner on Mar 10, 2025. It is now read-only.

Latest commit

 

History

History
266 lines (139 loc) · 7.18 KB

File metadata and controls

266 lines (139 loc) · 7.18 KB

@ceramic-sdk/model-client v0.2.1Docs


Ceramic SDK / @ceramic-sdk/model-client / ModelClient

Class: ModelClient

Represents a client for interacting with Ceramic models.

The ModelClient class extends the StreamClient class to provide additional methods specific to working with Ceramic models, including fetching and creating model definitions, retrieving initialization events, and decoding stream data.

Extends

Constructors

new ModelClient()

new ModelClient(params): ModelClient

Creates a new instance of StreamClient.

Parameters

params: StreamClientParams

Configuration object containing parameters for initializing the StreamClient.

Returns

ModelClient

Inherited from

StreamClient.constructor

Accessors

ceramic

get ceramic(): CeramicClient

Retrieves the Ceramic HTTP client instance used to interact with the Ceramic server.

This client is essential for executing API requests to fetch stream data.

Returns

CeramicClient

The CeramicClient instance associated with this StreamClient.

Inherited from

StreamClient.ceramic

Defined in

Methods

createDefinition()

createDefinition(definition, signer?): Promise<StreamID>

Creates a model definition and returns the resulting stream ID.

Parameters

definition: MapIn<RequiredProps<object>, $TypeOf> & MapIn<OptionalProps<object>, $TypeOf> | MapIn<RequiredProps<object>, $TypeOf> & MapIn<OptionalProps<object>, $TypeOf>

The model JSON definition to post.

signer?: DID

(Optional) A DID instance for signing the model definition.

Returns

Promise<StreamID>

A promise that resolves to the StreamID of the posted model.

Throws

Will throw an error if the definition is invalid or the signing process fails.


getDID()

getDID(provided?): DID

Retrieves a Decentralized Identifier (DID).

This method provides access to a DID, which is required for authentication or event signing operations. The caller can optionally provide a DID; if none is provided, the instance's DID is used instead.

Parameters

provided?: DID

An optional DID object to use. If not supplied, the instance's DID is returned.

Returns

DID

The DID object, either provided or attached to the instance.

Throws

Will throw an error if neither a provided DID nor an instance DID is available.

Example

const did = client.getDID();
console.log(did.id); // Outputs the DID identifier

Inherited from

StreamClient.getDID


getDocumentModel()

getDocumentModel(streamID): Promise<string>

Retrieves the stringified model stream ID from a model instance document stream ID.

Parameters

streamID: string | StreamID

The document stream ID, either as a StreamID object or string.

Returns

Promise<string>

A promise that resolves to the stringified model stream ID.

Throws

Will throw an error if the stream ID or its state is invalid.


getInitEvent()

getInitEvent(streamID): Promise<MapIn<RequiredProps<object>, $TypeOf> & MapIn<OptionalProps<object>, $TypeOf>>

Retrieves the signed initialization event of a model based on its stream ID.

Parameters

streamID: string | StreamID

The stream ID of the model, either as a StreamID object or string.

Returns

Promise<MapIn<RequiredProps<object>, $TypeOf> & MapIn<OptionalProps<object>, $TypeOf>>

A promise that resolves to the SignedEvent for the model.

Throws

Will throw an error if the stream ID is invalid or the request fails.


getModelDefinition()

getModelDefinition(streamID): Promise<MapIn<RequiredProps<object>, $TypeOf> & MapIn<OptionalProps<object>, $TypeOf> | MapIn<RequiredProps<object>, $TypeOf> & MapIn<OptionalProps<object>, $TypeOf>>

Retrieves a model's JSON definition based on the model's stream ID.

Parameters

streamID: string | StreamID

The stream ID of the model, either as a StreamID object or string.

Returns

Promise<MapIn<RequiredProps<object>, $TypeOf> & MapIn<OptionalProps<object>, $TypeOf> | MapIn<RequiredProps<object>, $TypeOf> & MapIn<OptionalProps<object>, $TypeOf>>

A promise that resolves to the ModelDefinition for the specified model.

Throws

Will throw an error if the stream ID is invalid or the data cannot be decoded.


getPayload()

getPayload(streamID, verifier?): Promise<MapIn<object, $TypeOf>>

Retrieves the payload of the initialization event for a model based on its stream ID.

Parameters

streamID: string | StreamID

The stream ID of the model, either as a StreamID object or string.

verifier?: DID

(Optional) A DID instance for verifying the event payload.

Returns

Promise<MapIn<object, $TypeOf>>

A promise that resolves to the ModelInitEventPayload.

Throws

Will throw an error if the event or payload is invalid or verification fails.


getStreamState()

getStreamState(streamId): Promise<StreamState>

Fetches the current stream state of a specific stream by its ID.

This method interacts with the Ceramic HTTP API to retrieve the state of a stream. The stream ID can either be a multibase-encoded string or a StreamID object.

Parameters

streamId: string | StreamID

The unique identifier of the stream to fetch.

  • Can be a multibase-encoded string or a StreamID object.

Returns

Promise<StreamState>

A Promise that resolves to the StreamState object, representing the current state of the stream.

Throws

Will throw an error if the API request fails or returns an error response (if stream is not found).

Example

const streamState = await client.getStreamState('kjzl6cwe1...');
console.log(streamState);

Inherited from

StreamClient.getStreamState