smartrequest/ts/index.ts

48 lines
1.5 KiB
TypeScript
Raw Normal View History

2018-06-13 20:34:49 +00:00
import * as https from 'https';
2017-01-28 23:51:47 +00:00
2018-06-13 20:34:49 +00:00
import * as plugins from './smartrequest.plugins';
import * as interfaces from './smartrequest.interfaces';
2017-01-28 23:51:47 +00:00
2018-06-13 20:34:49 +00:00
import { request } from './smartrequest.request';
2017-01-28 23:51:47 +00:00
2018-06-13 20:34:49 +00:00
export { request } from './smartrequest.request';
export { ISmartRequestOptions } from './smartrequest.interfaces';
2017-01-28 23:51:47 +00:00
2017-01-29 00:12:42 +00:00
export let get = async (domainArg: string, optionsArg: interfaces.ISmartRequestOptions = {}) => {
2018-06-13 20:34:49 +00:00
optionsArg.method = 'GET';
let response = await request(domainArg, optionsArg);
return response;
};
2017-01-28 23:51:47 +00:00
2017-01-29 00:12:42 +00:00
export let post = async (domainArg: string, optionsArg: interfaces.ISmartRequestOptions = {}) => {
2018-06-13 20:34:49 +00:00
optionsArg.method = 'POST';
if (
2018-06-13 20:34:49 +00:00
typeof optionsArg.requestBody === 'object' &&
(!optionsArg.headers || !optionsArg.headers['Content-Type'])
) {
// make sure headers exist
2018-06-13 20:34:49 +00:00
if (!optionsArg.headers) {
optionsArg.headers = {};
}
// assign the right Content-Type, leaving all other headers in place
Object.assign(optionsArg.headers, {
'Content-Type': 'application/json'
2018-06-13 20:34:49 +00:00
});
}
2018-06-13 20:34:49 +00:00
let response = await request(domainArg, optionsArg);
return response;
};
2017-01-28 23:51:47 +00:00
2017-01-29 00:12:42 +00:00
export let put = async (domainArg: string, optionsArg: interfaces.ISmartRequestOptions = {}) => {
2018-06-13 20:34:49 +00:00
optionsArg.method = 'PUT';
let response = await request(domainArg, optionsArg);
return response;
};
2017-01-28 23:51:47 +00:00
2017-01-29 00:12:42 +00:00
export let del = async (domainArg: string, optionsArg: interfaces.ISmartRequestOptions = {}) => {
2018-06-13 20:34:49 +00:00
optionsArg.method = 'DELETE';
let response = await request(domainArg, optionsArg);
return response;
};