@hedystia/types 1.10.2 → 1.10.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.
@@ -0,0 +1,27 @@
1
+
2
+ $ tsdown --config-loader unrun
3
+ ℹ tsdown v0.21.2 powered by rolldown v1.0.0-rc.9
4
+ ℹ config file: /home/zastinian/Documents/codes/Framework/Packages/types/tsdown.config.ts (unrun)
5
+ ℹ entry: src/index.ts
6
+ ℹ tsconfig: tsconfig.json
7
+ ℹ Build start
8
+ ℹ Cleaning 14 files
9
+ ℹ [CJS] dist/index.cjs 1.22 kB │ gzip: 0.52 kB
10
+ ℹ [CJS] dist/utils.cjs.map 4.09 kB │ gzip: 1.34 kB
11
+ ℹ [CJS] dist/utils.cjs 2.25 kB │ gzip: 0.74 kB
12
+ ℹ [CJS] dist/index.cjs.map 1.86 kB │ gzip: 0.75 kB
13
+ ℹ [CJS] 4 files, total: 9.42 kB
14
+ ℹ [CJS] dist/index.d.cts 0.36 kB │ gzip: 0.23 kB
15
+ ℹ [CJS] dist/types.d.cts 0.41 kB │ gzip: 0.24 kB
16
+ ℹ [CJS] dist/utils.d.cts 0.16 kB │ gzip: 0.14 kB
17
+ ℹ [CJS] 3 files, total: 0.94 kB
18
+ ✔ Build complete in 774ms
19
+ ℹ [ESM] dist/index.mjs 0.98 kB │ gzip: 0.44 kB
20
+ ℹ [ESM] dist/utils.mjs.map 3.80 kB │ gzip: 1.28 kB
21
+ ℹ [ESM] dist/utils.mjs 2.13 kB │ gzip: 0.77 kB
22
+ ℹ [ESM] dist/index.mjs.map 1.78 kB │ gzip: 0.74 kB
23
+ ℹ [ESM] dist/index.d.mts 0.36 kB │ gzip: 0.23 kB
24
+ ℹ [ESM] dist/types.d.mts 0.41 kB │ gzip: 0.24 kB
25
+ ℹ [ESM] dist/utils.d.mts 0.16 kB │ gzip: 0.14 kB
26
+ ℹ [ESM] 7 files, total: 9.63 kB
27
+ ✔ Build complete in 779ms
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@hedystia/types",
3
- "version": "1.10.2",
3
+ "version": "1.10.3",
4
4
  "description": "Next-gen TypeScript framework for building type-safe APIs at lightspeed",
5
5
  "homepage": "https://docs.hedystia.com/plugins/types",
6
6
  "dependencies": {
7
- "@hedystia/validations": "workspace:*"
7
+ "@hedystia/validations": "^1.10.2"
8
8
  },
9
9
  "devDependencies": {
10
10
  "@types/bun": "^1.3.3"
@@ -32,7 +32,7 @@
32
32
  "license": "MIT",
33
33
  "scripts": {
34
34
  "build": "tsdown --config-loader unrun",
35
- "publish": "npm publish --access public",
35
+ "publish": "bun publish --access public",
36
36
  "dev": "tsdown --watch"
37
37
  },
38
38
  "repository": {
package/src/index.ts ADDED
@@ -0,0 +1,24 @@
1
+ import { writeFile } from "fs/promises";
2
+ import type { RouteInfo } from "./types";
3
+ import { schemaToTypeString } from "./utils";
4
+
5
+ export async function generateTypes(routes: RouteInfo[], filePath: string): Promise<void> {
6
+ const typeDefinitions = routes
7
+ .map((route) => {
8
+ const responseType = schemaToTypeString(route.response);
9
+ const paramsType = schemaToTypeString(route.params);
10
+ const queryType = schemaToTypeString(route.query);
11
+ const bodyType = schemaToTypeString(route.body);
12
+ const headersType = schemaToTypeString(route.headers);
13
+ const dataType = schemaToTypeString(route.data);
14
+ const errorType = schemaToTypeString(route.error);
15
+ return `{method:"${route.method}";path:"${route.path}";params:${paramsType};query:${queryType};body:${bodyType};headers:${headersType};response:${responseType};data:${dataType};error:${errorType}}`;
16
+ })
17
+ .join(",");
18
+
19
+ const content = `// Automatic Hedystia type generation\nexport type AppRoutes=[${typeDefinitions}];`;
20
+ await writeFile(filePath, content, "utf8");
21
+ }
22
+ export type { TypeGeneratorOptions } from "./types";
23
+ export type { RouteInfo };
24
+ export { schemaToTypeString };
package/src/types.ts ADDED
@@ -0,0 +1,16 @@
1
+ export interface RouteInfo {
2
+ method: string;
3
+ path: string;
4
+ params?: unknown;
5
+ query?: unknown;
6
+ headers?: unknown;
7
+ body?: unknown;
8
+ response?: unknown;
9
+ data?: unknown;
10
+ error?: unknown;
11
+ }
12
+
13
+ export type TypeGeneratorOptions = {
14
+ includeSubscriptions?: boolean;
15
+ includeWebSocket?: boolean;
16
+ };
package/src/utils.ts ADDED
@@ -0,0 +1,94 @@
1
+ import {
2
+ AnySchemaType,
3
+ ArraySchema,
4
+ BooleanSchemaType,
5
+ InstanceOfSchema,
6
+ LiteralSchema,
7
+ NullSchemaType,
8
+ NumberSchemaType,
9
+ ObjectSchemaType,
10
+ OptionalSchema,
11
+ StringSchemaType,
12
+ UnionSchema,
13
+ } from "@hedystia/validations";
14
+
15
+ export function schemaToTypeString(schema: any): string {
16
+ if (!schema) {
17
+ return "any";
18
+ }
19
+
20
+ if (schema instanceof StringSchemaType) {
21
+ return "string";
22
+ }
23
+ if (schema instanceof NumberSchemaType) {
24
+ return "number";
25
+ }
26
+ if (schema instanceof BooleanSchemaType) {
27
+ return "boolean";
28
+ }
29
+ if (schema instanceof NullSchemaType) {
30
+ return "null";
31
+ }
32
+ if (schema instanceof AnySchemaType) {
33
+ return "any";
34
+ }
35
+
36
+ if (schema instanceof OptionalSchema) {
37
+ const inner = (schema as any).innerSchema;
38
+ return `${schemaToTypeString(inner)} | undefined`;
39
+ }
40
+
41
+ if (schema instanceof ArraySchema) {
42
+ const inner = (schema as any).innerSchema;
43
+ const innerType = schemaToTypeString(inner);
44
+ return innerType.includes("|") || innerType.includes("{")
45
+ ? `(${innerType})[]`
46
+ : `${innerType}[]`;
47
+ }
48
+
49
+ if (schema instanceof UnionSchema) {
50
+ const schemas = (schema as any).schemas || [];
51
+ if (schemas.length === 0) {
52
+ return "any";
53
+ }
54
+ return schemas.map((s: any) => schemaToTypeString(s)).join(" | ");
55
+ }
56
+
57
+ if (schema instanceof LiteralSchema) {
58
+ const val = (schema as any).value;
59
+ return typeof val === "string" ? `'${val}'` : String(val);
60
+ }
61
+
62
+ if (schema instanceof InstanceOfSchema) {
63
+ const ctor = (schema as any).classConstructor;
64
+ return ctor ? ctor.name : "object";
65
+ }
66
+
67
+ if (schema instanceof ObjectSchemaType) {
68
+ const definition = (schema as any).definition;
69
+ if (!definition || Object.keys(definition).length === 0) {
70
+ return "{}";
71
+ }
72
+
73
+ const validIdentifierRegex = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
74
+ const properties = Object.entries(definition)
75
+ .map(([key, value]) => {
76
+ const finalKey = validIdentifierRegex.test(key) ? key : `"${key}"`;
77
+ const isOptional = value instanceof OptionalSchema;
78
+ const optionalMarker = isOptional ? "?" : "";
79
+
80
+ let typeStr = schemaToTypeString(value);
81
+
82
+ if (isOptional) {
83
+ typeStr = typeStr.replace(" | undefined", "");
84
+ }
85
+
86
+ return `${finalKey}${optionalMarker}:${typeStr}`;
87
+ })
88
+ .join(";");
89
+
90
+ return `{${properties}}`;
91
+ }
92
+
93
+ return "any";
94
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,3 @@
1
+ {
2
+ "extends": "../../tsconfig.json"
3
+ }
@@ -0,0 +1,13 @@
1
+ import { defineConfig } from "tsdown";
2
+
3
+ export default defineConfig({
4
+ entry: ["src/index.ts"],
5
+ format: ["cjs", "esm"],
6
+ dts: true,
7
+ sourcemap: true,
8
+ treeshake: true,
9
+ clean: true,
10
+ unbundle: true,
11
+ outputOptions: { exports: "named" },
12
+ deps: { neverBundle: ["@hedystia/validations", "bun"] },
13
+ });