service.js
2.82 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
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
// Import Axios HTTP Library
import axios from 'axios';
// Public class exported globally
class LemaApi {
constructor() {
this.helloWorldMethodUrl = "helloworld/hello";
this.productOptionsUrl = "helloworld/GetProductOptions";
this.jobsOptionsUrl = "helloworld/GetJobsOptions";
// Axios singleton instance
this._axios = axios.create({
baseURL: 'http://localhost:3001/api/',
timeout: 10000,
headers: {
'X-Custom-Header': 'foobar',
'Content-Type': 'application/json' // default
}
});
}
// Method 1
getHelloWorld(callback) {
this._axios.get(this.helloWorldMethodUrl)
.then(function (response) {
callback(response.data);
})
.catch(function (error) {
console.log(error);
});
};
// Lista dei prodotti
getProductOptions() {
return this._axios.get(this.productOptionsUrl)
.then(function (response) {
return {options: response.data}
})
.catch(function (error) {
console.log(error);
});
};
// Lista dei settori merceologici
getJobsOptions() {
return this._axios.get(this.jobsOptionsUrl)
.then(function (response) {
console.log(response.data);
return { options: response.data }
})
.catch(function (error) {
console.log(error);
});
};
// Using axios Library. https://www.npmjs.com/package/axios
/*axiosGet() {
this._axios.get('/user?ID=12345')
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
}
axiosGet2() {
// Optionally the request above could also be done as
this._axios.get('/user', {
params: {
ID: 12345
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
}*/
}
// Private module functions
function checkStatus(response) {
if (response.status >= 200 && response.status < 300) {
return response;
} else {
const error = new Error(`HTTP Error ${response.statusText}`);
error.status = response.statusText;
error.response = response;
console.log(error);
throw error;
}
}
function parseJSON(response) {
return response.json();
}
// Export singleton Service Class
export default new LemaApi();