2018-06-18 05:39:42 +00:00
|
|
|
// This file implements methods to get and post JSON in a simple manner.
|
|
|
|
|
|
|
|
import * as interfaces from './smartrequest.interfaces';
|
|
|
|
import { request } from './smartrequest.request';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gets Json and puts the right headers + handles response aggregation
|
|
|
|
* @param domainArg
|
2018-07-15 21:21:07 +00:00
|
|
|
* @param optionsArg
|
2018-06-18 05:39:42 +00:00
|
|
|
*/
|
2018-07-15 21:21:07 +00:00
|
|
|
export const getJson = async (
|
|
|
|
domainArg: string,
|
|
|
|
optionsArg: interfaces.ISmartRequestOptions = {}
|
|
|
|
) => {
|
2018-06-18 05:39:42 +00:00
|
|
|
optionsArg.method = 'GET';
|
|
|
|
optionsArg.headers = {
|
2018-07-15 21:21:07 +00:00
|
|
|
...optionsArg.headers
|
|
|
|
};
|
2018-06-18 05:39:42 +00:00
|
|
|
let response = await request(domainArg, optionsArg);
|
|
|
|
return response;
|
|
|
|
};
|
|
|
|
|
2018-07-15 21:21:07 +00:00
|
|
|
export const postJson = async (
|
|
|
|
domainArg: string,
|
|
|
|
optionsArg: interfaces.ISmartRequestOptions = {}
|
|
|
|
) => {
|
2018-06-18 05:39:42 +00:00
|
|
|
optionsArg.method = 'POST';
|
|
|
|
if (
|
|
|
|
typeof optionsArg.requestBody === 'object' &&
|
|
|
|
(!optionsArg.headers || !optionsArg.headers['Content-Type'])
|
|
|
|
) {
|
|
|
|
// make sure headers exist
|
|
|
|
if (!optionsArg.headers) {
|
|
|
|
optionsArg.headers = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// assign the right Content-Type, leaving all other headers in place
|
|
|
|
optionsArg.headers = {
|
|
|
|
...optionsArg.headers,
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
let response = await request(domainArg, optionsArg);
|
|
|
|
return response;
|
|
|
|
};
|
|
|
|
|
2018-07-15 21:21:07 +00:00
|
|
|
export const putJson = async (
|
|
|
|
domainArg: string,
|
|
|
|
optionsArg: interfaces.ISmartRequestOptions = {}
|
|
|
|
) => {
|
2018-06-18 05:39:42 +00:00
|
|
|
optionsArg.method = 'PUT';
|
|
|
|
let response = await request(domainArg, optionsArg);
|
|
|
|
return response;
|
|
|
|
};
|
|
|
|
|
2018-07-15 21:21:07 +00:00
|
|
|
export const delJson = async (
|
|
|
|
domainArg: string,
|
|
|
|
optionsArg: interfaces.ISmartRequestOptions = {}
|
|
|
|
) => {
|
2018-06-18 05:39:42 +00:00
|
|
|
optionsArg.method = 'DELETE';
|
|
|
|
let response = await request(domainArg, optionsArg);
|
|
|
|
return response;
|
2018-07-15 21:21:07 +00:00
|
|
|
};
|