-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
46 lines (33 loc) · 1.11 KB
/
index.js
File metadata and controls
46 lines (33 loc) · 1.11 KB
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
/**
* @flow
*/
import { NativeModules } from 'react-native';
const { HTTPModule: HTTPModuleCore } = NativeModules;
const isURL = (url: String) => url.toLowerCase().includes('http');
class HTTPModule {
baseURL = '';
headers = null;
constructor(baseURL: string, headers: Object = {}) {
this.baseURL = baseURL;
this.headers = headers;
}
getURL = (url: String) => {
let URL = url;
if (!isURL(URL)) {
URL = this.baseURL + url;
}
return URL;
}
getHeaders = (headers: Object) => {
let HEADERS = headers;
if (!HEADERS) {
HEADERS = this.headers || {};
}
return JSON.stringify(HEADERS);
}
getBody = (body: Object) => JSON.stringify(body || {})
get = (url:String, headers?:Object = {}) => this.request(url, 'get', headers, JSON.stringify({}));
post = (url: String, headers: Object = {}, body: Object = {}) => this.request(url, 'post', headers, body);
request = (url: String, method: string, headers: Object = {}, body: Object = {}) => HTTPModuleCore.request(this.getURL(url), method, this.getHeaders(headers), this.getBody(body))
}
export default HTTPModule;