@rg-dev/stdlib 1.0.33 → 1.0.34

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.
@@ -24,7 +24,7 @@ __export(common_env_exports, {
24
24
  catchInline: () => catchInline,
25
25
  doSafe: () => doSafe,
26
26
  fetchHelperJSON: () => fetchHelperJSON,
27
- fetchHelperPostJSON: () => fetchHelperPostJSON,
27
+ fetchHelperPost: () => fetchHelperPost,
28
28
  fetchHelperText: () => fetchHelperText,
29
29
  isNonEmptyString: () => isNonEmptyString,
30
30
  isNumber: () => isNumber,
@@ -99,11 +99,15 @@ async function fetchHelperJSON(url, config) {
99
99
  throwIfNotJSONResponse(res);
100
100
  return await res.json();
101
101
  }
102
- async function fetchHelperPostJSON(url, body, config) {
102
+ async function fetchHelperPost(url, body, config) {
103
+ const isObject = typeof body == "object";
103
104
  config = config || {};
104
105
  config.method = "POST";
105
- config.body = typeof body == "string" ? body : JSON.stringify(body);
106
- config.headers["Content-Type"] = "application/json";
106
+ config.body = isObject ? JSON.stringify(body) : body;
107
+ config.headers = config.headers || {};
108
+ if (isObject) {
109
+ config.headers["Content-Type"] = "application/json";
110
+ }
107
111
  const res = await fetch(url, config);
108
112
  if (!res.ok) {
109
113
  const text = await res.text();
@@ -42,7 +42,7 @@ declare class StringBuilder implements IStringBuilder {
42
42
  }
43
43
 
44
44
  declare function fetchHelperJSON<T extends Record<string, any>>(url: string, config?: RequestInit): Promise<T>;
45
- declare function fetchHelperPostJSON(url: string, body: any, config?: RequestInit): Promise<Response>;
45
+ declare function fetchHelperPost(url: string, body: any, config?: RequestInit): Promise<Response>;
46
46
  declare function fetchHelperText(url: string, config?: RequestInit): Promise<string>;
47
47
 
48
48
  type MyFn<T extends Record<string, (...args: any[]) => any>> = {
@@ -74,4 +74,4 @@ declare function isRunningOnServer(): boolean;
74
74
  declare function useServer(fn: () => (Promise<any> | void), onError?: (err: Error) => void): Promise<any>;
75
75
  declare function isNonEmptyString(str?: string): boolean;
76
76
 
77
- export { type AsyncReturnType, type MaybeFunction, type MyFn, Optional, StringBuilder, catchInline, doSafe, fetchHelperJSON, fetchHelperPostJSON, fetchHelperText, isNonEmptyString, isNumber, isRunningOnServer, promiseRetry, promiseWithTimeout, sleep, useServer };
77
+ export { type AsyncReturnType, type MaybeFunction, type MyFn, Optional, StringBuilder, catchInline, doSafe, fetchHelperJSON, fetchHelperPost, fetchHelperText, isNonEmptyString, isNumber, isRunningOnServer, promiseRetry, promiseWithTimeout, sleep, useServer };
@@ -42,7 +42,7 @@ declare class StringBuilder implements IStringBuilder {
42
42
  }
43
43
 
44
44
  declare function fetchHelperJSON<T extends Record<string, any>>(url: string, config?: RequestInit): Promise<T>;
45
- declare function fetchHelperPostJSON(url: string, body: any, config?: RequestInit): Promise<Response>;
45
+ declare function fetchHelperPost(url: string, body: any, config?: RequestInit): Promise<Response>;
46
46
  declare function fetchHelperText(url: string, config?: RequestInit): Promise<string>;
47
47
 
48
48
  type MyFn<T extends Record<string, (...args: any[]) => any>> = {
@@ -74,4 +74,4 @@ declare function isRunningOnServer(): boolean;
74
74
  declare function useServer(fn: () => (Promise<any> | void), onError?: (err: Error) => void): Promise<any>;
75
75
  declare function isNonEmptyString(str?: string): boolean;
76
76
 
77
- export { type AsyncReturnType, type MaybeFunction, type MyFn, Optional, StringBuilder, catchInline, doSafe, fetchHelperJSON, fetchHelperPostJSON, fetchHelperText, isNonEmptyString, isNumber, isRunningOnServer, promiseRetry, promiseWithTimeout, sleep, useServer };
77
+ export { type AsyncReturnType, type MaybeFunction, type MyFn, Optional, StringBuilder, catchInline, doSafe, fetchHelperJSON, fetchHelperPost, fetchHelperText, isNonEmptyString, isNumber, isRunningOnServer, promiseRetry, promiseWithTimeout, sleep, useServer };
package/lib/common-env.js CHANGED
@@ -61,11 +61,15 @@ async function fetchHelperJSON(url, config) {
61
61
  throwIfNotJSONResponse(res);
62
62
  return await res.json();
63
63
  }
64
- async function fetchHelperPostJSON(url, body, config) {
64
+ async function fetchHelperPost(url, body, config) {
65
+ const isObject = typeof body == "object";
65
66
  config = config || {};
66
67
  config.method = "POST";
67
- config.body = typeof body == "string" ? body : JSON.stringify(body);
68
- config.headers["Content-Type"] = "application/json";
68
+ config.body = isObject ? JSON.stringify(body) : body;
69
+ config.headers = config.headers || {};
70
+ if (isObject) {
71
+ config.headers["Content-Type"] = "application/json";
72
+ }
69
73
  const res = await fetch(url, config);
70
74
  if (!res.ok) {
71
75
  const text = await res.text();
@@ -200,7 +204,7 @@ export {
200
204
  catchInline,
201
205
  doSafe,
202
206
  fetchHelperJSON,
203
- fetchHelperPostJSON,
207
+ fetchHelperPost,
204
208
  fetchHelperText,
205
209
  isNonEmptyString,
206
210
  isNumber,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rg-dev/stdlib",
3
- "version": "1.0.33",
3
+ "version": "1.0.34",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1",