@rg-dev/stdlib 1.0.31 → 1.0.33
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.
- package/lib/common-env.cjs +14 -1
- package/lib/common-env.d.cts +2 -1
- package/lib/common-env.d.ts +2 -1
- package/lib/common-env.js +14 -1
- package/package.json +1 -1
package/lib/common-env.cjs
CHANGED
|
@@ -24,6 +24,7 @@ __export(common_env_exports, {
|
|
|
24
24
|
catchInline: () => catchInline,
|
|
25
25
|
doSafe: () => doSafe,
|
|
26
26
|
fetchHelperJSON: () => fetchHelperJSON,
|
|
27
|
+
fetchHelperPostJSON: () => fetchHelperPostJSON,
|
|
27
28
|
fetchHelperText: () => fetchHelperText,
|
|
28
29
|
isNonEmptyString: () => isNonEmptyString,
|
|
29
30
|
isNumber: () => isNumber,
|
|
@@ -98,6 +99,18 @@ async function fetchHelperJSON(url, config) {
|
|
|
98
99
|
throwIfNotJSONResponse(res);
|
|
99
100
|
return await res.json();
|
|
100
101
|
}
|
|
102
|
+
async function fetchHelperPostJSON(url, body, config) {
|
|
103
|
+
config = config || {};
|
|
104
|
+
config.method = "POST";
|
|
105
|
+
config.body = typeof body == "string" ? body : JSON.stringify(body);
|
|
106
|
+
config.headers["Content-Type"] = "application/json";
|
|
107
|
+
const res = await fetch(url, config);
|
|
108
|
+
if (!res.ok) {
|
|
109
|
+
const text = await res.text();
|
|
110
|
+
throw new Error(`Error ${text || "No content"} Status: ${res.status} Request URL: ${url}`);
|
|
111
|
+
}
|
|
112
|
+
return res;
|
|
113
|
+
}
|
|
101
114
|
async function fetchHelperText(url, config) {
|
|
102
115
|
const res = await fetch(url, config);
|
|
103
116
|
if (!res.ok) {
|
|
@@ -107,7 +120,7 @@ async function fetchHelperText(url, config) {
|
|
|
107
120
|
return await res.text();
|
|
108
121
|
}
|
|
109
122
|
function throwIfNotJSONResponse(res) {
|
|
110
|
-
const contentType = res.headers
|
|
123
|
+
const contentType = res.headers.get("content-type");
|
|
111
124
|
if (!(contentType == null ? void 0 : contentType.toLowerCase().includes("application/json"))) {
|
|
112
125
|
throw new Error(`Response is not JSON (content-type: ${contentType}, URL: ${res.url})`);
|
|
113
126
|
}
|
package/lib/common-env.d.cts
CHANGED
|
@@ -42,6 +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
46
|
declare function fetchHelperText(url: string, config?: RequestInit): Promise<string>;
|
|
46
47
|
|
|
47
48
|
type MyFn<T extends Record<string, (...args: any[]) => any>> = {
|
|
@@ -73,4 +74,4 @@ declare function isRunningOnServer(): boolean;
|
|
|
73
74
|
declare function useServer(fn: () => (Promise<any> | void), onError?: (err: Error) => void): Promise<any>;
|
|
74
75
|
declare function isNonEmptyString(str?: string): boolean;
|
|
75
76
|
|
|
76
|
-
export { type AsyncReturnType, type MaybeFunction, type MyFn, Optional, StringBuilder, catchInline, doSafe, fetchHelperJSON, fetchHelperText, isNonEmptyString, isNumber, isRunningOnServer, promiseRetry, promiseWithTimeout, sleep, useServer };
|
|
77
|
+
export { type AsyncReturnType, type MaybeFunction, type MyFn, Optional, StringBuilder, catchInline, doSafe, fetchHelperJSON, fetchHelperPostJSON, fetchHelperText, isNonEmptyString, isNumber, isRunningOnServer, promiseRetry, promiseWithTimeout, sleep, useServer };
|
package/lib/common-env.d.ts
CHANGED
|
@@ -42,6 +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
46
|
declare function fetchHelperText(url: string, config?: RequestInit): Promise<string>;
|
|
46
47
|
|
|
47
48
|
type MyFn<T extends Record<string, (...args: any[]) => any>> = {
|
|
@@ -73,4 +74,4 @@ declare function isRunningOnServer(): boolean;
|
|
|
73
74
|
declare function useServer(fn: () => (Promise<any> | void), onError?: (err: Error) => void): Promise<any>;
|
|
74
75
|
declare function isNonEmptyString(str?: string): boolean;
|
|
75
76
|
|
|
76
|
-
export { type AsyncReturnType, type MaybeFunction, type MyFn, Optional, StringBuilder, catchInline, doSafe, fetchHelperJSON, fetchHelperText, isNonEmptyString, isNumber, isRunningOnServer, promiseRetry, promiseWithTimeout, sleep, useServer };
|
|
77
|
+
export { type AsyncReturnType, type MaybeFunction, type MyFn, Optional, StringBuilder, catchInline, doSafe, fetchHelperJSON, fetchHelperPostJSON, fetchHelperText, isNonEmptyString, isNumber, isRunningOnServer, promiseRetry, promiseWithTimeout, sleep, useServer };
|
package/lib/common-env.js
CHANGED
|
@@ -61,6 +61,18 @@ async function fetchHelperJSON(url, config) {
|
|
|
61
61
|
throwIfNotJSONResponse(res);
|
|
62
62
|
return await res.json();
|
|
63
63
|
}
|
|
64
|
+
async function fetchHelperPostJSON(url, body, config) {
|
|
65
|
+
config = config || {};
|
|
66
|
+
config.method = "POST";
|
|
67
|
+
config.body = typeof body == "string" ? body : JSON.stringify(body);
|
|
68
|
+
config.headers["Content-Type"] = "application/json";
|
|
69
|
+
const res = await fetch(url, config);
|
|
70
|
+
if (!res.ok) {
|
|
71
|
+
const text = await res.text();
|
|
72
|
+
throw new Error(`Error ${text || "No content"} Status: ${res.status} Request URL: ${url}`);
|
|
73
|
+
}
|
|
74
|
+
return res;
|
|
75
|
+
}
|
|
64
76
|
async function fetchHelperText(url, config) {
|
|
65
77
|
const res = await fetch(url, config);
|
|
66
78
|
if (!res.ok) {
|
|
@@ -70,7 +82,7 @@ async function fetchHelperText(url, config) {
|
|
|
70
82
|
return await res.text();
|
|
71
83
|
}
|
|
72
84
|
function throwIfNotJSONResponse(res) {
|
|
73
|
-
const contentType = res.headers
|
|
85
|
+
const contentType = res.headers.get("content-type");
|
|
74
86
|
if (!(contentType == null ? void 0 : contentType.toLowerCase().includes("application/json"))) {
|
|
75
87
|
throw new Error(`Response is not JSON (content-type: ${contentType}, URL: ${res.url})`);
|
|
76
88
|
}
|
|
@@ -188,6 +200,7 @@ export {
|
|
|
188
200
|
catchInline,
|
|
189
201
|
doSafe,
|
|
190
202
|
fetchHelperJSON,
|
|
203
|
+
fetchHelperPostJSON,
|
|
191
204
|
fetchHelperText,
|
|
192
205
|
isNonEmptyString,
|
|
193
206
|
isNumber,
|