Skip to content

Commit

Permalink
docs(js-lib): readme update (#74)
Browse files Browse the repository at this point in the history
* chore: update library readme

* chore: add fdp-storage reference to the readme
  • Loading branch information
tomicvladan authored Sep 1, 2022
1 parent 3e4de5e commit 423c23d
Showing 1 changed file with 60 additions and 27 deletions.
87 changes: 60 additions & 27 deletions library/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,85 @@ npm install --save @fairdatasociety/blossom

## Usage

### Blossom class

All interaction with the Blossom browser extension is established through the Blossom class:

```typescript
import { Blossom } from '@fairdatasociety/blossom'
```

An instance of the class can be configured with an address of an RPC server. By default it will use local
`fdp-contract` image listenning on port 9545. You can run such image using `docker`:
By default the class will connect to the Blossom browser extension using its ID from the Google store. If you
are running your version of the extension the class can be configured with a different extension ID.

```bash
docker run -p 9545:9545 fairdatasociety/swarm-test-blockchain:1.2.0
```typescript
const blossom = new Blossom() // Using the default Blossom ID from the Google store
```

```typescript
const blossom = new Blossom('Blossom Extension ID...') // Using custom Blossom ID
```

> **_NOTE_:** For more information regarding the `swarm-test-blockchain` image check this repo
> [fdp-contracts](https://github.com/fairDataSociety/fdp-contracts)
Each dApp should be executed from a BZZ link (e.g. http://127.0.0.1:1633/bzz/dApp-ens-name/). In that case
dApp's ID is available as `blossom.dappId` property.

You can get an enum of all supported RPC addresses:
To test if connection with the Blossom extension is established, call the `echo` method:

```typescript
import { Networks } from '@fairdatasociety/blossom'
const text = await blossom.echo<string>('test')
console.log(text) // 'test'
```

Th second optional parameter is the Blossom extension ID. By default it will use the extension ID of the
extension in the Chrome store. But you can provide different one if you are running your own version of the
extension.
### FDP Storage

There might be multiple instances of the class, but when an instance is not needed anymore, the connection of
that instance should be closed by calling the `closeConnection` method.
If the user is logged in, dApp can access its own pod. Each dApp can have only one pod and its name must be
the same as the `blossom.dappId` property.

Here is an example how to register a new user using the library:
To check if dApp's pod is already created:

```typescript
import { Blossom } from '@fairdatasociety/blossom'
const podIsCreated = await blossom.fdpStorage.personalStorage.isDappPodCreated()
```

If not created, then it can be created calling:

```typescript
const pod = await blossom.fdpStorage.personalStorage.create(blossom.dappId)
```

Afterwards, dApp can execute various operations on that pod, like creating, reading files and directories,
etc.

For example, to create a directory:

```typescript
const directory = await blossom.fdpStorage.directory.create(blossom.dappId, '/example')
```

Then, to upload a file there:

```typescript
const file = await blossom.fdpStorage.file.uploadData(blossom.dappId, '/example/new-file.txt', 'File content')
```

async function registerExample() {
const blossom = new Blossom()
await blossom.register('new_user', 'pass123')
}
And to download the same file:

registerExample()
```typescript
const content = await blossom.fdpStorage.file.downloadData(blossom.dappId, '/example/new-file.txt')
console.log(content.text()) // 'File content'
```

> **_NOTE_:** For more available methods, check the
> [fdp-storage repo](https://github.com/fairDataSociety/fdp-storage#usage)
### Terminating connection

Once when the instance of the Blossom class is not needed anymore, connection with the extension should be
terminated to avoid memory leaking.

```typescript
blossom.closeConnection()
```

## Development

Expand All @@ -78,14 +115,10 @@ Blossom extension. Because of that, tests need web pages that are going to be in
pages are located inside the `test/webpages` directory. Each page contains elements that can be interacted
with to trigger various events, and placeholder elements that are used to show results of various operations.

Before running tests, the complete environment must be started. That means, an instance of the `fdp-contracts`
image should be running locally:

```bash
docker run -p 9545:9545 fairdatasociety/swarm-test-blockchain:1.2.0
```
Before running tests, the complete environment must be started. To start the environment and the extension
check the [extension's readme](../README.md#setting-up-the-environment).

Also a web server that will serve the test web pages:
To run a web server that will serve the test web pages, execute:

```bash
npm run serve
Expand Down

0 comments on commit 423c23d

Please sign in to comment.