@naturalcycles/js-lib 14.127.0 → 14.128.1
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/dist/http/fetcher.js +15 -9
- package/dist/string/stringifyAny.d.ts +15 -1
- package/dist/string/stringifyAny.js +20 -2
- package/dist-esm/http/fetcher.js +15 -9
- package/dist-esm/string/stringifyAny.js +18 -1
- package/package.json +1 -1
- package/src/http/fetcher.ts +19 -14
- package/src/string/stringifyAny.ts +21 -2
package/dist/http/fetcher.js
CHANGED
|
@@ -145,16 +145,22 @@ class Fetcher {
|
|
|
145
145
|
if (mode === 'json') {
|
|
146
146
|
if (res.fetchResponse.body) {
|
|
147
147
|
const text = await res.fetchResponse.text();
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
148
|
+
if (text) {
|
|
149
|
+
try {
|
|
150
|
+
res.body = text;
|
|
151
|
+
res.body = JSON.parse(text, req.jsonReviver);
|
|
152
|
+
}
|
|
153
|
+
catch (err) {
|
|
154
|
+
res.ok = false;
|
|
155
|
+
res.err = (0, error_util_1._anyToError)(err, http_error_1.HttpError, (0, object_util_1._filterNullishValues)({
|
|
156
|
+
httpStatusCode: 0,
|
|
157
|
+
url: req.url,
|
|
158
|
+
}));
|
|
159
|
+
}
|
|
151
160
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
res.
|
|
155
|
-
httpStatusCode: 0,
|
|
156
|
-
url: req.url,
|
|
157
|
-
}));
|
|
161
|
+
else {
|
|
162
|
+
// Body had a '' (empty string)
|
|
163
|
+
res.body = {};
|
|
158
164
|
}
|
|
159
165
|
}
|
|
160
166
|
else {
|
|
@@ -1,4 +1,18 @@
|
|
|
1
1
|
import type { Reviver } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Allows to set Global "stringifyFunction" that will be used to "pretty-print" objects
|
|
4
|
+
* in various cases.
|
|
5
|
+
*
|
|
6
|
+
* Used, for example, by _stringifyAny() to pretty-print objects/arrays.
|
|
7
|
+
*
|
|
8
|
+
* Defaults to _safeJsonStringify.
|
|
9
|
+
*
|
|
10
|
+
* Node.js project can set it to _inspectAny, which allows to use `util.inspect`
|
|
11
|
+
* as pretty-printing function.
|
|
12
|
+
*
|
|
13
|
+
* It's recommended that this function is circular-reference-safe.
|
|
14
|
+
*/
|
|
15
|
+
export declare function setGlobalStringifyFunction(fn: JsonStringifyFunction): void;
|
|
2
16
|
export type JsonStringifyFunction = (obj: any, reviver?: Reviver, space?: number) => string;
|
|
3
17
|
export interface StringifyAnyOptions {
|
|
4
18
|
/**
|
|
@@ -22,7 +36,7 @@ export interface StringifyAnyOptions {
|
|
|
22
36
|
* Allows to pass custom "stringify function".
|
|
23
37
|
* E.g in Node.js you can pass `util.inspect` instead.
|
|
24
38
|
*
|
|
25
|
-
*
|
|
39
|
+
* Defaults to `globalStringifyFunction`, which defaults to `_safeJsonStringify`
|
|
26
40
|
*/
|
|
27
41
|
stringifyFn?: JsonStringifyFunction;
|
|
28
42
|
}
|
|
@@ -1,9 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports._stringifyAny = void 0;
|
|
3
|
+
exports._stringifyAny = exports.setGlobalStringifyFunction = void 0;
|
|
4
4
|
const error_util_1 = require("../error/error.util");
|
|
5
5
|
const json_util_1 = require("./json.util");
|
|
6
6
|
const safeJsonStringify_1 = require("./safeJsonStringify");
|
|
7
|
+
let globalStringifyFunction = safeJsonStringify_1._safeJsonStringify;
|
|
8
|
+
/**
|
|
9
|
+
* Allows to set Global "stringifyFunction" that will be used to "pretty-print" objects
|
|
10
|
+
* in various cases.
|
|
11
|
+
*
|
|
12
|
+
* Used, for example, by _stringifyAny() to pretty-print objects/arrays.
|
|
13
|
+
*
|
|
14
|
+
* Defaults to _safeJsonStringify.
|
|
15
|
+
*
|
|
16
|
+
* Node.js project can set it to _inspectAny, which allows to use `util.inspect`
|
|
17
|
+
* as pretty-printing function.
|
|
18
|
+
*
|
|
19
|
+
* It's recommended that this function is circular-reference-safe.
|
|
20
|
+
*/
|
|
21
|
+
function setGlobalStringifyFunction(fn) {
|
|
22
|
+
globalStringifyFunction = fn;
|
|
23
|
+
}
|
|
24
|
+
exports.setGlobalStringifyFunction = setGlobalStringifyFunction;
|
|
7
25
|
/**
|
|
8
26
|
* Inspired by inspectAny from nodejs-lib, which is based on util.inpect that is not available in the Browser.
|
|
9
27
|
* Potentially can do this (with extra 2Kb gz size): https://github.com/deecewan/browser-util-inspect
|
|
@@ -86,7 +104,7 @@ function _stringifyAny(obj, opt = {}) {
|
|
|
86
104
|
// Other
|
|
87
105
|
//
|
|
88
106
|
try {
|
|
89
|
-
const { stringifyFn =
|
|
107
|
+
const { stringifyFn = globalStringifyFunction } = opt;
|
|
90
108
|
s = stringifyFn(obj, undefined, 2);
|
|
91
109
|
}
|
|
92
110
|
catch {
|
package/dist-esm/http/fetcher.js
CHANGED
|
@@ -153,16 +153,22 @@ export class Fetcher {
|
|
|
153
153
|
if (mode === 'json') {
|
|
154
154
|
if (res.fetchResponse.body) {
|
|
155
155
|
const text = await res.fetchResponse.text();
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
156
|
+
if (text) {
|
|
157
|
+
try {
|
|
158
|
+
res.body = text;
|
|
159
|
+
res.body = JSON.parse(text, req.jsonReviver);
|
|
160
|
+
}
|
|
161
|
+
catch (err) {
|
|
162
|
+
res.ok = false;
|
|
163
|
+
res.err = _anyToError(err, HttpError, _filterNullishValues({
|
|
164
|
+
httpStatusCode: 0,
|
|
165
|
+
url: req.url,
|
|
166
|
+
}));
|
|
167
|
+
}
|
|
159
168
|
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
res.
|
|
163
|
-
httpStatusCode: 0,
|
|
164
|
-
url: req.url,
|
|
165
|
-
}));
|
|
169
|
+
else {
|
|
170
|
+
// Body had a '' (empty string)
|
|
171
|
+
res.body = {};
|
|
166
172
|
}
|
|
167
173
|
}
|
|
168
174
|
else {
|
|
@@ -1,6 +1,23 @@
|
|
|
1
1
|
import { _isErrorObject, _isHttpErrorObject, _isHttpErrorResponse } from '../error/error.util';
|
|
2
2
|
import { _jsonParseIfPossible } from './json.util';
|
|
3
3
|
import { _safeJsonStringify } from './safeJsonStringify';
|
|
4
|
+
let globalStringifyFunction = _safeJsonStringify;
|
|
5
|
+
/**
|
|
6
|
+
* Allows to set Global "stringifyFunction" that will be used to "pretty-print" objects
|
|
7
|
+
* in various cases.
|
|
8
|
+
*
|
|
9
|
+
* Used, for example, by _stringifyAny() to pretty-print objects/arrays.
|
|
10
|
+
*
|
|
11
|
+
* Defaults to _safeJsonStringify.
|
|
12
|
+
*
|
|
13
|
+
* Node.js project can set it to _inspectAny, which allows to use `util.inspect`
|
|
14
|
+
* as pretty-printing function.
|
|
15
|
+
*
|
|
16
|
+
* It's recommended that this function is circular-reference-safe.
|
|
17
|
+
*/
|
|
18
|
+
export function setGlobalStringifyFunction(fn) {
|
|
19
|
+
globalStringifyFunction = fn;
|
|
20
|
+
}
|
|
4
21
|
/**
|
|
5
22
|
* Inspired by inspectAny from nodejs-lib, which is based on util.inpect that is not available in the Browser.
|
|
6
23
|
* Potentially can do this (with extra 2Kb gz size): https://github.com/deecewan/browser-util-inspect
|
|
@@ -83,7 +100,7 @@ export function _stringifyAny(obj, opt = {}) {
|
|
|
83
100
|
// Other
|
|
84
101
|
//
|
|
85
102
|
try {
|
|
86
|
-
const { stringifyFn =
|
|
103
|
+
const { stringifyFn = globalStringifyFunction } = opt;
|
|
87
104
|
s = stringifyFn(obj, undefined, 2);
|
|
88
105
|
}
|
|
89
106
|
catch (_a) {
|
package/package.json
CHANGED
package/src/http/fetcher.ts
CHANGED
|
@@ -213,20 +213,25 @@ export class Fetcher {
|
|
|
213
213
|
if (mode === 'json') {
|
|
214
214
|
if (res.fetchResponse.body) {
|
|
215
215
|
const text = await res.fetchResponse.text()
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
err
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
216
|
+
|
|
217
|
+
if (text) {
|
|
218
|
+
try {
|
|
219
|
+
res.body = text
|
|
220
|
+
res.body = JSON.parse(text, req.jsonReviver)
|
|
221
|
+
} catch (err) {
|
|
222
|
+
res.ok = false
|
|
223
|
+
res.err = _anyToError(
|
|
224
|
+
err,
|
|
225
|
+
HttpError,
|
|
226
|
+
_filterNullishValues({
|
|
227
|
+
httpStatusCode: 0,
|
|
228
|
+
url: req.url,
|
|
229
|
+
}),
|
|
230
|
+
)
|
|
231
|
+
}
|
|
232
|
+
} else {
|
|
233
|
+
// Body had a '' (empty string)
|
|
234
|
+
res.body = {}
|
|
230
235
|
}
|
|
231
236
|
} else {
|
|
232
237
|
// if no body: set responseBody as {}
|
|
@@ -3,6 +3,25 @@ import type { Reviver } from '../types'
|
|
|
3
3
|
import { _jsonParseIfPossible } from './json.util'
|
|
4
4
|
import { _safeJsonStringify } from './safeJsonStringify'
|
|
5
5
|
|
|
6
|
+
let globalStringifyFunction: JsonStringifyFunction = _safeJsonStringify
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Allows to set Global "stringifyFunction" that will be used to "pretty-print" objects
|
|
10
|
+
* in various cases.
|
|
11
|
+
*
|
|
12
|
+
* Used, for example, by _stringifyAny() to pretty-print objects/arrays.
|
|
13
|
+
*
|
|
14
|
+
* Defaults to _safeJsonStringify.
|
|
15
|
+
*
|
|
16
|
+
* Node.js project can set it to _inspectAny, which allows to use `util.inspect`
|
|
17
|
+
* as pretty-printing function.
|
|
18
|
+
*
|
|
19
|
+
* It's recommended that this function is circular-reference-safe.
|
|
20
|
+
*/
|
|
21
|
+
export function setGlobalStringifyFunction(fn: JsonStringifyFunction): void {
|
|
22
|
+
globalStringifyFunction = fn
|
|
23
|
+
}
|
|
24
|
+
|
|
6
25
|
export type JsonStringifyFunction = (obj: any, reviver?: Reviver, space?: number) => string
|
|
7
26
|
|
|
8
27
|
export interface StringifyAnyOptions {
|
|
@@ -30,7 +49,7 @@ export interface StringifyAnyOptions {
|
|
|
30
49
|
* Allows to pass custom "stringify function".
|
|
31
50
|
* E.g in Node.js you can pass `util.inspect` instead.
|
|
32
51
|
*
|
|
33
|
-
*
|
|
52
|
+
* Defaults to `globalStringifyFunction`, which defaults to `_safeJsonStringify`
|
|
34
53
|
*/
|
|
35
54
|
stringifyFn?: JsonStringifyFunction
|
|
36
55
|
}
|
|
@@ -120,7 +139,7 @@ export function _stringifyAny(obj: any, opt: StringifyAnyOptions = {}): string {
|
|
|
120
139
|
// Other
|
|
121
140
|
//
|
|
122
141
|
try {
|
|
123
|
-
const { stringifyFn =
|
|
142
|
+
const { stringifyFn = globalStringifyFunction } = opt
|
|
124
143
|
|
|
125
144
|
s = stringifyFn(obj, undefined, 2)
|
|
126
145
|
} catch {
|