Skip to content

Commit 2e88ff3

Browse files
AuHauagazso
andauthored
feat: data helpers usage (#46)
* feat: data helpers usage * chore: change wording Co-authored-by: Attila Gazso <[email protected]>
1 parent dc43c12 commit 2e88ff3

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

docs/user-documentation/pss.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ As a recipient, you have two ways how to receive the message. If you are expecti
124124
```ts
125125
const message = await bee.pssReceive('topic')
126126

127-
console.log(new TextDecoder("utf-8").decode(message)) // prints the received message
127+
console.log(message.text()) // prints the received message
128128
```
129129

130130
</TabItem>
@@ -133,7 +133,7 @@ console.log(new TextDecoder("utf-8").decode(message)) // prints the received mes
133133
```js
134134
const message = await bee.pssReceive('topic')
135135

136-
console.log(new TextDecoder("utf-8").decode(message)) // prints the received message
136+
console.log(message.text()) // prints the received message
137137
```
138138

139139
</TabItem>
@@ -153,7 +153,7 @@ If you want to subscribe to multiple messagees, use the `pssSubscribe` method.
153153

154154
```ts
155155
const handler = {
156-
onMessage: (message: Uint8Array) => {console.log(new TextDecoder("utf-8").decode(message))},
156+
onMessage: (message: Data) => {console.log(message.text())},
157157
onError: (error: BeeError) => {console.log(error)}
158158
}
159159

@@ -169,7 +169,7 @@ subscription.cancel()
169169

170170
```js
171171
const handler = {
172-
onMessage: (message: Uint8Array) => {console.log(new TextDecoder("utf-8").decode(message))},
172+
onMessage: (message: Data) => {console.log(message.text())},
173173
onError: (error: BeeError) => {console.log(error)}
174174
}
175175

docs/user-documentation/upload-download.md

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ Uploading your data to Swarm is easy with `bee-js`. Based on your needs you can
1616
### Data
1717

1818
You can upload and retrieve any `string` or `Uint8Array` data with [`uploadData`](./api/classes/bee#uploaddata) and [`downloadData`](./api/classes/bee#downloaddata) functions.
19+
20+
When you download data the return type is [`Data`](./api/types/data) interface which extends `Uint8Array` with convenience functions like:
21+
22+
- `text()` that converts the bytes into UTF-8 encoded string
23+
- `hex()` that converts the bytes into **non-prefixed** hex string
24+
- `json()` that converts the bytes into JSON object
25+
1926
<Tabs
2027
groupId="lang_preferrence"
2128
defaultValue="ts"
@@ -34,7 +41,7 @@ console.log(hash)
3441

3542
const retrievedData = await bee.downloadData(hash)
3643

37-
console.log(new TextDecoder("utf-8").decode(retrievedData)) // prints 'Bee is awesome!'
44+
console.log(retrievedData.text()) // prints 'Bee is awesome!'
3845
```
3946

4047
</TabItem>
@@ -49,7 +56,7 @@ console.log(hash)
4956

5057
const retrievedData = await bee.downloadData(hash)
5158

52-
console.log(new TextDecoder("utf-8").decode(retrievedData)) // prints 'Bee is awesome!'
59+
console.log(retrievedData.text()) // prints 'Bee is awesome!'
5360
```
5461

5562
</TabItem>
@@ -78,7 +85,7 @@ const retrievedFile = await bee.downloadFile(hash)
7885

7986
console.log(retrievedFile.name) // prints 'textfile.txt'
8087
console.log(retrievedFile.contentType) // prints 'application/octet-stream'
81-
console.log(new TextDecoder("utf-8").decode(retrievedFile.data)) // prints 'Bee is awesome!'
88+
console.log(retrievedFile.data.text()) // prints 'Bee is awesome!'
8289
```
8390

8491
</TabItem>
@@ -90,7 +97,7 @@ const retrievedFile = await bee.downloadFile(hash)
9097

9198
console.log(retrievedFile.name) // prints 'textfile.txt'
9299
console.log(retrievedFile.contentType) // prints 'application/octet-stream'
93-
console.log(new TextDecoder("utf-8").decode(retrievedFile.data)) // prints 'Bee is awesome!'
100+
console.log(retrievedFile.data.text()) // prints 'Bee is awesome!'
94101
```
95102

96103
</TabItem>
@@ -115,7 +122,7 @@ const retrievedFile = await bee.downloadFile(hash)
115122

116123
console.log(retrievedFile.name) // prints 'foo.txt'
117124
console.log(retrievedFile.contentType) // prints 'text/plain'
118-
console.log(new TextDecoder("utf-8").decode(retrievedFile.data)) // prints 'foo'
125+
console.log(retrievedFile.data.text()) // prints 'foo'
119126
```
120127

121128
</TabItem>
@@ -129,7 +136,7 @@ const retrievedFile = await bee.downloadFile(hash)
129136

130137
console.log(retrievedFile.name) // prints 'foo.txt'
131138
console.log(retrievedFile.contentType) // prints 'text/plain'
132-
console.log(new TextDecoder("utf-8").decode(retrievedFile.data)) // prints 'foo'
139+
console.log(retrievedFile.data.text()) // prints 'foo'
133140
```
134141

135142
</TabItem>
@@ -157,8 +164,8 @@ const hash = await bee.uploadFiles([ foo, bar ]) // upload
157164
const rFoo = await bee.downloadFileFromCollection(hash, './foo.txt') // download foo
158165
const rBar = await bee.downloadFileFromCollection(hash, './bar.txt') // download bar
159166

160-
console.log(new TextDecoder("utf-8").decode(rFoo.data)) // prints 'foo'
161-
console.log(new TextDecoder("utf-8").decode(rBar.data)) // prints 'bar'
167+
console.log(rFoo.data.text()) // prints 'foo'
168+
console.log(rBar.data.text()) // prints 'bar'
162169
```
163170

164171
</TabItem>
@@ -173,8 +180,8 @@ const hash = await bee.uploadFiles([ foo, bar ]) // upload
173180
const rFoo = await bee.downloadFileFromCollection(hash, './foo.txt') // download foo
174181
const rBar = await bee.downloadFileFromCollection(hash, './bar.txt') // download bar
175182

176-
console.log(new TextDecoder("utf-8").decode(rFoo.data)) // prints 'foo'
177-
console.log(new TextDecoder("utf-8").decode(rBar.data)) // prints 'bar'
183+
console.log(rFoo.data.text()) // prints 'foo'
184+
console.log(rBar.data.text()) // prints 'bar'
178185
```
179186

180187
</TabItem>
@@ -204,8 +211,8 @@ const hash = await bee.uploadFilesFromDirectory('./', true) // upload recursivel
204211
const rFoo = await bee.downloadFileFromCollection(hash, './foo.txt') // download foo
205212
const rBar = await bee.downloadFileFromCollection(hash, './dir/bar.txt') // download bar
206213

207-
console.log(new TextDecoder("utf-8").decode(rFoo.data)) // prints 'foo'
208-
console.log(new TextDecoder("utf-8").decode(rBar.data)) // prints 'bar'
214+
console.log(rFoo.data.text()) // prints 'foo'
215+
console.log(rBar.data.text()) // prints 'bar'
209216
```
210217

211218
</TabItem>
@@ -217,8 +224,8 @@ const hash = await bee.uploadFilesFromDirectory('./', true) // upload recursivel
217224
const rFoo = await bee.downloadFileFromCollection(hash, './foo.txt') // download foo
218225
const rBar = await bee.downloadFileFromCollection(hash, './dir/bar.txt') // download bar
219226

220-
console.log(new TextDecoder("utf-8").decode(rFoo.data)) // prints 'foo'
221-
console.log(new TextDecoder("utf-8").decode(rBar.data)) // prints 'bar'
227+
console.log(rFoo.data.text()) // prints 'foo'
228+
console.log(rBar.data.text()) // prints 'bar'
222229
```
223230

224231
</TabItem>

0 commit comments

Comments
 (0)