@portal-hq/utils 0.2.21 → 0.2.22-beta1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,8 +15,13 @@ class HttpRequest {
15
15
  // Sets headers on the request
16
16
  this.prepareRequest();
17
17
  // Sends the request with a formatted request body
18
- const body = this.prepareBody();
19
- this.request.send(body);
18
+ if (['POST', 'PUT'].includes(this.method)) {
19
+ const body = this.prepareBody();
20
+ this.request.send(body);
21
+ }
22
+ else {
23
+ this.request.send();
24
+ }
20
25
  // Adds an event listener for when the request state changes
21
26
  this.request.onreadystatechange = () => {
22
27
  if (this.request.readyState === 4) {
@@ -53,6 +58,9 @@ class HttpRequest {
53
58
  // Stringify if we're declaring this request to be a JSON request
54
59
  return JSON.stringify(this.body);
55
60
  }
61
+ else if (this.headers['Content-Type'] === 'text/plain') {
62
+ return this.body;
63
+ }
56
64
  else {
57
65
  // Otherwise, build a serialized string
58
66
  const params = [];
@@ -58,5 +58,20 @@ class HttpRequester {
58
58
  return response;
59
59
  });
60
60
  }
61
+ put(path, options) {
62
+ return __awaiter(this, void 0, void 0, function* () {
63
+ const requestOptions = {
64
+ method: 'PUT',
65
+ url: `${this.baseUrl}${path}`,
66
+ };
67
+ requestOptions.headers = this.buildHeaders(options && options.headers ? options.headers : {});
68
+ if (options && options.body) {
69
+ requestOptions.body = options.body;
70
+ }
71
+ const request = new index_1.HttpRequest(requestOptions);
72
+ const response = (yield request.send());
73
+ return response;
74
+ });
75
+ }
61
76
  }
62
77
  exports.default = HttpRequester;
@@ -13,8 +13,13 @@ class HttpRequest {
13
13
  // Sets headers on the request
14
14
  this.prepareRequest();
15
15
  // Sends the request with a formatted request body
16
- const body = this.prepareBody();
17
- this.request.send(body);
16
+ if (['POST', 'PUT'].includes(this.method)) {
17
+ const body = this.prepareBody();
18
+ this.request.send(body);
19
+ }
20
+ else {
21
+ this.request.send();
22
+ }
18
23
  // Adds an event listener for when the request state changes
19
24
  this.request.onreadystatechange = () => {
20
25
  if (this.request.readyState === 4) {
@@ -51,6 +56,9 @@ class HttpRequest {
51
56
  // Stringify if we're declaring this request to be a JSON request
52
57
  return JSON.stringify(this.body);
53
58
  }
59
+ else if (this.headers['Content-Type'] === 'text/plain') {
60
+ return this.body;
61
+ }
54
62
  else {
55
63
  // Otherwise, build a serialized string
56
64
  const params = [];
@@ -56,5 +56,20 @@ class HttpRequester {
56
56
  return response;
57
57
  });
58
58
  }
59
+ put(path, options) {
60
+ return __awaiter(this, void 0, void 0, function* () {
61
+ const requestOptions = {
62
+ method: 'PUT',
63
+ url: `${this.baseUrl}${path}`,
64
+ };
65
+ requestOptions.headers = this.buildHeaders(options && options.headers ? options.headers : {});
66
+ if (options && options.body) {
67
+ requestOptions.body = options.body;
68
+ }
69
+ const request = new HttpRequest(requestOptions);
70
+ const response = (yield request.send());
71
+ return response;
72
+ });
73
+ }
59
74
  }
60
75
  export default HttpRequester;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@portal-hq/utils",
3
- "version": "0.2.21",
3
+ "version": "0.2.22-beta1",
4
4
  "main": "lib/commonjs/index",
5
5
  "module": "lib/esm/index",
6
6
  "source": "src/index",
@@ -27,8 +27,12 @@ class HttpRequest {
27
27
  this.prepareRequest()
28
28
 
29
29
  // Sends the request with a formatted request body
30
- const body = this.prepareBody()
31
- this.request.send(body)
30
+ if (['POST', 'PUT'].includes(this.method)) {
31
+ const body = this.prepareBody()
32
+ this.request.send(body)
33
+ } else {
34
+ this.request.send()
35
+ }
32
36
 
33
37
  // Adds an event listener for when the request state changes
34
38
  this.request.onreadystatechange = () => {
@@ -63,10 +67,12 @@ class HttpRequest {
63
67
  }
64
68
  }
65
69
 
66
- private prepareBody(): string {
70
+ private prepareBody(): any {
67
71
  if (this.headers['Content-Type'] === 'application/json') {
68
72
  // Stringify if we're declaring this request to be a JSON request
69
73
  return JSON.stringify(this.body)
74
+ } else if (this.headers['Content-Type'] === 'text/plain') {
75
+ return this.body
70
76
  } else {
71
77
  // Otherwise, build a serialized string
72
78
  const params = []
@@ -73,6 +73,26 @@ class HttpRequester {
73
73
 
74
74
  return response
75
75
  }
76
+
77
+ public async put<T>(path: string, options: HttpOptions): Promise<T> {
78
+ const requestOptions = {
79
+ method: 'PUT',
80
+ url: `${this.baseUrl}${path}`,
81
+ } as HttpRequestOptions
82
+
83
+ requestOptions.headers = this.buildHeaders(
84
+ options && options.headers ? options.headers : {},
85
+ )
86
+
87
+ if (options && options.body) {
88
+ requestOptions.body = options.body
89
+ }
90
+
91
+ const request = new HttpRequest(requestOptions)
92
+ const response = (await request.send()) as T
93
+
94
+ return response
95
+ }
76
96
  }
77
97
 
78
98
  export default HttpRequester