This repository has been archived by the owner on Jun 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
RequestHandler.js
68 lines (59 loc) · 2.04 KB
/
RequestHandler.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
/*
* Created by Cosmo Cochrane on 4/20/17.
* Copyright (c) 2017 Giphy Inc.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
var ResponseHandler = require('./ResponseHandler');
require('es6-promise').polyfill();
require('isomorphic-fetch');
function RequestHandler(vals, endpoint, cb) {
function req(args, cb) {
var canceled = false;
var query_params_string = "?";
Object.keys(args.params).forEach((key) => {
query_params_string = query_params_string.concat(key + "=" + args.params[key] + "&");
});
var constructedRequest = new Promise((resolve, reject) => {
if (vals.type && vals.type !== 'gifs' && vals.type !== 'stickers') {
reject("The type argument was passed in incorrectly. It should be either 'gifs' or 'stickers'")
}
fetch(args.url + query_params_string, {
method: args.method
}).then((response) => { //calling the end function will send the actual request
if (canceled === true) {
return
} else {
response.json().then((data) => {
ResponseHandler(response, data,
(res) => {
resolve(res);
if (cb !== undefined) {
cb(res, null);
}
},
(err) => {
reject(err);
if (cb !== undefined) {
cb(null, err);
}
}, endpoint) //pass in args.url so you can determine before resolving the promise what request was just made
// we pass the response to our helper method imported from ./helpers/
})
}
}).catch((err) => {
reject(err)
})
})
//allows users to cancel outgoing requests
constructedRequest.cancel = function() {
canceled = true;
}
return constructedRequest
}
return req(vals, cb);
//return the promise
}
module.exports = RequestHandler