-
Notifications
You must be signed in to change notification settings - Fork 231
/
Copy pathhttp.js
125 lines (118 loc) · 3.41 KB
/
http.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
/**
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import Api from './api';
import HTTP_STATUS from './http-status';
const requestPromise = require('request-promise');
/**
* Isomorphic Http Promise Requests Class
* @flow
*/
export default class Http {
/**
* Request
* @param {String} method
* @param {String} url
* @param {Object} [data]
* @return {Promise}
*/
static request (
method: string,
url: string,
data: Object,
files: Object,
useMultipartFormData: Boolean
): Promise<*> {
if (typeof window !== 'undefined' && window.XMLHttpRequest) {
return Http.xmlHttpRequest(method, url, data);
}
return Http.requestPromise(method, url, data, files, useMultipartFormData);
}
/**
* XmlHttpRequest request
* @param {String} method
* @param {String} url
* @param {Object} [data]
* @return {Promise}
*/
static xmlHttpRequest (method, url, data): Promise<*> {
return new Promise((resolve, reject) => {
const request = new window.XMLHttpRequest();
request.open(method, url);
request.onload = function () {
try {
const response = JSON.parse(request.response);
if (request.status.toString() === HTTP_STATUS.OK) {
resolve(response);
} else {
reject(
{
body: response,
status: request.status
}
);
}
} catch (e) {
reject(
{
body: request.responseText,
status: request.status
}
);
}
};
request.setRequestHeader('Content-Type', 'application/json');
request.setRequestHeader('Accept', 'application/json');
request.send(JSON.stringify(data));
});
}
/**
* Request Promise
* @param {String} method The HTTP method name (e.g. 'GET').
* @param {String} url A full URL string.
* @param {Object} [data] A mapping of request parameters where a key
* is the parameter name and its value is a string or an object
* which can be JSON-encoded.
* @param {Object} [files] An optional mapping of file names to ReadStream
* objects. These files will be attached to the request.
* @param {Boolean} [useMultipartFormData] An optional flag to call with
* multipart/form-data.
* @return {Promise}
*/
static requestPromise (
method: string,
url: string,
data: Object,
files: Object,
useMultipartFormData: Boolean = false
): Promise<*> {
const options = {
method: method,
uri: url,
json: !useMultipartFormData,
headers: {'User-Agent': `fbbizsdk-nodejs-${Api.VERSION}`},
body: Object
};
// Prevent null or undefined input
// because it can be merged with the files argument later
if (!data) {
data = {};
}
options.body = data;
// Handle file attachments if provided
if (useMultipartFormData || (files && Object.keys(files).length > 0)) {
// Use formData instead of body (required by the request-promise library)
options.formData = Object.assign(data, files);
delete options.body;
}
return requestPromise(options).catch((response: Object) => {
throw response;
});
}
}