Skip to content

Commit b15411e

Browse files
authored
Merge pull request #53 from msgpack/more_example
add example for use with whatwg fetch
2 parents ae20f55 + d143e44 commit b15411e

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,24 @@ You can use `max${Type}Length` to limit the length of each type decoded.
6767

6868
### `decodeAsync(stream: AsyncIterable<ArrayLike<number>> | ReadableStream<ArrayLike<number>>, options?: DecodeAsyncOptions): Promise<unknown>`
6969

70-
It decodes `stream` in an async iterable of byte arrays and returns decoded data as `uknown` wrapped in `Promise`. This function works asyncronously.
70+
It decodes `stream` in an async iterable of byte arrays, and returns decoded object as `uknown` type, wrapped in `Promise`. This function works asyncronously.
7171

72-
Note that `decodeAsync()` acceps the same options as `decode()`.
72+
`DecodeAsyncOptions` is the same as `DecodeOptions` for `decode()`.
73+
74+
This function is designed to work with whatwg `fetch()` like this:
75+
76+
```typescript
77+
import { decodeAsync } from "@msgpack/msgpack";
78+
79+
const MSGPACK_TYPE = "application/x-msgpack";
80+
81+
const response = await fetch(url);
82+
const contentType = response.headers.get("Content-Type");
83+
if (contentType && contentType.startsWith(MSGPACK_TYPE) && response.body != null) {
84+
const object = await decodeAsync(response.body);
85+
// do something with object
86+
} else { /* handle errors */ }
87+
```
7388

7489
### `decodeArrayStream(stream: AsyncIterable< ArrayLike<number>> | ReadableStream<ArrayLike<number>>, options?: DecodeAsyncOptions): AsyncIterable<unknown>`
7590

0 commit comments

Comments
 (0)