diff --git a/index.d.ts b/index.d.ts index 153a2fe..7e17ae3 100644 --- a/index.d.ts +++ b/index.d.ts @@ -36,7 +36,7 @@ export type RequestHandler = ( urlExpression: string, response: ResponseHandler, asyncOrDelay?: boolean | number -) => void; +) => ResponseHandlerInstance; export type ResponseData = [number, { [k: string]: string }, string]; interface ExtraRequestData { @@ -47,8 +47,11 @@ export type ResponseHandler = { (request: FakeXMLHttpRequest & ExtraRequestData): | ResponseData | PromiseLike; +}; + +export type ResponseHandlerInstance = ResponseHandler & { async: boolean; numberOfCalls: number; -}; +} export default Server; diff --git a/src/pretender.ts b/src/pretender.ts index a5261db..0b3fb4f 100644 --- a/src/pretender.ts +++ b/src/pretender.ts @@ -1,7 +1,7 @@ import * as FakeFetch from 'whatwg-fetch'; import FakeXMLHttpRequest from 'fake-xml-http-request'; import { Params, QueryParams } from 'route-recognizer'; -import { ResponseHandler } from '../index.d'; +import { ResponseHandler, ResponseHandlerInstance } from '../index.d'; import Hosts from './hosts'; import parseURL from './parse-url'; import Registry from './registry'; @@ -155,7 +155,7 @@ export default class Pretender { url: string, handler: ResponseHandler, async: boolean - ): ResponseHandler { + ): ResponseHandlerInstance { if (!handler) { throw new Error( 'The function you tried passing to Pretender to handle ' + @@ -166,20 +166,22 @@ export default class Pretender { ); } - handler.numberOfCalls = 0; - handler.async = async; - this.handlers.push(handler); + const handlerInstance = handler as ResponseHandlerInstance; + + handlerInstance.numberOfCalls = 0; + handlerInstance.async = async; + this.handlers.push(handlerInstance); let registry = this.hosts.forURL(url)[verb]; registry.add([ { path: parseURL(url).fullpath, - handler: handler, + handler: handlerInstance, }, ]); - return handler; + return handlerInstance; } passthrough = PASSTHROUGH;