@chubbyts/chubbyts-decode-encode 2.5.2 → 2.6.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 +146 -47
- package/dist/decoder/json-type-decoder.d.ts +1 -1
- package/dist/decoder/json-type-decoder.js +2 -2
- package/dist/decoder/yaml-type-decoder.d.ts +1 -1
- package/dist/decoder/yaml-type-decoder.js +2 -2
- package/dist/encoder/json-type-encoder.d.ts +1 -1
- package/dist/encoder/json-type-encoder.js +2 -2
- package/dist/encoder/yaml-type-encoder.d.ts +1 -1
- package/dist/encoder/yaml-type-encoder.js +2 -2
- package/dist/service-factory.js +8 -1
- package/package.json +12 -5
package/README.md
CHANGED
|
@@ -19,7 +19,9 @@
|
|
|
19
19
|
|
|
20
20
|
## Description
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
Decode and encode request/response bodies by content type: json, jsonx, url-encoded and yaml.
|
|
23
|
+
|
|
24
|
+
Strings are decoded into plain data (objects, arrays, strings, numbers, booleans and null) and encoded back. Each format can be registered under additional content types, for example `application/problem+json` or `application/yaml`.
|
|
23
25
|
|
|
24
26
|
## Requirements
|
|
25
27
|
|
|
@@ -36,43 +38,65 @@ A simple decode/encode solution for json / jsonx / url-encoded / xml / yaml.
|
|
|
36
38
|
|
|
37
39
|
Through [NPM](https://www.npmjs.com) as [@chubbyts/chubbyts-decode-encode][1].
|
|
38
40
|
|
|
39
|
-
```
|
|
40
|
-
npm i @chubbyts/chubbyts-decode-encode@^2.
|
|
41
|
+
```sh
|
|
42
|
+
npm i @chubbyts/chubbyts-decode-encode@^2.6.0
|
|
41
43
|
```
|
|
42
44
|
|
|
43
45
|
## Usage
|
|
44
46
|
|
|
47
|
+
### Data
|
|
48
|
+
|
|
49
|
+
Every type decoder returns and every type encoder accepts `Data`, the json data model:
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
type Data = { [key: string]: Data } | Array<Data> | string | number | boolean | null;
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
The matching type guards `isNull`, `isBoolean`, `isNumber`, `isString`, `isArray` and `isObject` (plain objects only) can be imported from `@chubbyts/chubbyts-decode-encode/data`.
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
import { isObject } from '@chubbyts/chubbyts-decode-encode/data';
|
|
59
|
+
|
|
60
|
+
isObject({ key: 'value' }); // true
|
|
61
|
+
isObject(new Date()); // false
|
|
62
|
+
```
|
|
63
|
+
|
|
45
64
|
### Decoder
|
|
46
65
|
|
|
47
66
|
#### createDecoder
|
|
48
67
|
|
|
49
68
|
```ts
|
|
50
|
-
import { createDecoder } from '@chubbyts/chubbyts-decode-encode/
|
|
51
|
-
import { createJsonTypeDecoder } from '@chubbyts/chubbyts-decode-encode/
|
|
69
|
+
import { createDecoder } from '@chubbyts/chubbyts-decode-encode/decoder/decoder';
|
|
70
|
+
import { createJsonTypeDecoder } from '@chubbyts/chubbyts-decode-encode/decoder/json-type-decoder';
|
|
52
71
|
|
|
53
72
|
const decoder = createDecoder([createJsonTypeDecoder()]);
|
|
54
|
-
const data = decoder.decode('{"key":"value"}', 'application/json');
|
|
55
|
-
// data: {key:
|
|
73
|
+
const data = decoder.decode('{"key":"value"}', 'application/json');
|
|
74
|
+
// data: { key: 'value' }
|
|
56
75
|
const contentTypes = decoder.contentTypes;
|
|
57
76
|
// contentTypes: ['application/json']
|
|
58
77
|
```
|
|
59
78
|
|
|
79
|
+
The `Decoder` type accepts an optional third argument `context` (`Record<string, unknown>`). The shipped decoder ignores it, it exists for custom `Decoder` implementations, for example one that decodes depending on the current user.
|
|
80
|
+
|
|
60
81
|
#### createJsonTypeDecoder
|
|
61
82
|
|
|
62
83
|
```ts
|
|
63
|
-
import { createJsonTypeDecoder } from '@chubbyts/chubbyts-decode-encode/
|
|
84
|
+
import { createJsonTypeDecoder } from '@chubbyts/chubbyts-decode-encode/decoder/json-type-decoder';
|
|
64
85
|
|
|
65
86
|
const jsonTypeDecoder = createJsonTypeDecoder();
|
|
66
87
|
const data = jsonTypeDecoder.decode('{"key":"value"}');
|
|
67
|
-
// data: {key:
|
|
88
|
+
// data: { key: 'value' }
|
|
68
89
|
const contentType = jsonTypeDecoder.contentType;
|
|
69
90
|
// contentType: 'application/json'
|
|
91
|
+
|
|
92
|
+
// any other json based content type, like `application/problem+json` (RFC 9457)
|
|
93
|
+
const problemJsonTypeDecoder = createJsonTypeDecoder('application/problem+json');
|
|
70
94
|
```
|
|
71
95
|
|
|
72
96
|
#### createJsonxTypeDecoder
|
|
73
97
|
|
|
74
98
|
```ts
|
|
75
|
-
import { createJsonxTypeDecoder } from '@chubbyts/chubbyts-decode-encode/
|
|
99
|
+
import { createJsonxTypeDecoder } from '@chubbyts/chubbyts-decode-encode/decoder/jsonx-type-decoder';
|
|
76
100
|
|
|
77
101
|
const jsonxTypeDecoder = createJsonxTypeDecoder();
|
|
78
102
|
const data = jsonxTypeDecoder.decode(`
|
|
@@ -81,7 +105,7 @@ const data = jsonxTypeDecoder.decode(`
|
|
|
81
105
|
<json:string name="key">value</json:string>
|
|
82
106
|
</json:object>
|
|
83
107
|
`);
|
|
84
|
-
// data: {key:
|
|
108
|
+
// data: { key: 'value' }
|
|
85
109
|
const contentType = jsonxTypeDecoder.contentType;
|
|
86
110
|
// contentType: 'application/jsonx+xml'
|
|
87
111
|
```
|
|
@@ -89,25 +113,35 @@ const contentType = jsonxTypeDecoder.contentType;
|
|
|
89
113
|
#### createUrlEncodedTypeDecoder
|
|
90
114
|
|
|
91
115
|
```ts
|
|
92
|
-
import { createUrlEncodedTypeDecoder } from '@chubbyts/chubbyts-decode-encode/
|
|
116
|
+
import { createUrlEncodedTypeDecoder } from '@chubbyts/chubbyts-decode-encode/decoder/url-encoded-type-decoder';
|
|
93
117
|
|
|
94
118
|
const urlEncodedTypeDecoder = createUrlEncodedTypeDecoder();
|
|
95
119
|
const data = urlEncodedTypeDecoder.decode('key=value');
|
|
96
|
-
// data: {key:
|
|
120
|
+
// data: { key: 'value' }
|
|
97
121
|
const contentType = urlEncodedTypeDecoder.contentType;
|
|
98
122
|
// contentType: 'application/x-www-form-urlencoded'
|
|
99
123
|
```
|
|
100
124
|
|
|
125
|
+
Values are converted to their json type: `true` and `false` become booleans, `null` becomes `null` and a value becomes a number only if the number converts back to the exact same string, so `01` or `1.0` stay strings.
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
const data = urlEncodedTypeDecoder.decode('a=1&b=true&c=null&d=01&e[]=x');
|
|
129
|
+
// data: { a: 1, b: true, c: null, d: '01', e: ['x'] }
|
|
130
|
+
```
|
|
131
|
+
|
|
101
132
|
#### createYamlTypeDecoder
|
|
102
133
|
|
|
103
134
|
```ts
|
|
104
|
-
import { createYamlTypeDecoder } from '@chubbyts/chubbyts-decode-encode/
|
|
135
|
+
import { createYamlTypeDecoder } from '@chubbyts/chubbyts-decode-encode/decoder/yaml-type-decoder';
|
|
105
136
|
|
|
106
137
|
const yamlTypeDecoder = createYamlTypeDecoder();
|
|
107
138
|
const data = yamlTypeDecoder.decode('key: value');
|
|
108
|
-
// data: {key:
|
|
139
|
+
// data: { key: 'value' }
|
|
109
140
|
const contentType = yamlTypeDecoder.contentType;
|
|
110
141
|
// contentType: 'application/x-yaml'
|
|
142
|
+
|
|
143
|
+
// the registered media type `application/yaml` (RFC 9512)
|
|
144
|
+
const officialYamlTypeDecoder = createYamlTypeDecoder('application/yaml');
|
|
111
145
|
```
|
|
112
146
|
|
|
113
147
|
### Encoder
|
|
@@ -115,82 +149,147 @@ const contentType = yamlTypeDecoder.contentType;
|
|
|
115
149
|
#### createEncoder
|
|
116
150
|
|
|
117
151
|
```ts
|
|
118
|
-
import { createEncoder } from '@chubbyts/chubbyts-
|
|
119
|
-
import { createJsonTypeEncoder } from '@chubbyts/chubbyts-
|
|
152
|
+
import { createEncoder } from '@chubbyts/chubbyts-decode-encode/encoder/encoder';
|
|
153
|
+
import { createJsonTypeEncoder } from '@chubbyts/chubbyts-decode-encode/encoder/json-type-encoder';
|
|
120
154
|
|
|
121
155
|
const encoder = createEncoder([createJsonTypeEncoder()]);
|
|
122
|
-
const encodedData = encoder.encode({key:
|
|
123
|
-
// encodedData: {"key":"value"}
|
|
156
|
+
const encodedData = encoder.encode({ key: 'value' }, 'application/json');
|
|
157
|
+
// encodedData: '{"key":"value"}'
|
|
124
158
|
const contentTypes = encoder.contentTypes;
|
|
125
159
|
// contentTypes: ['application/json']
|
|
126
160
|
```
|
|
127
161
|
|
|
162
|
+
The `Encoder` type accepts an optional third argument `context` (`Record<string, unknown>`). The shipped encoder ignores it, it exists for custom `Encoder` implementations, for example one that encodes depending on the current user.
|
|
163
|
+
|
|
128
164
|
#### createJsonTypeEncoder
|
|
129
165
|
|
|
130
166
|
```ts
|
|
131
|
-
import { createJsonTypeEncoder } from '@chubbyts/chubbyts-
|
|
167
|
+
import { createJsonTypeEncoder } from '@chubbyts/chubbyts-decode-encode/encoder/json-type-encoder';
|
|
132
168
|
|
|
133
169
|
const jsonTypeEncoder = createJsonTypeEncoder();
|
|
134
|
-
const encodedData = jsonTypeEncoder.encode({key:
|
|
135
|
-
// encodedData: {"key":"value"}
|
|
170
|
+
const encodedData = jsonTypeEncoder.encode({ key: 'value' });
|
|
171
|
+
// encodedData: '{"key":"value"}'
|
|
136
172
|
const contentType = jsonTypeEncoder.contentType;
|
|
137
|
-
//
|
|
173
|
+
// contentType: 'application/json'
|
|
174
|
+
|
|
175
|
+
// pretty printed
|
|
176
|
+
const prettyJsonTypeEncoder = createJsonTypeEncoder(true);
|
|
177
|
+
|
|
178
|
+
// any other json based content type, like `application/problem+json` (RFC 9457)
|
|
179
|
+
const problemJsonTypeEncoder = createJsonTypeEncoder(false, 'application/problem+json');
|
|
138
180
|
```
|
|
139
181
|
|
|
140
182
|
#### createJsonxTypeEncoder
|
|
141
183
|
|
|
142
184
|
```ts
|
|
143
|
-
import { createJsonxTypeEncoder } from '@chubbyts/chubbyts-
|
|
185
|
+
import { createJsonxTypeEncoder } from '@chubbyts/chubbyts-decode-encode/encoder/jsonx-type-encoder';
|
|
144
186
|
|
|
145
|
-
const jsonxTypeEncoder = createJsonxTypeEncoder();
|
|
146
|
-
const encodedData = jsonxTypeEncoder.encode({key:
|
|
147
|
-
// encodedData:
|
|
148
|
-
//
|
|
187
|
+
const jsonxTypeEncoder = createJsonxTypeEncoder(true); // pretty printed, defaults to false
|
|
188
|
+
const encodedData = jsonxTypeEncoder.encode({ key: 'value' });
|
|
189
|
+
// encodedData:
|
|
190
|
+
// <?xml version="1.0" encoding="UTF-8"?>
|
|
191
|
+
// <json:object xsi:schemaLocation="http://www.datapower.com/schemas/json jsonx.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx">
|
|
149
192
|
// <json:string name="key">value</json:string>
|
|
150
193
|
// </json:object>
|
|
151
194
|
const contentType = jsonxTypeEncoder.contentType;
|
|
152
|
-
//
|
|
195
|
+
// contentType: 'application/jsonx+xml'
|
|
153
196
|
```
|
|
154
197
|
|
|
155
198
|
#### createUrlEncodedTypeEncoder
|
|
156
199
|
|
|
157
200
|
```ts
|
|
158
|
-
import { createUrlEncodedTypeEncoder } from '@chubbyts/chubbyts-
|
|
201
|
+
import { createUrlEncodedTypeEncoder } from '@chubbyts/chubbyts-decode-encode/encoder/url-encoded-type-encoder';
|
|
159
202
|
|
|
160
203
|
const urlEncodedTypeEncoder = createUrlEncodedTypeEncoder();
|
|
161
|
-
const encodedData = urlEncodedTypeEncoder.encode({
|
|
162
|
-
// encodedData:
|
|
204
|
+
const encodedData = urlEncodedTypeEncoder.encode({ a: 1, b: true, c: null, d: [1, 2] });
|
|
205
|
+
// encodedData: 'a=1&b=true&c=null&d[0]=1&d[1]=2'
|
|
163
206
|
const contentType = urlEncodedTypeEncoder.contentType;
|
|
164
|
-
//
|
|
207
|
+
// contentType: 'application/x-www-form-urlencoded'
|
|
165
208
|
```
|
|
166
209
|
|
|
167
210
|
#### createYamlTypeEncoder
|
|
168
211
|
|
|
169
212
|
```ts
|
|
170
|
-
import { createYamlTypeEncoder } from '@chubbyts/chubbyts-
|
|
213
|
+
import { createYamlTypeEncoder } from '@chubbyts/chubbyts-decode-encode/encoder/yaml-type-encoder';
|
|
171
214
|
|
|
172
215
|
const yamlTypeEncoder = createYamlTypeEncoder();
|
|
173
|
-
const encodedData = yamlTypeEncoder.encode({key:
|
|
174
|
-
// encodedData: key: value
|
|
216
|
+
const encodedData = yamlTypeEncoder.encode({ key: 'value' });
|
|
217
|
+
// encodedData: 'key: value\n'
|
|
175
218
|
const contentType = yamlTypeEncoder.contentType;
|
|
176
|
-
//
|
|
219
|
+
// contentType: 'application/x-yaml'
|
|
220
|
+
|
|
221
|
+
// the registered media type `application/yaml` (RFC 9512)
|
|
222
|
+
const officialYamlTypeEncoder = createYamlTypeEncoder('application/yaml');
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
### Custom type decoders / encoders
|
|
226
|
+
|
|
227
|
+
A type decoder / encoder is a plain object with a `contentType` and a `decode` / `encode` function. This adds [NDJSON](https://github.com/ndjson/ndjson-spec), one json document per line:
|
|
228
|
+
|
|
229
|
+
```ts
|
|
230
|
+
import type { Data } from '@chubbyts/chubbyts-decode-encode/data';
|
|
231
|
+
import { isArray } from '@chubbyts/chubbyts-decode-encode/data';
|
|
232
|
+
import type { TypeDecoder } from '@chubbyts/chubbyts-decode-encode/decoder/decoder';
|
|
233
|
+
import { DecodeError, createDecoder } from '@chubbyts/chubbyts-decode-encode/decoder/decoder';
|
|
234
|
+
import type { TypeEncoder } from '@chubbyts/chubbyts-decode-encode/encoder/encoder';
|
|
235
|
+
import { createEncoder } from '@chubbyts/chubbyts-decode-encode/encoder/encoder';
|
|
236
|
+
|
|
237
|
+
const createNdjsonTypeDecoder = (): TypeDecoder => ({
|
|
238
|
+
decode: (encodedData: string): Data => {
|
|
239
|
+
try {
|
|
240
|
+
return encodedData
|
|
241
|
+
.split('\n')
|
|
242
|
+
.filter((line) => line.trim() !== '')
|
|
243
|
+
.map((line) => JSON.parse(line));
|
|
244
|
+
} catch (e) {
|
|
245
|
+
throw new DecodeError(e instanceof Error ? e.message : String(e));
|
|
246
|
+
}
|
|
247
|
+
},
|
|
248
|
+
contentType: 'application/x-ndjson',
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
const createNdjsonTypeEncoder = (): TypeEncoder => ({
|
|
252
|
+
encode: (data: Data): string => (isArray(data) ? data : [data]).map((item) => JSON.stringify(item)).join('\n'),
|
|
253
|
+
contentType: 'application/x-ndjson',
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
const decoder = createDecoder([createNdjsonTypeDecoder()]);
|
|
257
|
+
const encoder = createEncoder([createNdjsonTypeEncoder()]);
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
### Errors
|
|
261
|
+
|
|
262
|
+
* `decode` throws a `DecodeError` (`@chubbyts/chubbyts-decode-encode/decoder/decoder`) if the encoded data is invalid, for example a json syntax error. In a http context this usually maps to `400 Bad Request`.
|
|
263
|
+
* `encode` of the jsonx type encoder throws an `EncodeError` (`@chubbyts/chubbyts-decode-encode/encoder/encoder`) if a value cannot be represented.
|
|
264
|
+
* `decode` / `encode` of the decoder / encoder throw an `Error` if the content type is not supported, the message lists the supported content types. In a http context this usually maps to `415 Unsupported Media Type` (decoder) or `406 Not Acceptable` (encoder).
|
|
265
|
+
|
|
266
|
+
```ts
|
|
267
|
+
import { DecodeError } from '@chubbyts/chubbyts-decode-encode/decoder/decoder';
|
|
268
|
+
|
|
269
|
+
try {
|
|
270
|
+
decoder.decode('{', 'application/json');
|
|
271
|
+
} catch (e) {
|
|
272
|
+
if (e instanceof DecodeError) {
|
|
273
|
+
// invalid request body
|
|
274
|
+
}
|
|
275
|
+
}
|
|
177
276
|
```
|
|
178
277
|
|
|
179
278
|
### Service factories (chubbyts-dic-config)
|
|
180
279
|
|
|
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/
|
|
280
|
+
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/service-factory`:
|
|
182
281
|
|
|
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
|
|
282
|
+
* `typeDecodersServiceFactory`: all shipped type decoders (json, jsonx, urlencoded, yaml as `application/x-yaml` and `application/yaml`)
|
|
283
|
+
* `typeEncodersServiceFactory`: all shipped type encoders (json, jsonx, urlencoded, yaml as `application/x-yaml` and `application/yaml`), `config.debug` enables pretty printing for json / jsonx
|
|
185
284
|
* `decoderServiceFactory`: the decoder, built from the `typeDecoders` service if registered, the shipped type decoders otherwise
|
|
186
285
|
* `encoderServiceFactory`: the encoder, built from the `typeEncoders` service if registered, the shipped type encoders otherwise
|
|
187
286
|
|
|
188
287
|
```ts
|
|
189
288
|
import type { ConfigFactory } from '@chubbyts/chubbyts-dic-config/dist/dic-config';
|
|
190
289
|
import { createContainerByConfigFactory } from '@chubbyts/chubbyts-dic-config/dist/dic-config';
|
|
191
|
-
import type { Decoder } from '@chubbyts/chubbyts-decode-encode/
|
|
192
|
-
import type { Encoder } from '@chubbyts/chubbyts-decode-encode/
|
|
193
|
-
import { decoderServiceFactory, encoderServiceFactory } from '@chubbyts/chubbyts-decode-encode/
|
|
290
|
+
import type { Decoder } from '@chubbyts/chubbyts-decode-encode/decoder/decoder';
|
|
291
|
+
import type { Encoder } from '@chubbyts/chubbyts-decode-encode/encoder/encoder';
|
|
292
|
+
import { decoderServiceFactory, encoderServiceFactory } from '@chubbyts/chubbyts-decode-encode/service-factory';
|
|
194
293
|
|
|
195
294
|
const container = createContainerByConfigFactory({
|
|
196
295
|
debug: false,
|
|
@@ -206,17 +305,17 @@ const decoder = container.get<Decoder>('decoder');
|
|
|
206
305
|
const encoder = container.get<Encoder>('encoder');
|
|
207
306
|
```
|
|
208
307
|
|
|
209
|
-
To replace the type decoders / encoders, register a `typeDecoders` / `typeEncoders` service:
|
|
308
|
+
To replace the type decoders / encoders, register a `typeDecoders` / `typeEncoders` service (`createNdjsonTypeDecoder` from [Custom type decoders / encoders](#custom-type-decoders--encoders)):
|
|
210
309
|
|
|
211
310
|
```ts
|
|
212
|
-
import type { TypeDecoder } from '@chubbyts/chubbyts-decode-encode/
|
|
213
|
-
import { createJsonTypeDecoder } from '@chubbyts/chubbyts-decode-encode/
|
|
311
|
+
import type { TypeDecoder } from '@chubbyts/chubbyts-decode-encode/decoder/decoder';
|
|
312
|
+
import { createJsonTypeDecoder } from '@chubbyts/chubbyts-decode-encode/decoder/json-type-decoder';
|
|
214
313
|
|
|
215
314
|
const container = createContainerByConfigFactory({
|
|
216
315
|
dependencies: {
|
|
217
316
|
factories: new Map<string, ConfigFactory>([
|
|
218
317
|
['decoder', decoderServiceFactory()],
|
|
219
|
-
['typeDecoders', (): Array<TypeDecoder> => [createJsonTypeDecoder(),
|
|
318
|
+
['typeDecoders', (): Array<TypeDecoder> => [createJsonTypeDecoder(), createNdjsonTypeDecoder()]],
|
|
220
319
|
]),
|
|
221
320
|
},
|
|
222
321
|
})();
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { TypeDecoder } from './decoder.js';
|
|
2
|
-
export declare const createJsonTypeDecoder: () => TypeDecoder;
|
|
2
|
+
export declare const createJsonTypeDecoder: (contentType?: string) => TypeDecoder;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { throwableToError } from '@chubbyts/chubbyts-throwable-to-error/dist/throwable-to-error';
|
|
2
2
|
import { DecodeError } from './decoder.js';
|
|
3
|
-
export const createJsonTypeDecoder = () => {
|
|
3
|
+
export const createJsonTypeDecoder = (contentType = 'application/json') => {
|
|
4
4
|
return {
|
|
5
5
|
decode: (encodedData) => {
|
|
6
6
|
try {
|
|
@@ -11,6 +11,6 @@ export const createJsonTypeDecoder = () => {
|
|
|
11
11
|
throw new DecodeError(error.message, error.stack);
|
|
12
12
|
}
|
|
13
13
|
},
|
|
14
|
-
contentType
|
|
14
|
+
contentType,
|
|
15
15
|
};
|
|
16
16
|
};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { TypeDecoder } from './decoder.js';
|
|
2
|
-
export declare const createYamlTypeDecoder: () => TypeDecoder;
|
|
2
|
+
export declare const createYamlTypeDecoder: (contentType?: string) => TypeDecoder;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { parse } from 'yaml';
|
|
2
2
|
import { throwableToError } from '@chubbyts/chubbyts-throwable-to-error/dist/throwable-to-error';
|
|
3
3
|
import { DecodeError } from './decoder.js';
|
|
4
|
-
export const createYamlTypeDecoder = () => {
|
|
4
|
+
export const createYamlTypeDecoder = (contentType = 'application/x-yaml') => {
|
|
5
5
|
return {
|
|
6
6
|
decode: (encodedData) => {
|
|
7
7
|
try {
|
|
@@ -12,6 +12,6 @@ export const createYamlTypeDecoder = () => {
|
|
|
12
12
|
throw new DecodeError(error.message, error.stack);
|
|
13
13
|
}
|
|
14
14
|
},
|
|
15
|
-
contentType
|
|
15
|
+
contentType,
|
|
16
16
|
};
|
|
17
17
|
};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { TypeEncoder } from './encoder.js';
|
|
2
|
-
export declare const createJsonTypeEncoder: (prettyPrint?: boolean) => TypeEncoder;
|
|
2
|
+
export declare const createJsonTypeEncoder: (prettyPrint?: boolean, contentType?: string) => TypeEncoder;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export const createJsonTypeEncoder = (prettyPrint = false) => {
|
|
1
|
+
export const createJsonTypeEncoder = (prettyPrint = false, contentType = 'application/json') => {
|
|
2
2
|
return {
|
|
3
3
|
encode: (data) => JSON.stringify(data, undefined, prettyPrint ? 4 : undefined),
|
|
4
|
-
contentType
|
|
4
|
+
contentType,
|
|
5
5
|
};
|
|
6
6
|
};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { TypeEncoder } from './encoder.js';
|
|
2
|
-
export declare const createYamlTypeEncoder: () => TypeEncoder;
|
|
2
|
+
export declare const createYamlTypeEncoder: (contentType?: string) => TypeEncoder;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { stringify } from 'yaml';
|
|
2
|
-
export const createYamlTypeEncoder = () => {
|
|
2
|
+
export const createYamlTypeEncoder = (contentType = 'application/x-yaml') => {
|
|
3
3
|
return {
|
|
4
4
|
encode: (data) => stringify(data, { indent: 4 }),
|
|
5
|
-
contentType
|
|
5
|
+
contentType,
|
|
6
6
|
};
|
|
7
7
|
};
|
package/dist/service-factory.js
CHANGED
|
@@ -10,7 +10,13 @@ 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
12
|
export const typeDecodersServiceFactory = createAbstractFactory(() => {
|
|
13
|
-
return [
|
|
13
|
+
return [
|
|
14
|
+
createJsonTypeDecoder(),
|
|
15
|
+
createJsonxTypeDecoder(),
|
|
16
|
+
createUrlEncodedTypeDecoder(),
|
|
17
|
+
createYamlTypeDecoder(),
|
|
18
|
+
createYamlTypeDecoder('application/yaml'),
|
|
19
|
+
];
|
|
14
20
|
});
|
|
15
21
|
export const decoderServiceFactory = createAbstractFactory((container, { resolveDependency }) => {
|
|
16
22
|
return createDecoder(resolveDependency(container, 'typeDecoders', typeDecodersServiceFactory));
|
|
@@ -22,6 +28,7 @@ export const typeEncodersServiceFactory = createAbstractFactory((container) => {
|
|
|
22
28
|
createJsonxTypeEncoder(debug),
|
|
23
29
|
createUrlEncodedTypeEncoder(),
|
|
24
30
|
createYamlTypeEncoder(),
|
|
31
|
+
createYamlTypeEncoder('application/yaml'),
|
|
25
32
|
];
|
|
26
33
|
});
|
|
27
34
|
export const encoderServiceFactory = createAbstractFactory((container, { resolveDependency }) => {
|
package/package.json
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chubbyts/chubbyts-decode-encode",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.
|
|
5
|
-
"description": "
|
|
4
|
+
"version": "2.6.0",
|
|
5
|
+
"description": "Decode and encode request/response bodies by content type: json, jsonx, url-encoded and yaml.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"chubbyts",
|
|
8
|
+
"content-type",
|
|
8
9
|
"decode",
|
|
9
10
|
"encode",
|
|
10
11
|
"json",
|
|
11
12
|
"jsonx",
|
|
13
|
+
"serializer",
|
|
12
14
|
"url-encoded",
|
|
13
15
|
"xml",
|
|
14
16
|
"yaml"
|
|
@@ -44,10 +46,15 @@
|
|
|
44
46
|
"import": "./dist/encoder/index.js",
|
|
45
47
|
"default": "./dist/encoder/index.js"
|
|
46
48
|
},
|
|
49
|
+
"./dist/*": {
|
|
50
|
+
"types": "./dist/*.d.ts",
|
|
51
|
+
"import": "./dist/*.js",
|
|
52
|
+
"default": "./dist/*.js"
|
|
53
|
+
},
|
|
47
54
|
"./*": {
|
|
48
|
-
"types": "
|
|
49
|
-
"import": "
|
|
50
|
-
"default": "
|
|
55
|
+
"types": "./dist/*.d.ts",
|
|
56
|
+
"import": "./dist/*.js",
|
|
57
|
+
"default": "./dist/*.js"
|
|
51
58
|
}
|
|
52
59
|
},
|
|
53
60
|
"engines": {
|