-
Notifications
You must be signed in to change notification settings - Fork 36
/
data-access-service.ts
64 lines (54 loc) · 1.91 KB
/
data-access-service.ts
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
import AjaxService from './ajax-service';
import { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios';
interface HttpError extends AxiosError, AxiosResponse {
message: string;
}
enum HttpMethod {
GET = 'GET',
POST = 'POST',
PUT = 'PUT',
DELETE = 'DELETE',
}
function deriveError(error: HttpError) {
if (!error) {
return new Error('An unknown error occurred');
} else if (!error.message) {
return `${error.status} ${error.statusText}`;
}
return error;
}
/**
* HTTP service capable of performing GET and POST requests
* This service will be injected into domain services (e.g. PatientService, MedicationService)
* Agnostic of prototype/production
*/
const request = (method: HttpMethod, url: string, data?: any, options?: AxiosRequestConfig) => {
const defaultOptions: AxiosRequestConfig = {
url: url,
method: method,
responseType: 'json',
};
if (data) {
defaultOptions.data = JSON.stringify(data);
defaultOptions.headers = {
'Content-Type': 'application/json',
};
}
let requestOptions = defaultOptions;
if (options) {
requestOptions = { ...defaultOptions, ...options };
}
// Resolve the original request, and wrap the response in another promise.
// This allows allows us to peer into the response before giving it back
// to the caller, which is helpful when handling situations where a response
// is technically successful from an AJAX perspective (200 OK), but failed
// server-side due an arbitrary error (i.e. validation error).
return new Promise((resolve, reject) => {
AjaxService.request(requestOptions)
.then(resolve)
.catch((error) => reject(deriveError(error)));
});
};
export const get = (url: string, options?: AxiosRequestConfig) => request(HttpMethod.GET, url, undefined, options);
export const post = (url: string, data?: any, options?: AxiosRequestConfig) =>
request(HttpMethod.POST, url, data, options);