forked from nodejs/undici
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Improve BodyReadable type definition (nodejs#1259)
* fix: Improve BodyReadable type definition * fix: Add tests for BodyReadable type
- Loading branch information
Showing
3 changed files
with
99 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { expectAssignable } from 'tsd' | ||
import BodyReadable from '../../types/readable' | ||
import { Blob } from 'buffer' | ||
|
||
expectAssignable<BodyReadable>(new BodyReadable()) | ||
|
||
{ | ||
const readable = new BodyReadable() | ||
|
||
// dump | ||
expectAssignable<Promise<void>>(readable.dump()) | ||
expectAssignable<Promise<void>>(readable.dump({ limit: 123 })) | ||
|
||
// text | ||
expectAssignable<Promise<string>>(readable.text()) | ||
|
||
// json | ||
expectAssignable<Promise<any>>(readable.json()) | ||
|
||
// blob | ||
expectAssignable<Promise<Blob>>(readable.blob()) | ||
|
||
// arrayBuffer | ||
expectAssignable<Promise<ArrayBuffer>>(readable.arrayBuffer()) | ||
|
||
// formData | ||
expectAssignable<Promise<never>>(readable.formData()) | ||
|
||
// bodyUsed | ||
expectAssignable<boolean>(readable.bodyUsed) | ||
|
||
// body | ||
expectAssignable<never | undefined>(readable.body) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { Readable } from "stream"; | ||
import { Blob } from 'buffer' | ||
|
||
export = BodyReadable | ||
|
||
declare class BodyReadable extends Readable { | ||
constructor( | ||
resume?: (this: Readable, size: number) => void | null, | ||
abort?: () => void | null, | ||
contentType?: string | ||
) | ||
|
||
/** Consumes and returns the body as a string | ||
* https://fetch.spec.whatwg.org/#dom-body-text | ||
*/ | ||
text(): Promise<string> | ||
|
||
/** Consumes and returns the body as a JavaScript Object | ||
* https://fetch.spec.whatwg.org/#dom-body-json | ||
*/ | ||
json(): Promise<any> | ||
|
||
/** Consumes and returns the body as a Blob | ||
* https://fetch.spec.whatwg.org/#dom-body-blob | ||
*/ | ||
blob(): Promise<Blob> | ||
|
||
/** Consumes and returns the body as an ArrayBuffer | ||
* https://fetch.spec.whatwg.org/#dom-body-arraybuffer | ||
*/ | ||
arrayBuffer(): Promise<ArrayBuffer> | ||
|
||
/** Not implemented | ||
* | ||
* https://fetch.spec.whatwg.org/#dom-body-formdata | ||
*/ | ||
formData(): Promise<never> | ||
|
||
/** Returns true if the body is not null and the body has been consumed | ||
* | ||
* Otherwise, returns false | ||
* | ||
* https://fetch.spec.whatwg.org/#dom-body-bodyused | ||
*/ | ||
readonly bodyUsed: boolean | ||
|
||
/** Throws on node 16.6.0 | ||
* | ||
* If body is null, it should return null as the body | ||
* | ||
* If body is not null, should return the body as a ReadableStream | ||
* | ||
* https://fetch.spec.whatwg.org/#dom-body-body | ||
*/ | ||
readonly body: never | undefined | ||
|
||
/** Dumps the response body by reading `limit` number of bytes. | ||
* @param opts.limit Number of bytes to read (optional) - Default: 262144 | ||
*/ | ||
dump(opts?: { limit: number }): Promise<void> | ||
} |