You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There is a type error that occurs if you try to get the number of calls a handler has made. In the index.d.ts file, the return type for any request http verb is void, when in the actual source code it is a function that returns a handler; particularly, the handler that was passed to the register function. This handler is the one that maintains state of how many calls has been made. Here is an example of how it is being used in the READMe.md file.
server.get(/* ... */);consthandler=server.handlers[0];// type error. handlers does not exist as a type for the server.// orconsthandler=server.get(/* ... */);// type error. http verb function returns void.// thenconstnumberOfCalls=handler.numberOfCalls;
register: functionregister(verb,url,handler,async){if(!handler){thrownewError('The function you tried passing to Pretender to handle '+verb+' '+url+' is undefined or missing.');}handler.numberOfCalls=0;handler.async=async;this.handlers.push(handler);varregistry=this.hosts.forURL(url)[verb];registry.add([{path: parseURL(url).fullpath,handler: handler}]);returnhandler;// notice how we are returning a handler in the register function.},
Of course, that leaves the issue of the handler not having the properties numberOfCalls and async as those seem to get added dynamically.
So the interface for ResponseHandler will also need to get updated but I dont know much about the codebase to get this done smoothly. Hopefully it should not be to much work to add these missing properties to the handler.
The text was updated successfully, but these errors were encountered:
LuisAverhoff
changed the title
The type for the request handler should be a handler and not void.
The return type for the request handler should be a response handler and not void.
Dec 10, 2019
@xg-wang the easiest way to handle this would be something like
register: functionregister(verb,url,handler: RequestHandler,async: Async){// Locally, work with a value w/ optional propertiesconsth: RequestHandler&{numberOfCalls?: number,async?: Async}=handler;h.numberOfCalls=0;h.async=async;this.handlers.push(h);varregistry=this.hosts.forURL(url)[verb];registry.add([{path: parseURL(url).fullpath,handler: h}]);// Ultimately return a type with non-optional extra propertiesreturnhasRequestHandler&{numberOfCalls: number;async: Async}},
There is a type error that occurs if you try to get the number of calls a handler has made. In the index.d.ts file, the return type for any request http verb is void, when in the actual source code it is a function that returns a handler; particularly, the handler that was passed to the register function. This handler is the one that maintains state of how many calls has been made. Here is an example of how it is being used in the READMe.md file.
My suggestion is to change
to
For reference, here is what the verbify function is doing.
and here is what the register function is doing.
Of course, that leaves the issue of the handler not having the properties
numberOfCalls
andasync
as those seem to get added dynamically.So the interface for
ResponseHandler
will also need to get updated but I dont know much about the codebase to get this done smoothly. Hopefully it should not be to much work to add these missing properties to the handler.The text was updated successfully, but these errors were encountered: