@chubbyts/chubbyts-decode-encode 2.4.1 → 2.5.1

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 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.4.1
40
+ npm i @chubbyts/chubbyts-decode-encode@^2.5.1
39
41
  ```
40
42
 
41
43
  ## Usage
@@ -174,6 +176,71 @@ 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`:
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
187
+
188
+ ```ts
189
+ import type { ConfigFactory } from '@chubbyts/chubbyts-dic-config/dist/dic-config';
190
+ import { createContainerByConfigFactory } from '@chubbyts/chubbyts-dic-config/dist/dic-config';
191
+ import type { Decoder } from '@chubbyts/chubbyts-decode-encode/dist/decoder/decoder';
192
+ import type { Encoder } from '@chubbyts/chubbyts-decode-encode/dist/encoder/encoder';
193
+ import { decoderServiceFactory, encoderServiceFactory } from '@chubbyts/chubbyts-decode-encode/dist/service-factory';
194
+
195
+ const container = createContainerByConfigFactory({
196
+ debug: false,
197
+ dependencies: {
198
+ factories: new Map<string, ConfigFactory>([
199
+ ['decoder', decoderServiceFactory()],
200
+ ['encoder', encoderServiceFactory()],
201
+ ]),
202
+ },
203
+ })();
204
+
205
+ const decoder = container.get<Decoder>('decoder');
206
+ const encoder = container.get<Encoder>('encoder');
207
+ ```
208
+
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
+ ```
224
+
225
+ #### With names
226
+
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`, ...).
228
+
229
+ ```ts
230
+ const container = createContainerByConfigFactory({
231
+ dependencies: {
232
+ factories: new Map<string, ConfigFactory>([
233
+ ['decoder', decoderServiceFactory()],
234
+ ['decoderapi', decoderServiceFactory('api')],
235
+ ['typeDecodersapi', (): Array<TypeDecoder> => [createJsonTypeDecoder()]],
236
+ ]),
237
+ },
238
+ })();
239
+
240
+ const decoder = container.get<Decoder>('decoder'); // all shipped type decoders
241
+ const apiDecoder = container.get<Decoder>('decoderapi'); // json only
242
+ ```
243
+
177
244
  ## Copyright
178
245
 
179
246
  2026 Dominik Zogg
@@ -184,3 +251,6 @@ const contentType = yamlTypeEncoder.contentType;
184
251
  [4]: https://www.npmjs.com/package/fast-xml-parser
185
252
  [5]: https://www.npmjs.com/package/qs
186
253
  [6]: https://www.npmjs.com/package/yaml
254
+ [7]: https://www.npmjs.com/package/@chubbyts/chubbyts-dic-types
255
+ [8]: https://www.npmjs.com/package/@chubbyts/chubbyts-dic-config
256
+ [9]: https://www.npmjs.com/package/@chubbyts/chubbyts-dic-config-factory
@@ -0,0 +1,6 @@
1
+ import type { Decoder, TypeDecoder } from './decoder/decoder.js';
2
+ import type { Encoder, TypeEncoder } from './encoder/encoder.js';
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>;
@@ -0,0 +1,29 @@
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
+ 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));
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.4.1",
4
+ "version": "2.5.1",
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",