-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
478 lines (440 loc) · 14.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
//. # Fluture Express
//.
//. [![NPM Version](https://badge.fury.io/js/fluture-express.svg)](https://www.npmjs.com/package/fluture-express)
//. [![Dependencies](https://david-dm.org/fluture-js/fluture-express.svg)](https://david-dm.org/fluture-js/fluture-express)
//. [![Code Coverage](https://codecov.io/gh/fluture-js/fluture-express/branch/master/graph/badge.svg)](https://codecov.io/gh/fluture-js/fluture-express)
//.
//. Create Express middleware using Futures from [Fluture][].
//.
//. Allows for the definition of pure functions to be used as Express
//. middleware. This has benefits for testing and developer sanity.
//. Another benefit of this particular approach, where every middleware is
//. wrapped individually, is that it plays nicely with existing Express
//. middleware, and they can be used interchangably.
//.
//. ## Usage
//.
//. ### Node
//.
//. ```console
//. $ npm install --save fluture-express
//. ```
//.
//. On Node 12 and up, this module can be loaded directly with `import` or
//. `require`. On Node versions below 12, `require` or the [esm][]-loader can
//. be used.
//.
//. ### Deno and Modern Browsers
//.
//. You can load the EcmaScript module from various content delivery networks:
//.
//. - [Skypack](https://cdn.skypack.dev/[email protected])
//. - [JSPM](https://jspm.dev/[email protected])
//. - [jsDelivr](https://cdn.jsdelivr.net/npm/[email protected]/+esm)
//.
//. ### Usage Example
//.
//. ```js
//. // index.js
//.
//. const {dispatcher} = require ('fluture-express');
//. const app = require ('express') ();
//. const dispatch = dispatcher ('./actions');
//.
//. app.use (dispatch ('welcome'));
//. app.listen (3000);
//. ```
//.
//. ```js
//. // actions/welcome.js
//.
//. const {Json} = require ('fluture-express');
//. const Future = require ('fluture');
//.
//. module.exports = locals => req => Future.go (function* () {
//. const user = yield locals.database.find ('sessions', locals.session.id);
//. return withStatus (418) (Json ({welcome: user.name}));
//. });
//. ```
//.
//. For a more in-depth example, see the `example` directory.
//.
//. ## Documentation
//.
//. ### Pseudo types
//.
//. #### `Req`
//.
//. The Express Request object.
//.
//. #### `Res a`
//.
//. The Express Response object with a `locals` property of type `a`.
import daggy from 'daggy';
import {fork, isFuture} from 'fluture';
import path from 'path';
import Z from 'sanctuary-type-classes';
const requireOrImport = file => (
/* c8 ignore next */
typeof require === 'function' ?
Promise.resolve (require (file)) :
import (file).then (module => module.default)
);
const cata = cases => catamorphic => catamorphic.cata (cases);
const cataWithDefault = def => pattern => catamorphic => {
const tags = catamorphic.constructor['@@tags'];
const defaultPattern = (
Object.fromEntries (tags.map (tag => [tag, _ => def]))
);
return catamorphic.cata ({
...defaultPattern,
...pattern,
});
};
const cataBool = cataWithDefault (false);
const deriveEq = type => {
const tags = type['@@tags'];
type.prototype['fantasy-land/equals'] = function FL$equals(other) {
const pattern = Object.fromEntries (tags.map (tag => [
tag,
(...xs) => cataBool ({[tag]: (...ys) => Z.equals (xs, ys)}),
]));
return this.cata (pattern) (other);
};
};
const runAction = (name, action, req, res, next) => {
const ret = action (res.locals) (req);
if (!isFuture (ret)) {
throw new TypeError (
`The "${name}" action did not return a Future, instead saw:\n\n ${ret}`
);
}
fork (next) (val => {
if (!Response.is (val)) {
throw new TypeError (`The Future returned by the "${
name
}" action did not resolve to a Response, instead saw:\n\n ${val}`);
}
val.cata ({
Respond: (head, body) => {
head.forEach (it => it.cata ({
Status: code => res.status (code),
Type: type => res.type (type),
Location: url => res.location (url),
Links: links => res.links (links),
Cookie: (key, value, options) => res.cookie (key, value, options),
ClearCookie: (key, options) => res.clearCookie (key, options),
HeaderPart: (key, value) => res.append (key, value),
Header: (key, value) => res.set (key, value),
}));
body.cata ({
None: _ => res.end (),
Send: data => res.send (data),
Json: data => res.json (data),
Stream: fork (next) (it => it.pipe (res)),
Render: (template, data) => res.render (template, data),
});
},
Next: locals => {
res.locals = locals;
next ();
},
});
}) (ret);
};
//. ### The Response type
//.
//. Fluture-Express mutates the response object for you, based on a
//. specification of what the response should be. This specification is
//. captured by the Response sum-type.
//.
//# Response :: Type
//.
//. The Response sum type encoded with [daggy][]. You probably don't need to
//. use this directly.
//.
//. ```hs
//. data Response a b = Respond (Array Head) (Body a)
//. | Next b
//. ```
export const Response = daggy.taggedSum ('Response', {
Respond: ['head', 'body'],
Next: ['locals'],
});
deriveEq (Response);
//# Head :: Type
//.
//. The Head sum type encoded with [daggy][]. You probably don't need to
//. use this directly.
//.
//. ```hs
//. data Head = Status Number
//. | Type String
//. | Location String
//. | Links (StrMap String)
//. | Cookie String String Object
//. | ClearCookie String Object
//. | HeaderPart String String
//. | Header String String
//. ```
export const Head = daggy.taggedSum ('Head', {
Status: ['code'],
Type: ['type'],
Location: ['url'],
Links: ['links'],
Cookie: ['key', 'value', 'options'],
ClearCookie: ['key', 'options'],
HeaderPart: ['key', 'value'],
Header: ['key', 'value'],
});
deriveEq (Head);
//# Body :: Type
//.
//. The Body sum type encoded with [daggy][]. You probably don't need to
//. use this directly.
//.
//. ```hs
//. data Body a = None
//. | Send Any
//. | Json JsonValue
//. | Stream (Future a Readable)
//. | Render String Object
//. ```
export const Body = daggy.taggedSum ('Body', {
None: [],
Send: ['data'],
Json: ['data'],
Stream: ['stream'],
Render: ['template', 'data'],
});
Body.prototype['fantasy-land/equals'] = function Body$FL$equals(other) {
return this.cata ({
None: _ => cataBool ({None: _ => true}),
Send: a => cataBool ({Send: b => Z.equals (a, b)}),
Json: a => cataBool ({Json: b => Z.equals (a, b)}),
Stream: a => cataBool ({Stream: b => a === b}),
Render: (...a) => cataBool ({Render: (...b) => Z.equals (a, b)}),
}) (other);
};
//# Stream -> Future a Readable -> Response a b
//.
//. Creates a streamed response given a mime type and a Future that produces
//. a Readable Stream when consumed. The Future is expected to produce a new
//. Stream every time it's consumed, or if it can't, reject with a value that
//. your Express error handler can handle.
//.
//. Uses a Content-Type of `application/octet-stream` unless overridden by
//. [`withType`](#withType), [`withHeader`](#withHeader),
//. or [`withoutHeader`](#withoutHeader).
export const Stream = stream => Response.Respond (
[Head.Type ('application/octet-stream')],
Body.Stream (stream)
);
//# Text :: String -> Response a b
//.
//. Indicates a textual response.
//.
//. Uses a Content-Type of `text/plain` unless overridden by
//. [`withType`](#withType), [`withHeader`](#withHeader),
//. or [`withoutHeader`](#withoutHeader).
export const Text = value => Response.Respond (
[Head.Type ('text/plain')],
Body.Send (value),
);
//# Json :: JsonValue -> Response a b
//.
//. Indicates a JSON response.
//.
//. Uses a Content-Type of `application/json` unless overridden by
//. [`withType`](#withType), [`withHeader`](#withHeader).
export const Json = value => Response.Respond (
[],
Body.Json (value),
);
//# Render :: String -> Object -> Response a b
//.
//. Indicates a response to be rendered using a template. The first argument
//. is the path to the template file, and the second is the data to inject into
//. the template. This uses Express' render method under the hood, so you can
//. configure it globally with `app.set ('view engine', engine)` and
//. `app.set ('views', path)`.
export const Render = view => data => Response.Respond (
[],
Body.Render (view, data)
);
//# Redirect :: String -> Response a b
//.
//. Indicates a redirection. The first argument will be the response status
//. code, and the second will be the value of the Location header.
//.
//. Unless overridden by [`withStatus`](#withStatus), the status code will be
//. set to 301 (Moved Permanently).
export const Redirect = location => Response.Respond (
[Head.Status (301), Head.Location (location)],
Body.None
);
//# Empty :: Response a b
//.
//. Indicates an empty response. The response status will be set to 204, and
//. no response body or Content-Type header will be sent.
export const Empty = Response.Respond (
[Head.Status (204)],
Body.None,
);
//# Next :: b -> Response a b
//.
//. Indicates that this middleware does not form a response. The supplied value
//. will be assigned to `res.locals` and the next middleware will be called.
export const Next = Response.Next;
//# withStatus :: Number -> Response a b -> Response a b
//.
//. Configure the status code by setting up a call to [`res.status`][].
export const withStatus = code => cata ({
Respond: (head, body) => Response.Respond (
[...head, Head.Status (code)],
body,
),
Next: Response.Next,
});
//# withType :: String -> Response a b -> Response a b
//.
//. Configure the Content-Type by setting up a call to [`res.type`][].
export const withType = type => cata ({
Respond: (head, body) => Response.Respond (
[...head, Head.Type (type)],
body,
),
Next: Response.Next,
});
//# withLocation :: String -> Response a b -> Response a b
//.
//. Configure the Location header by setting up a call to [`res.location`][].
export const withLocation = url => cata ({
Respond: (head, body) => Response.Respond (
[...head, Head.Location (url)],
body,
),
Next: Response.Next,
});
//# withLinks :: StrMap String -> Response a b -> Response a b
//.
//. Configure the Link header by setting up a call to [`res.links`][].
export const withLinks = links => cata ({
Respond: (head, body) => Response.Respond (
[...head, Head.Links (links)],
body,
),
Next: Response.Next,
});
//# withCookie :: CookieOptions -> String -> String -> Response a b -> Response a b
//.
//. Configure the Set-Cookie header by setting up a call to [`res.cookie`][].
export const withCookie = options => key => value => cata ({
Respond: (head, body) => Response.Respond (
[...head, Head.Cookie (key, value, options)],
body,
),
Next: Response.Next,
});
//# withClearCookie :: CookieOptions -> String -> Response a b -> Response a b
//.
//. Configure the Set-Cookie header by setting up a call to
//. [`res.clearCookie`][].
export const withClearCookie = options => key => cata ({
Respond: (head, body) => Response.Respond (
[...head, Head.ClearCookie (key, options)],
body,
),
Next: Response.Next,
});
//# withHeaderPart :: String -> String -> Response a b -> Response a b
//.
//. Append to a header by setting up a call to [`res.append`][].
export const withHeaderPart = key => value => cata ({
Respond: (head, body) => Response.Respond (
[...head, Head.HeaderPart (key, value)],
body,
),
Next: Response.Next,
});
//# withHeader :: String -> String -> Response a b -> Response a b
//.
//. Configure a header by setting up a call to [`res.set`][].
export const withHeader = key => value => cata ({
Respond: (head, body) => Response.Respond (
[...head, Head.Header (key, value)],
body,
),
Next: Response.Next,
});
//# withoutHeader :: String -> Response a b -> Response a b
//.
//. Removes a header from the Response. Also removes headers that would be
//. set by functions like [`withType`](#withType). For example:
//.
//. ```js
//. > withoutHeader ('Content-Type') (withType ('json') (Empty))
//. Empty
//. ```
export const withoutHeader = header => cata ({
Respond: (head, body) => Response.Respond (head.filter (cata ({
Status: _ => true,
Type: _ => header.toLowerCase () !== 'content-type',
Location: _ => header.toLowerCase () !== 'location',
Links: _ => header.toLowerCase () !== 'link',
Cookie: _ => header.toLowerCase () !== 'set-cookie',
ClearCookie: _ => header.toLowerCase () !== 'set-cookie',
HeaderPart: key => key.toLowerCase () !== header.toLowerCase (),
Header: key => key.toLowerCase () !== header.toLowerCase (),
})), body),
Next: Response.Next,
});
//. ### Middleware creation utilities
//.
//# middleware :: (b -> Req -> Future a (Response a c)) -> (Req, Res b, (a -> Undefined)) -> Undefined
//.
//. Converts an action to an Express middleware.
//.
//. Takes a function that returns a [Future][] of a [Response][], and returns
//. an Express middleware that uses the returned structure to make the
//. appropriate mutations to the [`res`][].
//.
//. If the Future rejects, the rejection reason is passed into `next` for
//. further [error handling with Express][].
export const middleware = action => function dispatcher(req, res, next) {
runAction (action.name || 'anonymous', action, req, res, next);
};
//# dispatcher :: String -> String -> (Req, Res a, (Any -> Undefined)) -> Promise Undefined
//.
//. Creates middleware that uses the export from the given file in the given
//. directory as an "action".
//.
//. It takes the file in two steps for convenience. You are encouraged to use
//. the first parameter to set up a sub-directory where all your actions live.
//.
//. The exported value should be a function of the same signature as given to
//. [`middleware`][].
export const dispatcher = directory => file => {
const eventualAction = requireOrImport (path.resolve (directory, file));
return function dispatcher(req, res, next) {
return eventualAction.then (action => {
runAction (file, action, req, res, next);
});
};
};
//. [Fluture]: https://github.com/fluture-js/Fluture
//. [Future]: https://github.com/fluture-js/Fluture#future
//. [Response]: #the-response-type
//. [`middleware`]: #middleware
//. [`res`]: #res-a
//. [error handling with Express]: https://expressjs.com/en/guide/error-handling.html
//. [daggy]: https://github.com/fantasyland/daggy
//. [esm]: https://github.com/standard-things/esm
//. [`res.status`]: https://expressjs.com/en/4x/api.html#res.status
//. [`res.type`]: https://expressjs.com/en/4x/api.html#res.type
//. [`res.location`]: https://expressjs.com/en/4x/api.html#res.location
//. [`res.links`]: https://expressjs.com/en/4x/api.html#res.links
//. [`res.cookie`]: https://expressjs.com/en/4x/api.html#res.cookie
//. [`res.clearCookie`]: https://expressjs.com/en/4x/api.html#res.clearCookie
//. [`res.append`]: https://expressjs.com/en/4x/api.html#res.append
//. [`res.set`]: https://expressjs.com/en/4x/api.html#res.set