http-mock-adapter icon indicating copy to clipboard operation
http-mock-adapter copied to clipboard

Is there a way to have MockServerCallback's server parameter be strongly typed?

Open Desync-o-tron opened this issue 2 years ago • 3 comments

I will write test code like this: dioAdapter.onPost("some_route", (server) => server.reply(...), data: Matchers.any); I am very much bugged that I do not know the type of 'server'. Moreover, I don't really know what to pass to server.reply()!

What I've tried: I've looked through the relevant docs, but I see no page on MockServerCallback. I've looked into the code and tried declaring the type of the callback that I pass into onPost(), but my linter won't allow me.

Desync-o-tron avatar May 23 '22 02:05 Desync-o-tron

Its type is basically dynamic. And basing on exact type is not allowed yet. Would you like to have kinda feature?

theiskaa avatar Jun 17 '22 15:06 theiskaa

I think the problem is not that the callback is not strongly typed because it is (it is defined as a typedef of type void Function(MockServer server);), but rather than MockServerCallback and MockServer types are not exposed by the package.

In my case I would like to reuse the MockServerCallback function between multiple adapter methods but I can't because of types not being exposed.

Example of what I would like to do:

adapter.onGet('/some/route', replyCallback);
adapter.onPost('/some/route', replyCallback);
adapter.onPut('/some/route', replyCallback);

void replyCallback(MockServer server) {
    server.reply(200, {'message': 'success'});
}

Right now the compiler complains about MockServer that is undefined because it cannot see it.

Could we expose it ? I can propose a PR

orevial avatar Jun 09 '23 13:06 orevial

I think the problem is not that the callback is not strongly typed because it is (it is defined as a typedef of type void Function(MockServer server);), but rather than MockServerCallback and MockServer types are not exposed by the package.

In my case I would like to reuse the MockServerCallback function between multiple adapter methods but I can't because of types not being exposed.

Example of what I would like to do:

adapter.onGet('/some/route', replyCallback);
adapter.onPost('/some/route', replyCallback);
adapter.onPut('/some/route', replyCallback);

void replyCallback(MockServer server) {
    server.reply(200, {'message': 'success'});
}

Right now the compiler complains about MockServer that is undefined because it cannot see it.

Could we expose it ? I can propose a PR

@theiskaa thoughts?

LukaGiorgadze avatar Jun 09 '23 14:06 LukaGiorgadze