Skip to content

Commit 2918f2a

Browse files
committed
doc: add examples to readme
1 parent 22dcbba commit 2918f2a

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

README.md

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,16 @@ npm install @msgpack/msgpack
4444

4545
### `encode(data: unknown, options?: EncodeOptions): Uint8Array`
4646

47-
It encodes `data` and returns a byte array as `Uint8Array`, throwing errors if `data` is, or includes, a non-serializable object such as `function` or a `symbol`.
47+
It encodes `data` and returns a byte array as `Uint8Array`, throwing errors if `data` is, or includes, a non-serializable object such as a `function` or a `symbol`.
48+
49+
for example:
50+
51+
```typescript
52+
import { encode } from "@msgpack/msgpack";
53+
54+
const encoded: Uint8Array = encode({ foo: "bar" });
55+
console.log(encoded);
56+
```
4857

4958
#### EncodeOptions
5059

@@ -61,6 +70,16 @@ It decodes `buffer` encoded as MessagePack, and returns a decoded object as `ukn
6170

6271
`buffer` must be an array of bytes, which is typically `Uint8Array`.
6372

73+
for example:
74+
75+
```typescript
76+
import { encode } from "@msgpack/msgpack";
77+
78+
const encoded: Uint8Array;
79+
const object = decode(encoded);
80+
console.log(object);
81+
```
82+
6483
#### DecodeOptions
6584

6685
Name|Type|Default
@@ -95,18 +114,45 @@ if (contentType && contentType.startsWith(MSGPACK_TYPE) && response.body != null
95114
} else { /* handle errors */ }
96115
```
97116

98-
### `decodeArrayStream(stream: AsyncIterable< ArrayLike<number>> | ReadableStream<ArrayLike<number>>, options?: DecodeAsyncOptions): AsyncIterable<unknown>`
117+
### `decodeArrayStream(stream: AsyncIterable<ArrayLike<number>> | ReadableStream<ArrayLike<number>>, options?: DecodeAsyncOptions): AsyncIterable<unknown>`
99118

100119
It is alike to `decodeAsync()`, but only accepts an array of items as the input `stream`, and emits the decoded item one by one.
101120

102121
It throws errors when the input is not an array-family.
103122

123+
for example:
124+
125+
```typescript
126+
import { encode } from "@msgpack/msgpack";
127+
128+
const stream: AsyncIterator<Uint8Array>;
129+
130+
// in an async function:
131+
for await (const item of decodeArrayStream(stream)) {
132+
console.log(item);
133+
}
134+
```
135+
136+
104137
### `decodeStream(stream: AsyncIterable<ArrayLike<number>> | ReadableStream<ArrayLike<number>>, options?: DecodeAsyncOptions): AsyncIterable<unknown>`
105138

106139
It is alike to `decodeAsync()` and `decodeArrayStream()`, but the input `stream` consists of independent MessagePack items.
107140

108141
In other words, it decodes an unlimited stream and emits an item one by one.
109142

143+
for example:
144+
145+
```typescript
146+
import { encode } from "@msgpack/msgpack";
147+
148+
const stream: AsyncIterator<Uint8Array>;
149+
150+
// in an async function:
151+
for await (const item of decodeArrayStream(stream)) {
152+
console.log(item);
153+
}
154+
```
155+
110156
### Extension Types
111157

112158
To handle [MessagePack Extension Types](https://github.com/msgpack/msgpack/blob/master/spec.md#extension-types), this library provides `ExtensionCodec` class.

0 commit comments

Comments
 (0)