@chubbyts/chubbyts-decode-encode 2.5.0 → 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
@@ -37,7 +37,7 @@ A simple decode/encode solution for json / jsonx / url-encoded / xml / yaml.
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.0
40
+ npm i @chubbyts/chubbyts-decode-encode@^2.5.1
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`, configured through `config.chubbyts.decodeEncode` (everything optional):
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
- 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
- },
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
- 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.
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: the config is then read from `config.chubbyts.decodeEncode.<name>` and the name gets appended to each service id (`decoderapi`, `jsonTypeDecoderapi`, `customTypeDecoderapi`, ...).
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
- ['encoderapi', encoderServiceFactory('api')],
233
- ['decoderadmin', decoderServiceFactory('admin')],
234
- ['encoderadmin', encoderServiceFactory('admin')],
235
+ ['typeDecodersapi', (): Array<TypeDecoder> => [createJsonTypeDecoder()]],
235
236
  ]),
236
237
  },
237
238
  })();
238
239
 
239
- const apiDecoder = container.get<Decoder>('decoderapi');
240
- const adminEncoder = container.get<Encoder>('encoderadmin');
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
- * 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>;
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>;
@@ -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 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
- }));
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.0",
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",