@chubbyts/chubbyts-decode-encode 1.0.0 → 1.0.3
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 +9 -7
- package/dist/decoder/index.d.ts +2 -0
- package/dist/decoder/index.js +4 -1
- package/dist/decoder/json-type-decoder.js +11 -1
- package/dist/decoder/jsonx-type-decoder.js +12 -3
- package/dist/decoder/url-encoded-type-decoder.js +10 -10
- package/dist/decoder/yaml-type-decoder.js +11 -1
- package/dist/encoder/index.d.ts +2 -0
- package/dist/encoder/index.js +4 -1
- package/dist/encoder/jsonx-type-encoder.js +23 -13
- package/dist/encoder/url-encoded-type-encoder.js +13 -28
- package/dist/encoder/yaml-type-encoder.js +1 -1
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -24,16 +24,17 @@ A simple decode/encode solution for json / jsonx / url-encoded / xml / yaml.
|
|
|
24
24
|
## Requirements
|
|
25
25
|
|
|
26
26
|
* node: 14
|
|
27
|
-
* [
|
|
28
|
-
* [
|
|
29
|
-
* [
|
|
27
|
+
* [@chubbyts/chubbyts-throwable-to-error][2]: ^1.0.0
|
|
28
|
+
* [fast-xml-parser][3]: ^4.0.8
|
|
29
|
+
* [qs][4]: ^6.10.5
|
|
30
|
+
* [yaml][5]: ^2.1.1
|
|
30
31
|
|
|
31
32
|
## Installation
|
|
32
33
|
|
|
33
34
|
Through [NPM](https://www.npmjs.com) as [@chubbyts/chubbyts-decode-encode][1].
|
|
34
35
|
|
|
35
36
|
```ts
|
|
36
|
-
npm i @chubbyts/chubbyts-decode-encode@^1.0.
|
|
37
|
+
npm i @chubbyts/chubbyts-decode-encode@^1.0.3
|
|
37
38
|
```
|
|
38
39
|
|
|
39
40
|
## Usage
|
|
@@ -177,6 +178,7 @@ const contentType = yamlTypeEncoder.contentType;
|
|
|
177
178
|
Dominik Zogg 2022
|
|
178
179
|
|
|
179
180
|
[1]: https://www.npmjs.com/package/@chubbyts/chubbyts-decode-encode
|
|
180
|
-
[2]: https://www.npmjs.com/package/
|
|
181
|
-
[3]: https://www.npmjs.com/package/
|
|
182
|
-
[4]: https://www.npmjs.com/package/
|
|
181
|
+
[2]: https://www.npmjs.com/package/@chubbyts/chubbyts-throwable-to-error
|
|
182
|
+
[3]: https://www.npmjs.com/package/fast-xml-parser
|
|
183
|
+
[4]: https://www.npmjs.com/package/qs
|
|
184
|
+
[5]: https://www.npmjs.com/package/yaml
|
package/dist/decoder/index.d.ts
CHANGED
package/dist/decoder/index.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createDecoder = void 0;
|
|
3
|
+
exports.createDecoder = exports.DecodeError = void 0;
|
|
4
|
+
class DecodeError extends Error {
|
|
5
|
+
}
|
|
6
|
+
exports.DecodeError = DecodeError;
|
|
4
7
|
const createDecoder = (decoderTypes) => {
|
|
5
8
|
const decoderTypeMap = new Map(decoderTypes.map((DecoderType) => [DecoderType.contentType, DecoderType]));
|
|
6
9
|
const contentTypes = Array.from(decoderTypeMap.keys());
|
|
@@ -1,9 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createJsonTypeDecoder = void 0;
|
|
4
|
+
const throwable_to_error_1 = require("@chubbyts/chubbyts-throwable-to-error/dist/throwable-to-error");
|
|
5
|
+
const _1 = require(".");
|
|
4
6
|
const createJsonTypeDecoder = () => {
|
|
5
7
|
const decode = (encodedData) => {
|
|
6
|
-
|
|
8
|
+
try {
|
|
9
|
+
return JSON.parse(encodedData);
|
|
10
|
+
}
|
|
11
|
+
catch (e) {
|
|
12
|
+
const error = (0, throwable_to_error_1.throwableToError)(e);
|
|
13
|
+
const decodeError = new _1.DecodeError(error.message);
|
|
14
|
+
decodeError.stack = error.stack;
|
|
15
|
+
throw decodeError;
|
|
16
|
+
}
|
|
7
17
|
};
|
|
8
18
|
const contentType = 'application/json';
|
|
9
19
|
return {
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createJsonxTypeDecoder = void 0;
|
|
4
|
+
const _1 = require(".");
|
|
4
5
|
const fast_xml_parser_1 = require("fast-xml-parser");
|
|
5
6
|
const jsonx_datatypes_1 = require("../jsonx-datatypes");
|
|
7
|
+
const throwable_to_error_1 = require("@chubbyts/chubbyts-throwable-to-error/dist/throwable-to-error");
|
|
6
8
|
const decodeHtmlEntities = (string) => string.replace(/&#\d+;/gm, (code) => String.fromCharCode(code.match(/\d+/)[0]));
|
|
7
9
|
const isWithName = (node) => typeof node === 'object' &&
|
|
8
10
|
null !== node &&
|
|
@@ -77,9 +79,16 @@ const convertObjectNode = (node) => {
|
|
|
77
79
|
};
|
|
78
80
|
const createJsonxTypeDecoder = () => {
|
|
79
81
|
const decode = (encodedData) => {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
82
|
+
try {
|
|
83
|
+
const parser = new fast_xml_parser_1.XMLParser({ preserveOrder: true, ignoreAttributes: false, htmlEntities: false });
|
|
84
|
+
return convertNode(parser.parse(encodedData)[1]);
|
|
85
|
+
}
|
|
86
|
+
catch (e) {
|
|
87
|
+
const error = (0, throwable_to_error_1.throwableToError)(e);
|
|
88
|
+
const decodeError = new _1.DecodeError(error.message);
|
|
89
|
+
decodeError.stack = error.stack;
|
|
90
|
+
throw decodeError;
|
|
91
|
+
}
|
|
83
92
|
};
|
|
84
93
|
const contentType = 'application/jsonx+xml';
|
|
85
94
|
return {
|
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createUrlEncodedTypeDecoder = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
4
|
+
const __1 = require("..");
|
|
5
|
+
const qs_1 = require("qs");
|
|
6
|
+
const decodeValue = (value) => {
|
|
7
|
+
if ((0, __1.isObject)(value)) {
|
|
8
|
+
return Object.fromEntries(Object.entries(value).map(([subKey, subValue]) => [subKey, decodeValue(subValue)]));
|
|
9
|
+
}
|
|
10
|
+
if ((0, __1.isArray)(value)) {
|
|
11
|
+
return value.map(decodeValue);
|
|
12
|
+
}
|
|
6
13
|
if (value === 'null') {
|
|
7
14
|
return null;
|
|
8
15
|
}
|
|
@@ -24,14 +31,7 @@ const optimizeValue = (value) => {
|
|
|
24
31
|
};
|
|
25
32
|
const createUrlEncodedTypeDecoder = () => {
|
|
26
33
|
const decode = (encodedData) => {
|
|
27
|
-
|
|
28
|
-
encodedData.split('&').forEach((keyValue) => {
|
|
29
|
-
const [rawKey, rawValue] = keyValue.split('=');
|
|
30
|
-
const key = rawKey.replace(/\[[^\]]+\]/gm, (match) => `.${match.match(/[^\[\]]+/)[0]}`);
|
|
31
|
-
const value = optimizeValue(decodeURIComponent(rawValue));
|
|
32
|
-
(0, object_path_1.set)(data, key, value);
|
|
33
|
-
});
|
|
34
|
-
return data;
|
|
34
|
+
return decodeValue((0, qs_1.parse)(encodedData, { depth: 100 }));
|
|
35
35
|
};
|
|
36
36
|
const contentType = 'application/x-www-form-urlencoded';
|
|
37
37
|
return {
|
|
@@ -1,10 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createYamlTypeDecoder = void 0;
|
|
4
|
+
const _1 = require(".");
|
|
4
5
|
const yaml_1 = require("yaml");
|
|
6
|
+
const throwable_to_error_1 = require("@chubbyts/chubbyts-throwable-to-error/dist/throwable-to-error");
|
|
5
7
|
const createYamlTypeDecoder = () => {
|
|
6
8
|
const decode = (encodedData) => {
|
|
7
|
-
|
|
9
|
+
try {
|
|
10
|
+
return (0, yaml_1.parse)(encodedData);
|
|
11
|
+
}
|
|
12
|
+
catch (e) {
|
|
13
|
+
const error = (0, throwable_to_error_1.throwableToError)(e);
|
|
14
|
+
const decodeError = new _1.DecodeError(error.message);
|
|
15
|
+
decodeError.stack = error.stack;
|
|
16
|
+
throw decodeError;
|
|
17
|
+
}
|
|
8
18
|
};
|
|
9
19
|
const contentType = 'application/x-yaml';
|
|
10
20
|
return {
|
package/dist/encoder/index.d.ts
CHANGED
package/dist/encoder/index.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createEncoder = void 0;
|
|
3
|
+
exports.createEncoder = exports.EncoderError = void 0;
|
|
4
|
+
class EncoderError extends Error {
|
|
5
|
+
}
|
|
6
|
+
exports.EncoderError = EncoderError;
|
|
4
7
|
const createEncoder = (encoderTypes) => {
|
|
5
8
|
const encoderTypeMap = new Map(encoderTypes.map((encoderType) => [encoderType.contentType, encoderType]));
|
|
6
9
|
const contentTypes = Array.from(encoderTypeMap.keys());
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createJsonxTypeEncoder = void 0;
|
|
4
|
+
const _1 = require(".");
|
|
4
5
|
const __1 = require("..");
|
|
5
6
|
const jsonx_datatypes_1 = require("../jsonx-datatypes");
|
|
6
7
|
const fast_xml_parser_1 = require("fast-xml-parser");
|
|
8
|
+
const throwable_to_error_1 = require("@chubbyts/chubbyts-throwable-to-error/dist/throwable-to-error");
|
|
7
9
|
const encodeHtmlEntities = (string) => string.replace(/[\u00A0-\u9999<>\&]/g, (i) => '&#' + i.charCodeAt(0) + ';');
|
|
8
10
|
const createXmlNode = () => {
|
|
9
11
|
return {
|
|
@@ -147,20 +149,28 @@ const createObjectNode = (value, key = undefined) => {
|
|
|
147
149
|
};
|
|
148
150
|
};
|
|
149
151
|
const createJsonxTypeEncoder = (prettyPrint = false) => {
|
|
152
|
+
const builder = new fast_xml_parser_1.XMLBuilder({
|
|
153
|
+
preserveOrder: true,
|
|
154
|
+
ignoreAttributes: false,
|
|
155
|
+
processEntities: false,
|
|
156
|
+
format: prettyPrint,
|
|
157
|
+
});
|
|
150
158
|
const encode = (data) => {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
159
|
+
try {
|
|
160
|
+
return builder.build([
|
|
161
|
+
createXmlNode(),
|
|
162
|
+
{
|
|
163
|
+
...createNode(data),
|
|
164
|
+
...createJsonxAttributes(),
|
|
165
|
+
},
|
|
166
|
+
]);
|
|
167
|
+
}
|
|
168
|
+
catch (e) {
|
|
169
|
+
const error = (0, throwable_to_error_1.throwableToError)(e);
|
|
170
|
+
const decodeError = new _1.EncoderError(error.message);
|
|
171
|
+
decodeError.stack = error.stack;
|
|
172
|
+
throw decodeError;
|
|
173
|
+
}
|
|
164
174
|
};
|
|
165
175
|
const contentType = 'application/jsonx+xml';
|
|
166
176
|
return {
|
|
@@ -1,44 +1,29 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createUrlEncodedTypeEncoder = void 0;
|
|
4
|
+
const qs_1 = require("qs");
|
|
4
5
|
const __1 = require("..");
|
|
5
|
-
const
|
|
6
|
-
if (
|
|
7
|
-
return
|
|
6
|
+
const encodeValue = (value) => {
|
|
7
|
+
if ((0, __1.isObject)(value)) {
|
|
8
|
+
return Object.fromEntries(Object.entries(value).map(([subKey, subValue]) => [subKey, encodeValue(subValue)]));
|
|
8
9
|
}
|
|
9
|
-
if (
|
|
10
|
-
return value
|
|
10
|
+
if ((0, __1.isArray)(value)) {
|
|
11
|
+
return value.map(encodeValue);
|
|
11
12
|
}
|
|
12
|
-
if (
|
|
13
|
+
if ((0, __1.isNumber)(value)) {
|
|
13
14
|
return value.toString();
|
|
14
15
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const getKeyValue = (path, key, value) => {
|
|
18
|
-
const subPath = '' !== path ? path + '[' + key + ']' : key;
|
|
19
|
-
if ((0, __1.isObject)(value) || (0, __1.isArray)(value)) {
|
|
20
|
-
const queryPart = buildQuery(value, subPath);
|
|
21
|
-
return queryPart !== '' ? queryPart + '&' : '';
|
|
22
|
-
}
|
|
23
|
-
return subPath + '=' + encodeURIComponent(getValueAsString(value)) + '&';
|
|
24
|
-
};
|
|
25
|
-
const buildQuery = (data, path = '') => {
|
|
26
|
-
let query = '';
|
|
27
|
-
if ((0, __1.isObject)(data)) {
|
|
28
|
-
Object.keys(data).forEach((key) => {
|
|
29
|
-
query += getKeyValue(path, key, data[key]);
|
|
30
|
-
});
|
|
16
|
+
if ((0, __1.isBoolean)(value)) {
|
|
17
|
+
return value ? 'true' : 'false';
|
|
31
18
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
query += getKeyValue(path, i.toString(), value);
|
|
35
|
-
});
|
|
19
|
+
if ((0, __1.isNull)(value)) {
|
|
20
|
+
return 'null';
|
|
36
21
|
}
|
|
37
|
-
return
|
|
22
|
+
return value;
|
|
38
23
|
};
|
|
39
24
|
const createUrlEncodedTypeEncoder = () => {
|
|
40
25
|
const encode = (data) => {
|
|
41
|
-
return
|
|
26
|
+
return (0, qs_1.stringify)(encodeValue(data), { encodeValuesOnly: true });
|
|
42
27
|
};
|
|
43
28
|
const contentType = 'application/x-www-form-urlencoded';
|
|
44
29
|
return {
|
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.createYamlTypeEncoder = void 0;
|
|
4
4
|
const yaml_1 = require("yaml");
|
|
5
5
|
const createYamlTypeEncoder = () => {
|
|
6
|
-
const encode = (data) => (0, yaml_1.stringify)(
|
|
6
|
+
const encode = (data) => (0, yaml_1.stringify)(data, { indent: 4 });
|
|
7
7
|
const contentType = 'application/x-yaml';
|
|
8
8
|
return {
|
|
9
9
|
encode,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chubbyts/chubbyts-decode-encode",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "A simple decode/encode solution for json / jsonx / url-encoded / xml / yaml.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"chubbyts",
|
|
@@ -48,8 +48,9 @@
|
|
|
48
48
|
"node": ">=14"
|
|
49
49
|
},
|
|
50
50
|
"dependencies": {
|
|
51
|
+
"@chubbyts/chubbyts-throwable-to-error": "^1.0.0",
|
|
51
52
|
"fast-xml-parser": "^4.0.8",
|
|
52
|
-
"
|
|
53
|
+
"qs": "^6.10.5",
|
|
53
54
|
"yaml": "^2.1.1"
|
|
54
55
|
},
|
|
55
56
|
"devDependencies": {
|
|
@@ -57,7 +58,7 @@
|
|
|
57
58
|
"@stryker-mutator/jest-runner": "^6.0.2",
|
|
58
59
|
"@types/jest": "^27.5.1",
|
|
59
60
|
"@types/node": "^14",
|
|
60
|
-
"@types/
|
|
61
|
+
"@types/qs": "^6.9.7",
|
|
61
62
|
"jest": "^28.1.0",
|
|
62
63
|
"prettier": "^2.6.2",
|
|
63
64
|
"ts-jest": "^28.0.3",
|