@settlemint/sdk-utils 2.3.2-pr6cb5dd2e → 2.3.2-pr74f654b5
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/README.md +51 -7
- package/dist/environment.cjs +25 -408
- package/dist/environment.d.cts +3 -176
- package/dist/environment.d.ts +3 -176
- package/dist/environment.js +4 -0
- package/dist/filesystem.cjs +39 -123
- package/dist/filesystem.d.cts +4 -59
- package/dist/filesystem.d.ts +4 -59
- package/dist/filesystem.js +5 -0
- package/dist/http.cjs +35 -147
- package/dist/http.d.cts +4 -50
- package/dist/http.d.ts +4 -50
- package/dist/http.js +5 -0
- package/dist/index.cjs +27 -184
- package/dist/index.d.cts +4 -122
- package/dist/index.d.ts +4 -122
- package/dist/index.js +7 -0
- package/dist/json.cjs +83 -0
- package/dist/json.cjs.map +1 -0
- package/dist/json.d.cts +56 -0
- package/dist/json.d.ts +56 -0
- package/dist/json.js +80 -0
- package/dist/json.js.map +1 -0
- package/dist/logging.cjs +34 -141
- package/dist/logging.d.cts +4 -70
- package/dist/logging.d.ts +4 -70
- package/dist/logging.js +5 -0
- package/dist/package-manager.cjs +51 -196
- package/dist/package-manager.d.cts +7 -114
- package/dist/package-manager.d.ts +7 -114
- package/dist/package-manager.js +9 -0
- package/dist/retry.cjs +69 -0
- package/dist/retry.cjs.map +1 -0
- package/dist/retry.d.cts +19 -0
- package/dist/retry.d.ts +19 -0
- package/dist/retry.js +46 -0
- package/dist/retry.js.map +1 -0
- package/dist/runtime.cjs +38 -42
- package/dist/runtime.d.cts +2 -32
- package/dist/runtime.d.ts +2 -32
- package/dist/runtime.js +3 -0
- package/dist/string.cjs +76 -0
- package/dist/string.cjs.map +1 -0
- package/dist/string.d.cts +58 -0
- package/dist/string.d.ts +58 -0
- package/dist/string.js +72 -0
- package/dist/string.js.map +1 -0
- package/dist/terminal.cjs +91 -258
- package/dist/terminal.d.cts +11 -219
- package/dist/terminal.d.ts +11 -219
- package/dist/terminal.js +12 -0
- package/dist/url.cjs +25 -0
- package/dist/url.cjs.map +1 -0
- package/dist/url.d.cts +20 -0
- package/dist/url.d.ts +20 -0
- package/dist/url.js +24 -0
- package/dist/url.js.map +1 -0
- package/dist/validation.cjs +89 -183
- package/dist/validation.d.cts +7 -239
- package/dist/validation.d.ts +7 -239
- package/dist/validation.js +8 -0
- package/package.json +6 -6
- package/dist/environment.cjs.map +0 -1
- package/dist/environment.mjs +0 -383
- package/dist/environment.mjs.map +0 -1
- package/dist/filesystem.cjs.map +0 -1
- package/dist/filesystem.mjs +0 -105
- package/dist/filesystem.mjs.map +0 -1
- package/dist/http.cjs.map +0 -1
- package/dist/http.mjs +0 -129
- package/dist/http.mjs.map +0 -1
- package/dist/index.cjs.map +0 -1
- package/dist/index.mjs +0 -156
- package/dist/index.mjs.map +0 -1
- package/dist/logging.cjs.map +0 -1
- package/dist/logging.mjs +0 -123
- package/dist/logging.mjs.map +0 -1
- package/dist/package-manager.cjs.map +0 -1
- package/dist/package-manager.mjs +0 -167
- package/dist/package-manager.mjs.map +0 -1
- package/dist/runtime.cjs.map +0 -1
- package/dist/runtime.mjs +0 -23
- package/dist/runtime.mjs.map +0 -1
- package/dist/terminal.cjs.map +0 -1
- package/dist/terminal.mjs +0 -230
- package/dist/terminal.mjs.map +0 -1
- package/dist/validation.cjs.map +0 -1
- package/dist/validation.mjs +0 -159
- package/dist/validation.mjs.map +0 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,122 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* @param defaultValue - The value to return if parsing fails or results in null/undefined
|
|
6
|
-
* @returns The parsed JSON value as type T, or the default value if parsing fails
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
* import { tryParseJson } from "@settlemint/sdk-utils";
|
|
10
|
-
*
|
|
11
|
-
* const config = tryParseJson<{ port: number }>(
|
|
12
|
-
* '{"port": 3000}',
|
|
13
|
-
* { port: 8080 }
|
|
14
|
-
* );
|
|
15
|
-
* // Returns: { port: 3000 }
|
|
16
|
-
*
|
|
17
|
-
* const invalid = tryParseJson<string[]>(
|
|
18
|
-
* 'invalid json',
|
|
19
|
-
* []
|
|
20
|
-
* );
|
|
21
|
-
* // Returns: []
|
|
22
|
-
*/
|
|
23
|
-
declare function tryParseJson<T>(value: string, defaultValue?: T | null): T | null;
|
|
24
|
-
/**
|
|
25
|
-
* Extracts a JSON object from a string.
|
|
26
|
-
*
|
|
27
|
-
* @param value - The string to extract the JSON object from
|
|
28
|
-
* @returns The parsed JSON object, or null if no JSON object is found
|
|
29
|
-
* @throws {Error} If the input string is too long (longer than 5000 characters)
|
|
30
|
-
* @example
|
|
31
|
-
* import { extractJsonObject } from "@settlemint/sdk-utils";
|
|
32
|
-
*
|
|
33
|
-
* const json = extractJsonObject<{ port: number }>(
|
|
34
|
-
* 'port info: {"port": 3000}',
|
|
35
|
-
* );
|
|
36
|
-
* // Returns: { port: 3000 }
|
|
37
|
-
*/
|
|
38
|
-
declare function extractJsonObject<T>(value: string): T | null;
|
|
39
|
-
/**
|
|
40
|
-
* Converts a value to a JSON stringifiable format.
|
|
41
|
-
*
|
|
42
|
-
* @param value - The value to convert
|
|
43
|
-
* @returns The JSON stringifiable value
|
|
44
|
-
*
|
|
45
|
-
* @example
|
|
46
|
-
* import { makeJsonStringifiable } from "@settlemint/sdk-utils";
|
|
47
|
-
*
|
|
48
|
-
* const json = makeJsonStringifiable<{ amount: bigint }>({ amount: BigInt(1000) });
|
|
49
|
-
* // Returns: '{"amount":"1000"}'
|
|
50
|
-
*/
|
|
51
|
-
declare function makeJsonStringifiable<T>(value: unknown): T;
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Retry a function when it fails.
|
|
55
|
-
* @param fn - The function to retry.
|
|
56
|
-
* @param maxRetries - The maximum number of retries.
|
|
57
|
-
* @param initialSleepTime - The initial time to sleep between exponential backoff retries.
|
|
58
|
-
* @param stopOnError - The function to stop on error.
|
|
59
|
-
* @returns The result of the function or undefined if it fails.
|
|
60
|
-
* @example
|
|
61
|
-
* import { retryWhenFailed } from "@settlemint/sdk-utils";
|
|
62
|
-
* import { readFile } from "node:fs/promises";
|
|
63
|
-
*
|
|
64
|
-
* const result = await retryWhenFailed(() => readFile("/path/to/file.txt"), 3, 1_000);
|
|
65
|
-
*/
|
|
66
|
-
declare function retryWhenFailed<T>(fn: () => Promise<T>, maxRetries?: number, initialSleepTime?: number, stopOnError?: (error: Error) => boolean): Promise<T>;
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Capitalizes the first letter of a string.
|
|
70
|
-
*
|
|
71
|
-
* @param val - The string to capitalize
|
|
72
|
-
* @returns The input string with its first letter capitalized
|
|
73
|
-
*
|
|
74
|
-
* @example
|
|
75
|
-
* import { capitalizeFirstLetter } from "@settlemint/sdk-utils";
|
|
76
|
-
*
|
|
77
|
-
* const capitalized = capitalizeFirstLetter("hello");
|
|
78
|
-
* // Returns: "Hello"
|
|
79
|
-
*/
|
|
80
|
-
declare function capitalizeFirstLetter(val: string): string;
|
|
81
|
-
/**
|
|
82
|
-
* Converts a camelCase string to a human-readable string.
|
|
83
|
-
*
|
|
84
|
-
* @param s - The camelCase string to convert
|
|
85
|
-
* @returns The human-readable string
|
|
86
|
-
*
|
|
87
|
-
* @example
|
|
88
|
-
* import { camelCaseToWords } from "@settlemint/sdk-utils";
|
|
89
|
-
*
|
|
90
|
-
* const words = camelCaseToWords("camelCaseString");
|
|
91
|
-
* // Returns: "Camel Case String"
|
|
92
|
-
*/
|
|
93
|
-
declare function camelCaseToWords(s: string): string;
|
|
94
|
-
/**
|
|
95
|
-
* Replaces underscores and hyphens with spaces.
|
|
96
|
-
*
|
|
97
|
-
* @param s - The string to replace underscores and hyphens with spaces
|
|
98
|
-
* @returns The input string with underscores and hyphens replaced with spaces
|
|
99
|
-
*
|
|
100
|
-
* @example
|
|
101
|
-
* import { replaceUnderscoresAndHyphensWithSpaces } from "@settlemint/sdk-utils";
|
|
102
|
-
*
|
|
103
|
-
* const result = replaceUnderscoresAndHyphensWithSpaces("Already_Spaced-Second");
|
|
104
|
-
* // Returns: "Already Spaced Second"
|
|
105
|
-
*/
|
|
106
|
-
declare function replaceUnderscoresAndHyphensWithSpaces(s: string): string;
|
|
107
|
-
/**
|
|
108
|
-
* Truncates a string to a maximum length and appends "..." if it is longer.
|
|
109
|
-
*
|
|
110
|
-
* @param value - The string to truncate
|
|
111
|
-
* @param maxLength - The maximum length of the string
|
|
112
|
-
* @returns The truncated string or the original string if it is shorter than the maximum length
|
|
113
|
-
*
|
|
114
|
-
* @example
|
|
115
|
-
* import { truncate } from "@settlemint/sdk-utils";
|
|
116
|
-
*
|
|
117
|
-
* const truncated = truncate("Hello, world!", 10);
|
|
118
|
-
* // Returns: "Hello, wor..."
|
|
119
|
-
*/
|
|
120
|
-
declare function truncate(value: string, maxLength: number): string;
|
|
121
|
-
|
|
122
|
-
export { camelCaseToWords, capitalizeFirstLetter, extractJsonObject, makeJsonStringifiable, replaceUnderscoresAndHyphensWithSpaces, retryWhenFailed, truncate, tryParseJson };
|
|
1
|
+
export * from "./json.js";
|
|
2
|
+
export * from "./retry.js";
|
|
3
|
+
export * from "./string.js";
|
|
4
|
+
export * from "./url.js";
|
package/dist/index.js
ADDED
package/dist/json.cjs
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/json.ts
|
|
3
|
+
/**
|
|
4
|
+
* Attempts to parse a JSON string into a typed value, returning a default value if parsing fails.
|
|
5
|
+
*
|
|
6
|
+
* @param value - The JSON string to parse
|
|
7
|
+
* @param defaultValue - The value to return if parsing fails or results in null/undefined
|
|
8
|
+
* @returns The parsed JSON value as type T, or the default value if parsing fails
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* import { tryParseJson } from "@settlemint/sdk-utils";
|
|
12
|
+
*
|
|
13
|
+
* const config = tryParseJson<{ port: number }>(
|
|
14
|
+
* '{"port": 3000}',
|
|
15
|
+
* { port: 8080 }
|
|
16
|
+
* );
|
|
17
|
+
* // Returns: { port: 3000 }
|
|
18
|
+
*
|
|
19
|
+
* const invalid = tryParseJson<string[]>(
|
|
20
|
+
* 'invalid json',
|
|
21
|
+
* []
|
|
22
|
+
* );
|
|
23
|
+
* // Returns: []
|
|
24
|
+
*/
|
|
25
|
+
function tryParseJson(value, defaultValue = null) {
|
|
26
|
+
try {
|
|
27
|
+
const parsed = JSON.parse(value);
|
|
28
|
+
if (parsed === undefined || parsed === null) {
|
|
29
|
+
return defaultValue;
|
|
30
|
+
}
|
|
31
|
+
return parsed;
|
|
32
|
+
} catch (err) {
|
|
33
|
+
return defaultValue;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Extracts a JSON object from a string.
|
|
38
|
+
*
|
|
39
|
+
* @param value - The string to extract the JSON object from
|
|
40
|
+
* @returns The parsed JSON object, or null if no JSON object is found
|
|
41
|
+
* @throws {Error} If the input string is too long (longer than 5000 characters)
|
|
42
|
+
* @example
|
|
43
|
+
* import { extractJsonObject } from "@settlemint/sdk-utils";
|
|
44
|
+
*
|
|
45
|
+
* const json = extractJsonObject<{ port: number }>(
|
|
46
|
+
* 'port info: {"port": 3000}',
|
|
47
|
+
* );
|
|
48
|
+
* // Returns: { port: 3000 }
|
|
49
|
+
*/
|
|
50
|
+
function extractJsonObject(value) {
|
|
51
|
+
if (value.length > 5e3) {
|
|
52
|
+
throw new Error("Input too long");
|
|
53
|
+
}
|
|
54
|
+
const result = /\{([\s\S]*)\}/.exec(value);
|
|
55
|
+
if (!result) {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
return tryParseJson(result[0]);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Converts a value to a JSON stringifiable format.
|
|
62
|
+
*
|
|
63
|
+
* @param value - The value to convert
|
|
64
|
+
* @returns The JSON stringifiable value
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* import { makeJsonStringifiable } from "@settlemint/sdk-utils";
|
|
68
|
+
*
|
|
69
|
+
* const json = makeJsonStringifiable<{ amount: bigint }>({ amount: BigInt(1000) });
|
|
70
|
+
* // Returns: '{"amount":"1000"}'
|
|
71
|
+
*/
|
|
72
|
+
function makeJsonStringifiable(value) {
|
|
73
|
+
if (value === undefined || value === null) {
|
|
74
|
+
return value;
|
|
75
|
+
}
|
|
76
|
+
return tryParseJson(JSON.stringify(value, (_, value$1) => typeof value$1 === "bigint" ? value$1.toString() : value$1));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
//#endregion
|
|
80
|
+
exports.extractJsonObject = extractJsonObject;
|
|
81
|
+
exports.makeJsonStringifiable = makeJsonStringifiable;
|
|
82
|
+
exports.tryParseJson = tryParseJson;
|
|
83
|
+
//# sourceMappingURL=json.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json.cjs","names":["value: string","defaultValue: T | null","value: unknown","value"],"sources":["../src/json.ts"],"sourcesContent":["/**\n * Attempts to parse a JSON string into a typed value, returning a default value if parsing fails.\n *\n * @param value - The JSON string to parse\n * @param defaultValue - The value to return if parsing fails or results in null/undefined\n * @returns The parsed JSON value as type T, or the default value if parsing fails\n *\n * @example\n * import { tryParseJson } from \"@settlemint/sdk-utils\";\n *\n * const config = tryParseJson<{ port: number }>(\n * '{\"port\": 3000}',\n * { port: 8080 }\n * );\n * // Returns: { port: 3000 }\n *\n * const invalid = tryParseJson<string[]>(\n * 'invalid json',\n * []\n * );\n * // Returns: []\n */\nexport function tryParseJson<T>(value: string, defaultValue: T | null = null): T | null {\n try {\n const parsed = JSON.parse(value) as T;\n if (parsed === undefined || parsed === null) {\n return defaultValue;\n }\n return parsed;\n } catch (err) {\n // Invalid json\n return defaultValue;\n }\n}\n\n/**\n * Extracts a JSON object from a string.\n *\n * @param value - The string to extract the JSON object from\n * @returns The parsed JSON object, or null if no JSON object is found\n * @throws {Error} If the input string is too long (longer than 5000 characters)\n * @example\n * import { extractJsonObject } from \"@settlemint/sdk-utils\";\n *\n * const json = extractJsonObject<{ port: number }>(\n * 'port info: {\"port\": 3000}',\n * );\n * // Returns: { port: 3000 }\n */\nexport function extractJsonObject<T>(value: string): T | null {\n if (value.length > 5000) {\n throw new Error(\"Input too long\");\n }\n const result = /\\{([\\s\\S]*)\\}/.exec(value);\n if (!result) {\n return null;\n }\n return tryParseJson<T>(result[0]);\n}\n\n/**\n * Converts a value to a JSON stringifiable format.\n *\n * @param value - The value to convert\n * @returns The JSON stringifiable value\n *\n * @example\n * import { makeJsonStringifiable } from \"@settlemint/sdk-utils\";\n *\n * const json = makeJsonStringifiable<{ amount: bigint }>({ amount: BigInt(1000) });\n * // Returns: '{\"amount\":\"1000\"}'\n */\nexport function makeJsonStringifiable<T>(value: unknown): T {\n if (value === undefined || value === null) {\n return value as T;\n }\n return tryParseJson<T>(\n JSON.stringify(\n value,\n (_, value) => (typeof value === \"bigint\" ? value.toString() : value), // return everything else unchanged\n ),\n ) as T;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAsBA,SAAgB,aAAgBA,OAAeC,eAAyB,MAAgB;AACtF,KAAI;EACF,MAAM,SAAS,KAAK,MAAM,MAAM;AAChC,MAAI,WAAW,aAAa,WAAW,MAAM;AAC3C,UAAO;EACR;AACD,SAAO;CACR,SAAQ,KAAK;AAEZ,SAAO;CACR;AACF;;;;;;;;;;;;;;;AAgBD,SAAgB,kBAAqBD,OAAyB;AAC5D,KAAI,MAAM,SAAS,KAAM;AACvB,QAAM,IAAI,MAAM;CACjB;CACD,MAAM,SAAS,gBAAgB,KAAK,MAAM;AAC1C,MAAK,QAAQ;AACX,SAAO;CACR;AACD,QAAO,aAAgB,OAAO,GAAG;AAClC;;;;;;;;;;;;;AAcD,SAAgB,sBAAyBE,OAAmB;AAC1D,KAAI,UAAU,aAAa,UAAU,MAAM;AACzC,SAAO;CACR;AACD,QAAO,aACL,KAAK,UACH,OACA,CAAC,GAAGC,mBAAkBA,YAAU,WAAW,QAAM,UAAU,GAAGA,QAC/D,CACF;AACF"}
|
package/dist/json.d.cts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
//#region src/json.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Attempts to parse a JSON string into a typed value, returning a default value if parsing fails.
|
|
4
|
+
*
|
|
5
|
+
* @param value - The JSON string to parse
|
|
6
|
+
* @param defaultValue - The value to return if parsing fails or results in null/undefined
|
|
7
|
+
* @returns The parsed JSON value as type T, or the default value if parsing fails
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* import { tryParseJson } from "@settlemint/sdk-utils";
|
|
11
|
+
*
|
|
12
|
+
* const config = tryParseJson<{ port: number }>(
|
|
13
|
+
* '{"port": 3000}',
|
|
14
|
+
* { port: 8080 }
|
|
15
|
+
* );
|
|
16
|
+
* // Returns: { port: 3000 }
|
|
17
|
+
*
|
|
18
|
+
* const invalid = tryParseJson<string[]>(
|
|
19
|
+
* 'invalid json',
|
|
20
|
+
* []
|
|
21
|
+
* );
|
|
22
|
+
* // Returns: []
|
|
23
|
+
*/
|
|
24
|
+
declare function tryParseJson<T>(value: string, defaultValue?: T | null): T | null;
|
|
25
|
+
/**
|
|
26
|
+
* Extracts a JSON object from a string.
|
|
27
|
+
*
|
|
28
|
+
* @param value - The string to extract the JSON object from
|
|
29
|
+
* @returns The parsed JSON object, or null if no JSON object is found
|
|
30
|
+
* @throws {Error} If the input string is too long (longer than 5000 characters)
|
|
31
|
+
* @example
|
|
32
|
+
* import { extractJsonObject } from "@settlemint/sdk-utils";
|
|
33
|
+
*
|
|
34
|
+
* const json = extractJsonObject<{ port: number }>(
|
|
35
|
+
* 'port info: {"port": 3000}',
|
|
36
|
+
* );
|
|
37
|
+
* // Returns: { port: 3000 }
|
|
38
|
+
*/
|
|
39
|
+
declare function extractJsonObject<T>(value: string): T | null;
|
|
40
|
+
/**
|
|
41
|
+
* Converts a value to a JSON stringifiable format.
|
|
42
|
+
*
|
|
43
|
+
* @param value - The value to convert
|
|
44
|
+
* @returns The JSON stringifiable value
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* import { makeJsonStringifiable } from "@settlemint/sdk-utils";
|
|
48
|
+
*
|
|
49
|
+
* const json = makeJsonStringifiable<{ amount: bigint }>({ amount: BigInt(1000) });
|
|
50
|
+
* // Returns: '{"amount":"1000"}'
|
|
51
|
+
*/
|
|
52
|
+
declare function makeJsonStringifiable<T>(value: unknown): T;
|
|
53
|
+
|
|
54
|
+
//#endregion
|
|
55
|
+
export { extractJsonObject, makeJsonStringifiable, tryParseJson };
|
|
56
|
+
//# sourceMappingURL=json.d.cts.map
|
package/dist/json.d.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
//#region src/json.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Attempts to parse a JSON string into a typed value, returning a default value if parsing fails.
|
|
4
|
+
*
|
|
5
|
+
* @param value - The JSON string to parse
|
|
6
|
+
* @param defaultValue - The value to return if parsing fails or results in null/undefined
|
|
7
|
+
* @returns The parsed JSON value as type T, or the default value if parsing fails
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* import { tryParseJson } from "@settlemint/sdk-utils";
|
|
11
|
+
*
|
|
12
|
+
* const config = tryParseJson<{ port: number }>(
|
|
13
|
+
* '{"port": 3000}',
|
|
14
|
+
* { port: 8080 }
|
|
15
|
+
* );
|
|
16
|
+
* // Returns: { port: 3000 }
|
|
17
|
+
*
|
|
18
|
+
* const invalid = tryParseJson<string[]>(
|
|
19
|
+
* 'invalid json',
|
|
20
|
+
* []
|
|
21
|
+
* );
|
|
22
|
+
* // Returns: []
|
|
23
|
+
*/
|
|
24
|
+
declare function tryParseJson<T>(value: string, defaultValue?: T | null): T | null;
|
|
25
|
+
/**
|
|
26
|
+
* Extracts a JSON object from a string.
|
|
27
|
+
*
|
|
28
|
+
* @param value - The string to extract the JSON object from
|
|
29
|
+
* @returns The parsed JSON object, or null if no JSON object is found
|
|
30
|
+
* @throws {Error} If the input string is too long (longer than 5000 characters)
|
|
31
|
+
* @example
|
|
32
|
+
* import { extractJsonObject } from "@settlemint/sdk-utils";
|
|
33
|
+
*
|
|
34
|
+
* const json = extractJsonObject<{ port: number }>(
|
|
35
|
+
* 'port info: {"port": 3000}',
|
|
36
|
+
* );
|
|
37
|
+
* // Returns: { port: 3000 }
|
|
38
|
+
*/
|
|
39
|
+
declare function extractJsonObject<T>(value: string): T | null;
|
|
40
|
+
/**
|
|
41
|
+
* Converts a value to a JSON stringifiable format.
|
|
42
|
+
*
|
|
43
|
+
* @param value - The value to convert
|
|
44
|
+
* @returns The JSON stringifiable value
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* import { makeJsonStringifiable } from "@settlemint/sdk-utils";
|
|
48
|
+
*
|
|
49
|
+
* const json = makeJsonStringifiable<{ amount: bigint }>({ amount: BigInt(1000) });
|
|
50
|
+
* // Returns: '{"amount":"1000"}'
|
|
51
|
+
*/
|
|
52
|
+
declare function makeJsonStringifiable<T>(value: unknown): T;
|
|
53
|
+
|
|
54
|
+
//#endregion
|
|
55
|
+
export { extractJsonObject, makeJsonStringifiable, tryParseJson };
|
|
56
|
+
//# sourceMappingURL=json.d.ts.map
|
package/dist/json.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
//#region src/json.ts
|
|
2
|
+
/**
|
|
3
|
+
* Attempts to parse a JSON string into a typed value, returning a default value if parsing fails.
|
|
4
|
+
*
|
|
5
|
+
* @param value - The JSON string to parse
|
|
6
|
+
* @param defaultValue - The value to return if parsing fails or results in null/undefined
|
|
7
|
+
* @returns The parsed JSON value as type T, or the default value if parsing fails
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* import { tryParseJson } from "@settlemint/sdk-utils";
|
|
11
|
+
*
|
|
12
|
+
* const config = tryParseJson<{ port: number }>(
|
|
13
|
+
* '{"port": 3000}',
|
|
14
|
+
* { port: 8080 }
|
|
15
|
+
* );
|
|
16
|
+
* // Returns: { port: 3000 }
|
|
17
|
+
*
|
|
18
|
+
* const invalid = tryParseJson<string[]>(
|
|
19
|
+
* 'invalid json',
|
|
20
|
+
* []
|
|
21
|
+
* );
|
|
22
|
+
* // Returns: []
|
|
23
|
+
*/
|
|
24
|
+
function tryParseJson(value, defaultValue = null) {
|
|
25
|
+
try {
|
|
26
|
+
const parsed = JSON.parse(value);
|
|
27
|
+
if (parsed === undefined || parsed === null) {
|
|
28
|
+
return defaultValue;
|
|
29
|
+
}
|
|
30
|
+
return parsed;
|
|
31
|
+
} catch (err) {
|
|
32
|
+
return defaultValue;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Extracts a JSON object from a string.
|
|
37
|
+
*
|
|
38
|
+
* @param value - The string to extract the JSON object from
|
|
39
|
+
* @returns The parsed JSON object, or null if no JSON object is found
|
|
40
|
+
* @throws {Error} If the input string is too long (longer than 5000 characters)
|
|
41
|
+
* @example
|
|
42
|
+
* import { extractJsonObject } from "@settlemint/sdk-utils";
|
|
43
|
+
*
|
|
44
|
+
* const json = extractJsonObject<{ port: number }>(
|
|
45
|
+
* 'port info: {"port": 3000}',
|
|
46
|
+
* );
|
|
47
|
+
* // Returns: { port: 3000 }
|
|
48
|
+
*/
|
|
49
|
+
function extractJsonObject(value) {
|
|
50
|
+
if (value.length > 5e3) {
|
|
51
|
+
throw new Error("Input too long");
|
|
52
|
+
}
|
|
53
|
+
const result = /\{([\s\S]*)\}/.exec(value);
|
|
54
|
+
if (!result) {
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
return tryParseJson(result[0]);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Converts a value to a JSON stringifiable format.
|
|
61
|
+
*
|
|
62
|
+
* @param value - The value to convert
|
|
63
|
+
* @returns The JSON stringifiable value
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* import { makeJsonStringifiable } from "@settlemint/sdk-utils";
|
|
67
|
+
*
|
|
68
|
+
* const json = makeJsonStringifiable<{ amount: bigint }>({ amount: BigInt(1000) });
|
|
69
|
+
* // Returns: '{"amount":"1000"}'
|
|
70
|
+
*/
|
|
71
|
+
function makeJsonStringifiable(value) {
|
|
72
|
+
if (value === undefined || value === null) {
|
|
73
|
+
return value;
|
|
74
|
+
}
|
|
75
|
+
return tryParseJson(JSON.stringify(value, (_, value$1) => typeof value$1 === "bigint" ? value$1.toString() : value$1));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
//#endregion
|
|
79
|
+
export { extractJsonObject, makeJsonStringifiable, tryParseJson };
|
|
80
|
+
//# sourceMappingURL=json.js.map
|
package/dist/json.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json.js","names":["value: string","defaultValue: T | null","value: unknown","value"],"sources":["../src/json.ts"],"sourcesContent":["/**\n * Attempts to parse a JSON string into a typed value, returning a default value if parsing fails.\n *\n * @param value - The JSON string to parse\n * @param defaultValue - The value to return if parsing fails or results in null/undefined\n * @returns The parsed JSON value as type T, or the default value if parsing fails\n *\n * @example\n * import { tryParseJson } from \"@settlemint/sdk-utils\";\n *\n * const config = tryParseJson<{ port: number }>(\n * '{\"port\": 3000}',\n * { port: 8080 }\n * );\n * // Returns: { port: 3000 }\n *\n * const invalid = tryParseJson<string[]>(\n * 'invalid json',\n * []\n * );\n * // Returns: []\n */\nexport function tryParseJson<T>(value: string, defaultValue: T | null = null): T | null {\n try {\n const parsed = JSON.parse(value) as T;\n if (parsed === undefined || parsed === null) {\n return defaultValue;\n }\n return parsed;\n } catch (err) {\n // Invalid json\n return defaultValue;\n }\n}\n\n/**\n * Extracts a JSON object from a string.\n *\n * @param value - The string to extract the JSON object from\n * @returns The parsed JSON object, or null if no JSON object is found\n * @throws {Error} If the input string is too long (longer than 5000 characters)\n * @example\n * import { extractJsonObject } from \"@settlemint/sdk-utils\";\n *\n * const json = extractJsonObject<{ port: number }>(\n * 'port info: {\"port\": 3000}',\n * );\n * // Returns: { port: 3000 }\n */\nexport function extractJsonObject<T>(value: string): T | null {\n if (value.length > 5000) {\n throw new Error(\"Input too long\");\n }\n const result = /\\{([\\s\\S]*)\\}/.exec(value);\n if (!result) {\n return null;\n }\n return tryParseJson<T>(result[0]);\n}\n\n/**\n * Converts a value to a JSON stringifiable format.\n *\n * @param value - The value to convert\n * @returns The JSON stringifiable value\n *\n * @example\n * import { makeJsonStringifiable } from \"@settlemint/sdk-utils\";\n *\n * const json = makeJsonStringifiable<{ amount: bigint }>({ amount: BigInt(1000) });\n * // Returns: '{\"amount\":\"1000\"}'\n */\nexport function makeJsonStringifiable<T>(value: unknown): T {\n if (value === undefined || value === null) {\n return value as T;\n }\n return tryParseJson<T>(\n JSON.stringify(\n value,\n (_, value) => (typeof value === \"bigint\" ? value.toString() : value), // return everything else unchanged\n ),\n ) as T;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAsBA,SAAgB,aAAgBA,OAAeC,eAAyB,MAAgB;AACtF,KAAI;EACF,MAAM,SAAS,KAAK,MAAM,MAAM;AAChC,MAAI,WAAW,aAAa,WAAW,MAAM;AAC3C,UAAO;EACR;AACD,SAAO;CACR,SAAQ,KAAK;AAEZ,SAAO;CACR;AACF;;;;;;;;;;;;;;;AAgBD,SAAgB,kBAAqBD,OAAyB;AAC5D,KAAI,MAAM,SAAS,KAAM;AACvB,QAAM,IAAI,MAAM;CACjB;CACD,MAAM,SAAS,gBAAgB,KAAK,MAAM;AAC1C,MAAK,QAAQ;AACX,SAAO;CACR;AACD,QAAO,aAAgB,OAAO,GAAG;AAClC;;;;;;;;;;;;;AAcD,SAAgB,sBAAyBE,OAAmB;AAC1D,KAAI,UAAU,aAAa,UAAU,MAAM;AACzC,SAAO;CACR;AACD,QAAO,aACL,KAAK,UACH,OACA,CAAC,GAAGC,mBAAkBA,YAAU,WAAW,QAAM,UAAU,GAAGA,QAC/D,CACF;AACF"}
|
package/dist/logging.cjs
CHANGED
|
@@ -1,152 +1,45 @@
|
|
|
1
|
-
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
8
|
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
17
|
};
|
|
18
|
-
var
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
value: mod,
|
|
20
|
+
enumerable: true
|
|
21
|
+
}) : target, mod));
|
|
19
22
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
maskTokens: () => maskTokens,
|
|
25
|
-
requestLogger: () => requestLogger
|
|
26
|
-
});
|
|
27
|
-
module.exports = __toCommonJS(logging_exports);
|
|
28
|
-
|
|
29
|
-
// src/logging/mask-tokens.ts
|
|
30
|
-
var maskTokens = (output) => {
|
|
31
|
-
return output.replace(/sm_(pat|aat|sat)_[0-9a-zA-Z]+/g, "***");
|
|
32
|
-
};
|
|
23
|
+
//#endregion
|
|
24
|
+
const src_logging_logger_js = __toESM(require("./logging/logger.js"));
|
|
25
|
+
const src_logging_request_logger_js = __toESM(require("./logging/request-logger.js"));
|
|
26
|
+
const src_logging_mask_tokens_js = __toESM(require("./logging/mask-tokens.js"));
|
|
33
27
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
debug: 0,
|
|
39
|
-
info: 1,
|
|
40
|
-
warn: 2,
|
|
41
|
-
error: 3,
|
|
42
|
-
none: 4
|
|
43
|
-
};
|
|
44
|
-
const currentLevelValue = logLevels[level];
|
|
45
|
-
const formatArgs = (args) => {
|
|
46
|
-
if (args.length === 0 || args.every((arg) => arg === void 0 || arg === null)) {
|
|
47
|
-
return "";
|
|
48
|
-
}
|
|
49
|
-
const formatted = args.map((arg) => {
|
|
50
|
-
if (arg instanceof Error) {
|
|
51
|
-
return `
|
|
52
|
-
${arg.stack || arg.message}`;
|
|
53
|
-
}
|
|
54
|
-
if (typeof arg === "object" && arg !== null) {
|
|
55
|
-
return `
|
|
56
|
-
${JSON.stringify(arg, null, 2)}`;
|
|
57
|
-
}
|
|
58
|
-
return ` ${String(arg)}`;
|
|
59
|
-
}).join("");
|
|
60
|
-
return `, args:${formatted}`;
|
|
61
|
-
};
|
|
62
|
-
const shouldLog = (level2) => {
|
|
63
|
-
return logLevels[level2] >= currentLevelValue;
|
|
64
|
-
};
|
|
65
|
-
return {
|
|
66
|
-
debug: (message, ...args) => {
|
|
67
|
-
if (shouldLog("debug")) {
|
|
68
|
-
console.debug(`\x1B[32m${prefix}[DEBUG] ${maskTokens(message)}${maskTokens(formatArgs(args))}\x1B[0m`);
|
|
69
|
-
}
|
|
70
|
-
},
|
|
71
|
-
info: (message, ...args) => {
|
|
72
|
-
if (shouldLog("info")) {
|
|
73
|
-
console.info(`\x1B[34m${prefix}[INFO] ${maskTokens(message)}${maskTokens(formatArgs(args))}\x1B[0m`);
|
|
74
|
-
}
|
|
75
|
-
},
|
|
76
|
-
warn: (message, ...args) => {
|
|
77
|
-
if (shouldLog("warn")) {
|
|
78
|
-
console.warn(`\x1B[33m${prefix}[WARN] ${maskTokens(message)}${maskTokens(formatArgs(args))}\x1B[0m`);
|
|
79
|
-
}
|
|
80
|
-
},
|
|
81
|
-
error: (message, ...args) => {
|
|
82
|
-
if (shouldLog("error")) {
|
|
83
|
-
console.error(`\x1B[31m${prefix}[ERROR] ${maskTokens(message)}${maskTokens(formatArgs(args))}\x1B[0m`);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
var logger = createLogger();
|
|
89
|
-
|
|
90
|
-
// src/string.ts
|
|
91
|
-
function truncate(value, maxLength) {
|
|
92
|
-
if (value.length <= maxLength) {
|
|
93
|
-
return value;
|
|
28
|
+
Object.defineProperty(exports, 'createLogger', {
|
|
29
|
+
enumerable: true,
|
|
30
|
+
get: function () {
|
|
31
|
+
return src_logging_logger_js.createLogger;
|
|
94
32
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
var TRUNCATE_LENGTH = 50;
|
|
101
|
-
function requestLogger(logger2, name, fn) {
|
|
102
|
-
return async (...args) => {
|
|
103
|
-
const start = Date.now();
|
|
104
|
-
try {
|
|
105
|
-
return await fn(...args);
|
|
106
|
-
} finally {
|
|
107
|
-
const end = Date.now();
|
|
108
|
-
const duration = end - start;
|
|
109
|
-
const body = extractInfoFromBody(args[1]?.body ?? "{}");
|
|
110
|
-
const message = `${name} path: ${args[0]}, took ${formatDuration(duration)}`;
|
|
111
|
-
if (duration > WARNING_THRESHOLD) {
|
|
112
|
-
logger2.warn(message, body);
|
|
113
|
-
} else {
|
|
114
|
-
logger2.info(message, body);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
function formatDuration(duration) {
|
|
120
|
-
return duration < 1e3 ? `${duration}ms` : `${(duration / 1e3).toFixed(3)}s`;
|
|
121
|
-
}
|
|
122
|
-
function extractInfoFromBody(body) {
|
|
123
|
-
try {
|
|
124
|
-
const parsedBody = typeof body === "string" ? JSON.parse(body) : body;
|
|
125
|
-
if (parsedBody === null || parsedBody === void 0 || Object.keys(parsedBody).length === 0) {
|
|
126
|
-
return null;
|
|
127
|
-
}
|
|
128
|
-
const dataToKeep = {};
|
|
129
|
-
if ("query" in parsedBody) {
|
|
130
|
-
dataToKeep.query = truncate(parsedBody.query, TRUNCATE_LENGTH);
|
|
131
|
-
}
|
|
132
|
-
if ("variables" in parsedBody) {
|
|
133
|
-
dataToKeep.variables = truncate(JSON.stringify(parsedBody.variables), TRUNCATE_LENGTH);
|
|
134
|
-
}
|
|
135
|
-
if ("operationName" in parsedBody) {
|
|
136
|
-
dataToKeep.operationName = truncate(parsedBody.operationName, TRUNCATE_LENGTH);
|
|
137
|
-
}
|
|
138
|
-
if (Object.keys(dataToKeep).length > 0) {
|
|
139
|
-
return JSON.stringify(dataToKeep);
|
|
140
|
-
}
|
|
141
|
-
return truncate(JSON.stringify(parsedBody || "{}"), TRUNCATE_LENGTH);
|
|
142
|
-
} catch {
|
|
143
|
-
return "{}";
|
|
33
|
+
});
|
|
34
|
+
Object.defineProperty(exports, 'maskTokens', {
|
|
35
|
+
enumerable: true,
|
|
36
|
+
get: function () {
|
|
37
|
+
return src_logging_mask_tokens_js.maskTokens;
|
|
144
38
|
}
|
|
145
|
-
}
|
|
146
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
147
|
-
0 && (module.exports = {
|
|
148
|
-
createLogger,
|
|
149
|
-
maskTokens,
|
|
150
|
-
requestLogger
|
|
151
39
|
});
|
|
152
|
-
|
|
40
|
+
Object.defineProperty(exports, 'requestLogger', {
|
|
41
|
+
enumerable: true,
|
|
42
|
+
get: function () {
|
|
43
|
+
return src_logging_request_logger_js.requestLogger;
|
|
44
|
+
}
|
|
45
|
+
});
|