Skip to content

Commit 95e0b47

Browse files
authored
docs: convert examples to ts, run doc verifier (#434)
Enables type checking for docs so they are kept in sync with API changes.
1 parent ad084ec commit 95e0b47

File tree

10 files changed

+120
-54
lines changed

10 files changed

+120
-54
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"build": "aegir run build",
2828
"lint": "aegir run lint",
2929
"dep-check": "aegir run dep-check",
30+
"doc-check": "aegir run doc-check",
3031
"release": "run-s build docs:no-publish npm:release docs",
3132
"npm:release": "aegir run release --concurrency 1",
3233
"docs": "aegir docs",

packages/ipfs-unixfs-exporter/README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ The UnixFS Exporter provides a means to read DAGs from a blockstore given a CID.
2828

2929
## Example
3030

31-
```js
31+
```TypeScript
3232
// import a file and export it again
3333
import { importer } from 'ipfs-unixfs-importer'
3434
import { exporter } from 'ipfs-unixfs-exporter'
@@ -49,19 +49,23 @@ console.info(files[0].cid) // Qmbaz
4949

5050
const entry = await exporter(files[0].cid, blockstore)
5151

52+
if (entry.type !== 'file') {
53+
throw new Error('Unexpected entry type')
54+
}
55+
5256
console.info(entry.cid) // Qmqux
5357
console.info(entry.path) // Qmbaz/foo/bar.txt
5458
console.info(entry.name) // bar.txt
5559
console.info(entry.unixfs.fileSize()) // 4
5660

5761
// stream content from unixfs node
5862
const size = entry.unixfs.fileSize()
59-
const bytes = new Uint8Array(size)
63+
const bytes = new Uint8Array(Number(size))
6064
let offset = 0
6165

6266
for await (const buf of entry.content()) {
6367
bytes.set(buf, offset)
64-
offset += chunk.length
68+
offset += buf.byteLength
6569
}
6670

6771
console.info(bytes) // 0, 1, 2, 3

packages/ipfs-unixfs-exporter/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
"clean": "aegir clean",
135135
"lint": "aegir lint",
136136
"dep-check": "aegir dep-check",
137+
"doc-check": "aegir doc-check",
137138
"release": "aegir release"
138139
},
139140
"dependencies": {

packages/ipfs-unixfs-exporter/src/index.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* @example
77
*
8-
* ```js
8+
* ```TypeScript
99
* // import a file and export it again
1010
* import { importer } from 'ipfs-unixfs-importer'
1111
* import { exporter } from 'ipfs-unixfs-exporter'
@@ -26,19 +26,23 @@
2626
*
2727
* const entry = await exporter(files[0].cid, blockstore)
2828
*
29+
* if (entry.type !== 'file') {
30+
* throw new Error('Unexpected entry type')
31+
* }
32+
*
2933
* console.info(entry.cid) // Qmqux
3034
* console.info(entry.path) // Qmbaz/foo/bar.txt
3135
* console.info(entry.name) // bar.txt
3236
* console.info(entry.unixfs.fileSize()) // 4
3337
*
3438
* // stream content from unixfs node
3539
* const size = entry.unixfs.fileSize()
36-
* const bytes = new Uint8Array(size)
40+
* const bytes = new Uint8Array(Number(size))
3741
* let offset = 0
3842
*
3943
* for await (const buf of entry.content()) {
4044
* bytes.set(buf, offset)
41-
* offset += chunk.length
45+
* offset += buf.byteLength
4246
* }
4347
*
4448
* console.info(bytes) // 0, 1, 2, 3
@@ -180,7 +184,7 @@ export interface Exportable<T> {
180184
*
181185
* When `entry` is a file or a `raw` node, `offset` and/or `length` arguments can be passed to `entry.content()` to return slices of data:
182186
*
183-
* ```javascript
187+
* ```TypeScript
184188
* const length = 5
185189
* const data = new Uint8Array(length)
186190
* let offset = 0
@@ -201,7 +205,7 @@ export interface Exportable<T> {
201205
*
202206
* If `entry` is a directory, passing `offset` and/or `length` to `entry.content()` will limit the number of files returned from the directory.
203207
*
204-
* ```javascript
208+
* ```TypeScript
205209
* const entries = []
206210
*
207211
* for await (const entry of dir.content({
@@ -220,7 +224,7 @@ export interface Exportable<T> {
220224
/**
221225
* If the entry is a file, `entry.content()` returns an async iterator that yields one or more Uint8Arrays containing the file content:
222226
*
223-
* ```javascript
227+
* ```TypeScript
224228
* if (entry.type === 'file') {
225229
* for await (const chunk of entry.content()) {
226230
* // chunk is a Buffer
@@ -237,7 +241,7 @@ export interface UnixFSFile extends Exportable<Uint8Array> {
237241
/**
238242
* If the entry is a directory, `entry.content()` returns further `entry` objects:
239243
*
240-
* ```javascript
244+
* ```TypeScript
241245
* if (entry.type === 'directory') {
242246
* for await (const entry of dir.content()) {
243247
* console.info(entry.name)
@@ -264,7 +268,7 @@ export interface ObjectNode extends Exportable<any> {
264268
*
265269
* `entry.content()` returns an async iterator that yields a buffer containing the node content:
266270
*
267-
* ```javascript
271+
* ```TypeScript
268272
* for await (const chunk of entry.content()) {
269273
* // chunk is a Buffer
270274
* }
@@ -371,7 +375,7 @@ const cidAndRest = (path: string | Uint8Array | CID): { cid: CID, toResolve: str
371375
*
372376
* @example
373377
*
374-
* ```javascript
378+
* ```TypeScript
375379
* import { walkPath } from 'ipfs-unixfs-exporter'
376380
*
377381
* const entries = []

packages/ipfs-unixfs-importer/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Let's create a little directory to import:
3737

3838
And write the importing logic:
3939

40-
```js
40+
```TypeScript
4141
import { importer } from 'ipfs-unixfs-importer'
4242
import { MemoryBlockstore } from 'blockstore-core/memory'
4343
import * as fs from 'node:fs'
@@ -61,7 +61,7 @@ for await (const entry of importer(source, blockstore)) {
6161

6262
When run, metadata about DAGNodes in the created tree is printed until the root:
6363

64-
```js
64+
```
6565
{
6666
cid: CID, // see https://github.com/multiformats/js-cid
6767
path: 'tmp/foo/bar',

packages/ipfs-unixfs-importer/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@
158158
"clean": "aegir clean",
159159
"lint": "aegir lint",
160160
"dep-check": "aegir dep-check",
161+
"doc-check": "aegir doc-check",
161162
"release": "aegir release"
162163
},
163164
"dependencies": {

packages/ipfs-unixfs-importer/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*
1515
* And write the importing logic:
1616
*
17-
* ```js
17+
* ```TypeScript
1818
* import { importer } from 'ipfs-unixfs-importer'
1919
* import { MemoryBlockstore } from 'blockstore-core/memory'
2020
* import * as fs from 'node:fs'
@@ -38,7 +38,7 @@
3838
*
3939
* When run, metadata about DAGNodes in the created tree is printed until the root:
4040
*
41-
* ```js
41+
* ```
4242
* {
4343
* cid: CID, // see https://github.com/multiformats/js-cid
4444
* path: 'tmp/foo/bar',

packages/ipfs-unixfs/README.md

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,33 @@ The UnixFS spec can be found in the [ipfs/specs repository](http://github.com/ip
3030

3131
## Example - Create a file composed of several blocks
3232

33-
```JavaScript
33+
```TypeScript
34+
import { UnixFS } from 'ipfs-unixfs'
35+
3436
const data = new UnixFS({ type: 'file' })
35-
data.addBlockSize(256) // add the size of each block
36-
data.addBlockSize(256)
37+
data.addBlockSize(256n) // add the size of each block
38+
data.addBlockSize(256n)
3739
// ...
3840
```
3941

4042
## Example - Create a directory that contains several files
4143

4244
Creating a directory that contains several files is achieve by creating a unixfs element that identifies a MerkleDAG node as a directory. The links of that MerkleDAG node are the files that are contained in this directory.
4345

44-
```JavaScript
46+
```TypeScript
47+
import { UnixFS } from 'ipfs-unixfs'
48+
4549
const data = new UnixFS({ type: 'directory' })
4650
```
4751

4852
## Example - Create an unixfs Data element
4953

50-
```JavaScript
51-
const data = new UnixFS([options])
54+
```TypeScript
55+
import { UnixFS } from 'ipfs-unixfs'
56+
57+
const data = new UnixFS({
58+
// ...options
59+
})
5260
```
5361

5462
`options` is an optional object argument that might include the following keys:
@@ -67,48 +75,67 @@ const data = new UnixFS([options])
6775

6876
## Example - Add and remove a block size to the block size list
6977

70-
```JavaScript
71-
data.addBlockSize(<size in bytes>)
78+
```TypeScript
79+
import { UnixFS } from 'ipfs-unixfs'
80+
81+
const data = new UnixFS({ type: 'file' })
82+
const sizeInBytes = 100n
83+
data.addBlockSize(sizeInBytes)
7284
```
7385

74-
```JavaScript
75-
data.removeBlockSize(<index>)
86+
```TypeScript
87+
import { UnixFS } from 'ipfs-unixfs'
88+
89+
const data = new UnixFS({ type: 'file' })
90+
91+
const index = 0
92+
data.removeBlockSize(index)
7693
```
7794

7895
## Example - Get total fileSize
7996

80-
```JavaScript
97+
```TypeScript
98+
import { UnixFS } from 'ipfs-unixfs'
99+
100+
const data = new UnixFS({ type: 'file' })
81101
data.fileSize() // => size in bytes
82102
```
83103

84104
## Example - Marshal and unmarshal
85105

86-
```javascript
106+
```TypeScript
107+
import { UnixFS } from 'ipfs-unixfs'
108+
109+
const data = new UnixFS({ type: 'file' })
87110
const marshaled = data.marshal()
88-
const unmarshaled = Unixfs.unmarshal(marshaled)
111+
const unmarshaled = UnixFS.unmarshal(marshaled)
89112
```
90113

91114
## Example - Is this UnixFS entry a directory?
92115

93-
```JavaScript
94-
const dir = new Data({ type: 'directory' })
116+
```TypeScript
117+
import { UnixFS } from 'ipfs-unixfs'
118+
119+
const dir = new UnixFS({ type: 'directory' })
95120
dir.isDirectory() // true
96121

97-
const file = new Data({ type: 'file' })
122+
const file = new UnixFS({ type: 'file' })
98123
file.isDirectory() // false
99124
```
100125

101126
## Example - Has an mtime been set?
102127

103128
If no modification time has been set, no `mtime` property will be present on the `Data` instance:
104129

105-
```JavaScript
106-
const file = new Data({ type: 'file' })
130+
```TypeScript
131+
import { UnixFS } from 'ipfs-unixfs'
132+
133+
const file = new UnixFS({ type: 'file' })
107134
file.mtime // undefined
108135

109136
Object.prototype.hasOwnProperty.call(file, 'mtime') // false
110137

111-
const dir = new Data({ type: 'dir', mtime: new Date() })
138+
const dir = new UnixFS({ type: 'dir', mtime: { secs: 5n } })
112139
dir.mtime // { secs: Number, nsecs: Number }
113140
```
114141

packages/ipfs-unixfs/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
"clean": "aegir clean",
136136
"lint": "aegir lint",
137137
"dep-check": "aegir dep-check",
138+
"doc-check": "aegir doc-check",
138139
"release": "aegir release"
139140
},
140141
"dependencies": {

0 commit comments

Comments
 (0)