# @cuser/client

> client logic for reading and publishing message using restfull transport

Latest version **0.0.36** (published 2020-12-24) · ISC license · 0 weekly downloads

## Install

```sh
npm install @cuser/client
pnpm add @cuser/client
yarn add @cuser/client
bun add @cuser/client
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.36 |
| Published | 2020-12-24 |
| First published | 2020-12-02 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 9 |
| Unpacked size | 26.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | rubeniskov |

## Links

- npm: https://www.npmjs.com/package/@cuser/client
- npm.io page: https://npm.io/package/@cuser/client

## Dependencies (9)

- [cbor](https://npm.io/package/cbor.md) ^5.1.0
- [multiaddr](https://npm.io/package/multiaddr.md) ^8.1.1
- [node-fetch](https://npm.io/package/node-fetch.md) ^2.6.1
- [@cuser/auth](https://npm.io/package/@cuser/auth.md) ^0.0.36
- [@cuser/core](https://npm.io/package/@cuser/core.md) ^0.0.36
- [@cuser/reader](https://npm.io/package/@cuser/reader.md) ^0.0.36
- [blob-polyfill](https://npm.io/package/blob-polyfill.md) ^4.0.20200601
- [ipfs-pubsub-room](https://npm.io/package/ipfs-pubsub-room.md) ^2.0.1
- [async-iterator-to-array](https://npm.io/package/async-iterator-to-array.md) ^0.0.1

## Recent versions

- 0.0.36 (latest) — 2020-12-24
- 0.0.35 — 2020-12-23
- 0.0.34 — 2020-12-23
- 0.0.33 — 2020-12-23
- 0.0.32 — 2020-12-23
- 0.0.31 — 2020-12-23
- 0.0.30 — 2020-12-23
- 0.0.29 — 2020-12-23
- 0.0.27 — 2020-12-22
- 0.0.26 — 2020-12-22
- 0.0.25 — 2020-12-22
- 0.0.18 — 2020-12-22
- 0.0.17 — 2020-12-22
- 0.0.16 — 2020-12-22
- 0.0.12 — 2020-12-22
- … 3 more at https://npm.io/package/@cuser/client/versions

## README

<p align="center">
  <a href="./"><img width="250" src="https://raw.githubusercontent.com/rubeniskov/cuser/master/docs/logo.svg" alt="cuser logo" /></a>
</p>

# @cuser/client

## Status
[![codecov](https://codecov.io/gh/rubeniskov/cuser/branch/master/graph/badge.svg?flag=client)](https://codecov.io/gh/rubeniskov/cuser)
[![npm](https://img.shields.io/npm/v/@cuser/client.svg)](https://www.npmjs.com/package/@cuser/client)
[![npm-downloads](https://img.shields.io/npm/dw/@cuser/client)](https://www.npmjs.com/package/@cuser/client)


# 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](https://github.com/rubeniskov/cuser#getting-started) section to gets more information.

**`example`** 

```javascript
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

* [CuserReader](../globals.md#cuserreader)

  ↳ **CuserClient**

## Index

### Constructors

* [constructor](cuserclient.md#constructor)

### Properties

* [\_core](cuserclient.md#_core)
* [\_process](cuserclient.md#_process)

### Methods

* [authenticate](cuserclient.md#authenticate)
* [deleteMessage](cuserclient.md#deletemessage)
* [getMessage](cuserclient.md#getmessage)
* [getMessages](cuserclient.md#getmessages)
* [publishMessage](cuserclient.md#publishmessage)
* [subscribe](cuserclient.md#subscribe)
* [updateMessage](cuserclient.md#updatemessage)

## Constructors

### constructor

\+ **new CuserClient**(`node`: IPFSAPI \| Promise<IPFSAPI\>, `cuserId`: string, `opts?`: CuserClientOptions & CuserReaderOptions & CuserCoreOptions): [CuserClient](cuserclient.md)

*Overrides void*

*Defined in [client/client.js:53](https://github.com/rubeniskov/cuser/blob/dceceb4/packages/client/client.js#L53)*

#### Parameters:

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

**Returns:** [CuserClient](cuserclient.md)

## Properties

### \_core

•  **\_core**: CuserCore

*Inherited from [CuserClient](cuserclient.md).[_core](cuserclient.md#_core)*

*Defined in [reader/reader.d.ts:44](https://github.com/rubeniskov/cuser/blob/dceceb4/packages/reader/reader.d.ts#L44)*

___

### \_process

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

*Inherited from [CuserClient](cuserclient.md).[_process](cuserclient.md#_process)*

*Defined in [reader/reader.d.ts:48](https://github.com/rubeniskov/cuser/blob/dceceb4/packages/reader/reader.d.ts#L48)*

## Methods

### authenticate

▸ **authenticate**(`username`: string, `avatar`: string): Promise<any\>

*Defined in [client/client.js:80](https://github.com/rubeniskov/cuser/blob/dceceb4/packages/client/client.js#L80)*

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](https://github.com/rubeniskov/cuser/blob/dceceb4/packages/client/client.js#L142)*

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](cuserclient.md).[getMessage](cuserclient.md#getmessage)*

*Defined in [reader/reader.d.ts:76](https://github.com/rubeniskov/cuser/blob/dceceb4/packages/reader/reader.d.ts#L76)*

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](cuserclient.md).[getMessages](cuserclient.md#getmessages)*

*Defined in [reader/reader.d.ts:70](https://github.com/rubeniskov/cuser/blob/dceceb4/packages/reader/reader.d.ts#L70)*

Gets messages from `ipfs` layer

**`example`** 
### Array
```javascript
const messages = reader.getMessages('custom_topic_id');
console.log(messages);
```
### Iterator
```javascript
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](https://github.com/rubeniskov/cuser/blob/dceceb4/packages/client/client.js#L97)*

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](https://github.com/rubeniskov/cuser/blob/dceceb4/packages/client/client.js#L184)*

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
```javascript
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](https://github.com/rubeniskov/cuser/blob/dceceb4/packages/client/client.js#L120)*

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]\>

---
_Source: https://npm.io/package/@cuser/client · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
