vona-core 5.0.48 → 5.0.50

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.
@@ -3,5 +3,5 @@ import type { VonaApplication } from '../core/application.ts';
3
3
  export declare class BeanSimple {
4
4
  protected app: VonaApplication;
5
5
  protected get ctx(): VonaContext;
6
- protected get bean(): import("./beanContainer.ts").BeanContainer;
6
+ protected get bean(): import("vona").BeanContainer;
7
7
  }
@@ -4,6 +4,7 @@ import { cast } from "../../types/utils/cast.js";
4
4
  import { BeanContainer } from "../bean/beanContainer.js";
5
5
  import { closeApp } from "../framework/useApp.js";
6
6
  import { AppUtil } from "../utils/util.js";
7
+ import { zodEnhance } from "../utils/zod-enhance.js";
7
8
  import { VonaAsyncLocalStorage } from "./asyncLocalStorage.js";
8
9
  import { contextBase } from "./context.js";
9
10
  import { AppMeta } from "./meta.js";
@@ -39,6 +40,8 @@ export class VonaApplication extends KoaApplication {
39
40
  const desc = Object.getOwnPropertyDescriptor(contextBase, key);
40
41
  Object.defineProperty(this.context, key, desc);
41
42
  }
43
+ // zod
44
+ zodEnhance(this);
42
45
  }
43
46
  get ctx() {
44
47
  return this.currentContext;
@@ -57,6 +57,13 @@ export const contextBase = {
57
57
  return undefined;
58
58
  return controller.prototype;
59
59
  },
60
+ getControllerBean() {
61
+ const self = cast(this);
62
+ const beanFullName = self.getControllerBeanFullName();
63
+ if (!beanFullName)
64
+ return;
65
+ return self.app.bean._getBean(beanFullName);
66
+ },
60
67
  getControllerBeanFullName() {
61
68
  const self = cast(this);
62
69
  const controller = self.getController();
@@ -14,6 +14,7 @@ export declare class AppMeta extends BeanSimple {
14
14
  isProd: boolean;
15
15
  isTest: boolean;
16
16
  isDev: boolean;
17
+ isLocal: boolean;
17
18
  error: ErrorClass;
18
19
  logger: AppLogger;
19
20
  locale: AppLocale;
@@ -13,6 +13,7 @@ export class AppMeta extends BeanSimple {
13
13
  isProd;
14
14
  isTest;
15
15
  isDev;
16
+ isLocal;
16
17
  error;
17
18
  logger;
18
19
  locale;
@@ -66,6 +67,7 @@ export class AppMeta extends BeanSimple {
66
67
  this.isProd = mode === 'prod';
67
68
  this.isTest = mode === 'test';
68
69
  this.isDev = mode === 'dev';
70
+ this.isLocal = this.isTest || this.isDev;
69
71
  }
70
72
  async waitAppStarted() {
71
73
  return new Promise((resolve, reject) => {
@@ -1,4 +1,3 @@
1
- import './zod-openapi.ts';
2
1
  export * from './customKey.ts';
3
2
  export * from './retry.ts';
4
3
  export * from './util.ts';
@@ -1,4 +1,5 @@
1
- import "./zod-openapi.js";
1
+ import { zodExtendOpenApi } from "./zod-openapi.js";
2
2
  export * from "./customKey.js";
3
3
  export * from "./retry.js";
4
4
  export * from "./util.js";
5
+ zodExtendOpenApi();
@@ -4,6 +4,7 @@ import type { BinaryToTextEncoding, HashOptions } from 'node:crypto';
4
4
  import type { IInstanceRecord, TypeMonkeyName, VonaConfigEnv, VonaContext } from '../../types/index.ts';
5
5
  import type { IBeanScopeRecord, TypeBeanScopeRecordKeys } from '../bean/type.ts';
6
6
  import type { IBeanSceneRecord } from '../decorator/interface/beanOptions.ts';
7
+ import type { ZodLocaleErrors } from './zod-enhance.ts';
7
8
  import { BeanSimple } from '../bean/beanSimple.ts';
8
9
  export interface IExecuteBeanCallbackParams {
9
10
  ctx: VonaContext;
@@ -45,6 +46,7 @@ export declare class AppUtil extends BeanSimple {
45
46
  getModuleConfigRaw<K extends TypeBeanScopeRecordKeys>(moduleName: K): IBeanScopeRecord[K] extends {
46
47
  config?: infer CONFIG;
47
48
  } ? CONFIG | undefined : undefined;
49
+ setLocaleErrors(localeErrors: ZodLocaleErrors, localeDefault?: string): void;
48
50
  }
49
51
  export declare function compose(chains: any, adapter?: any): (context: any, next?: any) => any;
50
52
  export declare function instanceDesp(instanceName: keyof IInstanceRecord | null | undefined): string;
@@ -9,6 +9,7 @@ import fse from 'fs-extra';
9
9
  import * as uuid from 'uuid';
10
10
  import { cast } from "../../types/index.js";
11
11
  import { BeanSimple } from "../bean/beanSimple.js";
12
+ import { zodSetLocaleErrors } from "./zod-enhance.js";
12
13
  const SymbolProdRootPath = Symbol('SymbolProdRootPath');
13
14
  export class AppUtil extends BeanSimple {
14
15
  [SymbolProdRootPath];
@@ -208,6 +209,9 @@ export class AppUtil extends BeanSimple {
208
209
  getModuleConfigRaw(moduleName) {
209
210
  return this.app.config.modules[moduleName];
210
211
  }
212
+ setLocaleErrors(localeErrors, localeDefault) {
213
+ return zodSetLocaleErrors(this.app, localeErrors, localeDefault);
214
+ }
211
215
  }
212
216
  export function compose(chains, adapter) {
213
217
  return _compose(chains, adapter);
@@ -0,0 +1,9 @@
1
+ import type { ILocaleInfos } from 'vona';
2
+ import type z from 'zod';
3
+ import type { VonaApplication } from '../core/application.ts';
4
+ export type ZodLocaleError = () => {
5
+ localeError: z.core.$ZodErrorMap;
6
+ };
7
+ export type ZodLocaleErrors = Record<keyof ILocaleInfos, ZodLocaleError>;
8
+ export declare function zodEnhance(app: VonaApplication): void;
9
+ export declare function zodSetLocaleErrors(app: VonaApplication, localeErrors: ZodLocaleErrors, localeDefault?: string): void;
@@ -0,0 +1,14 @@
1
+ import { setLocaleAdapter, setLocaleErrors } from '@cabloy/zod-errors-custom';
2
+ import { ZodMetadata } from '@cabloy/zod-openapi';
3
+ import { setParseAdapter } from '@cabloy/zod-query';
4
+ export function zodEnhance(app) {
5
+ setLocaleAdapter((text, ...args) => {
6
+ return app.meta.text(text, ...args);
7
+ });
8
+ setParseAdapter(ZodMetadata);
9
+ }
10
+ export function zodSetLocaleErrors(app, localeErrors, localeDefault) {
11
+ setLocaleErrors(() => {
12
+ return app.meta.locale.locale;
13
+ }, localeErrors, localeDefault);
14
+ }
@@ -1 +1 @@
1
- export {};
1
+ export declare function zodExtendOpenApi(): void;
@@ -1,3 +1,27 @@
1
- import { extendZodWithOpenApi } from '@asteasolutions/zod-to-openapi';
1
+ import { hashkey, isNil } from '@cabloy/utils';
2
+ import { ZodMetadata } from '@cabloy/zod-openapi';
3
+ import { extendZodWithOpenApi } from '@cabloy/zod-to-openapi';
2
4
  import { z } from 'zod';
3
- extendZodWithOpenApi(z);
5
+ import { deepExtend } from "./util.js";
6
+ export function zodExtendOpenApi() {
7
+ extendZodWithOpenApi(z);
8
+ const openapiOriginal = z.ZodType.prototype.openapi;
9
+ z.ZodType.prototype.openapi = function (...args) {
10
+ // refId
11
+ if (typeof args[0] === 'string')
12
+ return openapiOriginal.call(this, ...args);
13
+ const refId = ZodMetadata.getRefId(this);
14
+ if (!refId)
15
+ return openapiOriginal.call(this, ...args);
16
+ // metadata
17
+ const metadata = args[0];
18
+ if (isNil(metadata))
19
+ return openapiOriginal.call(this, ...args);
20
+ const options = args[1];
21
+ // refId: update
22
+ const refIdNew = `${refId}_${hashkey(metadata)}`;
23
+ const metadataOld = ZodMetadata.getOpenapiMetadata(this);
24
+ const metadataNew = deepExtend({}, metadataOld, metadata);
25
+ return openapiOriginal.call(this, refIdNew, metadataNew, options);
26
+ };
27
+ }
@@ -15,6 +15,7 @@ export interface ContextBase {
15
15
  set ctxCaller(value: VonaContext);
16
16
  getController: () => Constructable | undefined;
17
17
  getControllerPrototype: () => object | undefined;
18
+ getControllerBean: <T = any>() => T | undefined;
18
19
  getControllerBeanFullName: () => string | undefined;
19
20
  getHandler: () => Function | undefined;
20
21
  getHandlerName: () => MetadataKey | undefined;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vona-core",
3
3
  "type": "module",
4
- "version": "5.0.48",
4
+ "version": "5.0.50",
5
5
  "description": "vona",
6
6
  "publishConfig": {
7
7
  "access": "public"
@@ -25,7 +25,6 @@
25
25
  "dist"
26
26
  ],
27
27
  "dependencies": {
28
- "@asteasolutions/zod-to-openapi": "^7.3.0",
29
28
  "@cabloy/compose": "^2.0.14",
30
29
  "@cabloy/extend": "^3.1.10",
31
30
  "@cabloy/json5": "^1.0.18",
@@ -35,6 +34,10 @@
35
34
  "@cabloy/set": "^1.0.17",
36
35
  "@cabloy/utils": "^1.0.39",
37
36
  "@cabloy/word-utils": "^2.0.1",
37
+ "@cabloy/zod-errors-custom": "^2.0.2",
38
+ "@cabloy/zod-openapi": "^1.0.1",
39
+ "@cabloy/zod-query": "^2.0.1",
40
+ "@cabloy/zod-to-openapi": "^8.1.1",
38
41
  "chalk": "^5.3.0",
39
42
  "fs-extra": "^10.1.0",
40
43
  "koa": "^3.0.0",
@@ -44,7 +47,7 @@
44
47
  "uuid": "^11.0.3",
45
48
  "winston": "^3.17.0",
46
49
  "winston-daily-rotate-file": "^5.0.0",
47
- "zod": "^3.25.58"
50
+ "zod": "^4.1.5"
48
51
  },
49
52
  "gitHead": "0eab9dc4a5622caffe89e7b1b3f02c08ccbc4c4b",
50
53
  "scripts": {