diff --git a/types/nock/index.d.ts b/types/nock/index.d.ts index b081072f08..5ce577ea49 100644 --- a/types/nock/index.d.ts +++ b/types/nock/index.d.ts @@ -10,6 +10,8 @@ /// +import { ReadStream } from 'fs'; +import { ClientRequest } from "http"; import { Url } from 'url'; export = nock; @@ -39,14 +41,25 @@ declare namespace nock { let back: NockBack; + interface POJO { [k: string]: any; } + type RequestBodyMatcher = string | Buffer | RegExp | POJO | { (body: any): boolean; }; + interface RequestHeaderMatcher { [key: string]: string | RegExp | { (headerValue: string): boolean; }; } + interface HttpHeaders { [key: string]: string | string[] | { (req: any, res: any, body: string): any; }; } type InterceptFunction = ( uri: string | RegExp | { (uri: string): boolean; }, - requestBody?: string | RegExp | { (body: any): boolean; } | any, + requestBody?: RequestBodyMatcher, interceptorOptions?: Options ) => Interceptor; + + type ReplyBody = string | Buffer | ReadStream | POJO; type ReplyCallback = (err: any, result: ReplyCallbackResult) => void; - type ReplyCallbackResult = string | [number, string | any] | [number, string | any, HttpHeaders] | any; + type ReplyCallbackResult = ReplyBody | [number, ReplyBody] | [number, ReplyBody, HttpHeaders]; + interface ReplyCallbackContext extends Interceptor { + req: ClientRequest & { + headers: { [k: string]: string } + }; + } interface Scope extends NodeJS.EventEmitter { get: InterceptFunction; @@ -61,7 +74,7 @@ declare namespace nock { intercept: ( uri: string | RegExp | { (uri: string): boolean; }, method: string, - requestBody?: string | RegExp | { (body: any): boolean; } | any, + requestBody?: RequestBodyMatcher, options?: Options ) => Interceptor; @@ -86,12 +99,19 @@ declare namespace nock { } interface Interceptor { - query(params: boolean | { (queryObject: any): boolean; } | any): this; + query(params: boolean | string | { (queryObject: any): boolean; } | POJO): this; - reply(responseCode: number, body?: string | any, headers?: HttpHeaders): Scope; - reply(responseCode: number, callback: (uri: string, body: string, cb?: ReplyCallback) => ReplyCallbackResult, headers?: HttpHeaders): Scope; - reply(callback: (uri: string, body: string, cb?: ReplyCallback) => ReplyCallbackResult, headers?: HttpHeaders): Scope; - replyWithError(errorMessage: string | any): Scope; + // tslint (as of 5.16) is under the impression that the callback types can be unified, + // however, doing so causes the params to lose their inherited types during use. + /* tslint:disable:unified-signatures */ + reply(callback: (this: ReplyCallbackContext, uri: string, body: ReplyBody, cb: ReplyCallback) => void): Scope; + reply(callback: (this: ReplyCallbackContext, uri: string, body: ReplyBody) => ReplyCallbackResult): Scope; + reply(responseCode: number, callback: (this: ReplyCallbackContext, uri: string, body: ReplyBody, cb: ReplyCallback) => void): Scope; + reply(responseCode: number, callback: (this: ReplyCallbackContext, uri: string, body: ReplyBody) => ReplyCallbackResult): Scope; + reply(responseCode: number, body?: ReplyBody, headers?: HttpHeaders): Scope; + /* tslint:enable:unified-signatures */ + + replyWithError(errorMessage: string | POJO): Scope; replyWithFile(responseCode: number, fileName: string, headers?: HttpHeaders): Scope; matchHeader(name: string, value: string | RegExp | { (value: string): boolean; }): this; @@ -112,7 +132,7 @@ declare namespace nock { interface Options { allowUnmocked?: boolean; - reqheaders?: { [key: string]: string | RegExp | { (headerValue: string): boolean; }; }; + reqheaders?: RequestHeaderMatcher; badheaders?: string[]; filteringScope?: { (scope: string): boolean; }; encodedQueryParams?: boolean; @@ -147,11 +167,11 @@ declare namespace nock { port?: number | string; method?: string; path: string; - body?: string | any; + body?: RequestBodyMatcher; status?: number; - response?: string | any; + response?: ReplyBody; headers?: HttpHeaders; - reqheaders?: { [key: string]: string | RegExp | { (headerValue: string): boolean; }; }; + reqheaders?: RequestHeaderMatcher; options?: Options; } diff --git a/types/nock/nock-tests.ts b/types/nock/nock-tests.ts index c7f01cc6bf..1ab1ed027b 100644 --- a/types/nock/nock-tests.ts +++ b/types/nock/nock-tests.ts @@ -2,16 +2,16 @@ import nock = require('nock'); import * as fs from 'fs'; import { URL } from 'url'; -let scope: nock.Scope; +let scope: nock.Scope = nock("http://example.com"); let inst: nock.Interceptor; -let str: string; +let str = "foo"; let strings: string[]; -let bool: boolean; +let bool = true; let defs: nock.NockDefinition[]; -let options: nock.Options; +let options: nock.Options = {}; const num = 42; -const obj: {[k: string]: any} = {}; +const obj: { [k: string]: any } = {}; const regex = /test/; inst = scope.head(str); @@ -244,6 +244,11 @@ nock('http://example.com') .query(true) .reply(200, {results: [{id: 'pgte'}]}); +nock('http://example.com', { encodedQueryParams: true }) + .get('/users') + .query('foo%5Bbar%5D%3Dhello%20world%21') + .reply(200, { results: [{ id: 'pgte' }] }); + // Specifying replies scope = nock('http://myapp.iriscouch.com') .get('/users/1') @@ -310,6 +315,7 @@ scope = nock('http://www.google.com') scope = nock('http://www.google.com') .get('/cat-poems') .reply(function(uri, requestBody) { + str = this.req.path; console.log('path:', this.req.path); console.log('headers:', this.req.headers); // ... diff --git a/types/nock/tsconfig.json b/types/nock/tsconfig.json index 30c4ce3aa7..483c3b042d 100644 --- a/types/nock/tsconfig.json +++ b/types/nock/tsconfig.json @@ -5,8 +5,8 @@ "es6" ], "noImplicitAny": true, - "noImplicitThis": false, - "strictNullChecks": false, + "noImplicitThis": true, + "strictNullChecks": true, "strictFunctionTypes": true, "baseUrl": "../", "typeRoots": [ @@ -20,4 +20,4 @@ "index.d.ts", "nock-tests.ts" ] -} \ No newline at end of file +} diff --git a/types/nock/tslint.json b/types/nock/tslint.json index d9d49e375e..3db14f85ea 100644 --- a/types/nock/tslint.json +++ b/types/nock/tslint.json @@ -1,6 +1 @@ -{ - "extends": "dtslint/dt.json", - "rules": { - "no-any-union": false - } -} +{ "extends": "dtslint/dt.json" }