@duplojs/data-parser-tools 0.2.3 → 0.2.5
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 +167 -2
- package/dist/toJsonSchema/override.cjs +5 -4
- package/dist/toJsonSchema/override.mjs +5 -4
- package/dist/toJsonSchema/render.cjs +11 -12
- package/dist/toJsonSchema/render.d.ts +8 -8
- package/dist/toJsonSchema/render.mjs +11 -12
- package/dist/toJsonSchema/transformer/create.d.ts +9 -7
- package/dist/toJsonSchema/transformer/defaults/date.cjs +1 -2
- package/dist/toJsonSchema/transformer/defaults/date.mjs +1 -2
- package/dist/toJsonSchema/transformer/defaults/file.cjs +17 -0
- package/dist/toJsonSchema/transformer/defaults/file.d.ts +5 -0
- package/dist/toJsonSchema/transformer/defaults/file.mjs +15 -0
- package/dist/toJsonSchema/transformer/defaults/index.cjs +2 -0
- package/dist/toJsonSchema/transformer/defaults/index.d.ts +1 -1
- package/dist/toJsonSchema/transformer/defaults/index.mjs +2 -0
- package/dist/toJsonSchema/transformer/defaults/literal.cjs +59 -23
- package/dist/toJsonSchema/transformer/defaults/literal.d.ts +12 -4
- package/dist/toJsonSchema/transformer/defaults/literal.mjs +60 -24
- package/dist/toJsonSchema/transformer/defaults/nullable.cjs +11 -2
- package/dist/toJsonSchema/transformer/defaults/nullable.mjs +11 -2
- package/dist/toJsonSchema/transformer/defaults/object.cjs +1 -1
- package/dist/toJsonSchema/transformer/defaults/object.mjs +1 -1
- package/dist/toJsonSchema/transformer/defaults/optional.cjs +10 -1
- package/dist/toJsonSchema/transformer/defaults/optional.mjs +10 -1
- package/dist/toJsonSchema/transformer/defaults/time.cjs +1 -1
- package/dist/toJsonSchema/transformer/defaults/time.mjs +1 -1
- package/dist/toJsonSchema/transformer/defaults/union.cjs +3 -3
- package/dist/toJsonSchema/transformer/defaults/union.mjs +3 -3
- package/dist/toJsonSchema/transformer/transformer.cjs +10 -10
- package/dist/toJsonSchema/transformer/transformer.d.ts +5 -5
- package/dist/toJsonSchema/transformer/transformer.mjs +10 -10
- package/dist/toTypescript/index.cjs +2 -0
- package/dist/toTypescript/index.mjs +1 -0
- package/dist/toTypescript/override.cjs +5 -7
- package/dist/toTypescript/override.mjs +5 -7
- package/dist/toTypescript/render.cjs +7 -1
- package/dist/toTypescript/render.d.ts +6 -6
- package/dist/toTypescript/render.mjs +7 -1
- package/dist/toTypescript/transformer/create.d.ts +10 -5
- package/dist/toTypescript/transformer/defaults/array.d.ts +1 -2
- package/dist/toTypescript/transformer/defaults/bigint.d.ts +1 -2
- package/dist/toTypescript/transformer/defaults/boolean.d.ts +1 -2
- package/dist/toTypescript/transformer/defaults/date.cjs +17 -7
- package/dist/toTypescript/transformer/defaults/date.d.ts +1 -2
- package/dist/toTypescript/transformer/defaults/date.mjs +19 -9
- package/dist/toTypescript/transformer/defaults/empty.d.ts +1 -2
- package/dist/toTypescript/transformer/defaults/file.cjs +13 -0
- package/dist/toTypescript/transformer/defaults/file.d.ts +1 -0
- package/dist/toTypescript/transformer/defaults/file.mjs +11 -0
- package/dist/toTypescript/transformer/defaults/index.cjs +2 -0
- package/dist/toTypescript/transformer/defaults/index.d.ts +1 -1
- package/dist/toTypescript/transformer/defaults/index.mjs +2 -0
- package/dist/toTypescript/transformer/defaults/lazy.d.ts +1 -2
- package/dist/toTypescript/transformer/defaults/literal.d.ts +1 -2
- package/dist/toTypescript/transformer/defaults/nil.d.ts +1 -2
- package/dist/toTypescript/transformer/defaults/nullable.cjs +11 -2
- package/dist/toTypescript/transformer/defaults/nullable.d.ts +1 -2
- package/dist/toTypescript/transformer/defaults/nullable.mjs +11 -2
- package/dist/toTypescript/transformer/defaults/number.d.ts +1 -2
- package/dist/toTypescript/transformer/defaults/object.cjs +2 -10
- package/dist/toTypescript/transformer/defaults/object.d.ts +1 -2
- package/dist/toTypescript/transformer/defaults/object.mjs +2 -10
- package/dist/toTypescript/transformer/defaults/optional.cjs +11 -2
- package/dist/toTypescript/transformer/defaults/optional.d.ts +1 -2
- package/dist/toTypescript/transformer/defaults/optional.mjs +11 -2
- package/dist/toTypescript/transformer/defaults/pipe.d.ts +1 -2
- package/dist/toTypescript/transformer/defaults/record.cjs +2 -1
- package/dist/toTypescript/transformer/defaults/record.d.ts +1 -2
- package/dist/toTypescript/transformer/defaults/record.mjs +3 -2
- package/dist/toTypescript/transformer/defaults/recover.d.ts +1 -2
- package/dist/toTypescript/transformer/defaults/string.d.ts +1 -2
- package/dist/toTypescript/transformer/defaults/templateLiteral.d.ts +1 -2
- package/dist/toTypescript/transformer/defaults/time.cjs +17 -7
- package/dist/toTypescript/transformer/defaults/time.d.ts +1 -2
- package/dist/toTypescript/transformer/defaults/time.mjs +18 -8
- package/dist/toTypescript/transformer/defaults/transform.d.ts +1 -2
- package/dist/toTypescript/transformer/defaults/tuple.d.ts +1 -2
- package/dist/toTypescript/transformer/defaults/union.d.ts +1 -2
- package/dist/toTypescript/transformer/defaults/unknown.d.ts +1 -2
- package/dist/toTypescript/transformer/hook.d.ts +3 -2
- package/dist/toTypescript/transformer/importTypesTransformer.cjs +10 -0
- package/dist/toTypescript/transformer/importTypesTransformer.d.ts +2 -0
- package/dist/toTypescript/transformer/importTypesTransformer.mjs +8 -0
- package/dist/toTypescript/transformer/includesUndefinedTypeNode.cjs +16 -0
- package/dist/toTypescript/transformer/includesUndefinedTypeNode.d.ts +2 -0
- package/dist/toTypescript/transformer/includesUndefinedTypeNode.mjs +14 -0
- package/dist/toTypescript/transformer/index.cjs +2 -0
- package/dist/toTypescript/transformer/index.d.ts +1 -0
- package/dist/toTypescript/transformer/index.mjs +1 -0
- package/dist/toTypescript/transformer/transformer.cjs +8 -0
- package/dist/toTypescript/transformer/transformer.d.ts +4 -3
- package/dist/toTypescript/transformer/transformer.mjs +8 -0
- package/package.json +3 -2
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
import { DP } from "@duplojs/utils";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
type JsonPrimitive = string | number | boolean | null;
|
|
3
|
+
type JsonType = "string" | "number" | "integer" | "boolean" | "null";
|
|
4
|
+
export type JsonSchemaLiteral = {
|
|
5
|
+
const: JsonPrimitive;
|
|
6
|
+
type: JsonType;
|
|
7
|
+
} | {
|
|
8
|
+
enum: readonly [JsonPrimitive];
|
|
9
|
+
type: JsonType;
|
|
10
|
+
} | {
|
|
11
|
+
enum: readonly [null];
|
|
12
|
+
};
|
|
6
13
|
export declare const literalTransformer: (schema: DP.DataParsers, params: import("..").TransformerParams) => import("..").MaybeTransformerEither;
|
|
14
|
+
export {};
|
|
@@ -1,45 +1,81 @@
|
|
|
1
|
-
import { DP, A, P, isType, pipe } from '@duplojs/utils';
|
|
1
|
+
import { DP, A, P, isType, justReturn, pipe } from '@duplojs/utils';
|
|
2
2
|
import { createTransformer } from '../create.mjs';
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
function isOldVersion(version) {
|
|
5
|
+
return (version === "jsonSchema4"
|
|
6
|
+
|| version === "openApi3");
|
|
7
|
+
}
|
|
8
|
+
const literalTransformer = createTransformer(DP.literalKind.has, (schema, { success, version, }) => {
|
|
5
9
|
const reduced = A.reduce(schema.definition.value, A.reduceFrom({
|
|
6
10
|
literals: [],
|
|
7
|
-
|
|
11
|
+
isOptional: false,
|
|
8
12
|
}), ({ element, lastValue, next, nextWithObject, }) => P.match(element)
|
|
9
|
-
.when(isType("undefined"), () => nextWithObject(lastValue, {
|
|
13
|
+
.when(isType("undefined"), () => nextWithObject(lastValue, { isOptional: true }))
|
|
10
14
|
.when(isType("bigint"), (value) => nextWithObject(lastValue, {
|
|
11
|
-
literals: A.push(lastValue.literals,
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
+
literals: A.push(lastValue.literals, isOldVersion(version)
|
|
16
|
+
? {
|
|
17
|
+
enum: [value.toString()],
|
|
18
|
+
type: "string",
|
|
19
|
+
}
|
|
20
|
+
: {
|
|
21
|
+
const: value.toString(),
|
|
22
|
+
type: "string",
|
|
23
|
+
}),
|
|
15
24
|
}))
|
|
16
25
|
.when(isType("string"), (value) => nextWithObject(lastValue, {
|
|
17
|
-
literals: A.push(lastValue.literals,
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
26
|
+
literals: A.push(lastValue.literals, isOldVersion(version)
|
|
27
|
+
? {
|
|
28
|
+
enum: [value],
|
|
29
|
+
type: "string",
|
|
30
|
+
}
|
|
31
|
+
: {
|
|
32
|
+
const: value,
|
|
33
|
+
type: "string",
|
|
34
|
+
}),
|
|
21
35
|
}))
|
|
22
36
|
.when(isType("number"), (value) => nextWithObject(lastValue, {
|
|
23
|
-
literals: A.push(lastValue.literals,
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
37
|
+
literals: A.push(lastValue.literals, isOldVersion(version)
|
|
38
|
+
? {
|
|
39
|
+
enum: [value],
|
|
40
|
+
type: Number.isInteger(value)
|
|
41
|
+
? "integer"
|
|
42
|
+
: "number",
|
|
43
|
+
}
|
|
44
|
+
: {
|
|
45
|
+
const: value,
|
|
46
|
+
type: Number.isInteger(value)
|
|
47
|
+
? "integer"
|
|
48
|
+
: "number",
|
|
49
|
+
}),
|
|
27
50
|
}))
|
|
28
51
|
.when(isType("boolean"), (value) => nextWithObject(lastValue, {
|
|
29
|
-
literals: A.push(lastValue.literals,
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
52
|
+
literals: A.push(lastValue.literals, isOldVersion(version)
|
|
53
|
+
? {
|
|
54
|
+
enum: [value],
|
|
55
|
+
type: "boolean",
|
|
56
|
+
}
|
|
57
|
+
: {
|
|
58
|
+
const: value,
|
|
59
|
+
type: "boolean",
|
|
60
|
+
}),
|
|
33
61
|
}))
|
|
34
62
|
.when(isType("null"), () => nextWithObject(lastValue, {
|
|
35
|
-
literals: A.push(lastValue.literals,
|
|
36
|
-
|
|
63
|
+
literals: A.push(lastValue.literals, P.match(version)
|
|
64
|
+
.with("openApi3", justReturn({
|
|
65
|
+
enum: [null],
|
|
66
|
+
}))
|
|
67
|
+
.with("jsonSchema4", justReturn({
|
|
68
|
+
type: "null",
|
|
69
|
+
enum: [null],
|
|
70
|
+
}))
|
|
71
|
+
.otherwise(justReturn({
|
|
37
72
|
type: "null",
|
|
38
|
-
|
|
73
|
+
const: null,
|
|
74
|
+
}))),
|
|
39
75
|
}))
|
|
40
76
|
.otherwise(() => next(lastValue)));
|
|
41
77
|
const schemaDefinition = pipe(reduced.literals, P.when(A.lengthEqual(0), () => ({})), P.when(A.lengthEqual(1), A.first), P.otherwise((value) => ({ anyOf: value })));
|
|
42
|
-
return success(schemaDefinition, reduced.
|
|
78
|
+
return success(schemaDefinition, reduced.isOptional);
|
|
43
79
|
});
|
|
44
80
|
|
|
45
81
|
export { literalTransformer };
|
|
@@ -9,12 +9,21 @@ const nullableTransformer = create.createTransformer(utils.DP.nullableKind.has,
|
|
|
9
9
|
return innerResult;
|
|
10
10
|
}
|
|
11
11
|
const inner = utils.unwrap(innerResult);
|
|
12
|
-
return utils.pipe(
|
|
12
|
+
return utils.pipe({
|
|
13
|
+
mode,
|
|
14
|
+
coalescingValue: schema.definition.coalescingValue,
|
|
15
|
+
}, (value) => utils.P.match(value)
|
|
16
|
+
.with(utils.P.union({ mode: "in" }, {
|
|
17
|
+
mode: "out",
|
|
18
|
+
coalescingValue: undefined,
|
|
19
|
+
}), () => ({
|
|
13
20
|
anyOf: [
|
|
14
21
|
inner.schema,
|
|
15
22
|
{ type: "null" },
|
|
16
23
|
],
|
|
17
|
-
}))
|
|
24
|
+
}))
|
|
25
|
+
.with({ mode: "out" }, () => inner.schema)
|
|
26
|
+
.exhaustive(), (schema) => success(schema, inner.isOptional));
|
|
18
27
|
});
|
|
19
28
|
|
|
20
29
|
exports.nullableTransformer = nullableTransformer;
|
|
@@ -7,12 +7,21 @@ const nullableTransformer = createTransformer(DP.nullableKind.has, (schema, { tr
|
|
|
7
7
|
return innerResult;
|
|
8
8
|
}
|
|
9
9
|
const inner = unwrap(innerResult);
|
|
10
|
-
return pipe(
|
|
10
|
+
return pipe({
|
|
11
|
+
mode,
|
|
12
|
+
coalescingValue: schema.definition.coalescingValue,
|
|
13
|
+
}, (value) => P.match(value)
|
|
14
|
+
.with(P.union({ mode: "in" }, {
|
|
15
|
+
mode: "out",
|
|
16
|
+
coalescingValue: undefined,
|
|
17
|
+
}), () => ({
|
|
11
18
|
anyOf: [
|
|
12
19
|
inner.schema,
|
|
13
20
|
{ type: "null" },
|
|
14
21
|
],
|
|
15
|
-
}))
|
|
22
|
+
}))
|
|
23
|
+
.with({ mode: "out" }, () => inner.schema)
|
|
24
|
+
.exhaustive(), (schema) => success(schema, inner.isOptional));
|
|
16
25
|
});
|
|
17
26
|
|
|
18
27
|
export { nullableTransformer };
|
|
@@ -14,7 +14,7 @@ const objectTransformer = create.createTransformer(utils.DP.objectKind.has, (sch
|
|
|
14
14
|
...lastValue.properties,
|
|
15
15
|
[key]: inner.schema,
|
|
16
16
|
},
|
|
17
|
-
required: inner.
|
|
17
|
+
required: inner.isOptional
|
|
18
18
|
? lastValue.required
|
|
19
19
|
: utils.A.push(lastValue.required ?? [], key),
|
|
20
20
|
})))));
|
|
@@ -12,7 +12,7 @@ const objectTransformer = createTransformer(DP.objectKind.has, (schema, { transf
|
|
|
12
12
|
...lastValue.properties,
|
|
13
13
|
[key]: inner.schema,
|
|
14
14
|
},
|
|
15
|
-
required: inner.
|
|
15
|
+
required: inner.isOptional
|
|
16
16
|
? lastValue.required
|
|
17
17
|
: A.push(lastValue.required ?? [], key),
|
|
18
18
|
})))));
|
|
@@ -3,6 +3,15 @@
|
|
|
3
3
|
var utils = require('@duplojs/utils');
|
|
4
4
|
var create = require('../create.cjs');
|
|
5
5
|
|
|
6
|
-
const optionalTransformer = create.createTransformer(utils.DP.optionalKind.has, (schema, { transformer, success, mode, }) => utils.pipe(schema.definition.inner, transformer, utils.E.whenIsRight((inner) => utils.pipe(
|
|
6
|
+
const optionalTransformer = create.createTransformer(utils.DP.optionalKind.has, (schema, { transformer, success, mode, }) => utils.pipe(schema.definition.inner, transformer, utils.E.whenIsRight((inner) => utils.pipe({
|
|
7
|
+
mode,
|
|
8
|
+
coalescingValue: schema.definition.coalescingValue,
|
|
9
|
+
}, (value) => utils.P.match(value)
|
|
10
|
+
.with(utils.P.union({ mode: "in" }, {
|
|
11
|
+
mode: "out",
|
|
12
|
+
coalescingValue: undefined,
|
|
13
|
+
}), () => true)
|
|
14
|
+
.with({ mode: "out" }, () => inner.isOptional)
|
|
15
|
+
.exhaustive(), (isOptional) => success(inner.schema, isOptional)))));
|
|
7
16
|
|
|
8
17
|
exports.optionalTransformer = optionalTransformer;
|
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
import { DP, pipe, E, P } from '@duplojs/utils';
|
|
2
2
|
import { createTransformer } from '../create.mjs';
|
|
3
3
|
|
|
4
|
-
const optionalTransformer = createTransformer(DP.optionalKind.has, (schema, { transformer, success, mode, }) => pipe(schema.definition.inner, transformer, E.whenIsRight((inner) => pipe(
|
|
4
|
+
const optionalTransformer = createTransformer(DP.optionalKind.has, (schema, { transformer, success, mode, }) => pipe(schema.definition.inner, transformer, E.whenIsRight((inner) => pipe({
|
|
5
|
+
mode,
|
|
6
|
+
coalescingValue: schema.definition.coalescingValue,
|
|
7
|
+
}, (value) => P.match(value)
|
|
8
|
+
.with(P.union({ mode: "in" }, {
|
|
9
|
+
mode: "out",
|
|
10
|
+
coalescingValue: undefined,
|
|
11
|
+
}), () => true)
|
|
12
|
+
.with({ mode: "out" }, () => inner.isOptional)
|
|
13
|
+
.exhaustive(), (isOptional) => success(inner.schema, isOptional)))));
|
|
5
14
|
|
|
6
15
|
export { optionalTransformer };
|
|
@@ -6,7 +6,7 @@ var create = require('../create.cjs');
|
|
|
6
6
|
const timeTransformer = create.createTransformer(utils.DP.timeKind.has, (schema, { success, mode, }) => {
|
|
7
7
|
const base = {
|
|
8
8
|
type: "string",
|
|
9
|
-
pattern: utils.D.
|
|
9
|
+
pattern: utils.D.serializeTheTimeRegex.source,
|
|
10
10
|
};
|
|
11
11
|
if (mode === "in" && schema.definition.coerce) {
|
|
12
12
|
return success({
|
|
@@ -4,7 +4,7 @@ import { createTransformer } from '../create.mjs';
|
|
|
4
4
|
const timeTransformer = createTransformer(DP.timeKind.has, (schema, { success, mode, }) => {
|
|
5
5
|
const base = {
|
|
6
6
|
type: "string",
|
|
7
|
-
pattern: D.
|
|
7
|
+
pattern: D.serializeTheTimeRegex.source,
|
|
8
8
|
};
|
|
9
9
|
if (mode === "in" && schema.definition.coerce) {
|
|
10
10
|
return success({
|
|
@@ -4,9 +4,9 @@ var utils = require('@duplojs/utils');
|
|
|
4
4
|
var create = require('../create.cjs');
|
|
5
5
|
|
|
6
6
|
const unionTransformer = create.createTransformer(utils.DP.unionKind.has, (schema, { transformer, success, }) => {
|
|
7
|
-
let
|
|
7
|
+
let isOptional = false;
|
|
8
8
|
const optionsResult = utils.A.reduce(schema.definition.options, utils.A.reduceFrom([]), ({ element, lastValue, nextPush, exit }) => utils.pipe(transformer(element), utils.when(utils.E.isLeft, exit), utils.E.whenIsRight((option) => {
|
|
9
|
-
|
|
9
|
+
isOptional ||= option.isOptional;
|
|
10
10
|
return nextPush(lastValue, option.schema);
|
|
11
11
|
})));
|
|
12
12
|
if (utils.E.isLeft(optionsResult)) {
|
|
@@ -14,7 +14,7 @@ const unionTransformer = create.createTransformer(utils.DP.unionKind.has, (schem
|
|
|
14
14
|
}
|
|
15
15
|
return success({
|
|
16
16
|
anyOf: optionsResult,
|
|
17
|
-
},
|
|
17
|
+
}, isOptional);
|
|
18
18
|
});
|
|
19
19
|
|
|
20
20
|
exports.unionTransformer = unionTransformer;
|
|
@@ -2,9 +2,9 @@ import { DP, A, pipe, when, E } from '@duplojs/utils';
|
|
|
2
2
|
import { createTransformer } from '../create.mjs';
|
|
3
3
|
|
|
4
4
|
const unionTransformer = createTransformer(DP.unionKind.has, (schema, { transformer, success, }) => {
|
|
5
|
-
let
|
|
5
|
+
let isOptional = false;
|
|
6
6
|
const optionsResult = A.reduce(schema.definition.options, A.reduceFrom([]), ({ element, lastValue, nextPush, exit }) => pipe(transformer(element), when(E.isLeft, exit), E.whenIsRight((option) => {
|
|
7
|
-
|
|
7
|
+
isOptional ||= option.isOptional;
|
|
8
8
|
return nextPush(lastValue, option.schema);
|
|
9
9
|
})));
|
|
10
10
|
if (E.isLeft(optionsResult)) {
|
|
@@ -12,7 +12,7 @@ const unionTransformer = createTransformer(DP.unionKind.has, (schema, { transfor
|
|
|
12
12
|
}
|
|
13
13
|
return success({
|
|
14
14
|
anyOf: optionsResult,
|
|
15
|
-
},
|
|
15
|
+
}, isOptional);
|
|
16
16
|
});
|
|
17
17
|
|
|
18
18
|
export { unionTransformer };
|
|
@@ -3,11 +3,11 @@
|
|
|
3
3
|
var utils = require('@duplojs/utils');
|
|
4
4
|
|
|
5
5
|
function buildRef(name, version) {
|
|
6
|
-
if (version === "
|
|
7
|
-
|| version === "
|
|
6
|
+
if (version === "openApi3"
|
|
7
|
+
|| version === "openApi31") {
|
|
8
8
|
return `#/components/schemas/${name}`;
|
|
9
9
|
}
|
|
10
|
-
if (version === "
|
|
10
|
+
if (version === "jsonSchema202012") {
|
|
11
11
|
return `#/$defs/${name}`;
|
|
12
12
|
}
|
|
13
13
|
return `#/definitions/${name}`;
|
|
@@ -33,22 +33,22 @@ function transformer(schema, params) {
|
|
|
33
33
|
if (currentDeclaration) {
|
|
34
34
|
return utils.E.right("buildSuccess", {
|
|
35
35
|
schema: { $ref: buildRef(currentDeclaration.name, params.version) },
|
|
36
|
-
|
|
36
|
+
isOptional: currentDeclaration.isOptional,
|
|
37
37
|
});
|
|
38
38
|
}
|
|
39
39
|
const currentIdentifier = utils.whenElse(params.recursiveDataParsers, utils.A.includes(currentSchema), () => {
|
|
40
40
|
const identifier = currentSchema.definition.identifier ?? `RecursiveType${params.context.size}`;
|
|
41
41
|
params.context.set(currentSchema, {
|
|
42
42
|
name: identifier,
|
|
43
|
-
|
|
43
|
+
isOptional: false,
|
|
44
44
|
});
|
|
45
45
|
return identifier;
|
|
46
46
|
}, utils.justReturn(currentSchema.definition.identifier));
|
|
47
47
|
const functionParams = {
|
|
48
|
-
success(result,
|
|
48
|
+
success(result, isOptional = false) {
|
|
49
49
|
return utils.E.right("buildSuccess", {
|
|
50
50
|
schema: result,
|
|
51
|
-
|
|
51
|
+
isOptional,
|
|
52
52
|
});
|
|
53
53
|
},
|
|
54
54
|
transformer(schema) {
|
|
@@ -75,13 +75,13 @@ function transformer(schema, params) {
|
|
|
75
75
|
return result;
|
|
76
76
|
}
|
|
77
77
|
if (currentIdentifier) {
|
|
78
|
-
const { schema: builtSchema,
|
|
78
|
+
const { schema: builtSchema, isOptional } = utils.unwrap(result);
|
|
79
79
|
params.context.set(currentSchema, {
|
|
80
80
|
name: currentIdentifier,
|
|
81
81
|
schema: builtSchema,
|
|
82
|
-
|
|
82
|
+
isOptional,
|
|
83
83
|
});
|
|
84
|
-
return functionParams.success({ $ref: buildRef(currentIdentifier, params.version) },
|
|
84
|
+
return functionParams.success({ $ref: buildRef(currentIdentifier, params.version) }, isOptional);
|
|
85
85
|
}
|
|
86
86
|
return result;
|
|
87
87
|
}
|
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
import { E, type DP } from "@duplojs/utils";
|
|
2
|
-
import { type MapContext, type DataParserNotSupportedEither, type createTransformer, type TransformerMode, type
|
|
2
|
+
import { type MapContext, type DataParserNotSupportedEither, type createTransformer, type TransformerMode, type DataParserErrorEither, type SupportedVersions } from "./create";
|
|
3
3
|
import { type TransformerHook } from "./hook";
|
|
4
4
|
export interface TransformerFunctionParams {
|
|
5
5
|
readonly transformers: readonly ReturnType<typeof createTransformer>[];
|
|
6
6
|
readonly context: MapContext;
|
|
7
7
|
readonly mode: TransformerMode;
|
|
8
|
-
readonly version:
|
|
8
|
+
readonly version: SupportedVersions;
|
|
9
9
|
readonly hooks: readonly TransformerHook[];
|
|
10
10
|
readonly recursiveDataParsers: DP.DataParser[];
|
|
11
11
|
}
|
|
12
|
-
export declare function buildRef(name: string, version:
|
|
13
|
-
export declare function transformer(schema: DP.DataParser, params: TransformerFunctionParams): import("./create").TransformerSuccessEither | DataParserNotSupportedEither | DataParserErrorEither | E.
|
|
12
|
+
export declare function buildRef(name: string, version: SupportedVersions): string;
|
|
13
|
+
export declare function transformer(schema: DP.DataParser, params: TransformerFunctionParams): import("./create").TransformerSuccessEither | DataParserNotSupportedEither | DataParserErrorEither | E.Right<"buildSuccess", {
|
|
14
14
|
readonly schema: {
|
|
15
15
|
readonly $ref: string;
|
|
16
16
|
};
|
|
17
|
-
readonly
|
|
17
|
+
readonly isOptional: boolean;
|
|
18
18
|
}>;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { A, E, whenElse, justReturn, unwrap } from '@duplojs/utils';
|
|
2
2
|
|
|
3
3
|
function buildRef(name, version) {
|
|
4
|
-
if (version === "
|
|
5
|
-
|| version === "
|
|
4
|
+
if (version === "openApi3"
|
|
5
|
+
|| version === "openApi31") {
|
|
6
6
|
return `#/components/schemas/${name}`;
|
|
7
7
|
}
|
|
8
|
-
if (version === "
|
|
8
|
+
if (version === "jsonSchema202012") {
|
|
9
9
|
return `#/$defs/${name}`;
|
|
10
10
|
}
|
|
11
11
|
return `#/definitions/${name}`;
|
|
@@ -31,22 +31,22 @@ function transformer(schema, params) {
|
|
|
31
31
|
if (currentDeclaration) {
|
|
32
32
|
return E.right("buildSuccess", {
|
|
33
33
|
schema: { $ref: buildRef(currentDeclaration.name, params.version) },
|
|
34
|
-
|
|
34
|
+
isOptional: currentDeclaration.isOptional,
|
|
35
35
|
});
|
|
36
36
|
}
|
|
37
37
|
const currentIdentifier = whenElse(params.recursiveDataParsers, A.includes(currentSchema), () => {
|
|
38
38
|
const identifier = currentSchema.definition.identifier ?? `RecursiveType${params.context.size}`;
|
|
39
39
|
params.context.set(currentSchema, {
|
|
40
40
|
name: identifier,
|
|
41
|
-
|
|
41
|
+
isOptional: false,
|
|
42
42
|
});
|
|
43
43
|
return identifier;
|
|
44
44
|
}, justReturn(currentSchema.definition.identifier));
|
|
45
45
|
const functionParams = {
|
|
46
|
-
success(result,
|
|
46
|
+
success(result, isOptional = false) {
|
|
47
47
|
return E.right("buildSuccess", {
|
|
48
48
|
schema: result,
|
|
49
|
-
|
|
49
|
+
isOptional,
|
|
50
50
|
});
|
|
51
51
|
},
|
|
52
52
|
transformer(schema) {
|
|
@@ -73,13 +73,13 @@ function transformer(schema, params) {
|
|
|
73
73
|
return result;
|
|
74
74
|
}
|
|
75
75
|
if (currentIdentifier) {
|
|
76
|
-
const { schema: builtSchema,
|
|
76
|
+
const { schema: builtSchema, isOptional } = unwrap(result);
|
|
77
77
|
params.context.set(currentSchema, {
|
|
78
78
|
name: currentIdentifier,
|
|
79
79
|
schema: builtSchema,
|
|
80
|
-
|
|
80
|
+
isOptional,
|
|
81
81
|
});
|
|
82
|
-
return functionParams.success({ $ref: buildRef(currentIdentifier, params.version) },
|
|
82
|
+
return functionParams.success({ $ref: buildRef(currentIdentifier, params.version) }, isOptional);
|
|
83
83
|
}
|
|
84
84
|
return result;
|
|
85
85
|
}
|
|
@@ -29,6 +29,7 @@ var union = require('./transformer/defaults/union.cjs');
|
|
|
29
29
|
var unknown = require('./transformer/defaults/unknown.cjs');
|
|
30
30
|
var date = require('./transformer/defaults/date.cjs');
|
|
31
31
|
var time = require('./transformer/defaults/time.cjs');
|
|
32
|
+
var includesUndefinedTypeNode = require('./transformer/includesUndefinedTypeNode.cjs');
|
|
32
33
|
|
|
33
34
|
|
|
34
35
|
|
|
@@ -60,3 +61,4 @@ exports.unionTransformer = union.unionTransformer;
|
|
|
60
61
|
exports.unknownTransformer = unknown.unknownTransformer;
|
|
61
62
|
exports.dateTransformer = date.dateTransformer;
|
|
62
63
|
exports.timeTransformer = time.timeTransformer;
|
|
64
|
+
exports.includesUndefinedTypeNode = includesUndefinedTypeNode.includesUndefinedTypeNode;
|
|
@@ -27,3 +27,4 @@ export { unionTransformer } from './transformer/defaults/union.mjs';
|
|
|
27
27
|
export { unknownTransformer } from './transformer/defaults/unknown.mjs';
|
|
28
28
|
export { dateTransformer } from './transformer/defaults/date.mjs';
|
|
29
29
|
export { timeTransformer } from './transformer/defaults/time.mjs';
|
|
30
|
+
export { includesUndefinedTypeNode } from './transformer/includesUndefinedTypeNode.mjs';
|
|
@@ -6,10 +6,8 @@ dataParser.dataParserInit.overrideHandler.setMethod("setIdentifier", (schema, id
|
|
|
6
6
|
schema.definition.identifier = identifier;
|
|
7
7
|
return schema;
|
|
8
8
|
});
|
|
9
|
-
dataParser.dataParserInit.overrideHandler.setMethod("addIdentifier", (schema, identifier) =>
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
},
|
|
15
|
-
}));
|
|
9
|
+
dataParser.dataParserInit.overrideHandler.setMethod("addIdentifier", (schema, identifier) => {
|
|
10
|
+
const newSchema = schema.clone();
|
|
11
|
+
newSchema.setIdentifier(identifier);
|
|
12
|
+
return newSchema;
|
|
13
|
+
});
|
|
@@ -4,10 +4,8 @@ dataParserInit.overrideHandler.setMethod("setIdentifier", (schema, identifier) =
|
|
|
4
4
|
schema.definition.identifier = identifier;
|
|
5
5
|
return schema;
|
|
6
6
|
});
|
|
7
|
-
dataParserInit.overrideHandler.setMethod("addIdentifier", (schema, identifier) =>
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
},
|
|
13
|
-
}));
|
|
7
|
+
dataParserInit.overrideHandler.setMethod("addIdentifier", (schema, identifier) => {
|
|
8
|
+
const newSchema = schema.clone();
|
|
9
|
+
newSchema.setIdentifier(identifier);
|
|
10
|
+
return newSchema;
|
|
11
|
+
});
|
|
@@ -5,6 +5,7 @@ require('./transformer/index.cjs');
|
|
|
5
5
|
var typescript = require('typescript');
|
|
6
6
|
var kind = require('./kind.cjs');
|
|
7
7
|
var getRecursiveDataParser = require('../utils/getRecursiveDataParser.cjs');
|
|
8
|
+
var importTypesTransformer = require('./transformer/importTypesTransformer.cjs');
|
|
8
9
|
var transformer = require('./transformer/transformer.cjs');
|
|
9
10
|
|
|
10
11
|
class DataParserToTypescriptRenderError extends utils.kindHeritage("data-parser-to-typescript-render-error", kind.createToTypescriptKind("data-parser-to-typescript-render-error"), Error) {
|
|
@@ -18,12 +19,14 @@ class DataParserToTypescriptRenderError extends utils.kindHeritage("data-parser-
|
|
|
18
19
|
}
|
|
19
20
|
function render(schema, params) {
|
|
20
21
|
const context = new Map(params.context);
|
|
22
|
+
const importType = new Map(params.importType);
|
|
21
23
|
const result = transformer.transformer(schema, {
|
|
22
24
|
...params,
|
|
23
25
|
context,
|
|
24
26
|
mode: params.mode ?? "out",
|
|
25
27
|
hooks: params.hooks ?? [],
|
|
26
28
|
recursiveDataParsers: getRecursiveDataParser.getRecursiveDataParser(schema),
|
|
29
|
+
importType,
|
|
27
30
|
});
|
|
28
31
|
if (utils.E.isLeft(result)) {
|
|
29
32
|
throw new DataParserToTypescriptRenderError(schema, result);
|
|
@@ -36,7 +39,10 @@ function render(schema, params) {
|
|
|
36
39
|
}
|
|
37
40
|
const sourceFile = typescript.createSourceFile("print.ts", "", typescript.ScriptTarget.Latest, false, typescript.ScriptKind.TS);
|
|
38
41
|
const printer = typescript.createPrinter();
|
|
39
|
-
return utils.pipe(
|
|
42
|
+
return utils.pipe([
|
|
43
|
+
...importTypesTransformer.importTypesTransformer(importType),
|
|
44
|
+
...context.values(),
|
|
45
|
+
], utils.G.map((value) => printer.printNode(typescript.EmitHint.Unspecified, value, sourceFile)), utils.A.from, utils.A.join("\n\n"), utils.S.trim);
|
|
40
46
|
}
|
|
41
47
|
|
|
42
48
|
exports.DataParserToTypescriptRenderError = DataParserToTypescriptRenderError;
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { type DataParserErrorEither, type DataParserNotSupportedEither, type MapContext, type TransformerMode, type TransformerHook, type createTransformer } from "./transformer";
|
|
1
|
+
import { type DataParserErrorEither, type DataParserNotSupportedEither, type MapContext, type TransformerMode, type TransformerHook, type createTransformer, type SupportedDataParsers, type MapImportType } from "./transformer";
|
|
3
2
|
export interface RenderParams {
|
|
4
3
|
readonly identifier: string;
|
|
5
4
|
readonly transformers: readonly ReturnType<typeof createTransformer>[];
|
|
6
5
|
readonly context?: MapContext;
|
|
7
6
|
readonly mode?: TransformerMode;
|
|
8
7
|
readonly hooks?: readonly TransformerHook[];
|
|
8
|
+
readonly importType?: MapImportType;
|
|
9
9
|
}
|
|
10
10
|
declare const DataParserToTypescriptRenderError_base: new (params: {
|
|
11
11
|
"@DuplojsDataParserToolsToTypescript/data-parser-to-typescript-render-error"?: unknown;
|
|
12
|
-
}, parentParams: [message?: string | undefined, options?: ErrorOptions | undefined]) => import("@duplojs/utils").Kind<import("@duplojs/utils").KindDefinition<"@DuplojsDataParserToolsToTypescript/data-parser-to-typescript-render-error", unknown>, unknown> & import("@duplojs/utils").Kind<import("@duplojs/utils").KindDefinition<"data-parser-to-typescript-render-error", unknown>, unknown> & Error;
|
|
12
|
+
}, parentParams: readonly [message?: string | undefined, options?: ErrorOptions | undefined]) => import("@duplojs/utils").Kind<import("@duplojs/utils").KindDefinition<"@DuplojsDataParserToolsToTypescript/data-parser-to-typescript-render-error", unknown>, unknown> & import("@duplojs/utils").Kind<import("@duplojs/utils").KindDefinition<"data-parser-to-typescript-render-error", unknown>, unknown> & Error;
|
|
13
13
|
export declare class DataParserToTypescriptRenderError extends DataParserToTypescriptRenderError_base {
|
|
14
|
-
schema:
|
|
14
|
+
schema: SupportedDataParsers;
|
|
15
15
|
error: DataParserNotSupportedEither | DataParserErrorEither;
|
|
16
|
-
constructor(schema:
|
|
16
|
+
constructor(schema: SupportedDataParsers, error: DataParserNotSupportedEither | DataParserErrorEither);
|
|
17
17
|
}
|
|
18
|
-
export declare function render(schema:
|
|
18
|
+
export declare function render(schema: SupportedDataParsers, params: RenderParams): string;
|
|
19
19
|
export {};
|
|
@@ -3,6 +3,7 @@ import './transformer/index.mjs';
|
|
|
3
3
|
import { factory, SyntaxKind, createSourceFile, ScriptTarget, ScriptKind, createPrinter, EmitHint } from 'typescript';
|
|
4
4
|
import { createToTypescriptKind } from './kind.mjs';
|
|
5
5
|
import { getRecursiveDataParser } from '../utils/getRecursiveDataParser.mjs';
|
|
6
|
+
import { importTypesTransformer } from './transformer/importTypesTransformer.mjs';
|
|
6
7
|
import { transformer } from './transformer/transformer.mjs';
|
|
7
8
|
|
|
8
9
|
class DataParserToTypescriptRenderError extends kindHeritage("data-parser-to-typescript-render-error", createToTypescriptKind("data-parser-to-typescript-render-error"), Error) {
|
|
@@ -16,12 +17,14 @@ class DataParserToTypescriptRenderError extends kindHeritage("data-parser-to-typ
|
|
|
16
17
|
}
|
|
17
18
|
function render(schema, params) {
|
|
18
19
|
const context = new Map(params.context);
|
|
20
|
+
const importType = new Map(params.importType);
|
|
19
21
|
const result = transformer(schema, {
|
|
20
22
|
...params,
|
|
21
23
|
context,
|
|
22
24
|
mode: params.mode ?? "out",
|
|
23
25
|
hooks: params.hooks ?? [],
|
|
24
26
|
recursiveDataParsers: getRecursiveDataParser(schema),
|
|
27
|
+
importType,
|
|
25
28
|
});
|
|
26
29
|
if (E.isLeft(result)) {
|
|
27
30
|
throw new DataParserToTypescriptRenderError(schema, result);
|
|
@@ -34,7 +37,10 @@ function render(schema, params) {
|
|
|
34
37
|
}
|
|
35
38
|
const sourceFile = createSourceFile("print.ts", "", ScriptTarget.Latest, false, ScriptKind.TS);
|
|
36
39
|
const printer = createPrinter();
|
|
37
|
-
return pipe(
|
|
40
|
+
return pipe([
|
|
41
|
+
...importTypesTransformer(importType),
|
|
42
|
+
...context.values(),
|
|
43
|
+
], G.map((value) => printer.printNode(EmitHint.Unspecified, value, sourceFile)), A.from, A.join("\n\n"), S.trim);
|
|
38
44
|
}
|
|
39
45
|
|
|
40
46
|
export { DataParserToTypescriptRenderError, render };
|
|
@@ -1,16 +1,21 @@
|
|
|
1
1
|
import { type TypeAliasDeclaration, type TypeNode } from "typescript";
|
|
2
2
|
import { type DP, E } from "@duplojs/utils";
|
|
3
|
-
|
|
4
|
-
export type
|
|
5
|
-
export type
|
|
6
|
-
export type
|
|
3
|
+
import { type SDP } from "@duplojs/server-utils";
|
|
4
|
+
export type TransformerSuccessEither = E.Right<"buildSuccess", TypeNode>;
|
|
5
|
+
export type DataParserNotSupportedEither = E.Left<"dataParserNotSupport", DP.DataParser>;
|
|
6
|
+
export type DataParserErrorEither = E.Left<"buildDataParserError", DP.DataParser>;
|
|
7
|
+
export type SupportedDataParsers = DP.DataParsers | SDP.DataParserFile;
|
|
8
|
+
export type MapContext = Map<SupportedDataParsers, TypeAliasDeclaration>;
|
|
9
|
+
export type MapImportType = Map<string, string[]>;
|
|
7
10
|
export type MaybeTransformerEither = TransformerSuccessEither | DataParserNotSupportedEither | DataParserErrorEither;
|
|
8
11
|
export type TransformerMode = "in" | "out";
|
|
9
12
|
export interface TransformerParams {
|
|
10
13
|
readonly mode: TransformerMode;
|
|
11
14
|
readonly context: MapContext;
|
|
15
|
+
readonly importType: MapImportType;
|
|
12
16
|
transformer(schema: DP.DataParser): MaybeTransformerEither;
|
|
13
17
|
success(result: TypeNode): TransformerSuccessEither;
|
|
14
18
|
buildError(): DataParserErrorEither;
|
|
19
|
+
addImport(path: string, typeName: string): void;
|
|
15
20
|
}
|
|
16
|
-
export declare function createTransformer<GenericDataParser extends
|
|
21
|
+
export declare function createTransformer<GenericDataParser extends SupportedDataParsers>(support: (schema: SupportedDataParsers) => schema is GenericDataParser, builder: (schema: GenericDataParser, params: TransformerParams) => MaybeTransformerEither): (schema: SupportedDataParsers, params: TransformerParams) => MaybeTransformerEither;
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export declare const arrayTransformer: (schema: DP.DataParsers, params: import("..").TransformerParams) => import("..").MaybeTransformerEither;
|
|
1
|
+
export declare const arrayTransformer: (schema: import("..").SupportedDataParsers, params: import("..").TransformerParams) => import("..").MaybeTransformerEither;
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export declare const bigIntTransformer: (schema: DP.DataParsers, params: import("..").TransformerParams) => import("..").MaybeTransformerEither;
|
|
1
|
+
export declare const bigIntTransformer: (schema: import("..").SupportedDataParsers, params: import("..").TransformerParams) => import("..").MaybeTransformerEither;
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export declare const booleanTransformer: (schema: DP.DataParsers, params: import("..").TransformerParams) => import("..").MaybeTransformerEither;
|
|
1
|
+
export declare const booleanTransformer: (schema: import("..").SupportedDataParsers, params: import("..").TransformerParams) => import("..").MaybeTransformerEither;
|