1 | import request, {Options, FormData} from 'then-request';
|
2 | import {Req, Res} from './messages';
|
3 |
|
4 | function init() {
|
5 | return (req: Req): Promise<Res> => {
|
6 |
|
7 | const {form, ...o} = req.o || {form: undefined};
|
8 | const opts: Options = o;
|
9 | if (form) {
|
10 | const fd = new FormData();
|
11 | form.forEach(entry => {
|
12 | fd.append(entry.key, entry.value, entry.fileName);
|
13 | });
|
14 | opts.form = fd;
|
15 | }
|
16 | return request(req.m, req.u, opts).then(response => ({
|
17 | s: response.statusCode,
|
18 | h: response.headers,
|
19 | b: response.body,
|
20 | u: response.url,
|
21 | }));
|
22 | };
|
23 | }
|
24 | module.exports = init;
|