-
Notifications
You must be signed in to change notification settings - Fork 231
/
Copy pathhttp.js
140 lines (132 loc) · 3.96 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* 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,
showHeader: Boolean,
): Promise<*> {
if (typeof window !== 'undefined' && window.XMLHttpRequest) {
return Http.xmlHttpRequest(method, url, data);
}
return Http.requestPromise(method, url, data, files, useMultipartFormData, showHeader);
}
/**
* 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 () {
let response;
try {
response = JSON.parse(request.response);
} catch (e) {
// JSON failed to parse. Create a placeholder response.
response = {
error: {
message: 'Failed to parse response JSON.',
}
};
reject(convertXhrErrorToRequestPromiseError(request, response));
return;
}
if (request.status.toString() !== HTTP_STATUS.OK) {
reject(convertXhrErrorToRequestPromiseError(request, response));
return;
}
resolve(response);
};
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,
showHeader: Boolean = false,
): Promise<*> {
const options = {
method: method,
uri: url,
json: !useMultipartFormData,
headers: {'User-Agent': `fbbizsdk-nodejs-${Api.VERSION}`},
body: Object,
resolveWithFullResponse: showHeader,
};
// 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;
});
}
}
/**
* Converts the given XHR error to an error that looks like one that would
* be returned by the request-promise API.
* @param {XMLHttpRequest} request
* @param {any} response
*/
function convertXhrErrorToRequestPromiseError(request, response) {
return {
name: 'StatusCodeError',
error: response,
statusCode: request.status,
};
}