-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi.jsx
More file actions
47 lines (39 loc) · 1.26 KB
/
api.jsx
File metadata and controls
47 lines (39 loc) · 1.26 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
47
import { Component } from 'react';
class Api extends Component {
static getQuerySperator = "/"; //Set Default Sperator ? or / etc
static baseAddress = window.location.href.indexOf("localhost") > -1 ?
"http://localhost:60001/StoreFront/" : "https://api.2paws.xyz/StoreFront/"; //Default API address
constructor() {
super();
}
/*
Get with params
*/
static async get(endpoint, params) {
var paramsString = "";
if (params) {
params.forEach(element => {
paramsString += this.getQuerySperator + element;
});
}
console.log(this.baseAddress + endpoint + paramsString);
let response = await fetch(this.baseAddress + endpoint + paramsString);
return await response.json();
}
/*
Post with body
Note : Content Type is set to JSON (Change if required)
*/
static async post(endpoint, _body) {
console.log(_body);
let response = await fetch(this.baseAddress + endpoint, {
method: 'post',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(_body)
});
return await response.json();
}
}
export default Api;