Skip to content

Commit

Permalink
Merge pull request #322 from mrijke/fix-321-responsehandler-types
Browse files Browse the repository at this point in the history
Fix #321: improve ResponseHandler types
  • Loading branch information
xg-wang authored Jul 30, 2021
2 parents b9170bf + 88f5ec1 commit a6d53af
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
7 changes: 5 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -47,8 +47,11 @@ export type ResponseHandler = {
(request: FakeXMLHttpRequest & ExtraRequestData):
| ResponseData
| PromiseLike<ResponseData>;
};

export type ResponseHandlerInstance = ResponseHandler & {
async: boolean;
numberOfCalls: number;
};
}

export default Server;
16 changes: 9 additions & 7 deletions src/pretender.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 ' +
Expand All @@ -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;
Expand Down

0 comments on commit a6d53af

Please sign in to comment.