mirror of
https://github.com/denoland/std.git
synced 2024-11-22 04:59:05 +00:00
4df10f6b54
* chore(streams): complete documentation * tweaks
24 lines
693 B
TypeScript
24 lines
693 B
TypeScript
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
|
// This module is browser compatible.
|
|
|
|
import { toText } from "./to_text.ts";
|
|
|
|
/**
|
|
* Converts a JSON-formatted {@linkcode ReadableSteam} of strings or
|
|
* {@linkcode Uint8Array}s to an object. Works the same as
|
|
* {@linkcode Response.json}.
|
|
*
|
|
* @example
|
|
* ```ts
|
|
* import { toJson } from "https://deno.land/std@$STD_VERSION/streams/to_json.ts";
|
|
*
|
|
* const stream = ReadableStream.from([JSON.stringify({ hello: "world" })]);
|
|
* await toJson(stream); // { hello: "world" }
|
|
* ```
|
|
*/
|
|
export function toJson(
|
|
readableStream: ReadableStream,
|
|
): Promise<unknown> {
|
|
return toText(readableStream).then(JSON.parse);
|
|
}
|