Skip to content

Commit

Permalink
Merge pull request #134 from corinnaerin/typescript_def
Browse files Browse the repository at this point in the history
Add typescript definition file
  • Loading branch information
howardabrams committed Apr 22, 2017
2 parents f787667 + 2231014 commit 6e65cb6
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
94 changes: 94 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { Request, Response } from 'express';

declare module 'node-mocks-http' {

export type RequestMethod =
'GET' | 'HEAD'| 'POST' | 'PUT' | 'DELETE' | 'TRACE' | 'CONNECT';

export interface Params {
[key: string]: any;
}

export interface Session {
[key: string]: string;
}

export interface Cookies {
[key: string]: string;
}

export interface Headers {
[key: string]: string;
}

export interface Query {
[key: string]: string;
}

export interface Files {
[key: string]: string;
}

export interface Body {
[key: string]: string;
}

export interface RequestOptions {
method?: RequestMethod;
url?: string;
originalUrl?: string;
path?: string;
params?: Params;
session?: Session;
cookies?: Cookies;
signedCookies?: Cookies;
headers?: Headers;
body?: Body;
query?: Query;
files?: Files;
}

export interface MockRequest extends Request {
_setParameter: (key: string, value: string) => void;
_setSessionVariable: (variable: string, value: string) => void;
_setCookiesVariable: (variable: string, value: string) => void;
_setSignedCookiesVariable: (variable: string, value: string) => void;
_setHeadersCookiesVariable: (variable: string, value: string) => void;
_setFilesCookiesVariable: (variable: string, value: string) => void;
_setMethod: (method: string) => void;
_setURL: (value: string) => void;
_setOriginalUrl: (value: string) => void;
_setBody: (body: Body) => void;
_addBody: (key: string, value: any) => void;
}

export interface ResponseOptions {
eventEmitter?: any;
writableStream?: any;
req?: any;
}

export interface MockResponse extends Response {
_isEndCalled: () => boolean;
_getHeaders: () => Headers;
_getData: () => any;
_getStatusCode: () => number;
_getStatusMessage: () => string;
_isJSON: () => boolean;
_isUTF8: () => boolean;
_isDataLengthValid: () => boolean;
_getRedirectUrl: () => string;
_getRenderData: () => any;
}

export function createRequest(options?: RequestOptions): MockRequest;

export function createResponse(options?: ResponseOptions): MockResponse;

export interface Mocks {
req: MockRequest;
res: MockResponse;
}

export function createMocks(reqOptions?: RequestOptions, resOptions?: ResponseOptions): Mocks;
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@
"url": "git://github.com/howardabrams/node-mocks-http.git"
},
"main": "./lib/http-mock.js",
"types": "./lib/index.d.ts",
"engines": {
"node": ">=0.6"
},
"dependencies": {
"@types/express": "^4.0.35",
"accepts": "^1.3.3",
"depd": "^1.1.0",
"fresh": "^0.3.0",
Expand Down

0 comments on commit 6e65cb6

Please sign in to comment.