raw-body: conditional return type based on options

This commit is contained in:
Blake Embrey
2017-01-23 19:58:07 -08:00
parent 99d41f818c
commit 7e3ebd00f0
2 changed files with 15 additions and 12 deletions
+12 -9
View File
@@ -4,13 +4,16 @@
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
import { Readable } from "stream";
declare namespace getRawBody {
export type Encoding = string | true;
export interface Options {
length?: number;
limit?: number;
encoding?: string;
encoding?: Encoding;
}
export interface RawBodyError extends Error {
@@ -19,18 +22,18 @@ declare namespace getRawBody {
expected?: number;
received?: number;
encoding?: string;
status?: number;
statusCode?: number;
type?: string;
status: number;
statusCode: number;
type: string;
}
export type Callback = (err: RawBodyError, res: string | Buffer) => void;
}
declare function getRawBody(stream: Readable, callback: getRawBody.Callback): void;
declare function getRawBody(stream: Readable, options: getRawBody.Options | string, callback: getRawBody.Callback): void;
declare function getRawBody(stream: Readable, callback: (err: getRawBody.RawBodyError, body: Buffer) => void): void;
declare function getRawBody(stream: Readable, options: (getRawBody.Options & { encoding: getRawBody.Encoding }) | getRawBody.Encoding, callback: (err: getRawBody.RawBodyError, body: string) => void): void;
declare function getRawBody(stream: Readable, options: getRawBody.Options, callback: (err: getRawBody.RawBodyError, body: Buffer) => void): void;
declare function getRawBody(stream: Readable, options?: getRawBody.Options | string): Promise<string | Buffer>;
declare function getRawBody(stream: Readable, options: (getRawBody.Options & { encoding: getRawBody.Encoding }) | getRawBody.Encoding): Promise<string>;
declare function getRawBody(stream: Readable, options?: getRawBody.Options): Promise<Buffer>;
export = getRawBody;
+3 -3
View File
@@ -2,11 +2,11 @@ import getRawBody = require("raw-body");
const stream: any = {};
const simple = getRawBody(stream);
const simple: Promise<Buffer> = getRawBody(stream);
const withEncoding = getRawBody(stream, "utf-8");
const withEncoding: Promise<string> = getRawBody(stream, "utf-8");
const withOptions = getRawBody(stream, {
const withOptions: Promise<string> = getRawBody(stream, {
encoding: "utf-8",
length: 1024,
limit: 512