@chubbyts/chubbyts-decode-encode 1.0.2 → 1.1.0
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 +12 -10
- package/dist/decoder/index.d.ts +3 -1
- 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/yaml-type-decoder.js +11 -1
- package/dist/encoder/index.d.ts +3 -1
- package/dist/encoder/index.js +4 -1
- package/dist/encoder/jsonx-type-encoder.js +23 -13
- package/package.json +2 -1
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.1.0
|
|
37
38
|
```
|
|
38
39
|
|
|
39
40
|
## Usage
|
|
@@ -47,7 +48,7 @@ import { createDecoder } from '@chubbyts/chubbyts-decode-encode/dist/decoder';
|
|
|
47
48
|
import { createJsonTypeDecoder } from '@chubbyts/chubbyts-decode-encode/dist/decoder/json-type-decoder';
|
|
48
49
|
|
|
49
50
|
const decoder = createDecoder([createJsonTypeDecoder()]);
|
|
50
|
-
const data = decoder.decode('{"key":"value"}', 'application/json');
|
|
51
|
+
const data = decoder.decode('{"key":"value"}', 'application/json'); // or with 3th argument, for example { user: 'username1' }
|
|
51
52
|
// data: {key: "value"}
|
|
52
53
|
const contentTypes = decoder.contentTypes;
|
|
53
54
|
// contentTypes: ['application/json']
|
|
@@ -87,7 +88,7 @@ const contentType = jsonxTypeDecoder.contentType;
|
|
|
87
88
|
```ts
|
|
88
89
|
import { createUrlEncodedTypeDecoder } from '@chubbyts/chubbyts-decode-encode/dist/decoder/url-encoded-type-decoder';
|
|
89
90
|
|
|
90
|
-
const urlEncodedTypeDecoder = createUrlEncodedTypeDecoder()
|
|
91
|
+
const urlEncodedTypeDecoder = createUrlEncodedTypeDecoder();
|
|
91
92
|
const data = urlEncodedTypeDecoder.decode('key=value');
|
|
92
93
|
// data: {key: "value"}
|
|
93
94
|
const contentType = urlEncodedTypeDecoder.contentType;
|
|
@@ -115,7 +116,7 @@ import { createEncoder } from '@chubbyts/chubbyts-encode-encode/dist/encoder';
|
|
|
115
116
|
import { createJsonTypeEncoder } from '@chubbyts/chubbyts-encode-encode/dist/encoder/json-type-encoder';
|
|
116
117
|
|
|
117
118
|
const encoder = createEncoder([createJsonTypeEncoder()]);
|
|
118
|
-
const encodedData = encoder.encode({key: "value"}, 'application/json');
|
|
119
|
+
const encodedData = encoder.encode({key: "value"}, 'application/json'); // or with 3th argument, for example { user: 'username1' }
|
|
119
120
|
// encodedData: {"key":"value"}
|
|
120
121
|
const contentTypes = encoder.contentTypes;
|
|
121
122
|
// contentTypes: ['application/json']
|
|
@@ -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
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { Data } from '..';
|
|
2
|
+
export declare class DecodeError extends Error {
|
|
3
|
+
}
|
|
2
4
|
export declare type TypeDecoder = {
|
|
3
5
|
decode: (encodedData: string) => Data;
|
|
4
6
|
contentType: string;
|
|
5
7
|
};
|
|
6
8
|
export declare type Decoder = {
|
|
7
|
-
decode: (encodedData: string, contentType: string) => Data;
|
|
9
|
+
decode: (encodedData: string, contentType: string, context?: Record<string, unknown>) => Data;
|
|
8
10
|
contentTypes: Array<string>;
|
|
9
11
|
};
|
|
10
12
|
export declare const createDecoder: (decoderTypes: Array<TypeDecoder>) => Decoder;
|
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,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
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { Data } from '..';
|
|
2
|
+
export declare class EncodeError extends Error {
|
|
3
|
+
}
|
|
2
4
|
export declare type TypeEncoder = {
|
|
3
5
|
encode: (data: Data) => string;
|
|
4
6
|
contentType: string;
|
|
5
7
|
};
|
|
6
8
|
export declare type Encoder = {
|
|
7
|
-
encode: (data: Data, contentType: string) => string;
|
|
9
|
+
encode: (data: Data, contentType: string, context?: Record<string, unknown>) => string;
|
|
8
10
|
contentTypes: Array<string>;
|
|
9
11
|
};
|
|
10
12
|
export declare const createEncoder: (encoderTypes: Array<TypeEncoder>) => Encoder;
|
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.EncodeError = void 0;
|
|
4
|
+
class EncodeError extends Error {
|
|
5
|
+
}
|
|
6
|
+
exports.EncodeError = EncodeError;
|
|
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.EncodeError(error.message);
|
|
171
|
+
decodeError.stack = error.stack;
|
|
172
|
+
throw decodeError;
|
|
173
|
+
}
|
|
164
174
|
};
|
|
165
175
|
const contentType = 'application/jsonx+xml';
|
|
166
176
|
return {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chubbyts/chubbyts-decode-encode",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "A simple decode/encode solution for json / jsonx / url-encoded / xml / yaml.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"chubbyts",
|
|
@@ -48,6 +48,7 @@
|
|
|
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"
|