-
Notifications
You must be signed in to change notification settings - Fork 4
/
compose.js
142 lines (102 loc) · 3 KB
/
compose.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
var ctor = () => (...fns) => (args) =>
fns.reduce((p, f) => p.then(f), Promise.resolve(args))
var compose = ctor()
var Request = {
defaults: require('./request/defaults'),
url: require('./request/url'),
proxy: require('./request/proxy'),
qs: require('./request/qs'),
form: require('./request/form'),
json: require('./request/json'),
body: require('./request/body'),
auth: require('./request/auth'),
length: require('./request/length'),
send: require('./request/send'),
}
var Response = {
buffer: require('./response/buffer'),
gzip: require('./response/gzip'),
string: require('./response/string'),
parse: require('./response/parse'),
status: require('./response/status'),
redirect: require('./response/redirect'),
}
var request = (Request) => (args) => compose(
Request.defaults(args),
(() =>
args.url ? Request.url(args.url) : ({options}) => ({options})
)(),
(() =>
args.proxy ? Request.proxy(args.proxy) : ({options}) => ({options})
)(),
(() =>
args.qs ? Request.qs(args.qs, args.redirect) : ({options}) => ({options})
)(),
(() =>
args.cookie ? Request.cookie(args.cookie) : ({options}) => ({options})
)(),
(() =>
args.form ? Request.form(args.form) :
args.json ? Request.json(args.json) :
args.multipart ? Request.multipart(args.multipart) :
args.body ? Request.body(args.body) :
({options}) => ({options})
)(),
(() =>
args.auth ? Request.auth(args.auth) :
args.oauth ? Request.oauth(args.oauth) :
({options, body}) => ({options, body})
)(),
(() => ({options, body}) =>
body ? Request.length()({options, body}) : {options}
)(),
Request.send(),
)()
var client = (Request, Response) => (args) => compose(
_ => request(Request)(args),
(() =>
args.cookie ? Response.cookie(args.cookie) :
({options, res}) => ({options, res})
)(),
Response.buffer(),
Response.gzip(),
Response.string(args.encoding),
Response.parse(),
Response.status(),
Response.redirect(args, client(Request, Response)),
)()
var buffer = (Request, Response) => (args) => compose(
_ => request(Request)(args),
(() =>
args.cookie ? Response.cookie(args.cookie) :
({options, res}) => ({options, res})
)(),
Response.buffer(),
Response.gzip(),
Response.status(),
Response.redirect(args, buffer(Request, Response)),
)()
var stream = (Request, Response) => (args) => compose(
_ => request(Request)(args),
(() =>
args.cookie ? Response.cookie(args.cookie) :
({options, res}) => ({options, res})
)(),
Response.status(),
// TODO: should buffer the read chunks and re-write them
// Response.redirect(args, stream),
)()
var extend = (mw) => ((
req = Object.assign({}, Request, mw.Request),
res = Object.assign({}, Response, mw.Response),
) =>
Object.assign(ctor(), {
Request: req,
Response: res,
client: client(req, res),
buffer: buffer(req, res),
stream: stream(req, res),
extend,
})
)()
module.exports = extend({Request, Response})