UNPKG

706 BPlain TextView Raw
1import request, {Options, FormData} from 'then-request';
2import {Req, Res} from './messages';
3
4function init() {
5 return (req: Req): Promise<Res> => {
6 // Note how even though we return a promise, the resulting rpc client will be synchronous
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}
24module.exports = init;