@chubbyts/chubbyts-decode-encode 2.4.1 → 2.5.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 +70 -1
- package/dist/service-factory.d.ts +37 -0
- package/dist/service-factory.js +72 -0
- package/package.json +4 -1
package/README.md
CHANGED
|
@@ -24,6 +24,8 @@ 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.0
|
|
28
|
+
* [@chubbyts/chubbyts-dic-types][7]: ^2.3.0
|
|
27
29
|
* [@chubbyts/chubbyts-throwable-to-error][2]: ^2.3.0
|
|
28
30
|
* [fast-xml-builder][3]: ^1.3.0
|
|
29
31
|
* [fast-xml-parser][4]: ^5.10.1
|
|
@@ -35,7 +37,7 @@ A simple decode/encode solution for json / jsonx / url-encoded / xml / yaml.
|
|
|
35
37
|
Through [NPM](https://www.npmjs.com) as [@chubbyts/chubbyts-decode-encode][1].
|
|
36
38
|
|
|
37
39
|
```ts
|
|
38
|
-
npm i @chubbyts/chubbyts-decode-encode@^2.
|
|
40
|
+
npm i @chubbyts/chubbyts-decode-encode@^2.5.0
|
|
39
41
|
```
|
|
40
42
|
|
|
41
43
|
## Usage
|
|
@@ -174,6 +176,70 @@ const contentType = yamlTypeEncoder.contentType;
|
|
|
174
176
|
// contentTypes: 'application/x-yaml'
|
|
175
177
|
```
|
|
176
178
|
|
|
179
|
+
### Service factories (chubbyts-dic-config)
|
|
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`, configured through `config.chubbyts.decodeEncode` (everything optional):
|
|
182
|
+
|
|
183
|
+
```ts
|
|
184
|
+
import type { ConfigFactory } from '@chubbyts/chubbyts-dic-config/dist/dic-config';
|
|
185
|
+
import { createContainerByConfigFactory } from '@chubbyts/chubbyts-dic-config/dist/dic-config';
|
|
186
|
+
import type { Decoder } from '@chubbyts/chubbyts-decode-encode/dist/decoder/decoder';
|
|
187
|
+
import type { Encoder } from '@chubbyts/chubbyts-decode-encode/dist/encoder/encoder';
|
|
188
|
+
import type { DecodeEncodeConfig } from '@chubbyts/chubbyts-decode-encode/dist/service-factory';
|
|
189
|
+
import { decoderServiceFactory, encoderServiceFactory } from '@chubbyts/chubbyts-decode-encode/dist/service-factory';
|
|
190
|
+
|
|
191
|
+
const container = createContainerByConfigFactory({
|
|
192
|
+
chubbyts: {
|
|
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
|
+
},
|
|
203
|
+
dependencies: {
|
|
204
|
+
factories: new Map<string, ConfigFactory>([
|
|
205
|
+
['decoder', decoderServiceFactory()],
|
|
206
|
+
['encoder', encoderServiceFactory()],
|
|
207
|
+
]),
|
|
208
|
+
},
|
|
209
|
+
})();
|
|
210
|
+
|
|
211
|
+
const decoder = container.get<Decoder>('decoder');
|
|
212
|
+
const encoder = container.get<Encoder>('encoder');
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
The `decoderServiceFactory` / `encoderServiceFactory` use the services listed in `typeDecoders` / `typeEncoders` (defaults to all shipped ones, order matters) of the container if registered, and creates the shipped ones through `jsonTypeDecoderServiceFactory`, `jsonxTypeDecoderServiceFactory`, `urlEncodedTypeDecoderServiceFactory`, `yamlTypeDecoderServiceFactory` / `jsonTypeEncoderServiceFactory`, `jsonxTypeEncoderServiceFactory`, `urlEncodedTypeEncoderServiceFactory`, `yamlTypeEncoderServiceFactory` otherwise. Register any of them under its name to replace it or to share it with other services, any other id (e.g. `customTypeDecoder`) has to be registered. `prettyPrint` is passed to the json / jsonx type encoders.
|
|
216
|
+
|
|
217
|
+
#### With names
|
|
218
|
+
|
|
219
|
+
To use different decoders / encoders in different parts of an application, the same factories can be registered multiple times with a name: the config is then read from `config.chubbyts.decodeEncode.<name>` and the name gets appended to each service id (`decoderapi`, `jsonTypeDecoderapi`, `customTypeDecoderapi`, ...).
|
|
220
|
+
|
|
221
|
+
```ts
|
|
222
|
+
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
|
+
dependencies: {
|
|
230
|
+
factories: new Map<string, ConfigFactory>([
|
|
231
|
+
['decoderapi', decoderServiceFactory('api')],
|
|
232
|
+
['encoderapi', encoderServiceFactory('api')],
|
|
233
|
+
['decoderadmin', decoderServiceFactory('admin')],
|
|
234
|
+
['encoderadmin', encoderServiceFactory('admin')],
|
|
235
|
+
]),
|
|
236
|
+
},
|
|
237
|
+
})();
|
|
238
|
+
|
|
239
|
+
const apiDecoder = container.get<Decoder>('decoderapi');
|
|
240
|
+
const adminEncoder = container.get<Encoder>('encoderadmin');
|
|
241
|
+
```
|
|
242
|
+
|
|
177
243
|
## Copyright
|
|
178
244
|
|
|
179
245
|
2026 Dominik Zogg
|
|
@@ -184,3 +250,6 @@ const contentType = yamlTypeEncoder.contentType;
|
|
|
184
250
|
[4]: https://www.npmjs.com/package/fast-xml-parser
|
|
185
251
|
[5]: https://www.npmjs.com/package/qs
|
|
186
252
|
[6]: https://www.npmjs.com/package/yaml
|
|
253
|
+
[7]: https://www.npmjs.com/package/@chubbyts/chubbyts-dic-types
|
|
254
|
+
[8]: https://www.npmjs.com/package/@chubbyts/chubbyts-dic-config
|
|
255
|
+
[9]: https://www.npmjs.com/package/@chubbyts/chubbyts-dic-config-factory
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { AbstractFactory } from '@chubbyts/chubbyts-dic-config-factory/dist/dic-config-factory';
|
|
2
|
+
import type { Decoder, TypeDecoder } from './decoder/decoder.js';
|
|
3
|
+
import type { Encoder, TypeEncoder } from './encoder/encoder.js';
|
|
4
|
+
/**
|
|
5
|
+
* The configuration read by the service factories from `config.chubbyts.decodeEncode` (or
|
|
6
|
+
* `config.chubbyts.decodeEncode.<name>` for named factories). Everything is optional.
|
|
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>;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { createAbstractFactory } from '@chubbyts/chubbyts-dic-config-factory/dist/dic-config-factory';
|
|
2
|
+
import { createDecoder } from './decoder/decoder.js';
|
|
3
|
+
import { createJsonTypeDecoder } from './decoder/json-type-decoder.js';
|
|
4
|
+
import { createJsonxTypeDecoder } from './decoder/jsonx-type-decoder.js';
|
|
5
|
+
import { createUrlEncodedTypeDecoder } from './decoder/url-encoded-type-decoder.js';
|
|
6
|
+
import { createYamlTypeDecoder } from './decoder/yaml-type-decoder.js';
|
|
7
|
+
import { createEncoder } from './encoder/encoder.js';
|
|
8
|
+
import { createJsonTypeEncoder } from './encoder/json-type-encoder.js';
|
|
9
|
+
import { createJsonxTypeEncoder } from './encoder/jsonx-type-encoder.js';
|
|
10
|
+
import { createUrlEncodedTypeEncoder } from './encoder/url-encoded-type-encoder.js';
|
|
11
|
+
import { createYamlTypeEncoder } from './encoder/yaml-type-encoder.js';
|
|
12
|
+
const resolveDecodeEncodeConfig = (container, resolveConfig) => {
|
|
13
|
+
return resolveConfig(container.get('config').chubbyts?.decodeEncode ?? {});
|
|
14
|
+
};
|
|
15
|
+
export const jsonTypeDecoderServiceFactory = createAbstractFactory(() => {
|
|
16
|
+
return createJsonTypeDecoder();
|
|
17
|
+
});
|
|
18
|
+
export const jsonxTypeDecoderServiceFactory = createAbstractFactory(() => {
|
|
19
|
+
return createJsonxTypeDecoder();
|
|
20
|
+
});
|
|
21
|
+
export const urlEncodedTypeDecoderServiceFactory = createAbstractFactory(() => {
|
|
22
|
+
return createUrlEncodedTypeDecoder();
|
|
23
|
+
});
|
|
24
|
+
export const yamlTypeDecoderServiceFactory = createAbstractFactory(() => {
|
|
25
|
+
return createYamlTypeDecoder();
|
|
26
|
+
});
|
|
27
|
+
const typeDecoderServiceFactories = new Map([
|
|
28
|
+
['jsonTypeDecoder', jsonTypeDecoderServiceFactory],
|
|
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
|
+
}));
|
|
72
|
+
});
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chubbyts/chubbyts-decode-encode",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.
|
|
4
|
+
"version": "2.5.0",
|
|
5
5
|
"description": "A simple decode/encode solution for json / jsonx / url-encoded / xml / yaml.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"chubbyts",
|
|
@@ -54,6 +54,8 @@
|
|
|
54
54
|
"node": ">=22"
|
|
55
55
|
},
|
|
56
56
|
"dependencies": {
|
|
57
|
+
"@chubbyts/chubbyts-dic-config-factory": "^1.0.0",
|
|
58
|
+
"@chubbyts/chubbyts-dic-types": "^2.3.0",
|
|
57
59
|
"@chubbyts/chubbyts-throwable-to-error": "^2.3.0",
|
|
58
60
|
"fast-xml-builder": "^1.3.0",
|
|
59
61
|
"fast-xml-parser": "^5.10.1",
|
|
@@ -61,6 +63,7 @@
|
|
|
61
63
|
"yaml": "^2.9.0"
|
|
62
64
|
},
|
|
63
65
|
"devDependencies": {
|
|
66
|
+
"@chubbyts/chubbyts-dic-config": "^2.3.0",
|
|
64
67
|
"@chubbyts/chubbyts-function-mock": "^2.3.0",
|
|
65
68
|
"@chubbyts/chubbyts-oxlint": "^1.0.0",
|
|
66
69
|
"@stryker-mutator/core": "^9.6.1",
|