jax-hono 1.0.20 → 1.0.21

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/core/generated.ts CHANGED
@@ -1,87 +1,87 @@
1
1
  import { existsSync } from 'node:fs';
2
2
  import { cwd, execPath } from 'node:process';
3
3
  import { dirname, resolve } from 'node:path';
4
-
5
- export interface JaxGenerated {}
6
-
7
- export type GeneratedControllers = JaxGenerated extends { controllers: infer T }
8
- ? T
9
- : Record<string, any>;
10
- export type GeneratedModels = JaxGenerated extends { models: infer T }
11
- ? T
12
- : Record<string, any>;
13
- export type GeneratedServices = JaxGenerated extends { services: infer T }
14
- ? T
15
- : Record<string, any>;
16
- export type GeneratedPlugins = JaxGenerated extends { plugins: infer T }
17
- ? T
18
- : Record<string, any>;
19
- export type GeneratedProjectConfig = JaxGenerated extends { config: infer T }
20
- ? T
21
- : Record<string, unknown>;
22
-
23
- type GeneratedModuleMap = Record<string, unknown>;
24
-
25
- const generatedModules: GeneratedModuleMap = {};
26
-
27
- export function registerGeneratedModules(modules: GeneratedModuleMap) {
28
- Object.assign(generatedModules, modules);
29
- }
30
-
4
+
5
+ export interface JaxGenerated {}
6
+
7
+ export type GeneratedControllers = JaxGenerated extends { controllers: infer T }
8
+ ? T
9
+ : Record<string, any>;
10
+ export type GeneratedModels = JaxGenerated extends { models: infer T }
11
+ ? T
12
+ : Record<string, any>;
13
+ export type GeneratedServices = JaxGenerated extends { services: infer T }
14
+ ? T
15
+ : Record<string, any>;
16
+ export type GeneratedPlugins = JaxGenerated extends { plugins: infer T }
17
+ ? T
18
+ : Record<string, any>;
19
+ export type GeneratedProjectConfig = JaxGenerated extends { config: infer T }
20
+ ? T
21
+ : Record<string, unknown>;
22
+
23
+ type GeneratedModuleMap = Record<string, unknown>;
24
+
25
+ const generatedModules: GeneratedModuleMap = {};
26
+
27
+ export function registerGeneratedModules(modules: GeneratedModuleMap) {
28
+ Object.assign(generatedModules, modules);
29
+ }
30
+
31
31
  export function getProjectGeneratedDir() {
32
32
  const configuredDir = Bun.env.JAX_HONO_GENERATED_DIR;
33
-
34
- if (configuredDir) {
35
- return resolve(cwd(), configuredDir);
36
- }
37
-
33
+
34
+ if (configuredDir) {
35
+ return resolve(cwd(), configuredDir);
36
+ }
37
+
38
38
  return findProjectGeneratedDir();
39
39
  }
40
40
 
41
41
  export function getProjectDir() {
42
42
  return dirname(getProjectGeneratedDir());
43
43
  }
44
-
45
- export function requireGeneratedModule<T>(fileName: string): T {
46
- if (fileName in generatedModules) {
47
- return generatedModules[fileName] as T;
48
- }
49
-
50
- const filePath = findGeneratedFile(fileName);
51
-
52
- if (!existsSync(filePath)) {
53
- throw new Error(
54
- `Jax generated file not found: ${filePath}. Run "bun run setup" first, or set JAX_HONO_GENERATED_DIR.`
55
- );
56
- }
57
-
58
- return require(filePath) as T;
59
- }
60
-
61
- function findProjectGeneratedDir() {
62
- const candidates = getGeneratedDirCandidates();
63
-
64
- return candidates.find((dir) => existsSync(dir)) ?? candidates[0];
65
- }
66
-
67
- function findGeneratedFile(fileName: string) {
68
- const configuredDir = Bun.env.JAX_HONO_GENERATED_DIR;
69
-
70
- if (configuredDir) {
71
- return resolve(cwd(), configuredDir, fileName);
72
- }
73
-
74
- for (const dir of getGeneratedDirCandidates()) {
75
- const filePath = resolve(dir, fileName);
76
-
77
- if (existsSync(filePath)) {
78
- return filePath;
79
- }
80
- }
81
-
82
- return resolve(getGeneratedDirCandidates()[0], fileName);
83
- }
84
-
44
+
45
+ export function requireGeneratedModule<T>(fileName: string): T {
46
+ if (fileName in generatedModules) {
47
+ return generatedModules[fileName] as T;
48
+ }
49
+
50
+ const filePath = findGeneratedFile(fileName);
51
+
52
+ if (!existsSync(filePath)) {
53
+ throw new Error(
54
+ `Jax generated file not found: ${filePath}. Run "bun run setup" first, or set JAX_HONO_GENERATED_DIR.`
55
+ );
56
+ }
57
+
58
+ return require(filePath) as T;
59
+ }
60
+
61
+ function findProjectGeneratedDir() {
62
+ const candidates = getGeneratedDirCandidates();
63
+
64
+ return candidates.find((dir) => existsSync(dir)) ?? candidates[0];
65
+ }
66
+
67
+ function findGeneratedFile(fileName: string) {
68
+ const configuredDir = Bun.env.JAX_HONO_GENERATED_DIR;
69
+
70
+ if (configuredDir) {
71
+ return resolve(cwd(), configuredDir, fileName);
72
+ }
73
+
74
+ for (const dir of getGeneratedDirCandidates()) {
75
+ const filePath = resolve(dir, fileName);
76
+
77
+ if (existsSync(filePath)) {
78
+ return filePath;
79
+ }
80
+ }
81
+
82
+ return resolve(getGeneratedDirCandidates()[0], fileName);
83
+ }
84
+
85
85
  function getGeneratedDirCandidates() {
86
86
  const cwdDir = cwd();
87
87
  const executableDir = dirname(execPath);
package/core/hono.ts CHANGED
@@ -1,63 +1,63 @@
1
- import { Hono } from 'hono';
2
- import { apiResponse } from '../middleware/api-response';
3
- import { registerCrudRoutes, type JaxCrudController } from '../helpers/crud';
4
- import type { GeneratedModels, GeneratedServices } from './generated';
5
- import type { JaxState } from '../types/context';
6
-
7
- declare module 'hono' {
8
- interface Context {
9
- state: JaxState;
10
- model: GeneratedModels;
11
- service: GeneratedServices;
12
- }
13
-
14
- interface ContextVariableMap {
15
- model: GeneratedModels;
16
- service: GeneratedServices;
17
- state: JaxState;
18
- }
19
- }
20
-
21
- export type JaxHono = Hono & {
22
- ready?: Promise<void>;
23
- close?: () => Promise<void>;
24
- jax?: unknown;
25
- resources(path: string, controller: JaxCrudController): JaxHono;
26
- };
27
-
28
- export function createHono(): JaxHono {
29
- const app = new Hono();
30
-
31
- return withJaxRoutes(app);
32
- }
33
-
34
- export function createApi() {
35
- const api = createHono();
36
-
37
- // API 路由默认使用 { code, msg, data } 响应结构,并统一兜底异常返回。
38
- api.use('*', apiResponse);
39
- api.notFound((c) => c.fail('接口不存在'));
40
- api.onError((error, c) => {
41
- const message = error instanceof Error ? error.message : String(error);
42
-
43
- console.error('API 请求失败:', error);
44
-
45
- return c.fail(message);
46
- });
47
-
48
- return api;
49
- }
50
-
51
- function withJaxRoutes(app: Hono): JaxHono {
52
- const jaxApp = app as JaxHono;
53
-
54
- jaxApp.resources = (path, controller) => {
55
- registerCrudRoutes(jaxApp, path, controller);
56
-
57
- return jaxApp;
58
- };
59
-
60
- return jaxApp;
61
- }
62
-
63
- export { Hono };
1
+ import { Hono } from 'hono';
2
+ import { apiResponse } from '../middleware/api-response';
3
+ import { registerCrudRoutes, type JaxCrudController } from '../helpers/crud';
4
+ import type { GeneratedModels, GeneratedServices } from './generated';
5
+ import type { JaxState } from '../types/context';
6
+
7
+ declare module 'hono' {
8
+ interface Context {
9
+ state: JaxState;
10
+ model: GeneratedModels;
11
+ service: GeneratedServices;
12
+ }
13
+
14
+ interface ContextVariableMap {
15
+ model: GeneratedModels;
16
+ service: GeneratedServices;
17
+ state: JaxState;
18
+ }
19
+ }
20
+
21
+ export type JaxHono = Hono & {
22
+ ready?: Promise<void>;
23
+ close?: () => Promise<void>;
24
+ jax?: unknown;
25
+ resources(path: string, controller: JaxCrudController): JaxHono;
26
+ };
27
+
28
+ export function createHono(): JaxHono {
29
+ const app = new Hono();
30
+
31
+ return withJaxRoutes(app);
32
+ }
33
+
34
+ export function createApi() {
35
+ const api = createHono();
36
+
37
+ // API 路由默认使用 { code, msg, data } 响应结构,并统一兜底异常返回。
38
+ api.use('*', apiResponse);
39
+ api.notFound((c) => c.fail('接口不存在'));
40
+ api.onError((error, c) => {
41
+ const message = error instanceof Error ? error.message : String(error);
42
+
43
+ console.error('API 请求失败:', error);
44
+
45
+ return c.fail(message);
46
+ });
47
+
48
+ return api;
49
+ }
50
+
51
+ function withJaxRoutes(app: Hono): JaxHono {
52
+ const jaxApp = app as JaxHono;
53
+
54
+ jaxApp.resources = (path, controller) => {
55
+ registerCrudRoutes(jaxApp, path, controller);
56
+
57
+ return jaxApp;
58
+ };
59
+
60
+ return jaxApp;
61
+ }
62
+
63
+ export { Hono };
package/core/service.ts CHANGED
@@ -1,44 +1,44 @@
1
- import type { Config } from '../config';
2
- import type { GeneratedModels, GeneratedPlugins, GeneratedServices } from './generated';
3
- import { getJaxContext } from './controller';
4
-
5
- type Plugins = GeneratedPlugins;
6
-
7
- export type ServiceContext = {
8
- config: Config;
9
- model: GeneratedModels;
10
- service: GeneratedServices;
11
- plugin: Plugins;
12
- };
13
-
14
- export abstract class Service {
15
- constructor(protected readonly appContext: ServiceContext) {}
16
-
17
- protected get context() {
18
- return getJaxContext();
19
- }
20
-
21
- protected get ctx() {
22
- return this.context;
23
- }
24
-
25
- protected get state() {
26
- return this.context.get('state');
27
- }
28
-
29
- protected get config() {
30
- return this.appContext.config;
31
- }
32
-
33
- protected get model() {
34
- return this.appContext.model;
35
- }
36
-
37
- protected get service() {
38
- return this.appContext.service;
39
- }
40
-
41
- protected get plugin(): Plugins {
42
- return this.appContext.plugin;
43
- }
44
- }
1
+ import type { Config } from '../config';
2
+ import type { GeneratedModels, GeneratedPlugins, GeneratedServices } from './generated';
3
+ import { getJaxContext } from './controller';
4
+
5
+ type Plugins = GeneratedPlugins;
6
+
7
+ export type ServiceContext = {
8
+ config: Config;
9
+ model: GeneratedModels;
10
+ service: GeneratedServices;
11
+ plugin: Plugins;
12
+ };
13
+
14
+ export abstract class Service {
15
+ constructor(protected readonly appContext: ServiceContext) {}
16
+
17
+ protected get context() {
18
+ return getJaxContext();
19
+ }
20
+
21
+ protected get ctx() {
22
+ return this.context;
23
+ }
24
+
25
+ protected get state() {
26
+ return this.context.get('state');
27
+ }
28
+
29
+ protected get config() {
30
+ return this.appContext.config;
31
+ }
32
+
33
+ protected get model() {
34
+ return this.appContext.model;
35
+ }
36
+
37
+ protected get service() {
38
+ return this.appContext.service;
39
+ }
40
+
41
+ protected get plugin(): Plugins {
42
+ return this.appContext.plugin;
43
+ }
44
+ }