1
1
# momi
2
2
3
- [ ![ Greenkeeper badge] ( https://badges.greenkeeper.io/fluture-js/momi.svg )] ( https://greenkeeper.io/ )
4
-
5
3
** Mo** nadic ** Mi** ddleware
6
4
7
5
> ` npm install --save monastic fluture momi `
@@ -11,11 +9,13 @@ mechanism which encodes several effects:
11
9
12
10
- ** A build-up of state** through mutation of the ` req ` parameter
13
11
- ** An eventual response** through mutation of the ` res ` parameter
14
- - ** Inversion of control** over the continuation by means of calling the ` next ` parameter
15
- - ** Possible error branch** by means of calling the ` next ` parameter with a value
12
+ - ** Inversion of control** over the continuation by means of calling the
13
+ ` next ` parameter
14
+ - ** Possible error branch** by means of calling the ` next ` parameter with
15
+ a value
16
16
17
- If we would want to encode all of these effects into a data-structure, we could
18
- use a ` StateT(Future) -> StateT(Future) ` structure:
17
+ If we would want to encode all of these effects into a data-structure, we
18
+ could use a ` StateT(Future) -> StateT(Future) ` structure:
19
19
20
20
- ** A build-up of state** through the ` State ` monad
21
21
- ** An eventual response** through the right-sided value of ` Future `
@@ -27,49 +27,80 @@ Middleware monad. This packages exposes the Middleware monad, comprised of
27
27
` State ` from [ monastic] [ ] and ` Future ` from [ Fluture] [ ] . Besides the
28
28
monad itself, it also exposes some utility functions and structures for
29
29
practically applying Middleware. One such utility is the ` App ` class,
30
- which allows composition of functions over Middleware to be written more like
31
- what you are used to from middleware as it comes with Connect, Express or Koa.
30
+ which allows composition of functions over Middleware to be written more
31
+ like what you are used to from middleware as it comes with Express or Koa.
32
32
33
33
## Usage
34
34
35
+ ### Node
36
+
37
+ ``` console
38
+ $ npm install --save momi
39
+ ```
40
+
41
+ On Node 12 and up, this module can be loaded directly with ` import ` or
42
+ ` require ` . On Node versions below 12, ` require ` or the [ esm] [ ] -loader can
43
+ be used.
44
+
45
+ ### Deno and Modern Browsers
46
+
47
+ You can load the EcmaScript module from various content delivery networks:
48
+
49
+ - [ Skypack
] ( https://cdn.skypack.dev/[email protected] )
50
+ - [ JSPM
] ( https://jspm.dev/[email protected] )
51
+ - [ jsDelivr
] ( https://cdn.jsdelivr.net/npm/[email protected] /+esm )
52
+
53
+ ### Old Browsers and Code Pens
54
+
55
+ There's a [ UMD] [ ] file included in the NPM package, also available via
56
+ jsDelivr:
https://cdn.jsdelivr.net/npm/[email protected] /dist/umd.js
57
+
58
+ This file adds ` momi ` to the global scope, or use CommonJS/AMD
59
+ when available.
60
+
61
+ ## Usage Example
62
+
35
63
``` js
36
64
import Z from ' sanctuary-type-classes' ;
37
65
import qs from ' querystring' ;
66
+ import http from ' http' ;
38
67
39
68
import {compose , constant } from ' monastic' ;
40
69
import {go , mount , get , put } from ' momi' ;
41
70
42
- const queryParseMiddleware = go (function * (next ) {
71
+ const queryParseMiddleware = go (function * (next ) {
43
72
const req = yield get;
44
- const query = qs .parse (req .url .split (' ?' )[1 ]);
45
- yield put (Object .assign ({query}, req));
73
+ const query = qs .parse (req .url .split (' ?' )[1 ]);
74
+ yield put (Object .assign ({query}, req));
46
75
return yield next;
47
76
});
48
77
49
- const echoMiddleware = Z .map (req => ({
78
+ const echoMiddleware = Z .map (req => ({
50
79
status: 200 ,
51
80
headers: {' X-Powered-By' : ' momi' },
52
- body: req .query .echo
81
+ body: req .query .echo ,
53
82
}), get);
54
83
55
- const app = compose (
84
+ const app = compose (
56
85
queryParseMiddleware,
57
- constant (echoMiddleware)
86
+ constant (echoMiddleware)
58
87
);
59
88
60
- mount ( app, 3000 );
89
+ mount (http, app, 3000 );
61
90
```
62
91
63
92
## Examples
64
93
65
94
- ** [ Readme] [ example-1 ] ** the code from [ Usage] ( #usage ) , ready to run.
66
95
- ** [ Express] [ example-2 ] ** shows how to embed Momi within Express.
67
- - ** [ Bootstrap] [ example-3 ] ** an extensive example showing application structure.
96
+ - ** [ Bootstrap] [ example-3 ] ** an example showing application structure.
68
97
- ** [ Real World] [ example-4 ] ** how momi is being used in real life.
69
98
70
- [ monastic ] : https://github.com/wearereasonablepeople /monastic
99
+ [ monastic ] : https://github.com/dicearr /monastic
71
100
[ Fluture ] : https://github.com/fluture-js/Fluture
72
101
[ example-1 ] : https://github.com/fluture-js/momi/tree/master/examples/readme
73
102
[ example-2 ] : https://github.com/fluture-js/momi/tree/master/examples/express
74
103
[ example-3 ] : https://github.com/fluture-js/momi/tree/master/examples/bootstrap
75
104
[ example-4 ] : https://github.com/Avaq/node-server-skeleton/tree/master/src/bootstrap
105
+ [ esm ] : https://github.com/standard-things/esm
106
+ [ UMD ] : https://github.com/umdjs/umd
0 commit comments