mirror of
https://github.com/FlipsideCrypto/DefinitelyTyped.git
synced 2026-02-06 10:56:53 +00:00
[nock] Fix Interceptor.reply. (#35385)
I was working on removing the `no-any-union` lint override and realized
that `any` was being used in several places where a plain objected was
intended. It was also covering up that reply bodies can be Buffers or
Streams.
[Docs for specifying replies](https://github.com/nock/nock#specifying-replies)
Note: There was a bug in place. Previously, the types allowed for `reply`
to take two args, a callback and an object of headers. However, this
form is not documented and turns out to not be functional when
inspecting [the source](cb56669e0b/lib/interceptor.js (L68-L72)).
I’ve confirmed with the lib maintainers that the bug was in `@types`.
https://github.com/nock/nock/issues/1513
This commit is contained in:
parent
ba4b237d16
commit
bcfb2c65f2
44
types/nock/index.d.ts
vendored
44
types/nock/index.d.ts
vendored
@ -10,6 +10,8 @@
|
||||
|
||||
/// <reference types="node" />
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@ -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);
|
||||
// ...
|
||||
|
||||
@ -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"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1 @@
|
||||
{
|
||||
"extends": "dtslint/dt.json",
|
||||
"rules": {
|
||||
"no-any-union": false
|
||||
}
|
||||
}
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
|
||||
Loading…
Reference in New Issue
Block a user