@miroir-framework/jzod-ts 0.5.1 → 0.5.3

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.
@@ -1,3 +1,3 @@
1
- export { jzodToTsCode, jzodToTsTypeAliasesAndZodText, TsTypeAliasesAndZodText as JzodTypeAliasesAndZodText, TsTypeAliases, } from "./JzodToTs";
2
- export { printTsTypeAlias, printTsTypeAliases } from "./tools";
3
- export { jzodArraySchema, jzodAttributeSchema, jzodAttributeDateValidationsSchema, jzodAttributeDateWithValidationsSchema, jzodAttributeNumberValidationsSchema, jzodAttributeNumberWithValidationsSchema, jzodAttributeStringValidationsSchema, jzodAttributeStringWithValidationsSchema, jzodElementSchema, jzodEnumAttributeTypesSchema, jzodEnumElementTypesSchema, jzodEnumSchema, jzodFunctionSchema, jzodIntersectionSchema, jzodLazySchema, jzodLiteralSchema, jzodMapSchema, jzodObjectSchema, jzodPromiseSchema, jzodRecordSchema, jzodReferenceSchema, jzodSetSchema, jzodTupleSchema, jzodUnionSchema, JzodArray, JzodAttribute, JzodAttributeDateValidations, JzodAttributeDateWithValidations, JzodAttributeNumberValidations, JzodAttributeNumberWithValidations, JzodAttributeStringValidations, JzodAttributeStringWithValidations, JzodElement, JzodEnum, JzodEnumTypes, JzodFunction, JzodEnumElementTypes, JzodIntersection, JzodLazy, JzodLiteral, JzodMap, JzodObject, JzodPromise, JzodRecord, JzodReference, JzodSet, JzodUnion, JzodTuple, } from "./JzodTsInterface";
1
+ export { jzodToTsCode, jzodToTsTypeAliasesAndZodText, TsTypeAliasesAndZodText as JzodTypeAliasesAndZodText, TsTypeAliases, } from "./JzodToTs";
2
+ export { printTsTypeAlias, printTsTypeAliases } from "./tools";
3
+ export { jzodArray, jzodAttribute, jzodAttributeDateValidations, jzodAttributeDateWithValidations, jzodAttributeNumberValidations, jzodAttributeNumberWithValidations, jzodAttributeStringValidations, jzodAttributeStringWithValidations, jzodElement, jzodEnumAttributeTypes, jzodEnumElementTypes, jzodEnum, jzodFunction, jzodIntersection, jzodLazy, jzodLiteral, jzodMapSchema, jzodObject, jzodPromise, jzodRecord, jzodReference, jzodSet, jzodTuple, jzodUnion, JzodArray, JzodAttribute, JzodAttributeDateValidations, JzodAttributeDateWithValidations, JzodAttributeNumberValidations, JzodAttributeNumberWithValidations, JzodAttributeStringValidations, JzodAttributeStringWithValidations, JzodElement, JzodEnum, JzodEnumTypes, JzodFunction, JzodEnumElementTypes, JzodIntersection, JzodLazy, JzodLiteral, JzodMap, JzodObject, JzodPromise, JzodRecord, JzodReference, JzodSet, JzodUnion, JzodTuple, } from "./JzodTsInterface";
@@ -1,4 +1,4 @@
1
- import ts from "typescript";
2
- import { TsTypeAliases } from "./JzodToTs";
3
- export declare function printTsTypeAlias(typeAlias: ts.TypeAliasDeclaration, exportPrefix?: boolean): string;
4
- export declare function printTsTypeAliases(typeAliases: TsTypeAliases, exportPrefix?: boolean): string;
1
+ import ts from "typescript";
2
+ import { TsTypeAliases } from "./JzodToTs";
3
+ export declare function printTsTypeAlias(typeAlias: ts.TypeAliasDeclaration, exportPrefix?: boolean): string;
4
+ export declare function printTsTypeAliases(typeAliases: TsTypeAliases, exportPrefix?: boolean): string;
package/jest.config.js CHANGED
@@ -1,63 +1,63 @@
1
- // import { pathsToModuleNameMapper } from 'ts-jest';
2
- // import compilerOptions from './tsconfig'
3
-
4
- const esModules = ['uuid'].join('|');
5
-
6
- export default (path, options) =>({
7
- "verbose": true,
8
- "rootDir": ".",
9
- "moduleDirectories": [
10
- "node_modules",
11
- ],
12
- transformIgnorePatterns: [`<rootDir>\/(?!(/node_modules/(?!${esModules})))\/`],
13
- // modulePaths: ["./packages/miroir-standalone-app/src/"], // <-- This will be set to 'baseUrl' value
14
- modulePaths: ["./src/"], // <-- This will be set to 'baseUrl' value
15
- // modulePaths: [compilerOptions.baseUrl], // <-- This will be set to 'baseUrl' value
16
- moduleNameMapper:
17
- // Object.assign(
18
- {
19
- // "^miroir-fwk(.*)$": "<rootDir>/src/$1",
20
- // "^(.*)$": "$1",
21
- '\\.(css|scss|sass)$': 'identity-obj-proxy',
22
- }
23
- // pathsToModuleNameMapper(compilerOptions.paths /*, { prefix: '<rootDir>/' } */)
24
- // )
25
- ,
26
- // resolver:options.defaultResolver(path, {...options}),
27
- testEnvironment: "node",
28
- moduleFileExtensions:[
29
- "ts", "tsx", "js", "mjs", "cjs", "jsx", "json", "node"
30
- ],
31
- // preset:'ts-jest/presets/default-esm',
32
- "transform": {
33
- "^.+\\.tsx?$": [
34
- "ts-jest",
35
- {
36
- // useESM:true,
37
- // tsconfig: "../../tsconfig.json"
38
- tsconfig:
39
- {
40
- resolveJsonModule: true,
41
- esModuleInterop: true,
42
- allowSyntheticDefaultImports:true,
43
- allowJs: true,
44
- moduleResolution: "node",
45
- // module: "ESNext",
46
- // target: "ES2017",
47
- // rootDir: "./src/",
48
- traceResolution: true,
49
- module:"commonjs"
50
- },
51
- }
52
- ],
53
- "^.+\\.js?$": ["babel-jest"],
54
- },
55
- // moduleNameMapper: {
56
- // "^miroir-fwk\/(.+)$": "<rootDir>/src/$1",
57
- // '\\.(css|scss|sass)$': 'identity-obj-proxy',
58
- // },
59
- "testPathIgnorePatterns": [
60
- "./node_modules/",
61
- "./.cache/"
62
- ],
1
+ // import { pathsToModuleNameMapper } from 'ts-jest';
2
+ // import compilerOptions from './tsconfig'
3
+
4
+ const esModules = ['uuid'].join('|');
5
+
6
+ export default (path, options) =>({
7
+ "verbose": true,
8
+ "rootDir": ".",
9
+ "moduleDirectories": [
10
+ "node_modules",
11
+ ],
12
+ transformIgnorePatterns: [`<rootDir>\/(?!(/node_modules/(?!${esModules})))\/`],
13
+ // modulePaths: ["./packages/miroir-standalone-app/src/"], // <-- This will be set to 'baseUrl' value
14
+ modulePaths: ["./src/"], // <-- This will be set to 'baseUrl' value
15
+ // modulePaths: [compilerOptions.baseUrl], // <-- This will be set to 'baseUrl' value
16
+ moduleNameMapper:
17
+ // Object.assign(
18
+ {
19
+ // "^miroir-fwk(.*)$": "<rootDir>/src/$1",
20
+ // "^(.*)$": "$1",
21
+ '\\.(css|scss|sass)$': 'identity-obj-proxy',
22
+ }
23
+ // pathsToModuleNameMapper(compilerOptions.paths /*, { prefix: '<rootDir>/' } */)
24
+ // )
25
+ ,
26
+ // resolver:options.defaultResolver(path, {...options}),
27
+ testEnvironment: "node",
28
+ moduleFileExtensions:[
29
+ "ts", "tsx", "js", "mjs", "cjs", "jsx", "json", "node"
30
+ ],
31
+ // preset:'ts-jest/presets/default-esm',
32
+ "transform": {
33
+ "^.+\\.tsx?$": [
34
+ "ts-jest",
35
+ {
36
+ // useESM:true,
37
+ // tsconfig: "../../tsconfig.json"
38
+ tsconfig:
39
+ {
40
+ resolveJsonModule: true,
41
+ esModuleInterop: true,
42
+ allowSyntheticDefaultImports:true,
43
+ allowJs: true,
44
+ moduleResolution: "node",
45
+ // module: "ESNext",
46
+ // target: "ES2017",
47
+ // rootDir: "./src/",
48
+ traceResolution: true,
49
+ module:"commonjs"
50
+ },
51
+ }
52
+ ],
53
+ "^.+\\.js?$": ["babel-jest"],
54
+ },
55
+ // moduleNameMapper: {
56
+ // "^miroir-fwk\/(.+)$": "<rootDir>/src/$1",
57
+ // '\\.(css|scss|sass)$': 'identity-obj-proxy',
58
+ // },
59
+ "testPathIgnorePatterns": [
60
+ "./node_modules/",
61
+ "./.cache/"
62
+ ],
63
63
  })
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@miroir-framework/jzod-ts",
3
3
  "private": false,
4
- "version": "0.5.1",
4
+ "version": "0.5.3",
5
5
  "description": "The Typescript-related functionalities of Jzod.",
6
6
  "main": "dist/bundle.js",
7
7
  "types": "dist/bundle.d.ts",
@@ -15,7 +15,7 @@
15
15
  "bugs": "https://github.com/miroir-framework/jzod/issues",
16
16
  "license": "MIT",
17
17
  "dependencies": {
18
- "@miroir-framework/jzod": "0.5.1",
18
+ "@miroir-framework/jzod": "0.5.3",
19
19
  "zod": "^3.22.2",
20
20
  "zod-to-ts": "^1.2.0"
21
21
  },
package/rollup.config.js CHANGED
@@ -1,36 +1,36 @@
1
- import typescript from 'rollup-plugin-typescript2';
2
- import dts from 'rollup-plugin-dts';
3
-
4
- export default [
5
- {
6
- input: ["src/index.ts"],
7
- external: [
8
- 'zod',
9
- 'zod-to-ts',
10
- '@miroir-framework/jzod',
11
- ],
12
- output: [
13
- {
14
- file: `dist/bundle.js`,
15
- entryFileNames: "[name].js",
16
- format: "es",
17
- exports: "named"
18
- }
19
- ],
20
- plugins: [
21
- typescript(),
22
- ],
23
- },
24
- {
25
- input: ["dist/src/index.d.ts"],
26
- output: [
27
- {
28
- file: `dist/bundle.d.ts`,
29
- format: 'es',
30
- }
31
- ],
32
- plugins: [
33
- dts(),
34
- ],
35
- }
36
- ];
1
+ import typescript from 'rollup-plugin-typescript2';
2
+ import dts from 'rollup-plugin-dts';
3
+
4
+ export default [
5
+ {
6
+ input: ["src/index.ts"],
7
+ external: [
8
+ 'zod',
9
+ 'zod-to-ts',
10
+ '@miroir-framework/jzod',
11
+ ],
12
+ output: [
13
+ {
14
+ file: `dist/bundle.js`,
15
+ entryFileNames: "[name].js",
16
+ format: "es",
17
+ exports: "named"
18
+ }
19
+ ],
20
+ plugins: [
21
+ typescript(),
22
+ ],
23
+ },
24
+ {
25
+ input: ["dist/src/index.d.ts"],
26
+ output: [
27
+ {
28
+ file: `dist/bundle.d.ts`,
29
+ format: 'es',
30
+ }
31
+ ],
32
+ plugins: [
33
+ dts(),
34
+ ],
35
+ }
36
+ ];
package/src/JzodToTs.ts CHANGED
@@ -1,104 +1,109 @@
1
- import ts from "typescript";
2
- import { ZodLazy } from "zod";
3
- import { createTypeAlias, printNode, withGetType, zodToTs } from "zod-to-ts";
4
-
5
- import { jzodElementSchemaToZodSchemaAndDescription } from "@miroir-framework/jzod";
6
-
7
- import { JzodElement } from "./JzodTsInterface";
8
-
9
- import { printTsTypeAliases } from "./tools";
10
- // ################################################################################################
11
- export type TsTypeAliases = {
12
- [k: string]: ts.TypeAliasDeclaration;
13
- }
14
-
15
- // ################################################################################################
16
- export interface TsTypeAliasesAndZodText {
17
- contextTsTypeAliases: { [k: string]: ts.TypeAliasDeclaration },
18
- contextZodText: { [k: string]: string },
19
- mainTsTypeAlias: ts.TypeAliasDeclaration,
20
- mainZodText: string
21
- }
22
-
23
- // ################################################################################################
24
- export function jzodToTsTypeAliasesAndZodText(
25
- element: JzodElement,
26
- typeName?: string,
27
- ): TsTypeAliasesAndZodText
28
- {
29
- const elementZodSchemaAndDescription = jzodElementSchemaToZodSchemaAndDescription(
30
- element,
31
- () => ({}),
32
- () => ({}),
33
- (innerReference: ZodLazy<any>, relativeReference: string | undefined) =>
34
- withGetType(innerReference, (ts) => {
35
- return ts.factory.createTypeReferenceNode(
36
- ts.factory.createIdentifier(relativeReference ? relativeReference : "RELATIVEPATH_NOT_DEFINED")
37
- );
38
- }
39
- )
40
- );
41
-
42
- const contextTsTypesString = Object.fromEntries(
43
- Object.entries(elementZodSchemaAndDescription.contextZodSchema ?? {}).map((curr) => {
44
- // console.log("getTsCodeCorrespondingToZodSchemaAndDescription ", JSON.stringify(curr));
45
- const tsNode = zodToTs(curr[1], typeName).node;
46
- const typeAlias = createTypeAlias(tsNode, curr[0]);
47
- return [curr[0], typeAlias];
48
- })
49
- );
50
-
51
- const tsTypeStringNode = zodToTs(elementZodSchemaAndDescription.zodSchema, typeName).node;
52
- const tsTypeStringTypeAlias = createTypeAlias(tsTypeStringNode, typeName??"");
53
-
54
- return {
55
- contextTsTypeAliases: contextTsTypesString,
56
- contextZodText: elementZodSchemaAndDescription.contextZodText??{},
57
- mainZodText: elementZodSchemaAndDescription.zodText,
58
- mainTsTypeAlias: tsTypeStringTypeAlias,
59
- };
60
- }
61
-
62
-
63
- // ################################################################################################
64
- export function jzodToTsCode (
65
- jzodElement: JzodElement,
66
- exportPrefix: boolean = true,
67
- typeName?: string,
68
- ): string {
69
- // console.log(
70
- // "getTsCodeCorrespondingToZodSchemaAndDescription jzodSchemaZodSchemaAndDescription.contextZodSchema",
71
- // "jzodElement",
72
- // JSON.stringify(jzodElement)
73
- // );
74
-
75
- const schemaName = typeName?typeName.replace(/^(.)(.*)$/, (a, b, c) => b.toLowerCase() + c):"";
76
- // console.log("getTsCodeCorrespondingToZodSchemaAndDescription convertedJsonZodSchema", bodyJsCode);
77
-
78
- const header = `import { ZodType, ZodTypeAny, z } from "zod";`;
79
-
80
- const typeAliasesAndZodText = jzodToTsTypeAliasesAndZodText(jzodElement, typeName);
81
-
82
- const bodyJsCode = `export const ${schemaName}: z.ZodType<${schemaName}> = ${typeAliasesAndZodText.mainZodText};`;
83
-
84
- const contextTsTypesString = printTsTypeAliases(typeAliasesAndZodText.contextTsTypeAliases, exportPrefix);
85
- // console.log("getTsCodeCorrespondingToZodSchemaAndDescription contextTsTypesString",contextTsTypesString);
86
-
87
- const contextJsCode = typeAliasesAndZodText.contextZodText
88
- ? Object.entries(typeAliasesAndZodText.contextZodText).reduce((acc, curr) => {
89
- return `${acc}
90
- export const ${curr[0]}=${curr[1]};`;
91
- }, "")
92
- : ""
93
- ;
94
-
95
- const tsTypesString = (exportPrefix?"export ":"") + printNode(typeAliasesAndZodText.mainTsTypeAlias);
96
- // console.log("getTsCodeCorrespondingToZodSchemaAndDescription tsTypeString",tsTypesString);
97
-
98
- return `${header}
99
- ${contextTsTypesString}
100
- ${tsTypesString}
101
- ${contextJsCode}
102
- ${bodyJsCode}
103
- `;
104
- }
1
+ import ts from "typescript";
2
+ import { ZodLazy } from "zod";
3
+ import { createTypeAlias, printNode, withGetType, zodToTs } from "zod-to-ts";
4
+
5
+ import { jzodElementSchemaToZodSchemaAndDescription } from "@miroir-framework/jzod";
6
+
7
+ import { JzodElement } from "./JzodTsInterface";
8
+
9
+ import { printTsTypeAliases } from "./tools";
10
+ // ################################################################################################
11
+ export type TsTypeAliases = {
12
+ [k: string]: ts.TypeAliasDeclaration;
13
+ }
14
+
15
+ // ################################################################################################
16
+ export interface TsTypeAliasesAndZodText {
17
+ contextTsTypeAliases: { [k: string]: ts.TypeAliasDeclaration },
18
+ contextZodText: { [k: string]: string },
19
+ mainTsTypeAlias: ts.TypeAliasDeclaration,
20
+ mainZodText: string
21
+ }
22
+
23
+ // ################################################################################################
24
+ export function jzodToTsTypeAliasesAndZodText(
25
+ element: JzodElement,
26
+ typeName?: string,
27
+ ): TsTypeAliasesAndZodText
28
+ {
29
+ const elementZodSchemaAndDescription = jzodElementSchemaToZodSchemaAndDescription(
30
+ element,
31
+ () => ({}),
32
+ () => ({}),
33
+ (innerReference: ZodLazy<any>, relativeReference: string | undefined) =>
34
+ withGetType(innerReference, (ts) => {
35
+ const actualTypeName = relativeReference?relativeReference.replace(/^(.)(.*)$/, (a, b, c) => b.toUpperCase() + c):"";
36
+ return ts.factory.createTypeReferenceNode(
37
+
38
+ ts.factory.createIdentifier(actualTypeName ? actualTypeName : "RELATIVEPATH_NOT_DEFINED")
39
+ );
40
+ }
41
+ )
42
+ );
43
+
44
+ const contextTsTypesString = Object.fromEntries(
45
+ Object.entries(elementZodSchemaAndDescription.contextZodSchema ?? {}).map((curr) => {
46
+ const actualTypeName = curr[0]?curr[0].replace(/^(.)(.*)$/, (a, b, c) => b.toUpperCase() + c):"";
47
+ // console.log("getTsCodeCorrespondingToZodSchemaAndDescription ", JSON.stringify(curr));
48
+ const tsNode = zodToTs(curr[1], typeName).node;
49
+ const typeAlias = createTypeAlias(tsNode, actualTypeName);
50
+ return [curr[0], typeAlias];
51
+ })
52
+ );
53
+
54
+ const tsTypeStringNode = zodToTs(elementZodSchemaAndDescription.zodSchema, typeName).node;
55
+ const tsTypeStringTypeAlias = createTypeAlias(tsTypeStringNode, typeName??"");
56
+
57
+ return {
58
+ contextTsTypeAliases: contextTsTypesString,
59
+ contextZodText: elementZodSchemaAndDescription.contextZodText??{},
60
+ mainZodText: elementZodSchemaAndDescription.zodText,
61
+ mainTsTypeAlias: tsTypeStringTypeAlias,
62
+ };
63
+ }
64
+
65
+
66
+ // ################################################################################################
67
+ export function jzodToTsCode (
68
+ jzodElement: JzodElement,
69
+ exportPrefix: boolean = true,
70
+ typeName?: string,
71
+ ): string {
72
+ // console.log(
73
+ // "getTsCodeCorrespondingToZodSchemaAndDescription jzodSchemaZodSchemaAndDescription.contextZodSchema",
74
+ // "jzodElement",
75
+ // JSON.stringify(jzodElement)
76
+ // );
77
+
78
+ const schemaName = typeName?typeName.replace(/^(.)(.*)$/, (a, b, c) => b.toLowerCase() + c):"";
79
+ const actualTypeName = typeName?typeName.replace(/^(.)(.*)$/, (a, b, c) => b.toUpperCase() + c):"";
80
+ // console.log("getTsCodeCorrespondingToZodSchemaAndDescription convertedJsonZodSchema", bodyJsCode);
81
+
82
+ const header = `import { ZodType, ZodTypeAny, z } from "zod";`;
83
+
84
+ const typeAliasesAndZodText = jzodToTsTypeAliasesAndZodText(jzodElement, actualTypeName);
85
+
86
+ const bodyJsCode = `export const ${schemaName}: z.ZodType<${actualTypeName}> = ${typeAliasesAndZodText.mainZodText};`;
87
+
88
+ const contextTsTypesString = printTsTypeAliases(typeAliasesAndZodText.contextTsTypeAliases, exportPrefix);
89
+ // console.log("getTsCodeCorrespondingToZodSchemaAndDescription contextTsTypesString",contextTsTypesString);
90
+
91
+ const contextJsCode = typeAliasesAndZodText.contextZodText
92
+ ? Object.entries(typeAliasesAndZodText.contextZodText).reduce((acc, curr) => {
93
+ const contextTypeName = curr[0]?curr[0].replace(/^(.)(.*)$/, (a, b, c) => b.toUpperCase() + c):"";
94
+ return `${acc}
95
+ export const ${curr[0]}:z.ZodType<${contextTypeName}> = ${curr[1]};`;
96
+ }, "")
97
+ : ""
98
+ ;
99
+
100
+ const tsTypesString = (exportPrefix?"export ":"") + printNode(typeAliasesAndZodText.mainTsTypeAlias);
101
+ // console.log("getTsCodeCorrespondingToZodSchemaAndDescription tsTypeString",tsTypesString);
102
+
103
+ return `${header}
104
+ ${contextTsTypesString}
105
+ ${tsTypesString}
106
+ ${contextJsCode}
107
+ ${bodyJsCode}
108
+ `;
109
+ }