@chubbyts/chubbyts-decode-encode 2.5.0 → 2.5.2
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 +35 -34
- package/dist/service-factory.d.ts +4 -35
- package/dist/service-factory.js +17 -60
- package/package.json +18 -18
package/README.md
CHANGED
|
@@ -24,20 +24,20 @@ A simple decode/encode solution for json / jsonx / url-encoded / xml / yaml.
|
|
|
24
24
|
## Requirements
|
|
25
25
|
|
|
26
26
|
* node: 22
|
|
27
|
-
* [@chubbyts/chubbyts-dic-config-factory][9]: ^1.0.
|
|
28
|
-
* [@chubbyts/chubbyts-dic-types][7]: ^2.3.
|
|
29
|
-
* [@chubbyts/chubbyts-throwable-to-error][2]: ^2.3.
|
|
30
|
-
* [fast-xml-builder][3]: ^1.3.
|
|
31
|
-
* [fast-xml-parser][4]: ^5.
|
|
32
|
-
* [qs][5]: ^6.
|
|
33
|
-
* [yaml][6]: ^2.9.
|
|
27
|
+
* [@chubbyts/chubbyts-dic-config-factory][9]: ^1.0.1
|
|
28
|
+
* [@chubbyts/chubbyts-dic-types][7]: ^2.3.1
|
|
29
|
+
* [@chubbyts/chubbyts-throwable-to-error][2]: ^2.3.4
|
|
30
|
+
* [fast-xml-builder][3]: ^1.3.1
|
|
31
|
+
* [fast-xml-parser][4]: ^5.11.2
|
|
32
|
+
* [qs][5]: ^6.16.1
|
|
33
|
+
* [yaml][6]: ^2.9.1
|
|
34
34
|
|
|
35
35
|
## Installation
|
|
36
36
|
|
|
37
37
|
Through [NPM](https://www.npmjs.com) as [@chubbyts/chubbyts-decode-encode][1].
|
|
38
38
|
|
|
39
39
|
```ts
|
|
40
|
-
npm i @chubbyts/chubbyts-decode-encode@^2.5.
|
|
40
|
+
npm i @chubbyts/chubbyts-decode-encode@^2.5.2
|
|
41
41
|
```
|
|
42
42
|
|
|
43
43
|
## Usage
|
|
@@ -178,28 +178,22 @@ const contentType = yamlTypeEncoder.contentType;
|
|
|
178
178
|
|
|
179
179
|
### Service factories (chubbyts-dic-config)
|
|
180
180
|
|
|
181
|
-
The package ships service factories (abstract factories built on [chubbyts-dic-config-factory][9]) for a [chubbyts-dic-config][8] (or any [chubbyts-dic-types][7] compatible) container within `@chubbyts/chubbyts-decode-encode/dist/service-factory
|
|
181
|
+
The package ships service factories (abstract factories built on [chubbyts-dic-config-factory][9]) for a [chubbyts-dic-config][8] (or any [chubbyts-dic-types][7] compatible) container within `@chubbyts/chubbyts-decode-encode/dist/service-factory`:
|
|
182
|
+
|
|
183
|
+
* `typeDecodersServiceFactory`: all shipped type decoders (json, jsonx, urlencoded, yaml)
|
|
184
|
+
* `typeEncodersServiceFactory`: all shipped type encoders (json, jsonx, urlencoded, yaml), `config.debug` enables pretty printing for json / jsonx
|
|
185
|
+
* `decoderServiceFactory`: the decoder, built from the `typeDecoders` service if registered, the shipped type decoders otherwise
|
|
186
|
+
* `encoderServiceFactory`: the encoder, built from the `typeEncoders` service if registered, the shipped type encoders otherwise
|
|
182
187
|
|
|
183
188
|
```ts
|
|
184
189
|
import type { ConfigFactory } from '@chubbyts/chubbyts-dic-config/dist/dic-config';
|
|
185
190
|
import { createContainerByConfigFactory } from '@chubbyts/chubbyts-dic-config/dist/dic-config';
|
|
186
191
|
import type { Decoder } from '@chubbyts/chubbyts-decode-encode/dist/decoder/decoder';
|
|
187
192
|
import type { Encoder } from '@chubbyts/chubbyts-decode-encode/dist/encoder/encoder';
|
|
188
|
-
import type { DecodeEncodeConfig } from '@chubbyts/chubbyts-decode-encode/dist/service-factory';
|
|
189
193
|
import { decoderServiceFactory, encoderServiceFactory } from '@chubbyts/chubbyts-decode-encode/dist/service-factory';
|
|
190
194
|
|
|
191
195
|
const container = createContainerByConfigFactory({
|
|
192
|
-
|
|
193
|
-
decodeEncode: {
|
|
194
|
-
decoder: {
|
|
195
|
-
// typeDecoders: ['jsonTypeDecoder', 'jsonxTypeDecoder', 'urlEncodedTypeDecoder', 'yamlTypeDecoder'],
|
|
196
|
-
},
|
|
197
|
-
encoder: {
|
|
198
|
-
// typeEncoders: ['jsonTypeEncoder', 'jsonxTypeEncoder', 'urlEncodedTypeEncoder', 'yamlTypeEncoder'],
|
|
199
|
-
// prettyPrint: false,
|
|
200
|
-
},
|
|
201
|
-
} satisfies DecodeEncodeConfig,
|
|
202
|
-
},
|
|
196
|
+
debug: false,
|
|
203
197
|
dependencies: {
|
|
204
198
|
factories: new Map<string, ConfigFactory>([
|
|
205
199
|
['decoder', decoderServiceFactory()],
|
|
@@ -212,32 +206,39 @@ const decoder = container.get<Decoder>('decoder');
|
|
|
212
206
|
const encoder = container.get<Encoder>('encoder');
|
|
213
207
|
```
|
|
214
208
|
|
|
215
|
-
|
|
209
|
+
To replace the type decoders / encoders, register a `typeDecoders` / `typeEncoders` service:
|
|
210
|
+
|
|
211
|
+
```ts
|
|
212
|
+
import type { TypeDecoder } from '@chubbyts/chubbyts-decode-encode/dist/decoder/decoder';
|
|
213
|
+
import { createJsonTypeDecoder } from '@chubbyts/chubbyts-decode-encode/dist/decoder/json-type-decoder';
|
|
214
|
+
|
|
215
|
+
const container = createContainerByConfigFactory({
|
|
216
|
+
dependencies: {
|
|
217
|
+
factories: new Map<string, ConfigFactory>([
|
|
218
|
+
['decoder', decoderServiceFactory()],
|
|
219
|
+
['typeDecoders', (): Array<TypeDecoder> => [createJsonTypeDecoder(), createCustomTypeDecoder()]],
|
|
220
|
+
]),
|
|
221
|
+
},
|
|
222
|
+
})();
|
|
223
|
+
```
|
|
216
224
|
|
|
217
225
|
#### With names
|
|
218
226
|
|
|
219
|
-
To use different decoders / encoders in different parts of an application, the same factories can be registered multiple times with a name
|
|
227
|
+
To use different decoders / encoders in different parts of an application, the same factories can be registered multiple times with a name, which gets appended to each service id (`decoderapi`, `typeDecodersapi`, ...).
|
|
220
228
|
|
|
221
229
|
```ts
|
|
222
230
|
const container = createContainerByConfigFactory({
|
|
223
|
-
chubbyts: {
|
|
224
|
-
decodeEncode: {
|
|
225
|
-
api: { decoder: { typeDecoders: ['jsonTypeDecoder'] }, encoder: { typeEncoders: ['jsonTypeEncoder'] } },
|
|
226
|
-
admin: { encoder: { prettyPrint: true } },
|
|
227
|
-
} satisfies Record<string, DecodeEncodeConfig>,
|
|
228
|
-
},
|
|
229
231
|
dependencies: {
|
|
230
232
|
factories: new Map<string, ConfigFactory>([
|
|
233
|
+
['decoder', decoderServiceFactory()],
|
|
231
234
|
['decoderapi', decoderServiceFactory('api')],
|
|
232
|
-
['
|
|
233
|
-
['decoderadmin', decoderServiceFactory('admin')],
|
|
234
|
-
['encoderadmin', encoderServiceFactory('admin')],
|
|
235
|
+
['typeDecodersapi', (): Array<TypeDecoder> => [createJsonTypeDecoder()]],
|
|
235
236
|
]),
|
|
236
237
|
},
|
|
237
238
|
})();
|
|
238
239
|
|
|
239
|
-
const
|
|
240
|
-
const
|
|
240
|
+
const decoder = container.get<Decoder>('decoder'); // all shipped type decoders
|
|
241
|
+
const apiDecoder = container.get<Decoder>('decoderapi'); // json only
|
|
241
242
|
```
|
|
242
243
|
|
|
243
244
|
## Copyright
|
|
@@ -1,37 +1,6 @@
|
|
|
1
|
-
import type { AbstractFactory } from '@chubbyts/chubbyts-dic-config-factory/dist/dic-config-factory';
|
|
2
1
|
import type { Decoder, TypeDecoder } from './decoder/decoder.js';
|
|
3
2
|
import type { Encoder, TypeEncoder } from './encoder/encoder.js';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
export type DecodeEncodeConfig = {
|
|
9
|
-
decoder?: {
|
|
10
|
-
/**
|
|
11
|
-
* Service ids of the type decoders (order matters), defaults to all shipped ones:
|
|
12
|
-
* `jsonTypeDecoder`, `jsonxTypeDecoder`, `urlEncodedTypeDecoder`, `yamlTypeDecoder`.
|
|
13
|
-
*/
|
|
14
|
-
typeDecoders?: Array<string>;
|
|
15
|
-
};
|
|
16
|
-
encoder?: {
|
|
17
|
-
/**
|
|
18
|
-
* Service ids of the type encoders (order matters), defaults to all shipped ones:
|
|
19
|
-
* `jsonTypeEncoder`, `jsonxTypeEncoder`, `urlEncodedTypeEncoder`, `yamlTypeEncoder`.
|
|
20
|
-
*/
|
|
21
|
-
typeEncoders?: Array<string>;
|
|
22
|
-
/**
|
|
23
|
-
* Pretty print the output of the json / jsonx type encoders, defaults to `false`.
|
|
24
|
-
*/
|
|
25
|
-
prettyPrint?: boolean;
|
|
26
|
-
};
|
|
27
|
-
};
|
|
28
|
-
export declare const jsonTypeDecoderServiceFactory: AbstractFactory<TypeDecoder>;
|
|
29
|
-
export declare const jsonxTypeDecoderServiceFactory: AbstractFactory<TypeDecoder>;
|
|
30
|
-
export declare const urlEncodedTypeDecoderServiceFactory: AbstractFactory<TypeDecoder>;
|
|
31
|
-
export declare const yamlTypeDecoderServiceFactory: AbstractFactory<TypeDecoder>;
|
|
32
|
-
export declare const decoderServiceFactory: AbstractFactory<Decoder>;
|
|
33
|
-
export declare const jsonTypeEncoderServiceFactory: AbstractFactory<TypeEncoder>;
|
|
34
|
-
export declare const jsonxTypeEncoderServiceFactory: AbstractFactory<TypeEncoder>;
|
|
35
|
-
export declare const urlEncodedTypeEncoderServiceFactory: AbstractFactory<TypeEncoder>;
|
|
36
|
-
export declare const yamlTypeEncoderServiceFactory: AbstractFactory<TypeEncoder>;
|
|
37
|
-
export declare const encoderServiceFactory: AbstractFactory<Encoder>;
|
|
3
|
+
export declare const typeDecodersServiceFactory: import("@chubbyts/chubbyts-dic-config-factory/dist/dic-config-factory").AbstractFactory<TypeDecoder[]>;
|
|
4
|
+
export declare const decoderServiceFactory: import("@chubbyts/chubbyts-dic-config-factory/dist/dic-config-factory").AbstractFactory<Decoder>;
|
|
5
|
+
export declare const typeEncodersServiceFactory: import("@chubbyts/chubbyts-dic-config-factory/dist/dic-config-factory").AbstractFactory<TypeEncoder[]>;
|
|
6
|
+
export declare const encoderServiceFactory: import("@chubbyts/chubbyts-dic-config-factory/dist/dic-config-factory").AbstractFactory<Encoder>;
|
package/dist/service-factory.js
CHANGED
|
@@ -9,64 +9,21 @@ import { createJsonTypeEncoder } from './encoder/json-type-encoder.js';
|
|
|
9
9
|
import { createJsonxTypeEncoder } from './encoder/jsonx-type-encoder.js';
|
|
10
10
|
import { createUrlEncodedTypeEncoder } from './encoder/url-encoded-type-encoder.js';
|
|
11
11
|
import { createYamlTypeEncoder } from './encoder/yaml-type-encoder.js';
|
|
12
|
-
const
|
|
13
|
-
return
|
|
14
|
-
};
|
|
15
|
-
export const
|
|
16
|
-
return
|
|
17
|
-
});
|
|
18
|
-
export const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
});
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
['jsonxTypeDecoder', jsonxTypeDecoderServiceFactory],
|
|
30
|
-
['urlEncodedTypeDecoder', urlEncodedTypeDecoderServiceFactory],
|
|
31
|
-
['yamlTypeDecoder', yamlTypeDecoderServiceFactory],
|
|
32
|
-
]);
|
|
33
|
-
export const decoderServiceFactory = createAbstractFactory((container, { name, resolveConfig, resolveDependency }) => {
|
|
34
|
-
const { typeDecoders = Array.from(typeDecoderServiceFactories.keys()) } = resolveDecodeEncodeConfig(container, resolveConfig).decoder ?? {};
|
|
35
|
-
return createDecoder(typeDecoders.map((id) => {
|
|
36
|
-
const typeDecoderServiceFactory = typeDecoderServiceFactories.get(id);
|
|
37
|
-
// a registered service wins over the shipped factory, so that any part can be replaced or shared between services
|
|
38
|
-
return typeDecoderServiceFactory
|
|
39
|
-
? resolveDependency(container, id, typeDecoderServiceFactory)
|
|
40
|
-
: container.get(id + name);
|
|
41
|
-
}));
|
|
42
|
-
});
|
|
43
|
-
export const jsonTypeEncoderServiceFactory = createAbstractFactory((container, { resolveConfig }) => {
|
|
44
|
-
const { prettyPrint } = resolveDecodeEncodeConfig(container, resolveConfig).encoder ?? {};
|
|
45
|
-
return createJsonTypeEncoder(prettyPrint);
|
|
46
|
-
});
|
|
47
|
-
export const jsonxTypeEncoderServiceFactory = createAbstractFactory((container, { resolveConfig }) => {
|
|
48
|
-
const { prettyPrint } = resolveDecodeEncodeConfig(container, resolveConfig).encoder ?? {};
|
|
49
|
-
return createJsonxTypeEncoder(prettyPrint);
|
|
50
|
-
});
|
|
51
|
-
export const urlEncodedTypeEncoderServiceFactory = createAbstractFactory(() => {
|
|
52
|
-
return createUrlEncodedTypeEncoder();
|
|
53
|
-
});
|
|
54
|
-
export const yamlTypeEncoderServiceFactory = createAbstractFactory(() => {
|
|
55
|
-
return createYamlTypeEncoder();
|
|
56
|
-
});
|
|
57
|
-
const typeEncoderServiceFactories = new Map([
|
|
58
|
-
['jsonTypeEncoder', jsonTypeEncoderServiceFactory],
|
|
59
|
-
['jsonxTypeEncoder', jsonxTypeEncoderServiceFactory],
|
|
60
|
-
['urlEncodedTypeEncoder', urlEncodedTypeEncoderServiceFactory],
|
|
61
|
-
['yamlTypeEncoder', yamlTypeEncoderServiceFactory],
|
|
62
|
-
]);
|
|
63
|
-
export const encoderServiceFactory = createAbstractFactory((container, { name, resolveConfig, resolveDependency }) => {
|
|
64
|
-
const { typeEncoders = Array.from(typeEncoderServiceFactories.keys()) } = resolveDecodeEncodeConfig(container, resolveConfig).encoder ?? {};
|
|
65
|
-
return createEncoder(typeEncoders.map((id) => {
|
|
66
|
-
const typeEncoderServiceFactory = typeEncoderServiceFactories.get(id);
|
|
67
|
-
// a registered service wins over the shipped factory, so that any part can be replaced or shared between services
|
|
68
|
-
return typeEncoderServiceFactory
|
|
69
|
-
? resolveDependency(container, id, typeEncoderServiceFactory)
|
|
70
|
-
: container.get(id + name);
|
|
71
|
-
}));
|
|
12
|
+
export const typeDecodersServiceFactory = createAbstractFactory(() => {
|
|
13
|
+
return [createJsonTypeDecoder(), createJsonxTypeDecoder(), createUrlEncodedTypeDecoder(), createYamlTypeDecoder()];
|
|
14
|
+
});
|
|
15
|
+
export const decoderServiceFactory = createAbstractFactory((container, { resolveDependency }) => {
|
|
16
|
+
return createDecoder(resolveDependency(container, 'typeDecoders', typeDecodersServiceFactory));
|
|
17
|
+
});
|
|
18
|
+
export const typeEncodersServiceFactory = createAbstractFactory((container) => {
|
|
19
|
+
const { debug = false } = container.get('config');
|
|
20
|
+
return [
|
|
21
|
+
createJsonTypeEncoder(debug),
|
|
22
|
+
createJsonxTypeEncoder(debug),
|
|
23
|
+
createUrlEncodedTypeEncoder(),
|
|
24
|
+
createYamlTypeEncoder(),
|
|
25
|
+
];
|
|
26
|
+
});
|
|
27
|
+
export const encoderServiceFactory = createAbstractFactory((container, { resolveDependency }) => {
|
|
28
|
+
return createEncoder(resolveDependency(container, 'typeEncoders', typeEncodersServiceFactory));
|
|
72
29
|
});
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chubbyts/chubbyts-decode-encode",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.5.
|
|
4
|
+
"version": "2.5.2",
|
|
5
5
|
"description": "A simple decode/encode solution for json / jsonx / url-encoded / xml / yaml.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"chubbyts",
|
|
@@ -54,27 +54,27 @@
|
|
|
54
54
|
"node": ">=22"
|
|
55
55
|
},
|
|
56
56
|
"dependencies": {
|
|
57
|
-
"@chubbyts/chubbyts-dic-config-factory": "^1.0.
|
|
58
|
-
"@chubbyts/chubbyts-dic-types": "^2.3.
|
|
59
|
-
"@chubbyts/chubbyts-throwable-to-error": "^2.3.
|
|
60
|
-
"fast-xml-builder": "^1.3.
|
|
61
|
-
"fast-xml-parser": "^5.
|
|
62
|
-
"qs": "^6.
|
|
63
|
-
"yaml": "^2.9.
|
|
57
|
+
"@chubbyts/chubbyts-dic-config-factory": "^1.0.1",
|
|
58
|
+
"@chubbyts/chubbyts-dic-types": "^2.3.1",
|
|
59
|
+
"@chubbyts/chubbyts-throwable-to-error": "^2.3.4",
|
|
60
|
+
"fast-xml-builder": "^1.3.1",
|
|
61
|
+
"fast-xml-parser": "^5.11.2",
|
|
62
|
+
"qs": "^6.16.1",
|
|
63
|
+
"yaml": "^2.9.1"
|
|
64
64
|
},
|
|
65
65
|
"devDependencies": {
|
|
66
|
-
"@chubbyts/chubbyts-dic-config": "^2.3.
|
|
67
|
-
"@chubbyts/chubbyts-function-mock": "^2.3.
|
|
68
|
-
"@chubbyts/chubbyts-oxlint": "^1.0.
|
|
69
|
-
"@stryker-mutator/core": "^
|
|
70
|
-
"@stryker-mutator/vitest-runner": "^
|
|
71
|
-
"@types/node": "^26.
|
|
66
|
+
"@chubbyts/chubbyts-dic-config": "^2.3.1",
|
|
67
|
+
"@chubbyts/chubbyts-function-mock": "^2.3.1",
|
|
68
|
+
"@chubbyts/chubbyts-oxlint": "^1.0.2",
|
|
69
|
+
"@stryker-mutator/core": "^10.0.0",
|
|
70
|
+
"@stryker-mutator/vitest-runner": "^10.0.0",
|
|
71
|
+
"@types/node": "^26.6.5",
|
|
72
72
|
"@types/qs": "^6.15.1",
|
|
73
|
-
"@vitest/coverage-v8": "^4.1.
|
|
74
|
-
"oxlint": "^1.
|
|
75
|
-
"prettier": "^3.9.
|
|
73
|
+
"@vitest/coverage-v8": "^4.1.12",
|
|
74
|
+
"oxlint": "^1.89.0",
|
|
75
|
+
"prettier": "^3.9.10",
|
|
76
76
|
"typescript": "^7.0.2",
|
|
77
|
-
"vitest": "^4.1.
|
|
77
|
+
"vitest": "^4.1.12"
|
|
78
78
|
},
|
|
79
79
|
"publishConfig": {
|
|
80
80
|
"access": "public"
|