@duplojs/data-parser-tools 0.2.2 → 0.2.4

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.
Files changed (43) hide show
  1. package/dist/toJsonSchema/index.cjs +1 -0
  2. package/dist/toJsonSchema/index.mjs +1 -1
  3. package/dist/toJsonSchema/override.cjs +5 -4
  4. package/dist/toJsonSchema/override.mjs +5 -4
  5. package/dist/toJsonSchema/render.cjs +14 -25
  6. package/dist/toJsonSchema/render.d.ts +32 -14
  7. package/dist/toJsonSchema/render.mjs +16 -27
  8. package/dist/toJsonSchema/transformer/create.d.ts +5 -5
  9. package/dist/toJsonSchema/transformer/defaults/literal.cjs +59 -23
  10. package/dist/toJsonSchema/transformer/defaults/literal.d.ts +12 -4
  11. package/dist/toJsonSchema/transformer/defaults/literal.mjs +60 -24
  12. package/dist/toJsonSchema/transformer/defaults/nullable.cjs +11 -2
  13. package/dist/toJsonSchema/transformer/defaults/nullable.mjs +11 -2
  14. package/dist/toJsonSchema/transformer/defaults/object.cjs +1 -1
  15. package/dist/toJsonSchema/transformer/defaults/object.mjs +1 -1
  16. package/dist/toJsonSchema/transformer/defaults/optional.cjs +10 -1
  17. package/dist/toJsonSchema/transformer/defaults/optional.mjs +10 -1
  18. package/dist/toJsonSchema/transformer/defaults/union.cjs +3 -3
  19. package/dist/toJsonSchema/transformer/defaults/union.mjs +3 -3
  20. package/dist/toJsonSchema/transformer/index.cjs +1 -0
  21. package/dist/toJsonSchema/transformer/index.mjs +1 -1
  22. package/dist/toJsonSchema/transformer/transformer.cjs +14 -12
  23. package/dist/toJsonSchema/transformer/transformer.d.ts +8 -2
  24. package/dist/toJsonSchema/transformer/transformer.mjs +14 -13
  25. package/dist/toTypescript/index.cjs +2 -0
  26. package/dist/toTypescript/index.mjs +1 -0
  27. package/dist/toTypescript/override.cjs +5 -7
  28. package/dist/toTypescript/override.mjs +5 -7
  29. package/dist/toTypescript/transformer/defaults/nullable.cjs +11 -2
  30. package/dist/toTypescript/transformer/defaults/nullable.mjs +11 -2
  31. package/dist/toTypescript/transformer/defaults/object.cjs +2 -10
  32. package/dist/toTypescript/transformer/defaults/object.mjs +2 -10
  33. package/dist/toTypescript/transformer/defaults/optional.cjs +11 -2
  34. package/dist/toTypescript/transformer/defaults/optional.mjs +11 -2
  35. package/dist/toTypescript/transformer/defaults/record.cjs +2 -1
  36. package/dist/toTypescript/transformer/defaults/record.mjs +3 -2
  37. package/dist/toTypescript/transformer/includesUndefinedTypeNode.cjs +16 -0
  38. package/dist/toTypescript/transformer/includesUndefinedTypeNode.d.ts +2 -0
  39. package/dist/toTypescript/transformer/includesUndefinedTypeNode.mjs +14 -0
  40. package/dist/toTypescript/transformer/index.cjs +2 -0
  41. package/dist/toTypescript/transformer/index.d.ts +1 -0
  42. package/dist/toTypescript/transformer/index.mjs +1 -0
  43. package/package.json +2 -2
@@ -38,6 +38,7 @@ exports.createToJsonSchemaKind = kind.createToJsonSchemaKind;
38
38
  exports.defaultTransformers = index.defaultTransformers;
39
39
  exports.createTransformer = create.createTransformer;
40
40
  exports.supportedVersions = create.supportedVersions;
41
+ exports.buildRef = transformer.buildRef;
41
42
  exports.transformer = transformer.transformer;
42
43
  exports.arrayTransformer = array.arrayTransformer;
43
44
  exports.bigIntTransformer = bigint.bigIntTransformer;
@@ -4,7 +4,7 @@ export { DataParserToJsonSchemaRenderError, render } from './render.mjs';
4
4
  export { createToJsonSchemaKind } from './kind.mjs';
5
5
  export { defaultTransformers } from './transformer/defaults/index.mjs';
6
6
  export { createTransformer, supportedVersions } from './transformer/create.mjs';
7
- export { transformer } from './transformer/transformer.mjs';
7
+ export { buildRef, transformer } from './transformer/transformer.mjs';
8
8
  export { arrayTransformer } from './transformer/defaults/array.mjs';
9
9
  export { bigIntTransformer } from './transformer/defaults/bigint.mjs';
10
10
  export { booleanTransformer } from './transformer/defaults/boolean.mjs';
@@ -6,7 +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) => schema.construct({
10
- ...schema.definition,
11
- identifier,
12
- }));
9
+ dataParser.dataParserInit.overrideHandler.setMethod("addIdentifier", (schema, identifier) => {
10
+ const newSchema = schema.clone();
11
+ newSchema.setIdentifier(identifier);
12
+ return newSchema;
13
+ });
@@ -4,7 +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) => schema.construct({
8
- ...schema.definition,
9
- identifier,
10
- }));
7
+ dataParserInit.overrideHandler.setMethod("addIdentifier", (schema, identifier) => {
8
+ const newSchema = schema.clone();
9
+ newSchema.setIdentifier(identifier);
10
+ return newSchema;
11
+ });
@@ -16,21 +16,6 @@ class DataParserToJsonSchemaRenderError extends utils.kindHeritage("data-parser-
16
16
  this.error = error;
17
17
  }
18
18
  }
19
- function buildRef(name, version) {
20
- if (version === create.supportedVersions.openApi3 || version === create.supportedVersions.openApi31) {
21
- return { $ref: `#/components/schemas/${name}` };
22
- }
23
- if (version === create.supportedVersions.jsonSchema202012) {
24
- return { $ref: `#/$defs/${name}` };
25
- }
26
- return { $ref: `#/definitions/${name}` };
27
- }
28
- function getDefinitionKey(version) {
29
- if (version === create.supportedVersions.jsonSchema202012 || version === create.supportedVersions.openApi31) {
30
- return "$defs";
31
- }
32
- return "definitions";
33
- }
34
19
  function render(schema, params) {
35
20
  const context = new Map(params.context);
36
21
  const version = create.supportedVersions[params.version];
@@ -58,23 +43,27 @@ function render(schema, params) {
58
43
  ...definitions,
59
44
  [params.identifier]: built.schema,
60
45
  };
61
- if (utils.or(version, [
62
- utils.equal(create.supportedVersions.openApi3),
63
- utils.equal(create.supportedVersions.openApi31),
64
- ])) {
65
- return JSON.stringify({
46
+ if (version === create.supportedVersions.openApi3
47
+ || version === create.supportedVersions.openApi31) {
48
+ return {
49
+ $ref: transformer.buildRef(params.identifier, version),
66
50
  openapi: version,
67
51
  components: {
68
52
  schemas: definitionsWithIdentifier,
69
53
  },
70
- ...buildRef(params.identifier, version),
71
- }, null, 4);
54
+ };
55
+ }
56
+ if (version === create.supportedVersions.jsonSchema202012) {
57
+ return {
58
+ $ref: transformer.buildRef(params.identifier, version),
59
+ $schema: version,
60
+ $defs: definitionsWithIdentifier,
61
+ };
72
62
  }
73
- const definitionKey = getDefinitionKey(version);
74
63
  return {
64
+ $ref: transformer.buildRef(params.identifier, version),
75
65
  $schema: version,
76
- ...buildRef(params.identifier, version),
77
- [definitionKey]: definitionsWithIdentifier,
66
+ definitions: definitionsWithIdentifier,
78
67
  };
79
68
  }
80
69
 
@@ -1,13 +1,5 @@
1
- import { type DP } from "@duplojs/utils";
2
- import { type DataParserErrorEither, type DataParserNotSupportedEither, type MapContext, type TransformerMode, type TransformerHook, type createTransformer, type SupportedVersions } from "./transformer";
3
- export interface RenderParams {
4
- readonly identifier: string;
5
- readonly transformers: readonly ReturnType<typeof createTransformer>[];
6
- readonly context?: MapContext;
7
- readonly mode?: TransformerMode;
8
- readonly hooks?: readonly TransformerHook[];
9
- readonly version: SupportedVersions;
10
- }
1
+ import { type DP, type Or, type IsEqual } from "@duplojs/utils";
2
+ import { type DataParserErrorEither, type DataParserNotSupportedEither, type MapContext, type TransformerMode, type TransformerHook, type createTransformer, type SupportedVersions, type JsonSchema } from "./transformer";
11
3
  declare const DataParserToJsonSchemaRenderError_base: new (params: {
12
4
  "@DuplojsDataParserToolsToJsonSchema/data-parser-to-json-schema-render-error"?: unknown;
13
5
  }, parentParams: [message?: string | undefined, options?: ErrorOptions | undefined]) => Error & import("@duplojs/utils").Kind<import("@duplojs/utils").KindDefinition<"@DuplojsDataParserToolsToJsonSchema/data-parser-to-json-schema-render-error", unknown>, unknown> & import("@duplojs/utils").Kind<import("@duplojs/utils").KindDefinition<"data-parser-to-json-schema-render-error", unknown>, unknown>;
@@ -16,8 +8,34 @@ export declare class DataParserToJsonSchemaRenderError extends DataParserToJsonS
16
8
  error: DataParserNotSupportedEither | DataParserErrorEither;
17
9
  constructor(schema: DP.DataParsers, error: DataParserNotSupportedEither | DataParserErrorEither);
18
10
  }
19
- export declare function render(schema: DP.DataParsers, params: RenderParams): string | {
20
- $ref: string;
21
- $schema: "http://json-schema.org/draft-04/schema#" | "http://json-schema.org/draft-07/schema#" | "https://json-schema.org/draft/2020-12/schema";
22
- };
11
+ export interface RenderParams<GenericVersion extends unknown> {
12
+ readonly identifier: string;
13
+ readonly transformers: readonly ReturnType<typeof createTransformer>[];
14
+ readonly context?: MapContext;
15
+ readonly mode?: TransformerMode;
16
+ readonly hooks?: readonly TransformerHook[];
17
+ readonly version: GenericVersion;
18
+ }
19
+ type RenderResult<GenericVersion extends keyof SupportedVersions> = Or<[
20
+ IsEqual<GenericVersion, "openApi3">,
21
+ IsEqual<GenericVersion, "openApi31">
22
+ ]> extends true ? {
23
+ $ref: `#/components/schemas/${string}`;
24
+ openapi: SupportedVersions[GenericVersion];
25
+ components: {
26
+ schemas: Record<string, JsonSchema>;
27
+ };
28
+ } : Or<[
29
+ IsEqual<GenericVersion, "jsonSchema7">,
30
+ IsEqual<GenericVersion, "jsonSchema4">
31
+ ]> extends true ? {
32
+ $ref: `#/$defs/${string}`;
33
+ $schema: SupportedVersions[GenericVersion];
34
+ definitions: Record<string, JsonSchema>;
35
+ } : IsEqual<GenericVersion, "jsonSchema202012"> extends true ? {
36
+ $ref: `#/definitions/${string}`;
37
+ $schema: SupportedVersions[GenericVersion];
38
+ $defs: Record<string, JsonSchema>;
39
+ } : never;
40
+ export declare function render<GenericVersion extends keyof SupportedVersions>(schema: DP.DataParsers, params: RenderParams<GenericVersion>): RenderResult<GenericVersion>;
23
41
  export {};
@@ -1,9 +1,9 @@
1
- import { kindHeritage, E, unwrap, G, or, equal } from '@duplojs/utils';
1
+ import { kindHeritage, E, unwrap, G } from '@duplojs/utils';
2
2
  import './transformer/index.mjs';
3
3
  import { createToJsonSchemaKind } from './kind.mjs';
4
4
  import { getRecursiveDataParser } from '../utils/getRecursiveDataParser.mjs';
5
5
  import { supportedVersions } from './transformer/create.mjs';
6
- import { transformer } from './transformer/transformer.mjs';
6
+ import { transformer, buildRef } from './transformer/transformer.mjs';
7
7
 
8
8
  class DataParserToJsonSchemaRenderError extends kindHeritage("data-parser-to-json-schema-render-error", createToJsonSchemaKind("data-parser-to-json-schema-render-error"), Error) {
9
9
  schema;
@@ -14,21 +14,6 @@ class DataParserToJsonSchemaRenderError extends kindHeritage("data-parser-to-jso
14
14
  this.error = error;
15
15
  }
16
16
  }
17
- function buildRef(name, version) {
18
- if (version === supportedVersions.openApi3 || version === supportedVersions.openApi31) {
19
- return { $ref: `#/components/schemas/${name}` };
20
- }
21
- if (version === supportedVersions.jsonSchema202012) {
22
- return { $ref: `#/$defs/${name}` };
23
- }
24
- return { $ref: `#/definitions/${name}` };
25
- }
26
- function getDefinitionKey(version) {
27
- if (version === supportedVersions.jsonSchema202012 || version === supportedVersions.openApi31) {
28
- return "$defs";
29
- }
30
- return "definitions";
31
- }
32
17
  function render(schema, params) {
33
18
  const context = new Map(params.context);
34
19
  const version = supportedVersions[params.version];
@@ -56,23 +41,27 @@ function render(schema, params) {
56
41
  ...definitions,
57
42
  [params.identifier]: built.schema,
58
43
  };
59
- if (or(version, [
60
- equal(supportedVersions.openApi3),
61
- equal(supportedVersions.openApi31),
62
- ])) {
63
- return JSON.stringify({
44
+ if (version === supportedVersions.openApi3
45
+ || version === supportedVersions.openApi31) {
46
+ return {
47
+ $ref: buildRef(params.identifier, version),
64
48
  openapi: version,
65
49
  components: {
66
50
  schemas: definitionsWithIdentifier,
67
51
  },
68
- ...buildRef(params.identifier, version),
69
- }, null, 4);
52
+ };
53
+ }
54
+ if (version === supportedVersions.jsonSchema202012) {
55
+ return {
56
+ $ref: buildRef(params.identifier, version),
57
+ $schema: version,
58
+ $defs: definitionsWithIdentifier,
59
+ };
70
60
  }
71
- const definitionKey = getDefinitionKey(version);
72
61
  return {
62
+ $ref: buildRef(params.identifier, version),
73
63
  $schema: version,
74
- ...buildRef(params.identifier, version),
75
- [definitionKey]: definitionsWithIdentifier,
64
+ definitions: definitionsWithIdentifier,
76
65
  };
77
66
  }
78
67
 
@@ -9,7 +9,7 @@ export interface JsonSchemaAnyOf {
9
9
  export type JsonSchema = JsonSchemaRef | JsonSchemaAnyOf | JsonSchemaArray | JsonSchemaBigInt | JsonSchemaBoolean | JsonSchemaDate | JsonSchemaEmpty | JsonSchemaLiteral | JsonSchemaNil | JsonSchemaNullableAnyOf | JsonSchemaNumber | JsonSchemaObject | JsonSchemaRecord | JsonSchemaString | JsonSchemaTemplateLiteral | JsonSchemaTuple | JsonSchemaUnion | JsonSchemaUnknown | JsonSchemaTime;
10
10
  export interface TransformerSuccess {
11
11
  readonly schema: JsonSchema;
12
- readonly canBeUndefined: boolean;
12
+ readonly isOptional: boolean;
13
13
  }
14
14
  export type TransformerSuccessEither = E.EitherRight<"buildSuccess", TransformerSuccess>;
15
15
  export type DataParserNotSupportedEither = E.EitherLeft<"dataParserNotSupport", DP.DataParser>;
@@ -17,7 +17,7 @@ export type DataParserErrorEither = E.EitherLeft<"buildDataParserError", DP.Data
17
17
  export interface MapContextValue {
18
18
  readonly name: string;
19
19
  readonly schema?: JsonSchema;
20
- readonly canBeUndefined: boolean;
20
+ readonly isOptional: boolean;
21
21
  }
22
22
  export type MapContext = Map<DP.DataParsers, MapContextValue>;
23
23
  export type MaybeTransformerEither = TransformerSuccessEither | DataParserNotSupportedEither | DataParserErrorEither;
@@ -29,14 +29,14 @@ export declare const supportedVersions: {
29
29
  readonly openApi3: "https://spec.openapis.org/oas/3.0.3";
30
30
  readonly openApi31: "https://spec.openapis.org/oas/3.1.0";
31
31
  };
32
- export type SupportedVersions = keyof typeof supportedVersions;
33
- export type SupportedVersionsUrl = typeof supportedVersions[SupportedVersions];
32
+ export type SupportedVersions = typeof supportedVersions;
33
+ export type SupportedVersionsUrl = typeof supportedVersions[keyof SupportedVersions];
34
34
  export interface TransformerParams {
35
35
  readonly mode: TransformerMode;
36
36
  readonly context: MapContext;
37
37
  readonly version: SupportedVersionsUrl;
38
38
  transformer(schema: DP.DataParser): MaybeTransformerEither;
39
- success(result: JsonSchema, canBeUndefined?: boolean): TransformerSuccessEither;
39
+ success(result: JsonSchema, isOptional?: boolean): TransformerSuccessEither;
40
40
  buildError(): DataParserErrorEither;
41
41
  }
42
42
  export declare function createTransformer<GenericDataParser extends DP.DataParsers>(support: (schema: DP.DataParsers) => schema is GenericDataParser, builder: (schema: GenericDataParser, params: TransformerParams) => MaybeTransformerEither): (schema: DP.DataParsers, params: TransformerParams) => MaybeTransformerEither;
@@ -3,45 +3,81 @@
3
3
  var utils = require('@duplojs/utils');
4
4
  var create = require('../create.cjs');
5
5
 
6
- const literalTransformer = create.createTransformer(utils.DP.literalKind.has, (schema, { success, }) => {
6
+ function isOldVersion(version) {
7
+ return (version === "http://json-schema.org/draft-04/schema#"
8
+ || version === "https://spec.openapis.org/oas/3.0.3");
9
+ }
10
+ const literalTransformer = create.createTransformer(utils.DP.literalKind.has, (schema, { success, version, }) => {
7
11
  const reduced = utils.A.reduce(schema.definition.value, utils.A.reduceFrom({
8
12
  literals: [],
9
- canBeUndefined: false,
13
+ isOptional: false,
10
14
  }), ({ element, lastValue, next, nextWithObject, }) => utils.P.match(element)
11
- .when(utils.isType("undefined"), () => nextWithObject(lastValue, { canBeUndefined: true }))
15
+ .when(utils.isType("undefined"), () => nextWithObject(lastValue, { isOptional: true }))
12
16
  .when(utils.isType("bigint"), (value) => nextWithObject(lastValue, {
13
- literals: utils.A.push(lastValue.literals, {
14
- const: value.toString(),
15
- type: "string",
16
- }),
17
+ literals: utils.A.push(lastValue.literals, isOldVersion(version)
18
+ ? {
19
+ enum: [value.toString()],
20
+ type: "string",
21
+ }
22
+ : {
23
+ const: value.toString(),
24
+ type: "string",
25
+ }),
17
26
  }))
18
27
  .when(utils.isType("string"), (value) => nextWithObject(lastValue, {
19
- literals: utils.A.push(lastValue.literals, {
20
- const: value,
21
- type: "string",
22
- }),
28
+ literals: utils.A.push(lastValue.literals, isOldVersion(version)
29
+ ? {
30
+ enum: [value],
31
+ type: "string",
32
+ }
33
+ : {
34
+ const: value,
35
+ type: "string",
36
+ }),
23
37
  }))
24
38
  .when(utils.isType("number"), (value) => nextWithObject(lastValue, {
25
- literals: utils.A.push(lastValue.literals, {
26
- const: value,
27
- type: Number.isInteger(value) ? "integer" : "number",
28
- }),
39
+ literals: utils.A.push(lastValue.literals, isOldVersion(version)
40
+ ? {
41
+ enum: [value],
42
+ type: Number.isInteger(value)
43
+ ? "integer"
44
+ : "number",
45
+ }
46
+ : {
47
+ const: value,
48
+ type: Number.isInteger(value)
49
+ ? "integer"
50
+ : "number",
51
+ }),
29
52
  }))
30
53
  .when(utils.isType("boolean"), (value) => nextWithObject(lastValue, {
31
- literals: utils.A.push(lastValue.literals, {
32
- const: value,
33
- type: "boolean",
34
- }),
54
+ literals: utils.A.push(lastValue.literals, isOldVersion(version)
55
+ ? {
56
+ enum: [value],
57
+ type: "boolean",
58
+ }
59
+ : {
60
+ const: value,
61
+ type: "boolean",
62
+ }),
35
63
  }))
36
64
  .when(utils.isType("null"), () => nextWithObject(lastValue, {
37
- literals: utils.A.push(lastValue.literals, {
38
- const: null,
65
+ literals: utils.A.push(lastValue.literals, utils.P.match(version)
66
+ .with("https://spec.openapis.org/oas/3.0.3", utils.justReturn({
67
+ enum: [null],
68
+ }))
69
+ .with("http://json-schema.org/draft-04/schema#", utils.justReturn({
70
+ type: "null",
71
+ enum: [null],
72
+ }))
73
+ .otherwise(utils.justReturn({
39
74
  type: "null",
40
- }),
75
+ const: null,
76
+ }))),
41
77
  }))
42
78
  .otherwise(() => next(lastValue)));
43
79
  const schemaDefinition = utils.pipe(reduced.literals, utils.P.when(utils.A.lengthEqual(0), () => ({})), utils.P.when(utils.A.lengthEqual(1), utils.A.first), utils.P.otherwise((value) => ({ anyOf: value })));
44
- return success(schemaDefinition, reduced.canBeUndefined);
80
+ return success(schemaDefinition, reduced.isOptional);
45
81
  });
46
82
 
47
83
  exports.literalTransformer = literalTransformer;
@@ -1,6 +1,14 @@
1
1
  import { DP } from "@duplojs/utils";
2
- export interface JsonSchemaLiteral {
3
- const?: string | number | boolean | null;
4
- type?: "string" | "number" | "integer" | "boolean" | "null";
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
- const literalTransformer = createTransformer(DP.literalKind.has, (schema, { success, }) => {
4
+ function isOldVersion(version) {
5
+ return (version === "http://json-schema.org/draft-04/schema#"
6
+ || version === "https://spec.openapis.org/oas/3.0.3");
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
- canBeUndefined: false,
11
+ isOptional: false,
8
12
  }), ({ element, lastValue, next, nextWithObject, }) => P.match(element)
9
- .when(isType("undefined"), () => nextWithObject(lastValue, { canBeUndefined: true }))
13
+ .when(isType("undefined"), () => nextWithObject(lastValue, { isOptional: true }))
10
14
  .when(isType("bigint"), (value) => nextWithObject(lastValue, {
11
- literals: A.push(lastValue.literals, {
12
- const: value.toString(),
13
- type: "string",
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
- const: value,
19
- type: "string",
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
- const: value,
25
- type: Number.isInteger(value) ? "integer" : "number",
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
- const: value,
31
- type: "boolean",
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
- const: null,
63
+ literals: A.push(lastValue.literals, P.match(version)
64
+ .with("https://spec.openapis.org/oas/3.0.3", justReturn({
65
+ enum: [null],
66
+ }))
67
+ .with("http://json-schema.org/draft-04/schema#", 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.canBeUndefined);
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(mode, utils.P.match("in", () => ({
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
- })), utils.P.match("out", () => inner.schema), utils.P.exhaustive, (schema) => success(schema, inner.canBeUndefined));
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(mode, P.match("in", () => ({
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
- })), P.match("out", () => inner.schema), P.exhaustive, (schema) => success(schema, inner.canBeUndefined));
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.canBeUndefined
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.canBeUndefined
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(mode, utils.P.match("in", () => true), utils.P.match("out", () => inner.canBeUndefined), utils.P.exhaustive, (value) => success(inner.schema, value)))));
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(mode, P.match("in", () => true), P.match("out", () => inner.canBeUndefined), P.exhaustive, (value) => success(inner.schema, value)))));
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 };
@@ -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 canBeUndefined = false;
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
- canBeUndefined ||= option.canBeUndefined;
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
- }, canBeUndefined);
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 canBeUndefined = false;
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
- canBeUndefined ||= option.canBeUndefined;
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
- }, canBeUndefined);
15
+ }, isOptional);
16
16
  });
17
17
 
18
18
  export { unionTransformer };
@@ -9,5 +9,6 @@ var index = require('./defaults/index.cjs');
9
9
 
10
10
  exports.createTransformer = create.createTransformer;
11
11
  exports.supportedVersions = create.supportedVersions;
12
+ exports.buildRef = transformer.buildRef;
12
13
  exports.transformer = transformer.transformer;
13
14
  exports.defaultTransformers = index.defaultTransformers;
@@ -1,4 +1,4 @@
1
1
  export { createTransformer, supportedVersions } from './create.mjs';
2
- export { transformer } from './transformer.mjs';
2
+ export { buildRef, transformer } from './transformer.mjs';
3
3
  import './hook.mjs';
4
4
  export { defaultTransformers } from './defaults/index.mjs';
@@ -3,13 +3,14 @@
3
3
  var utils = require('@duplojs/utils');
4
4
 
5
5
  function buildRef(name, version) {
6
- if (version === "https://spec.openapis.org/oas/3.0.3" || version === "https://spec.openapis.org/oas/3.1.0") {
7
- return { $ref: `#/components/schemas/${name}` };
6
+ if (version === "https://spec.openapis.org/oas/3.0.3"
7
+ || version === "https://spec.openapis.org/oas/3.1.0") {
8
+ return `#/components/schemas/${name}`;
8
9
  }
9
10
  if (version === "https://json-schema.org/draft/2020-12/schema") {
10
- return { $ref: `#/$defs/${name}` };
11
+ return `#/$defs/${name}`;
11
12
  }
12
- return { $ref: `#/definitions/${name}` };
13
+ return `#/definitions/${name}`;
13
14
  }
14
15
  function transformer(schema, params) {
15
16
  const currentSchema = utils.A.reduce(params.hooks, utils.A.reduceFrom(schema), ({ element: hook, lastValue, next, exit }) => {
@@ -31,23 +32,23 @@ function transformer(schema, params) {
31
32
  const currentDeclaration = params.context.get(currentSchema);
32
33
  if (currentDeclaration) {
33
34
  return utils.E.right("buildSuccess", {
34
- schema: buildRef(currentDeclaration.name, params.version),
35
- canBeUndefined: currentDeclaration.canBeUndefined,
35
+ schema: { $ref: buildRef(currentDeclaration.name, params.version) },
36
+ isOptional: currentDeclaration.isOptional,
36
37
  });
37
38
  }
38
39
  const currentIdentifier = utils.whenElse(params.recursiveDataParsers, utils.A.includes(currentSchema), () => {
39
40
  const identifier = currentSchema.definition.identifier ?? `RecursiveType${params.context.size}`;
40
41
  params.context.set(currentSchema, {
41
42
  name: identifier,
42
- canBeUndefined: false,
43
+ isOptional: false,
43
44
  });
44
45
  return identifier;
45
46
  }, utils.justReturn(currentSchema.definition.identifier));
46
47
  const functionParams = {
47
- success(result, canBeUndefined = false) {
48
+ success(result, isOptional = false) {
48
49
  return utils.E.right("buildSuccess", {
49
50
  schema: result,
50
- canBeUndefined,
51
+ isOptional,
51
52
  });
52
53
  },
53
54
  transformer(schema) {
@@ -74,15 +75,16 @@ function transformer(schema, params) {
74
75
  return result;
75
76
  }
76
77
  if (currentIdentifier) {
77
- const { schema: builtSchema, canBeUndefined } = utils.unwrap(result);
78
+ const { schema: builtSchema, isOptional } = utils.unwrap(result);
78
79
  params.context.set(currentSchema, {
79
80
  name: currentIdentifier,
80
81
  schema: builtSchema,
81
- canBeUndefined,
82
+ isOptional,
82
83
  });
83
- return functionParams.success(buildRef(currentIdentifier, params.version), canBeUndefined);
84
+ return functionParams.success({ $ref: buildRef(currentIdentifier, params.version) }, isOptional);
84
85
  }
85
86
  return result;
86
87
  }
87
88
 
89
+ exports.buildRef = buildRef;
88
90
  exports.transformer = transformer;
@@ -1,4 +1,4 @@
1
- import { type DP } from "@duplojs/utils";
1
+ import { E, type DP } from "@duplojs/utils";
2
2
  import { type MapContext, type DataParserNotSupportedEither, type createTransformer, type TransformerMode, type SupportedVersionsUrl, type DataParserErrorEither } from "./create";
3
3
  import { type TransformerHook } from "./hook";
4
4
  export interface TransformerFunctionParams {
@@ -9,4 +9,10 @@ export interface TransformerFunctionParams {
9
9
  readonly hooks: readonly TransformerHook[];
10
10
  readonly recursiveDataParsers: DP.DataParser[];
11
11
  }
12
- export declare function transformer(schema: DP.DataParser, params: TransformerFunctionParams): import("./create").TransformerSuccessEither | DataParserNotSupportedEither | DataParserErrorEither;
12
+ export declare function buildRef(name: string, version: SupportedVersionsUrl): string;
13
+ export declare function transformer(schema: DP.DataParser, params: TransformerFunctionParams): import("./create").TransformerSuccessEither | DataParserNotSupportedEither | DataParserErrorEither | E.EitherRight<"buildSuccess", {
14
+ readonly schema: {
15
+ readonly $ref: string;
16
+ };
17
+ readonly isOptional: boolean;
18
+ }>;
@@ -1,13 +1,14 @@
1
1
  import { A, E, whenElse, justReturn, unwrap } from '@duplojs/utils';
2
2
 
3
3
  function buildRef(name, version) {
4
- if (version === "https://spec.openapis.org/oas/3.0.3" || version === "https://spec.openapis.org/oas/3.1.0") {
5
- return { $ref: `#/components/schemas/${name}` };
4
+ if (version === "https://spec.openapis.org/oas/3.0.3"
5
+ || version === "https://spec.openapis.org/oas/3.1.0") {
6
+ return `#/components/schemas/${name}`;
6
7
  }
7
8
  if (version === "https://json-schema.org/draft/2020-12/schema") {
8
- return { $ref: `#/$defs/${name}` };
9
+ return `#/$defs/${name}`;
9
10
  }
10
- return { $ref: `#/definitions/${name}` };
11
+ return `#/definitions/${name}`;
11
12
  }
12
13
  function transformer(schema, params) {
13
14
  const currentSchema = A.reduce(params.hooks, A.reduceFrom(schema), ({ element: hook, lastValue, next, exit }) => {
@@ -29,23 +30,23 @@ function transformer(schema, params) {
29
30
  const currentDeclaration = params.context.get(currentSchema);
30
31
  if (currentDeclaration) {
31
32
  return E.right("buildSuccess", {
32
- schema: buildRef(currentDeclaration.name, params.version),
33
- canBeUndefined: currentDeclaration.canBeUndefined,
33
+ schema: { $ref: buildRef(currentDeclaration.name, params.version) },
34
+ isOptional: currentDeclaration.isOptional,
34
35
  });
35
36
  }
36
37
  const currentIdentifier = whenElse(params.recursiveDataParsers, A.includes(currentSchema), () => {
37
38
  const identifier = currentSchema.definition.identifier ?? `RecursiveType${params.context.size}`;
38
39
  params.context.set(currentSchema, {
39
40
  name: identifier,
40
- canBeUndefined: false,
41
+ isOptional: false,
41
42
  });
42
43
  return identifier;
43
44
  }, justReturn(currentSchema.definition.identifier));
44
45
  const functionParams = {
45
- success(result, canBeUndefined = false) {
46
+ success(result, isOptional = false) {
46
47
  return E.right("buildSuccess", {
47
48
  schema: result,
48
- canBeUndefined,
49
+ isOptional,
49
50
  });
50
51
  },
51
52
  transformer(schema) {
@@ -72,15 +73,15 @@ function transformer(schema, params) {
72
73
  return result;
73
74
  }
74
75
  if (currentIdentifier) {
75
- const { schema: builtSchema, canBeUndefined } = unwrap(result);
76
+ const { schema: builtSchema, isOptional } = unwrap(result);
76
77
  params.context.set(currentSchema, {
77
78
  name: currentIdentifier,
78
79
  schema: builtSchema,
79
- canBeUndefined,
80
+ isOptional,
80
81
  });
81
- return functionParams.success(buildRef(currentIdentifier, params.version), canBeUndefined);
82
+ return functionParams.success({ $ref: buildRef(currentIdentifier, params.version) }, isOptional);
82
83
  }
83
84
  return result;
84
85
  }
85
86
 
86
- export { transformer };
87
+ export { buildRef, transformer };
@@ -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
- ...schema,
11
- definition: {
12
- ...schema.definition,
13
- identifier,
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
- ...schema,
9
- definition: {
10
- ...schema.definition,
11
- identifier,
12
- },
13
- }));
7
+ dataParserInit.overrideHandler.setMethod("addIdentifier", (schema, identifier) => {
8
+ const newSchema = schema.clone();
9
+ newSchema.setIdentifier(identifier);
10
+ return newSchema;
11
+ });
@@ -10,10 +10,19 @@ const nullableTransformer = create.createTransformer(utils.DP.nullableKind.has,
10
10
  return innerResult;
11
11
  }
12
12
  const innerType = utils.unwrap(innerResult);
13
- return utils.pipe(mode, utils.P.match("in", () => utils.pipe(typescript.factory.createNull(), typescript.factory.createLiteralTypeNode, (value) => typescript.factory.createUnionTypeNode([
13
+ return utils.pipe({
14
+ mode,
15
+ coalescingValue: schema.definition.coalescingValue,
16
+ }, (value) => utils.P.match(value)
17
+ .with(utils.P.union({ mode: "in" }, {
18
+ mode: "out",
19
+ coalescingValue: undefined,
20
+ }), () => utils.pipe(typescript.factory.createNull(), typescript.factory.createLiteralTypeNode, (value) => typescript.factory.createUnionTypeNode([
14
21
  innerType,
15
22
  value,
16
- ]))), utils.P.match("out", () => innerType), utils.P.exhaustive, success);
23
+ ])))
24
+ .with({ mode: "out" }, () => innerType)
25
+ .exhaustive(), success);
17
26
  });
18
27
 
19
28
  exports.nullableTransformer = nullableTransformer;
@@ -8,10 +8,19 @@ const nullableTransformer = createTransformer(DP.nullableKind.has, (schema, { tr
8
8
  return innerResult;
9
9
  }
10
10
  const innerType = unwrap(innerResult);
11
- return pipe(mode, P.match("in", () => pipe(factory.createNull(), factory.createLiteralTypeNode, (value) => factory.createUnionTypeNode([
11
+ return pipe({
12
+ mode,
13
+ coalescingValue: schema.definition.coalescingValue,
14
+ }, (value) => P.match(value)
15
+ .with(P.union({ mode: "in" }, {
16
+ mode: "out",
17
+ coalescingValue: undefined,
18
+ }), () => pipe(factory.createNull(), factory.createLiteralTypeNode, (value) => factory.createUnionTypeNode([
12
19
  innerType,
13
20
  value,
14
- ]))), P.match("out", () => innerType), P.exhaustive, success);
21
+ ])))
22
+ .with({ mode: "out" }, () => innerType)
23
+ .exhaustive(), success);
15
24
  });
16
25
 
17
26
  export { nullableTransformer };
@@ -3,17 +3,9 @@
3
3
  var utils = require('@duplojs/utils');
4
4
  var typescript = require('typescript');
5
5
  var create = require('../create.cjs');
6
+ var includesUndefinedTypeNode = require('../includesUndefinedTypeNode.cjs');
6
7
 
7
- function includesUndefinedTypeNode(typeNode) {
8
- if (typeNode.kind === typescript.SyntaxKind.UndefinedKeyword) {
9
- return true;
10
- }
11
- if (typescript.isUnionTypeNode(typeNode)) {
12
- return typeNode.types.some((subTypeNode) => includesUndefinedTypeNode(subTypeNode));
13
- }
14
- return false;
15
- }
16
8
  const regexIdentifier = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
17
- const objectTransformer = create.createTransformer(utils.DP.objectKind.has, (schema, { transformer, success, }) => utils.pipe(schema.definition.shape, utils.O.entries, utils.A.reduce(utils.A.reduceFrom([]), ({ element: [key, value], lastValue, next, exit }) => utils.pipe(transformer(value), utils.when(utils.E.isLeft, exit), utils.E.whenIsRight((typeNode) => utils.pipe(includesUndefinedTypeNode(typeNode), utils.P.match(true, () => typescript.factory.createToken(typescript.SyntaxKind.QuestionToken)), utils.P.match(false, () => undefined), utils.P.exhaustive, (optional) => utils.pipe(key, utils.whenElse(utils.S.test(regexIdentifier), typescript.factory.createIdentifier, typescript.factory.createStringLiteral), (propertyName) => typescript.factory.createPropertySignature(undefined, propertyName, optional, typeNode), (typeElement) => utils.A.push(lastValue, typeElement), next))))), utils.when(utils.isType("array"), utils.innerPipe(typescript.factory.createTypeLiteralNode, success))));
9
+ const objectTransformer = create.createTransformer(utils.DP.objectKind.has, (schema, { transformer, success, }) => utils.pipe(schema.definition.shape, utils.O.entries, utils.A.reduce(utils.A.reduceFrom([]), ({ element: [key, value], lastValue, next, exit }) => utils.pipe(transformer(value), utils.when(utils.E.isLeft, exit), utils.E.whenIsRight((typeNode) => utils.pipe(includesUndefinedTypeNode.includesUndefinedTypeNode(typeNode), utils.P.match(true, () => typescript.factory.createToken(typescript.SyntaxKind.QuestionToken)), utils.P.match(false, () => undefined), utils.P.exhaustive, (optional) => utils.pipe(key, utils.whenElse(utils.S.test(regexIdentifier), typescript.factory.createIdentifier, typescript.factory.createStringLiteral), (propertyName) => typescript.factory.createPropertySignature(undefined, propertyName, optional, typeNode), (typeElement) => utils.A.push(lastValue, typeElement), next))))), utils.when(utils.isType("array"), utils.innerPipe(typescript.factory.createTypeLiteralNode, success))));
18
10
 
19
11
  exports.objectTransformer = objectTransformer;
@@ -1,16 +1,8 @@
1
1
  import { DP, pipe, O, A, when, E, P, whenElse, S, isType, innerPipe } from '@duplojs/utils';
2
- import { SyntaxKind, isUnionTypeNode, factory } from 'typescript';
2
+ import { factory, SyntaxKind } from 'typescript';
3
3
  import { createTransformer } from '../create.mjs';
4
+ import { includesUndefinedTypeNode } from '../includesUndefinedTypeNode.mjs';
4
5
 
5
- function includesUndefinedTypeNode(typeNode) {
6
- if (typeNode.kind === SyntaxKind.UndefinedKeyword) {
7
- return true;
8
- }
9
- if (isUnionTypeNode(typeNode)) {
10
- return typeNode.types.some((subTypeNode) => includesUndefinedTypeNode(subTypeNode));
11
- }
12
- return false;
13
- }
14
6
  const regexIdentifier = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
15
7
  const objectTransformer = createTransformer(DP.objectKind.has, (schema, { transformer, success, }) => pipe(schema.definition.shape, O.entries, A.reduce(A.reduceFrom([]), ({ element: [key, value], lastValue, next, exit }) => pipe(transformer(value), when(E.isLeft, exit), E.whenIsRight((typeNode) => pipe(includesUndefinedTypeNode(typeNode), P.match(true, () => factory.createToken(SyntaxKind.QuestionToken)), P.match(false, () => undefined), P.exhaustive, (optional) => pipe(key, whenElse(S.test(regexIdentifier), factory.createIdentifier, factory.createStringLiteral), (propertyName) => factory.createPropertySignature(undefined, propertyName, optional, typeNode), (typeElement) => A.push(lastValue, typeElement), next))))), when(isType("array"), innerPipe(factory.createTypeLiteralNode, success))));
16
8
 
@@ -4,9 +4,18 @@ var utils = require('@duplojs/utils');
4
4
  var typescript = require('typescript');
5
5
  var create = require('../create.cjs');
6
6
 
7
- const optionalTransformer = create.createTransformer(utils.DP.optionalKind.has, (schema, { transformer, mode, success, }) => utils.pipe(schema.definition.inner, transformer, utils.E.whenIsRight((innerType) => utils.pipe(mode, utils.P.match("in", () => typescript.factory.createUnionTypeNode([
7
+ const optionalTransformer = create.createTransformer(utils.DP.optionalKind.has, (schema, { transformer, mode, success, }) => utils.pipe(schema.definition.inner, transformer, utils.E.whenIsRight((innerType) => utils.pipe({
8
+ mode,
9
+ coalescingValue: schema.definition.coalescingValue,
10
+ }, (value) => utils.P.match(value)
11
+ .with(utils.P.union({ mode: "in" }, {
12
+ mode: "out",
13
+ coalescingValue: undefined,
14
+ }), () => typescript.factory.createUnionTypeNode([
8
15
  innerType,
9
16
  typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.UndefinedKeyword),
10
- ])), utils.P.match("out", () => innerType), utils.P.exhaustive, success))));
17
+ ]))
18
+ .with({ mode: "out" }, () => innerType)
19
+ .exhaustive(), success))));
11
20
 
12
21
  exports.optionalTransformer = optionalTransformer;
@@ -2,9 +2,18 @@ import { DP, pipe, E, P } from '@duplojs/utils';
2
2
  import { factory, SyntaxKind } from 'typescript';
3
3
  import { createTransformer } from '../create.mjs';
4
4
 
5
- const optionalTransformer = createTransformer(DP.optionalKind.has, (schema, { transformer, mode, success, }) => pipe(schema.definition.inner, transformer, E.whenIsRight((innerType) => pipe(mode, P.match("in", () => factory.createUnionTypeNode([
5
+ const optionalTransformer = createTransformer(DP.optionalKind.has, (schema, { transformer, mode, success, }) => pipe(schema.definition.inner, transformer, E.whenIsRight((innerType) => pipe({
6
+ mode,
7
+ coalescingValue: schema.definition.coalescingValue,
8
+ }, (value) => P.match(value)
9
+ .with(P.union({ mode: "in" }, {
10
+ mode: "out",
11
+ coalescingValue: undefined,
12
+ }), () => factory.createUnionTypeNode([
6
13
  innerType,
7
14
  factory.createKeywordTypeNode(SyntaxKind.UndefinedKeyword),
8
- ])), P.match("out", () => innerType), P.exhaustive, success))));
15
+ ]))
16
+ .with({ mode: "out" }, () => innerType)
17
+ .exhaustive(), success))));
9
18
 
10
19
  export { optionalTransformer };
@@ -3,6 +3,7 @@
3
3
  var utils = require('@duplojs/utils');
4
4
  var typescript = require('typescript');
5
5
  var create = require('../create.cjs');
6
+ var includesUndefinedTypeNode = require('../includesUndefinedTypeNode.cjs');
6
7
 
7
8
  const recordTransformer = create.createTransformer(utils.DP.recordKind.has, ({ definition: { key, value } }, { transformer, success, }) => {
8
9
  const keyResult = transformer(key);
@@ -15,7 +16,7 @@ const recordTransformer = create.createTransformer(utils.DP.recordKind.has, ({ d
15
16
  }
16
17
  const keyType = utils.unwrap(keyResult);
17
18
  const valueType = utils.unwrap(valueResult);
18
- const recordType = typescript.factory.createTypeReferenceNode("Record", [keyType, valueType]);
19
+ const recordType = utils.pipe(typescript.factory.createTypeReferenceNode("Record", [keyType, valueType]), utils.when(() => includesUndefinedTypeNode.includesUndefinedTypeNode(valueType), (recodeTypeNode) => typescript.factory.createTypeReferenceNode("Partial", [recodeTypeNode])));
19
20
  return success(recordType);
20
21
  });
21
22
 
@@ -1,6 +1,7 @@
1
- import { DP, E, unwrap } from '@duplojs/utils';
1
+ import { DP, E, unwrap, pipe, when } from '@duplojs/utils';
2
2
  import { factory } from 'typescript';
3
3
  import { createTransformer } from '../create.mjs';
4
+ import { includesUndefinedTypeNode } from '../includesUndefinedTypeNode.mjs';
4
5
 
5
6
  const recordTransformer = createTransformer(DP.recordKind.has, ({ definition: { key, value } }, { transformer, success, }) => {
6
7
  const keyResult = transformer(key);
@@ -13,7 +14,7 @@ const recordTransformer = createTransformer(DP.recordKind.has, ({ definition: {
13
14
  }
14
15
  const keyType = unwrap(keyResult);
15
16
  const valueType = unwrap(valueResult);
16
- const recordType = factory.createTypeReferenceNode("Record", [keyType, valueType]);
17
+ const recordType = pipe(factory.createTypeReferenceNode("Record", [keyType, valueType]), when(() => includesUndefinedTypeNode(valueType), (recodeTypeNode) => factory.createTypeReferenceNode("Partial", [recodeTypeNode])));
17
18
  return success(recordType);
18
19
  });
19
20
 
@@ -0,0 +1,16 @@
1
+ 'use strict';
2
+
3
+ var utils = require('@duplojs/utils');
4
+ var typescript = require('typescript');
5
+
6
+ function includesUndefinedTypeNode(typeNode) {
7
+ if (typeNode.kind === typescript.SyntaxKind.UndefinedKeyword) {
8
+ return true;
9
+ }
10
+ if (typescript.isUnionTypeNode(typeNode)) {
11
+ return utils.A.some(typeNode.types, (subTypeNode) => includesUndefinedTypeNode(subTypeNode));
12
+ }
13
+ return false;
14
+ }
15
+
16
+ exports.includesUndefinedTypeNode = includesUndefinedTypeNode;
@@ -0,0 +1,2 @@
1
+ import { type TypeNode } from "typescript";
2
+ export declare function includesUndefinedTypeNode(typeNode: TypeNode): boolean;
@@ -0,0 +1,14 @@
1
+ import { A } from '@duplojs/utils';
2
+ import { SyntaxKind, isUnionTypeNode } from 'typescript';
3
+
4
+ function includesUndefinedTypeNode(typeNode) {
5
+ if (typeNode.kind === SyntaxKind.UndefinedKeyword) {
6
+ return true;
7
+ }
8
+ if (isUnionTypeNode(typeNode)) {
9
+ return A.some(typeNode.types, (subTypeNode) => includesUndefinedTypeNode(subTypeNode));
10
+ }
11
+ return false;
12
+ }
13
+
14
+ export { includesUndefinedTypeNode };
@@ -4,9 +4,11 @@ var create = require('./create.cjs');
4
4
  var transformer = require('./transformer.cjs');
5
5
  require('./hook.cjs');
6
6
  var index = require('./defaults/index.cjs');
7
+ var includesUndefinedTypeNode = require('./includesUndefinedTypeNode.cjs');
7
8
 
8
9
 
9
10
 
10
11
  exports.createTransformer = create.createTransformer;
11
12
  exports.transformer = transformer.transformer;
12
13
  exports.defaultTransformers = index.defaultTransformers;
14
+ exports.includesUndefinedTypeNode = includesUndefinedTypeNode.includesUndefinedTypeNode;
@@ -2,4 +2,5 @@ export * from "./create";
2
2
  export * from "./transformer";
3
3
  export * from "./hook";
4
4
  export * from "./defaults";
5
+ export * from "./includesUndefinedTypeNode";
5
6
  export { defaultTransformers } from "./defaults";
@@ -2,3 +2,4 @@ export { createTransformer } from './create.mjs';
2
2
  export { transformer } from './transformer.mjs';
3
3
  import './hook.mjs';
4
4
  export { defaultTransformers } from './defaults/index.mjs';
5
+ export { includesUndefinedTypeNode } from './includesUndefinedTypeNode.mjs';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@duplojs/data-parser-tools",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "license": "MIT",
5
5
  "author": "duplojs",
6
6
  "contributors": [
@@ -71,7 +71,7 @@
71
71
  "vitest": "3.2.4"
72
72
  },
73
73
  "peerDependencies": {
74
- "@duplojs/utils": ">=1.3.30 <2.0.0"
74
+ "@duplojs/utils": ">=1.4.36 <2.0.0"
75
75
  },
76
76
  "dependencies": {
77
77
  "typescript": "5.9.2"