@portal-hq/utils
Advanced tools
Comparing version 0.2.21 to 0.2.22-beta1
@@ -18,4 +18,9 @@ "use strict"; | ||
// Sends the request with a formatted request body | ||
const body = this.prepareBody(); | ||
this.request.send(body); | ||
if (['POST', 'PUT'].includes(this.method)) { | ||
const body = this.prepareBody(); | ||
this.request.send(body); | ||
} | ||
else { | ||
this.request.send(); | ||
} | ||
// Adds an event listener for when the request state changes | ||
@@ -57,2 +62,5 @@ this.request.onreadystatechange = () => { | ||
} | ||
else if (this.headers['Content-Type'] === 'text/plain') { | ||
return this.body; | ||
} | ||
else { | ||
@@ -59,0 +67,0 @@ // Otherwise, build a serialized string |
@@ -61,3 +61,18 @@ "use strict"; | ||
} | ||
put(path, options) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const requestOptions = { | ||
method: 'PUT', | ||
url: `${this.baseUrl}${path}`, | ||
}; | ||
requestOptions.headers = this.buildHeaders(options && options.headers ? options.headers : {}); | ||
if (options && options.body) { | ||
requestOptions.body = options.body; | ||
} | ||
const request = new index_1.HttpRequest(requestOptions); | ||
const response = (yield request.send()); | ||
return response; | ||
}); | ||
} | ||
} | ||
exports.default = HttpRequester; |
@@ -16,4 +16,9 @@ import { HttpError } from '../errors'; | ||
// Sends the request with a formatted request body | ||
const body = this.prepareBody(); | ||
this.request.send(body); | ||
if (['POST', 'PUT'].includes(this.method)) { | ||
const body = this.prepareBody(); | ||
this.request.send(body); | ||
} | ||
else { | ||
this.request.send(); | ||
} | ||
// Adds an event listener for when the request state changes | ||
@@ -55,2 +60,5 @@ this.request.onreadystatechange = () => { | ||
} | ||
else if (this.headers['Content-Type'] === 'text/plain') { | ||
return this.body; | ||
} | ||
else { | ||
@@ -57,0 +65,0 @@ // Otherwise, build a serialized string |
@@ -59,3 +59,18 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
} | ||
put(path, options) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const requestOptions = { | ||
method: 'PUT', | ||
url: `${this.baseUrl}${path}`, | ||
}; | ||
requestOptions.headers = this.buildHeaders(options && options.headers ? options.headers : {}); | ||
if (options && options.body) { | ||
requestOptions.body = options.body; | ||
} | ||
const request = new HttpRequest(requestOptions); | ||
const response = (yield request.send()); | ||
return response; | ||
}); | ||
} | ||
} | ||
export default HttpRequester; |
{ | ||
"name": "@portal-hq/utils", | ||
"version": "0.2.21", | ||
"version": "0.2.22-beta1", | ||
"main": "lib/commonjs/index", | ||
@@ -5,0 +5,0 @@ "module": "lib/esm/index", |
@@ -30,4 +30,8 @@ import { | ||
// Sends the request with a formatted request body | ||
const body = this.prepareBody() | ||
this.request.send(body) | ||
if (['POST', 'PUT'].includes(this.method)) { | ||
const body = this.prepareBody() | ||
this.request.send(body) | ||
} else { | ||
this.request.send() | ||
} | ||
@@ -67,6 +71,8 @@ // Adds an event listener for when the request state changes | ||
private prepareBody(): string { | ||
private prepareBody(): any { | ||
if (this.headers['Content-Type'] === 'application/json') { | ||
// Stringify if we're declaring this request to be a JSON request | ||
return JSON.stringify(this.body) | ||
} else if (this.headers['Content-Type'] === 'text/plain') { | ||
return this.body | ||
} else { | ||
@@ -73,0 +79,0 @@ // Otherwise, build a serialized string |
@@ -76,4 +76,24 @@ import { HttpRequest } from '../index' | ||
} | ||
public async put<T>(path: string, options: HttpOptions): Promise<T> { | ||
const requestOptions = { | ||
method: 'PUT', | ||
url: `${this.baseUrl}${path}`, | ||
} as HttpRequestOptions | ||
requestOptions.headers = this.buildHeaders( | ||
options && options.headers ? options.headers : {}, | ||
) | ||
if (options && options.body) { | ||
requestOptions.body = options.body | ||
} | ||
const request = new HttpRequest(requestOptions) | ||
const response = (await request.send()) as T | ||
return response | ||
} | ||
} | ||
export default HttpRequester |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
100193
3136