To mock request or other midori internal selectors you can:
import{request}from'midori';
import{createMockRequest}from'midori/test';
jest.mock('midori/request',()=>{
returncreateSelector(jest.fn(()=>{
returncreateMockRequest({
url:'/foo',
method:'POST',
});
}));
});
If you're not using jest or just want to mock values for a single test, then runSelector also provides a factory function with which you can use to setup your mocks.
import{createSelector}from'midori';
import{runSelector}from'midori/test';
importmySelectorAfrom'./mySelectorA';
importmySelectorBfrom'./mySelectorB';
constmySelectorC=createSelector(
mySelectorA,
mySelectorB,
(a,b)=> a + b,
);
constresult=runSelector(mySelectorC,(inst)=>{
inst.mockValue(mySelectorA,1);
inst.mockValue(mySelectorB,1);
});
expect(result).toBe(2);
Advanced
Routing
The standard way of doing request dependent routing is by using match. Most frameworks allow you to only match against the request path and method, but midori makes no such compromises and you can use all kinds of predicates to determine the control flow of your application.
import{match,send,compose}from'midori';
import{path,host}from'midori/match';
constisFoo=path('/foo');// Match against URL path
constisLocalhost=host(/localhost/);// Match against `Host` header
constcreateApp=compose(
match(isFoo,send('Hello from foo')),
match(isLocalhost,send('You accessed from localhost')),
);
You can also create match conjunctions using every (i.e. all predicates must be true for the match to succeed).
import{match,send,compose}from'midori';
import{path,method,every}from'midori/match';
// This is roughly how `get()` works internally.
constisGetFoo=every(method('GET'),path('/foo'));
constapp=compose(
match(isGetFoo,send('Hello from foo')),
);
You can also take action based on when the match fails:
import{match,send,compose}from'midori';
import{path,host}from'midori/match';
constisFoo=path('/foo');// Match against URL path
constisLocalhost=host(/localhost/);// Match against `Host` header
constapp=compose(
match(isFoo,send('Hello from foo'),send('Hello not from foo')),
);
Connectors
You can connect midori to a number of other HTTP frameworks (like express, hapi).
import{send}from'midori';
constapp=send('Hello world.');
With express
Install dependencies:
npm install --save midori-express express
Create an express app and just use() your midori middleware as if it were express middleware:
importexpressfrom'express';
importcreateMiddlewarefrom'midori-express';
import{compose}from'midori';
constexpressApp=express();
expressApp.use(createMiddleware(app));
expressApp.listen(8080);
With hapi
Install dependencies:
npm install --save midori-hapi hapi
Create a hapi app and register your midori middleware as an extension:
import{Server}from'hapi';
importcreateExtfrom'midori-hapi';
constserver=newServer();
server.connection({port:8080});
server.ext(createExt(app));
server.start();
Migration & Middleware Compatibility
Coming from another framework? Prefer to write your middleware handlers like you do in those other frameworks? Not a problem.
Express
The traditional callback style that express uses is compatible with midori. You can connect your express middleware as follows: