Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@cuser/client

rubeniskov53ISC0.0.36TypeScript support: included

client logic for reading and publishing message using restfull transport

readme

cuser logo

@cuser/client

Status

codecov npm npm-downloads

Class: CuserClient

Cuser client instance which provides an interface to read, publish, edit and delete messages through ipfs. By default the client will be only allowed to read messages unless you provide a CuserServer url, which will enable the publishing capabilities to the users.

Note To enable publisher capabilities, you need deploy a CuserServer, please refer to getting started section to gets more information.

example

const { create } = require('ipfs');

const node = create({ ...ipfsOptions });
const cuserId = 'CUSER_SERVER_IDENTIFIER';
const client = new CuserClient(node, cuserId);
const topicId = 'custom-topic-id';

client.getMessages(topicId).then((messages) => {
 console.log(messages);
 // should return empty array when no comments
});

Hierarchy

Index

Constructors

Properties

Methods

Constructors

constructor

+ new CuserClient(node: IPFSAPI | Promise<IPFSAPI>, cuserId: string, opts?: CuserClientOptions & CuserReaderOptions & CuserCoreOptions): CuserClient

Overrides void

Defined in client/client.js:53

Parameters:

Name Type Default value
node IPFSAPI | Promise<IPFSAPI> -
cuserId string -
opts CuserClientOptions & CuserReaderOptions & CuserCoreOptions {}

Returns: CuserClient

Properties

_core

_core: CuserCore

Inherited from CuserClient._core

Defined in reader/reader.d.ts:44


_process

_process: (message: any, cursor: string) => Promise<CuserReaderMessageIteratorResult>

Inherited from CuserClient._process

Defined in reader/reader.d.ts:48

Methods

authenticate

authenticate(username: string, avatar: string): Promise<any>

Defined in client/client.js:80

Authenticates a user with the required fields of username and avatar, this will epect to recieve an access_token to be used in publishing operations

Parameters:

Name Type Description
username string |
avatar string data url scheme https://tools.ietf.org/html/rfc2397

Returns: Promise<any>


deleteMessage

deleteMessage(topicId: string, accessToken: string, messageId: string): Promise<[any, Response]>

Defined in client/client.js:142

Deletes message for certain topic using topicId as identifier and accessToken to identify the user

Parameters:

Name Type
topicId string
accessToken string
messageId string

Returns: Promise<[any, Response]>


getMessage

getMessage(cid: string): Promise<GraphMessage>

Inherited from CuserClient.getMessage

Defined in reader/reader.d.ts:76

Gets the message from ipfs using the CID given by parameter

Parameters:

Name Type
cid string

Returns: Promise<GraphMessage>


getMessages

getMessages(topicId: string, opts: CuserReaderMessagesIteratorOptions): Promise<CuserReaderMessageIteratorResult[]> | AsyncIterable<CuserReaderMessageIteratorResult>

Inherited from CuserClient.getMessages

Defined in reader/reader.d.ts:70

Gets messages from ipfs layer

example

Array

const messages = reader.getMessages('custom_topic_id');
console.log(messages);

Iterator

const messages = reader.getMessages('custom_topic_id', {
  iterator: true,
});
for await (let value of messages) {
  console.log(value);
}

Parameters:

Name Type
topicId string
opts CuserReaderMessagesIteratorOptions

Returns: Promise<CuserReaderMessageIteratorResult[]> | AsyncIterable<CuserReaderMessageIteratorResult>


publishMessage

publishMessage(topicId: string, accessToken: string, content: string): Promise<[any, Response]>

Defined in client/client.js:97

Publish a new message for certain topic using topicId as identifier and accessToken to identify the user

Parameters:

Name Type
topicId string
accessToken string
content string

Returns: Promise<[any, Response]>


subscribe

subscribe(topicId: string, subscriber: CuserClientSubscriber): function

Defined in client/client.js:184

Subscribe to message changes of a certain topic.

example This will attach the listener to three types of events:

  • created when a user publish a message
  • updated when a user updates a message
  • deleted when a user removes a message
    client.subscribe('CUSTOM_TOPIC_ID', ({ type, messageCid }) => {
    switch(type) {
     case 'created':
       // when a user publish a message
       // console.log(client.getMessage(messageCid));
       break;
     case 'updated':
       // when a user updates a message
       // console.log(client.getMessage(messageCid));
       break;
     case 'deleted':
       // when a user removes a message
       // console.log(client.getMessage(messageCid));
       break;
    }
    });

Parameters:

Name Type Description
topicId string topic identifier
subscriber CuserClientSubscriber function event subscriber

Returns: function


updateMessage

updateMessage(topicId: string, accessToken: string, messageId: string, content: string): Promise<[any, Response]>

Defined in client/client.js:120

Updates message for certain topic using topicId as identifier and accessToken to identify the user

Parameters:

Name Type
topicId string
accessToken string
messageId string
content string

Returns: Promise<[any, Response]>