@karmaniverous/jsonmap 1.0.1 → 2.0.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 +2 -42
- package/dist/index.cjs +205 -0
- package/dist/index.d.cts +73 -0
- package/dist/index.d.mts +73 -0
- package/dist/index.d.ts +73 -0
- package/dist/index.iife.js +204 -0
- package/dist/index.iife.min.js +1 -0
- package/dist/index.mjs +203 -0
- package/package.json +71 -74
- package/.env +0 -1
- package/dist/default/lib/JsonMap/JsonMap.js +0 -247
- package/dist/default/lib/index.js +0 -12
- package/dist/package.json +0 -3
- package/env/.env.dev +0 -2
- package/env/dynamic.js +0 -1
- package/lib/JsonMap/JsonMap.js +0 -233
- package/lib/index.js +0 -1
package/README.md
CHANGED
|
@@ -39,7 +39,7 @@ The `map` object is a [POJO](https://masteringjs.io/tutorials/fundamentals/pojo)
|
|
|
39
39
|
|
|
40
40
|
The transformation output will reflect the structure of your `map` object and include any static values. To add mapping logic, use a structured value that consists of an object with a single `$` key, like this:
|
|
41
41
|
|
|
42
|
-
```
|
|
42
|
+
```ts
|
|
43
43
|
const map = {
|
|
44
44
|
foo: 'static value passed directly to output',
|
|
45
45
|
// Structure passed directly to output.
|
|
@@ -111,47 +111,7 @@ const output = await jsonMap.transform(input);
|
|
|
111
111
|
```
|
|
112
112
|
|
|
113
113
|
The [unit tests](https://github.com/karmaniverous/jsonmap/blob/main/lib/JsonMap/JsonMap.test.js) demonstrate this example in action.
|
|
114
|
-
|
|
115
|
-
# API Documentation
|
|
116
|
-
|
|
117
|
-
<a name="JsonMap"></a>
|
|
118
|
-
|
|
119
|
-
## JsonMap
|
|
120
|
-
JsonMap class to apply transformations to a JSON object
|
|
121
|
-
|
|
122
|
-
**Kind**: global class
|
|
123
|
-
|
|
124
|
-
* [JsonMap](#JsonMap)
|
|
125
|
-
* [new JsonMap([map], [lib], [options])](#new_JsonMap_new)
|
|
126
|
-
* [.transform(input)](#JsonMap+transform) ⇒ <code>object</code>
|
|
127
|
-
|
|
128
|
-
<a name="new_JsonMap_new"></a>
|
|
129
|
-
|
|
130
|
-
### new JsonMap([map], [lib], [options])
|
|
131
|
-
Creates an instance of JsonMap.
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
| Param | Type | Description |
|
|
135
|
-
| --- | --- | --- |
|
|
136
|
-
| [map] | <code>object</code> | The data mapping configuration. |
|
|
137
|
-
| [lib] | <code>object</code> | A collection of function libraries. |
|
|
138
|
-
| [options] | <code>object</code> | Options object |
|
|
139
|
-
| [options.ignore] | <code>string</code> | Regex pattern of keys to ignore. Defaults to '^\\$'. |
|
|
140
|
-
| [options.logger] | <code>string</code> | Logger object |
|
|
141
|
-
|
|
142
|
-
<a name="JsonMap+transform"></a>
|
|
143
|
-
|
|
144
|
-
### jsonMap.transform(input) ⇒ <code>object</code>
|
|
145
|
-
Transforms the input data according to the map configuration.
|
|
146
|
-
|
|
147
|
-
**Kind**: instance method of [<code>JsonMap</code>](#JsonMap)
|
|
148
|
-
**Returns**: <code>object</code> - - The transformed data.
|
|
149
|
-
|
|
150
|
-
| Param | Type | Description |
|
|
151
|
-
| --- | --- | --- |
|
|
152
|
-
| input | <code>object</code> | The input data to be transformed. |
|
|
153
|
-
|
|
154
|
-
|
|
114
|
+
|
|
155
115
|
---
|
|
156
116
|
|
|
157
117
|
See more great templates and other tools on
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _ = require('lodash');
|
|
4
|
+
var nanoid = require('nanoid');
|
|
5
|
+
var tslog = require('tslog');
|
|
6
|
+
|
|
7
|
+
/******************************************************************************
|
|
8
|
+
Copyright (c) Microsoft Corporation.
|
|
9
|
+
|
|
10
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
11
|
+
purpose with or without fee is hereby granted.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
14
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
15
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
16
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
17
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
18
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
19
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
20
|
+
***************************************************************************** */
|
|
21
|
+
/* global Reflect, Promise, SuppressedError, Symbol */
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
25
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
26
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
27
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
28
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
29
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
30
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function __classPrivateFieldGet(receiver, state, kind, f) {
|
|
35
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
36
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
37
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
41
|
+
var e = new Error(message);
|
|
42
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
// Get package scope & name from package.json.
|
|
46
|
+
var _a$1, _b$1, _c;
|
|
47
|
+
const npmPackageRegex = /^(?:(?<packageScope>@[a-z0-9-~][a-z0-9-._~]*)\/)?(?<packageName>[a-z0-9-~][a-z0-9-._~]*)$/;
|
|
48
|
+
const { packageScope, packageName } = ((_c = (_b$1 = (_a$1 = process.env.npm_package_name) === null || _a$1 === void 0 ? void 0 : _a$1.match(npmPackageRegex)) === null || _b$1 === void 0 ? void 0 : _b$1.groups) !== null && _c !== void 0 ? _c : {});
|
|
49
|
+
|
|
50
|
+
var _a, _b;
|
|
51
|
+
// These are the presefined tslog log levels. You can specify
|
|
52
|
+
// LOG_LEVEL by any of the keys or values defined below.
|
|
53
|
+
const logLevels = {
|
|
54
|
+
silly: 0,
|
|
55
|
+
trace: 1,
|
|
56
|
+
debug: 2,
|
|
57
|
+
info: 3,
|
|
58
|
+
warn: 4,
|
|
59
|
+
error: 5,
|
|
60
|
+
fatal: 6,
|
|
61
|
+
};
|
|
62
|
+
const integerLogLevel = parseInt((_a = process.env.LOG_LEVEL) !== null && _a !== void 0 ? _a : '');
|
|
63
|
+
const resolvedLogLevel = Number.isNaN(integerLogLevel)
|
|
64
|
+
? logLevels[((_b = process.env.LOG_LEVEL) !== null && _b !== void 0 ? _b : '').toLowerCase()]
|
|
65
|
+
: integerLogLevel;
|
|
66
|
+
// Set your tslog instance options here! By default, logs are suppressed if LOG_LEVEL is invalid or undefined.
|
|
67
|
+
// See https://tslog.js.org for more info.
|
|
68
|
+
const logger = new tslog.Logger({
|
|
69
|
+
hideLogPositionForProduction: true,
|
|
70
|
+
minLevel: resolvedLogLevel,
|
|
71
|
+
name: packageName,
|
|
72
|
+
type: resolvedLogLevel === undefined ? 'hidden' : 'pretty',
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
var _JsonMap_instances, _JsonMap_transform, _JsonMap_resolvePath;
|
|
76
|
+
// The transformation process leverages JSON.stringify and JSON.parse to eliminate circular references.
|
|
77
|
+
const getJsonFns = () => {
|
|
78
|
+
const seen = new WeakSet();
|
|
79
|
+
const undefinedToken = nanoid.nanoid();
|
|
80
|
+
const replacer = (key, value) => {
|
|
81
|
+
if (typeof value === 'object' && value !== null) {
|
|
82
|
+
if (seen.has(value)) {
|
|
83
|
+
return 'CIRCULAR REFERENCE';
|
|
84
|
+
}
|
|
85
|
+
seen.add(value);
|
|
86
|
+
if (!_.isArrayLikeObject(value))
|
|
87
|
+
return Object.getOwnPropertyNames(value).reduce((v, p) => (Object.assign(Object.assign({}, v), { [p]: value[p] })), {});
|
|
88
|
+
}
|
|
89
|
+
return _.isUndefined(value) ? undefinedToken : value;
|
|
90
|
+
};
|
|
91
|
+
const reviver = (key, value) => value === undefinedToken ? undefined : value;
|
|
92
|
+
return { replacer, reviver };
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* JsonMap class to apply transformations to a JSON object
|
|
96
|
+
*/
|
|
97
|
+
class JsonMap {
|
|
98
|
+
constructor(map = {}, lib = {}, { ignore = /^\$/ } = {}) {
|
|
99
|
+
_JsonMap_instances.add(this);
|
|
100
|
+
this.map = map;
|
|
101
|
+
this.lib = lib;
|
|
102
|
+
this.ignore = _.isString(ignore) ? new RegExp(ignore) : ignore;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Transforms the input data according to the map configuration.
|
|
106
|
+
*/
|
|
107
|
+
transform(input) {
|
|
108
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
109
|
+
// Sets the input data and initializes an empty output object
|
|
110
|
+
this.input = input;
|
|
111
|
+
this.output = {};
|
|
112
|
+
// Perform transformation & eliminate recursion from result.
|
|
113
|
+
const { replacer, reviver } = getJsonFns();
|
|
114
|
+
const result = JSON.parse(JSON.stringify(yield __classPrivateFieldGet(this, _JsonMap_instances, "m", _JsonMap_transform).call(this, this.map, this.input, this.output), replacer), reviver);
|
|
115
|
+
// Recursively eliminate non-string keys & string keys starting with $ and not in ignoreExclusions.
|
|
116
|
+
const deep = (value) => value instanceof Object && !_.isArray(value)
|
|
117
|
+
? _.mapValues(_.pickBy(value, (v, k) => !this.ignore.test(k)), (value) => _.cloneDeepWith(value, deep))
|
|
118
|
+
: undefined;
|
|
119
|
+
return _.cloneDeepWith(result, deep);
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
_JsonMap_instances = new WeakSet(), _JsonMap_transform = function _JsonMap_transform(node_1, input_1, output_1) {
|
|
124
|
+
return __awaiter(this, arguments, void 0, function* (node, input, output, path = '') {
|
|
125
|
+
logger.debug('#transform params:\n', { node, input, output, path });
|
|
126
|
+
// Checks if the current node is an object and has only a '$' key
|
|
127
|
+
if (node instanceof Object && _.size(node) === 1 && '$' in node) {
|
|
128
|
+
// Retrieves the transformations to be applied (can be an array or a single object)
|
|
129
|
+
const transformations = _.castArray(node.$);
|
|
130
|
+
logger.debug('transformations:\n', transformations);
|
|
131
|
+
// Array to store the results of the transformations
|
|
132
|
+
const results = [];
|
|
133
|
+
// Iterates over each transformation
|
|
134
|
+
for (const transformation of transformations) {
|
|
135
|
+
logger.debug('processing transformation:\n', transformation);
|
|
136
|
+
// Resolves the object path for the transformation
|
|
137
|
+
const { obj: methodObj, path: methodPath } = __classPrivateFieldGet(this, _JsonMap_instances, "m", _JsonMap_resolvePath).call(this, transformation.method, results);
|
|
138
|
+
// Resolves the parameter paths for the transformation
|
|
139
|
+
const params = _.castArray(transformation.params).map((param) => {
|
|
140
|
+
const { obj: paramObj, path: paramPath } = __classPrivateFieldGet(this, _JsonMap_instances, "m", _JsonMap_resolvePath).call(this, param, results);
|
|
141
|
+
return paramObj
|
|
142
|
+
? paramPath
|
|
143
|
+
? _.get(paramObj, paramPath)
|
|
144
|
+
: paramObj
|
|
145
|
+
: paramPath;
|
|
146
|
+
});
|
|
147
|
+
logger.debug('resolved transformation params:\n', params);
|
|
148
|
+
// Calls the specified method on the resolved object with the resolved parameters
|
|
149
|
+
const result = (yield _.invoke(methodObj, methodPath, ...params));
|
|
150
|
+
logger.debug('transformation result:\n', result);
|
|
151
|
+
// Stores the result of the transformation
|
|
152
|
+
results.unshift(result);
|
|
153
|
+
}
|
|
154
|
+
// Sets the output at the specified path to the last result of the transformations & returns.
|
|
155
|
+
_.set(output, path, results[0]);
|
|
156
|
+
logger.debug('updated output:\n', output);
|
|
157
|
+
return results[0];
|
|
158
|
+
}
|
|
159
|
+
// Checks if the current node is an object
|
|
160
|
+
if (_.isObject(node)) {
|
|
161
|
+
// Creates an empty array or object based on whether the current node is an array or not
|
|
162
|
+
const transformedNode = (Array.isArray(node) ? [] : {});
|
|
163
|
+
// Iterates over each key-value pair in the current node in ascending order by key
|
|
164
|
+
for (const [key, value] of _.sortBy(Object.entries(node), ([key]) => key)) {
|
|
165
|
+
// Constructs the current path by appending the current key to the previous path (if any)
|
|
166
|
+
const currentPath = path ? `${path}.${key}` : key;
|
|
167
|
+
// Recursively calls #transform with the current value, input, output, and path
|
|
168
|
+
// Assigns the transformed value to the corresponding key in the transformedNode
|
|
169
|
+
transformedNode[key] = yield __classPrivateFieldGet(this, _JsonMap_instances, "m", _JsonMap_transform).call(this, value, input, output, currentPath);
|
|
170
|
+
}
|
|
171
|
+
// Sets the output at the specified path to the transformedNode & returnsd.
|
|
172
|
+
_.set(output, path, transformedNode);
|
|
173
|
+
return transformedNode;
|
|
174
|
+
}
|
|
175
|
+
// Sets the output at the specified path to the current node & returns.
|
|
176
|
+
_.set(output, path, node);
|
|
177
|
+
return node;
|
|
178
|
+
});
|
|
179
|
+
}, _JsonMap_resolvePath = function _JsonMap_resolvePath(path, results) {
|
|
180
|
+
// If the path is not a string, return it as is
|
|
181
|
+
if (!_.isString(path)) {
|
|
182
|
+
return { path };
|
|
183
|
+
}
|
|
184
|
+
// Defines special patterns and their corresponding resolution functions
|
|
185
|
+
const patterns = {
|
|
186
|
+
'^\\$\\.(?<obj>lib|input|output)\\.?(?<path>.*)': ({ obj, path }) => ({
|
|
187
|
+
obj: this[obj],
|
|
188
|
+
path,
|
|
189
|
+
}),
|
|
190
|
+
'^\\$(?<path>\\[\\d+\\].*)': ({ path }) => ({
|
|
191
|
+
obj: results,
|
|
192
|
+
path,
|
|
193
|
+
}),
|
|
194
|
+
};
|
|
195
|
+
// Iterates over the special patterns
|
|
196
|
+
for (const [pattern, resolve] of Object.entries(patterns)) {
|
|
197
|
+
const match = path.match(pattern);
|
|
198
|
+
if (match === null || match === void 0 ? void 0 : match.groups)
|
|
199
|
+
return resolve(match.groups);
|
|
200
|
+
}
|
|
201
|
+
// Returns the path as is if it does not match any special patterns
|
|
202
|
+
return { path };
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
exports.JsonMap = JsonMap;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
interface JsonMapOptions {
|
|
4
|
+
ignore?: string | RegExp;
|
|
5
|
+
}
|
|
6
|
+
declare const JsonMapDynamic: z.ZodObject<{
|
|
7
|
+
$: z.ZodUnion<[z.ZodObject<{
|
|
8
|
+
method: z.ZodString;
|
|
9
|
+
params: z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>;
|
|
10
|
+
}, "strip", z.ZodTypeAny, {
|
|
11
|
+
method: string;
|
|
12
|
+
params: string | string[];
|
|
13
|
+
}, {
|
|
14
|
+
method: string;
|
|
15
|
+
params: string | string[];
|
|
16
|
+
}>, z.ZodArray<z.ZodObject<{
|
|
17
|
+
method: z.ZodString;
|
|
18
|
+
params: z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>;
|
|
19
|
+
}, "strip", z.ZodTypeAny, {
|
|
20
|
+
method: string;
|
|
21
|
+
params: string | string[];
|
|
22
|
+
}, {
|
|
23
|
+
method: string;
|
|
24
|
+
params: string | string[];
|
|
25
|
+
}>, "many">]>;
|
|
26
|
+
}, "strip", z.ZodTypeAny, {
|
|
27
|
+
$: {
|
|
28
|
+
method: string;
|
|
29
|
+
params: string | string[];
|
|
30
|
+
} | {
|
|
31
|
+
method: string;
|
|
32
|
+
params: string | string[];
|
|
33
|
+
}[];
|
|
34
|
+
}, {
|
|
35
|
+
$: {
|
|
36
|
+
method: string;
|
|
37
|
+
params: string | string[];
|
|
38
|
+
} | {
|
|
39
|
+
method: string;
|
|
40
|
+
params: string | string[];
|
|
41
|
+
}[];
|
|
42
|
+
}>;
|
|
43
|
+
type JsonMapDynamic = z.infer<typeof JsonMapDynamic>;
|
|
44
|
+
declare const literalSchema: z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodNull]>;
|
|
45
|
+
type Literal = z.infer<typeof literalSchema>;
|
|
46
|
+
type Json = Literal | {
|
|
47
|
+
[key: string]: Json;
|
|
48
|
+
} | Json[];
|
|
49
|
+
type JsonMapLib = {
|
|
50
|
+
[key: string]: JsonMapLib | ((x: any) => any);
|
|
51
|
+
} | JsonMapLib[];
|
|
52
|
+
type JsonMapMap = Literal | {
|
|
53
|
+
[key: string]: JsonMapMap | JsonMapDynamic;
|
|
54
|
+
} | JsonMapMap[];
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* JsonMap class to apply transformations to a JSON object
|
|
58
|
+
*/
|
|
59
|
+
declare class JsonMap {
|
|
60
|
+
#private;
|
|
61
|
+
private map;
|
|
62
|
+
private lib;
|
|
63
|
+
private ignore;
|
|
64
|
+
private input;
|
|
65
|
+
private output;
|
|
66
|
+
constructor(map?: JsonMapMap, lib?: JsonMapLib, { ignore }?: JsonMapOptions);
|
|
67
|
+
/**
|
|
68
|
+
* Transforms the input data according to the map configuration.
|
|
69
|
+
*/
|
|
70
|
+
transform(input: Json): Promise<Json>;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export { JsonMap };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
interface JsonMapOptions {
|
|
4
|
+
ignore?: string | RegExp;
|
|
5
|
+
}
|
|
6
|
+
declare const JsonMapDynamic: z.ZodObject<{
|
|
7
|
+
$: z.ZodUnion<[z.ZodObject<{
|
|
8
|
+
method: z.ZodString;
|
|
9
|
+
params: z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>;
|
|
10
|
+
}, "strip", z.ZodTypeAny, {
|
|
11
|
+
method: string;
|
|
12
|
+
params: string | string[];
|
|
13
|
+
}, {
|
|
14
|
+
method: string;
|
|
15
|
+
params: string | string[];
|
|
16
|
+
}>, z.ZodArray<z.ZodObject<{
|
|
17
|
+
method: z.ZodString;
|
|
18
|
+
params: z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>;
|
|
19
|
+
}, "strip", z.ZodTypeAny, {
|
|
20
|
+
method: string;
|
|
21
|
+
params: string | string[];
|
|
22
|
+
}, {
|
|
23
|
+
method: string;
|
|
24
|
+
params: string | string[];
|
|
25
|
+
}>, "many">]>;
|
|
26
|
+
}, "strip", z.ZodTypeAny, {
|
|
27
|
+
$: {
|
|
28
|
+
method: string;
|
|
29
|
+
params: string | string[];
|
|
30
|
+
} | {
|
|
31
|
+
method: string;
|
|
32
|
+
params: string | string[];
|
|
33
|
+
}[];
|
|
34
|
+
}, {
|
|
35
|
+
$: {
|
|
36
|
+
method: string;
|
|
37
|
+
params: string | string[];
|
|
38
|
+
} | {
|
|
39
|
+
method: string;
|
|
40
|
+
params: string | string[];
|
|
41
|
+
}[];
|
|
42
|
+
}>;
|
|
43
|
+
type JsonMapDynamic = z.infer<typeof JsonMapDynamic>;
|
|
44
|
+
declare const literalSchema: z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodNull]>;
|
|
45
|
+
type Literal = z.infer<typeof literalSchema>;
|
|
46
|
+
type Json = Literal | {
|
|
47
|
+
[key: string]: Json;
|
|
48
|
+
} | Json[];
|
|
49
|
+
type JsonMapLib = {
|
|
50
|
+
[key: string]: JsonMapLib | ((x: any) => any);
|
|
51
|
+
} | JsonMapLib[];
|
|
52
|
+
type JsonMapMap = Literal | {
|
|
53
|
+
[key: string]: JsonMapMap | JsonMapDynamic;
|
|
54
|
+
} | JsonMapMap[];
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* JsonMap class to apply transformations to a JSON object
|
|
58
|
+
*/
|
|
59
|
+
declare class JsonMap {
|
|
60
|
+
#private;
|
|
61
|
+
private map;
|
|
62
|
+
private lib;
|
|
63
|
+
private ignore;
|
|
64
|
+
private input;
|
|
65
|
+
private output;
|
|
66
|
+
constructor(map?: JsonMapMap, lib?: JsonMapLib, { ignore }?: JsonMapOptions);
|
|
67
|
+
/**
|
|
68
|
+
* Transforms the input data according to the map configuration.
|
|
69
|
+
*/
|
|
70
|
+
transform(input: Json): Promise<Json>;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export { JsonMap };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
interface JsonMapOptions {
|
|
4
|
+
ignore?: string | RegExp;
|
|
5
|
+
}
|
|
6
|
+
declare const JsonMapDynamic: z.ZodObject<{
|
|
7
|
+
$: z.ZodUnion<[z.ZodObject<{
|
|
8
|
+
method: z.ZodString;
|
|
9
|
+
params: z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>;
|
|
10
|
+
}, "strip", z.ZodTypeAny, {
|
|
11
|
+
method: string;
|
|
12
|
+
params: string | string[];
|
|
13
|
+
}, {
|
|
14
|
+
method: string;
|
|
15
|
+
params: string | string[];
|
|
16
|
+
}>, z.ZodArray<z.ZodObject<{
|
|
17
|
+
method: z.ZodString;
|
|
18
|
+
params: z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>;
|
|
19
|
+
}, "strip", z.ZodTypeAny, {
|
|
20
|
+
method: string;
|
|
21
|
+
params: string | string[];
|
|
22
|
+
}, {
|
|
23
|
+
method: string;
|
|
24
|
+
params: string | string[];
|
|
25
|
+
}>, "many">]>;
|
|
26
|
+
}, "strip", z.ZodTypeAny, {
|
|
27
|
+
$: {
|
|
28
|
+
method: string;
|
|
29
|
+
params: string | string[];
|
|
30
|
+
} | {
|
|
31
|
+
method: string;
|
|
32
|
+
params: string | string[];
|
|
33
|
+
}[];
|
|
34
|
+
}, {
|
|
35
|
+
$: {
|
|
36
|
+
method: string;
|
|
37
|
+
params: string | string[];
|
|
38
|
+
} | {
|
|
39
|
+
method: string;
|
|
40
|
+
params: string | string[];
|
|
41
|
+
}[];
|
|
42
|
+
}>;
|
|
43
|
+
type JsonMapDynamic = z.infer<typeof JsonMapDynamic>;
|
|
44
|
+
declare const literalSchema: z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodNull]>;
|
|
45
|
+
type Literal = z.infer<typeof literalSchema>;
|
|
46
|
+
type Json = Literal | {
|
|
47
|
+
[key: string]: Json;
|
|
48
|
+
} | Json[];
|
|
49
|
+
type JsonMapLib = {
|
|
50
|
+
[key: string]: JsonMapLib | ((x: any) => any);
|
|
51
|
+
} | JsonMapLib[];
|
|
52
|
+
type JsonMapMap = Literal | {
|
|
53
|
+
[key: string]: JsonMapMap | JsonMapDynamic;
|
|
54
|
+
} | JsonMapMap[];
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* JsonMap class to apply transformations to a JSON object
|
|
58
|
+
*/
|
|
59
|
+
declare class JsonMap {
|
|
60
|
+
#private;
|
|
61
|
+
private map;
|
|
62
|
+
private lib;
|
|
63
|
+
private ignore;
|
|
64
|
+
private input;
|
|
65
|
+
private output;
|
|
66
|
+
constructor(map?: JsonMapMap, lib?: JsonMapLib, { ignore }?: JsonMapOptions);
|
|
67
|
+
/**
|
|
68
|
+
* Transforms the input data according to the map configuration.
|
|
69
|
+
*/
|
|
70
|
+
transform(input: Json): Promise<Json>;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export { JsonMap };
|