smartrequest/ts/smartrequest.jsonrest.ts

64 lines
1.6 KiB
TypeScript
Raw Normal View History

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
* @param optionsArg
2018-06-18 05:39:42 +00:00
*/
export const getJson = async (
domainArg: string,
optionsArg: interfaces.ISmartRequestOptions = {}
) => {
2018-06-18 05:39:42 +00:00
optionsArg.method = 'GET';
optionsArg.headers = {
...optionsArg.headers
};
2018-06-18 05:39:42 +00:00
let response = await request(domainArg, optionsArg);
return response;
};
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;
};
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;
};
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;
};