Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve TypeScript types #71

Merged
merged 1 commit into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {type Stream} from 'node:stream';
import {type Readable} from 'node:stream';
import {type Buffer} from 'node:buffer';

export class MaxBufferError extends Error {
Expand Down Expand Up @@ -49,7 +49,7 @@ console.log(await getStream(stream));
// ~~
```
*/
export default function getStream(stream: Stream, options?: Options): Promise<string>;
export default function getStream(stream: Readable, options?: Options): Promise<string>;

/**
Get the given `stream` as a buffer.
Expand All @@ -65,4 +65,4 @@ const stream = fs.createReadStream('unicorn.png');
console.log(await getStreamAsBuffer(stream));
```
*/
export function getStreamAsBuffer(stream: Stream, options?: Options): Promise<Buffer>;
export function getStreamAsBuffer(stream: Readable, options?: Options): Promise<Buffer>;
25 changes: 16 additions & 9 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import {type Buffer} from 'node:buffer';
import {type Stream} from 'node:stream';
import {type Readable} from 'node:stream';
import fs from 'node:fs';
import {expectType} from 'tsd';
import {expectType, expectError} from 'tsd';
import getStream, {getStreamAsBuffer, MaxBufferError} from './index.js';

const stream = fs.createReadStream('foo') as Stream;
const nodeStream = fs.createReadStream('foo') as Readable;

expectType<Promise<string>>(getStream(stream));
expectType<Promise<string>>(getStream(stream, {maxBuffer: 10}));
expectType<string>(await getStream(nodeStream));
expectType<string>(await getStream(nodeStream, {maxBuffer: 10}));
expectError(await getStream({}));
expectError(await getStream(nodeStream, {maxBuffer: '10'}));
expectError(await getStream(nodeStream, {unknownOption: 10}));
expectError(await getStream(nodeStream, {maxBuffer: 10}, {}));

expectType<Promise<Buffer>>(getStreamAsBuffer(stream));
expectType<Promise<Buffer>>(getStreamAsBuffer(stream, {maxBuffer: 10}));
expectType<Buffer>(await getStreamAsBuffer(nodeStream));
expectType<Buffer>(await getStreamAsBuffer(nodeStream, {maxBuffer: 10}));
expectError(await getStreamAsBuffer({}));
expectError(await getStreamAsBuffer(nodeStream, {maxBuffer: '10'}));
expectError(await getStreamAsBuffer(nodeStream, {unknownOption: 10}));
expectError(await getStreamAsBuffer(nodeStream, {maxBuffer: 10}, {}));

const maxBufferError = new MaxBufferError();
expectType<MaxBufferError>(maxBufferError);
expectType<MaxBufferError>(new MaxBufferError());