@common-stack/server-core 7.2.1-alpha.31 → 7.2.1-alpha.33
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/lib/index.d.ts +1 -0
- package/lib/index.js +1 -1
- package/lib/moleculer-generation/__tests__/moleculerEventHandler.test.d.ts +5 -0
- package/lib/moleculer-generation/__tests__/serviceGenerationUtils.test.d.ts +5 -0
- package/lib/moleculer-generation/__tests__/typedMoleculerService.test.d.ts +5 -0
- package/lib/moleculer-generation/__tests__/typedProxyService.test.d.ts +5 -0
- package/lib/moleculer-generation/index.d.ts +16 -0
- package/lib/moleculer-generation/moleculerEventHandler.d.ts +73 -0
- package/lib/moleculer-generation/moleculerEventHandler.js +91 -0
- package/lib/moleculer-generation/moleculerEventHandler.js.map +1 -0
- package/lib/moleculer-generation/serviceGenerationUtils.d.ts +99 -0
- package/lib/moleculer-generation/serviceGenerationUtils.js +146 -0
- package/lib/moleculer-generation/serviceGenerationUtils.js.map +1 -0
- package/lib/moleculer-generation/typedMoleculerService.d.ts +491 -0
- package/lib/moleculer-generation/typedMoleculerService.js +653 -0
- package/lib/moleculer-generation/typedMoleculerService.js.map +1 -0
- package/lib/moleculer-generation/typedProxyService.d.ts +116 -0
- package/lib/moleculer-generation/typedProxyService.js +205 -0
- package/lib/moleculer-generation/typedProxyService.js.map +1 -0
- package/lib/moleculer-generation/zodToMoleculer.d.ts +43 -0
- package/lib/moleculer-generation/zodToMoleculer.js +114 -0
- package/lib/moleculer-generation/zodToMoleculer.js.map +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file typed-proxy-service.ts
|
|
3
|
+
* @description Provides utilities for auto-generating proxy service methods
|
|
4
|
+
* with type-safe Zod schema validation from service interfaces.
|
|
5
|
+
*
|
|
6
|
+
* This allows proxy services to be lightweight and automatically stay in sync
|
|
7
|
+
* with their corresponding Moleculer services.
|
|
8
|
+
*/
|
|
9
|
+
import type { ServiceBroker } from 'moleculer';
|
|
10
|
+
import type { CdmLogger } from '@cdm-logger/core';
|
|
11
|
+
import type { z } from 'zod';
|
|
12
|
+
import { type ExcludedServiceMethods } from './serviceGenerationUtils';
|
|
13
|
+
/**
|
|
14
|
+
* Exclude base service CRUD methods and lifecycle methods from auto-generation
|
|
15
|
+
* @deprecated Use ExcludedServiceMethods from serviceGenerationUtils instead
|
|
16
|
+
*/
|
|
17
|
+
type ExcludedProxyMethods = ExcludedServiceMethods;
|
|
18
|
+
/**
|
|
19
|
+
* Extract all method names from a service interface (excluding base methods)
|
|
20
|
+
*/
|
|
21
|
+
export type ProxyServiceMethods<T> = {
|
|
22
|
+
[K in keyof T]: T[K] extends Function ? (K extends ExcludedProxyMethods ? never : K) : never;
|
|
23
|
+
}[keyof T];
|
|
24
|
+
/**
|
|
25
|
+
* Configuration for auto-generating proxy service methods
|
|
26
|
+
*/
|
|
27
|
+
export type AutoProxyServiceConfig<TService> = {
|
|
28
|
+
/**
|
|
29
|
+
* Zod schemas for method parameters
|
|
30
|
+
* Maps method names to their Zod schema objects
|
|
31
|
+
*/
|
|
32
|
+
schemas: Partial<{
|
|
33
|
+
[K in ProxyServiceMethods<TService>]: z.ZodObject<any>;
|
|
34
|
+
}>;
|
|
35
|
+
/**
|
|
36
|
+
* Optional: Override the action name for specific methods
|
|
37
|
+
* By default, action name is the method name
|
|
38
|
+
*/
|
|
39
|
+
actionNameOverrides?: Partial<{
|
|
40
|
+
[K in ProxyServiceMethods<TService>]: string;
|
|
41
|
+
}>;
|
|
42
|
+
/**
|
|
43
|
+
* Optional: Include only specific methods
|
|
44
|
+
* If not specified, all non-excluded methods are included
|
|
45
|
+
*/
|
|
46
|
+
include?: Array<ProxyServiceMethods<TService>>;
|
|
47
|
+
/**
|
|
48
|
+
* Optional: Exclude specific methods
|
|
49
|
+
* Takes precedence over include
|
|
50
|
+
*/
|
|
51
|
+
exclude?: Array<ProxyServiceMethods<TService>>;
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Auto-generate proxy service methods from service interface and Zod schemas
|
|
55
|
+
* Returns the proxy instance cast to the service type for type safety
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```typescript
|
|
59
|
+
* export class TagProxyService extends BaseProxyService2<ITagModel> {
|
|
60
|
+
* protected logger: CdmLogger.ILogger;
|
|
61
|
+
* protected topic = MoleculerTopics.Tag;
|
|
62
|
+
*
|
|
63
|
+
* constructor(
|
|
64
|
+
* @inject(SERVER_TYPES.ITagRepository) protected tagRepository: ITagRepository,
|
|
65
|
+
* @inject(CommonType.MOLECULER_BROKER) broker: ServiceBroker,
|
|
66
|
+
* @inject('Logger') logger: CdmLogger.ILogger,
|
|
67
|
+
* ) {
|
|
68
|
+
* super(broker, logger);
|
|
69
|
+
* this.logger = logger.child({ className: 'TagProxyService' });
|
|
70
|
+
*
|
|
71
|
+
* // 🎉 Auto-generate all proxy methods - just pass interface type and schemas!
|
|
72
|
+
* return generateProxyMethods<ITagService>(this, TagServiceSchemas);
|
|
73
|
+
* }
|
|
74
|
+
* }
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
export declare function generateProxyMethods<TService>(proxyInstance: any, schemasOrConfig: Partial<{
|
|
78
|
+
[K in ProxyServiceMethods<TService>]: z.ZodObject<any>;
|
|
79
|
+
}> | AutoProxyServiceConfig<TService>, broker: ServiceBroker, logger: CdmLogger.ILogger): asserts proxyInstance is TService;
|
|
80
|
+
/**
|
|
81
|
+
* Namespace for proxy service utilities
|
|
82
|
+
*/
|
|
83
|
+
export declare namespace ProxyService {
|
|
84
|
+
/**
|
|
85
|
+
* Generate proxy methods from service interface and Zod schemas
|
|
86
|
+
*/
|
|
87
|
+
const generate: typeof generateProxyMethods;
|
|
88
|
+
/**
|
|
89
|
+
* Debug utility: List all methods that would be generated
|
|
90
|
+
*/
|
|
91
|
+
function debugProxyMethods<TService>(servicePrototype: any, config: AutoProxyServiceConfig<TService>): {
|
|
92
|
+
methodName: string;
|
|
93
|
+
hasSchema: boolean;
|
|
94
|
+
paramNames: string[];
|
|
95
|
+
isIncluded: boolean;
|
|
96
|
+
isExcluded: boolean;
|
|
97
|
+
}[];
|
|
98
|
+
/**
|
|
99
|
+
* Print debug information about proxy methods
|
|
100
|
+
*/
|
|
101
|
+
function printProxyMethods<TService>(servicePrototype: any, config: AutoProxyServiceConfig<TService>, options?: {
|
|
102
|
+
onlyMissingSchemas?: boolean;
|
|
103
|
+
onlyIncluded?: boolean;
|
|
104
|
+
}): void;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Helper type to create a fully typed proxy service class
|
|
108
|
+
* This ensures the proxy implements all methods from the service interface
|
|
109
|
+
*/
|
|
110
|
+
export type TypedProxyService<TService> = TService & {
|
|
111
|
+
broker: ServiceBroker;
|
|
112
|
+
logger: CdmLogger.ILogger;
|
|
113
|
+
topic: string;
|
|
114
|
+
callAction<T, P = unknown>(command: string, params?: P): Promise<T>;
|
|
115
|
+
};
|
|
116
|
+
export {};
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import {isExcludedMethod,getAllMethodNames,getActionName,buildActionPath}from'./serviceGenerationUtils.js';// from package: store-mongo
|
|
2
|
+
/**
|
|
3
|
+
* @file typed-proxy-service.ts
|
|
4
|
+
* @description Provides utilities for auto-generating proxy service methods
|
|
5
|
+
* with type-safe Zod schema validation from service interfaces.
|
|
6
|
+
*
|
|
7
|
+
* This allows proxy services to be lightweight and automatically stay in sync
|
|
8
|
+
* with their corresponding Moleculer services.
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Extract parameter names from a Zod schema object
|
|
12
|
+
*/
|
|
13
|
+
function extractParamNamesFromZodSchema(schema) {
|
|
14
|
+
try {
|
|
15
|
+
const { shape } = schema;
|
|
16
|
+
return Object.keys(shape);
|
|
17
|
+
}
|
|
18
|
+
catch (error) {
|
|
19
|
+
return [];
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Add base proxy functionality to an instance
|
|
24
|
+
* Provides the callAction method that all proxy services need
|
|
25
|
+
*/
|
|
26
|
+
function addProxyBase(proxyInstance, broker, logger) {
|
|
27
|
+
// Add broker and logger
|
|
28
|
+
Object.defineProperty(proxyInstance, 'broker', {
|
|
29
|
+
value: broker,
|
|
30
|
+
writable: false,
|
|
31
|
+
enumerable: false,
|
|
32
|
+
configurable: false,
|
|
33
|
+
});
|
|
34
|
+
Object.defineProperty(proxyInstance, 'logger', {
|
|
35
|
+
value: logger,
|
|
36
|
+
writable: true,
|
|
37
|
+
enumerable: false,
|
|
38
|
+
configurable: false,
|
|
39
|
+
});
|
|
40
|
+
// Add callAction method
|
|
41
|
+
// eslint-disable-next-line no-param-reassign
|
|
42
|
+
proxyInstance.callAction = function callAction(command, params) {
|
|
43
|
+
const { topic } = this;
|
|
44
|
+
// Use shared utility for consistent action path building
|
|
45
|
+
const actionPath = buildActionPath(topic, command);
|
|
46
|
+
this.logger?.trace?.(`[ProxyService] Calling action: ${actionPath}`, { params });
|
|
47
|
+
return broker.call(actionPath, params);
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Auto-generate proxy service methods from service interface and Zod schemas
|
|
52
|
+
* Returns the proxy instance cast to the service type for type safety
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```typescript
|
|
56
|
+
* export class TagProxyService extends BaseProxyService2<ITagModel> {
|
|
57
|
+
* protected logger: CdmLogger.ILogger;
|
|
58
|
+
* protected topic = MoleculerTopics.Tag;
|
|
59
|
+
*
|
|
60
|
+
* constructor(
|
|
61
|
+
* @inject(SERVER_TYPES.ITagRepository) protected tagRepository: ITagRepository,
|
|
62
|
+
* @inject(CommonType.MOLECULER_BROKER) broker: ServiceBroker,
|
|
63
|
+
* @inject('Logger') logger: CdmLogger.ILogger,
|
|
64
|
+
* ) {
|
|
65
|
+
* super(broker, logger);
|
|
66
|
+
* this.logger = logger.child({ className: 'TagProxyService' });
|
|
67
|
+
*
|
|
68
|
+
* // 🎉 Auto-generate all proxy methods - just pass interface type and schemas!
|
|
69
|
+
* return generateProxyMethods<ITagService>(this, TagServiceSchemas);
|
|
70
|
+
* }
|
|
71
|
+
* }
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
function generateProxyMethods(proxyInstance, schemasOrConfig, broker, logger) {
|
|
75
|
+
// Normalize input - support both schemas object directly or config object
|
|
76
|
+
const config = 'schemas' in schemasOrConfig
|
|
77
|
+
? schemasOrConfig
|
|
78
|
+
: { schemas: schemasOrConfig };
|
|
79
|
+
const { schemas, include, exclude, actionNameOverrides } = config;
|
|
80
|
+
// Add base proxy functionality (broker, logger, callAction)
|
|
81
|
+
addProxyBase(proxyInstance, broker, logger);
|
|
82
|
+
// Get all method names from the schemas object (primary source of truth)
|
|
83
|
+
// This ensures ALL schema-defined methods are generated
|
|
84
|
+
const schemaMethodNames = Object.keys(schemas);
|
|
85
|
+
// Filter methods
|
|
86
|
+
const methodsToGenerate = schemaMethodNames.filter((methodName) => {
|
|
87
|
+
// Skip excluded base methods
|
|
88
|
+
if (isExcludedMethod(methodName)) {
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
// Skip if explicitly excluded
|
|
92
|
+
if (exclude && exclude.includes(methodName)) {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
// If include list is provided, only include those
|
|
96
|
+
if (include && !include.includes(methodName)) {
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
return true;
|
|
100
|
+
});
|
|
101
|
+
// Generate methods
|
|
102
|
+
for (const methodName of methodsToGenerate) {
|
|
103
|
+
const schema = schemas[methodName];
|
|
104
|
+
if (!schema) {
|
|
105
|
+
// eslint-disable-next-line no-continue
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
// Determine action name using shared utility for consistency
|
|
109
|
+
// Convention: Use camelCase (method name as-is) to match Moleculer service actions
|
|
110
|
+
let actionName;
|
|
111
|
+
if (actionNameOverrides && actionNameOverrides[methodName]) {
|
|
112
|
+
actionName = actionNameOverrides[methodName];
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
// Use shared utility to ensure consistency with Moleculer service
|
|
116
|
+
actionName = getActionName(methodName);
|
|
117
|
+
}
|
|
118
|
+
// Extract parameter names from Zod schema
|
|
119
|
+
const paramNames = extractParamNamesFromZodSchema(schema);
|
|
120
|
+
// Generate the proxy method
|
|
121
|
+
// eslint-disable-next-line no-param-reassign, func-names
|
|
122
|
+
proxyInstance[methodName] = function proxyMethod(...args) {
|
|
123
|
+
// Build params object from arguments
|
|
124
|
+
const params = {};
|
|
125
|
+
if (paramNames.length === 1 && args.length === 1 && typeof args[0] === 'object') {
|
|
126
|
+
// If single param and single object argument, use it directly
|
|
127
|
+
Object.assign(params, args[0]);
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
// Map positional arguments to parameter names
|
|
131
|
+
paramNames.forEach((paramName, index) => {
|
|
132
|
+
if (index < args.length) {
|
|
133
|
+
params[paramName] = args[index];
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
// Validate with Zod schema (optional - can be skipped for performance)
|
|
138
|
+
try {
|
|
139
|
+
schema.parse(params);
|
|
140
|
+
}
|
|
141
|
+
catch (error) {
|
|
142
|
+
console.warn(`[ProxyService] Zod validation failed for ${methodName}:`, error);
|
|
143
|
+
// Continue anyway - let Moleculer service handle validation
|
|
144
|
+
}
|
|
145
|
+
// Call the Moleculer action
|
|
146
|
+
return this.callAction(actionName, params);
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Namespace for proxy service utilities
|
|
152
|
+
*/
|
|
153
|
+
var ProxyService;
|
|
154
|
+
(function (ProxyService) {
|
|
155
|
+
/**
|
|
156
|
+
* Generate proxy methods from service interface and Zod schemas
|
|
157
|
+
*/
|
|
158
|
+
ProxyService.generate = generateProxyMethods;
|
|
159
|
+
/**
|
|
160
|
+
* Debug utility: List all methods that would be generated
|
|
161
|
+
*/
|
|
162
|
+
function debugProxyMethods(servicePrototype, config) {
|
|
163
|
+
const { schemas, include, exclude } = config;
|
|
164
|
+
const allMethods = getAllMethodNames(servicePrototype);
|
|
165
|
+
return allMethods
|
|
166
|
+
.filter((methodName) => !isExcludedMethod(methodName))
|
|
167
|
+
.map((methodName) => {
|
|
168
|
+
const schema = schemas[methodName];
|
|
169
|
+
const paramNames = schema ? extractParamNamesFromZodSchema(schema) : [];
|
|
170
|
+
return {
|
|
171
|
+
methodName,
|
|
172
|
+
hasSchema: !!schema,
|
|
173
|
+
paramNames,
|
|
174
|
+
isIncluded: !include || include.includes(methodName),
|
|
175
|
+
isExcluded: !!exclude && exclude.includes(methodName),
|
|
176
|
+
};
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
ProxyService.debugProxyMethods = debugProxyMethods;
|
|
180
|
+
/**
|
|
181
|
+
* Print debug information about proxy methods
|
|
182
|
+
*/
|
|
183
|
+
function printProxyMethods(servicePrototype, config, options) {
|
|
184
|
+
const methods = debugProxyMethods(servicePrototype, config);
|
|
185
|
+
const filtered = methods.filter((m) => {
|
|
186
|
+
if (options?.onlyMissingSchemas && m.hasSchema)
|
|
187
|
+
return false;
|
|
188
|
+
if (options?.onlyIncluded && !m.isIncluded)
|
|
189
|
+
return false;
|
|
190
|
+
return true;
|
|
191
|
+
});
|
|
192
|
+
console.log('\n=== Proxy Service Methods Debug ===\n');
|
|
193
|
+
filtered.forEach((m) => {
|
|
194
|
+
console.log(`${m.methodName}:`);
|
|
195
|
+
console.log(` - Has Schema: ${m.hasSchema}`);
|
|
196
|
+
console.log(` - Included: ${m.isIncluded}`);
|
|
197
|
+
console.log(` - Excluded: ${m.isExcluded}`);
|
|
198
|
+
if (m.paramNames.length > 0) {
|
|
199
|
+
console.log(` - Params: ${m.paramNames.join(', ')}`);
|
|
200
|
+
}
|
|
201
|
+
console.log('');
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
ProxyService.printProxyMethods = printProxyMethods;
|
|
205
|
+
})(ProxyService || (ProxyService = {}));export{ProxyService,generateProxyMethods};//# sourceMappingURL=typedProxyService.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"typedProxyService.js","sources":["../../src/moleculer-generation/typedProxyService.ts"],"sourcesContent":[null],"names":[],"mappings":"2GAAA;AACA;;;;;;;AAOG;AAgEH;;AAEG;AACH,SAAS,8BAA8B,CAAC,MAAwB,EAAA;AAC5D,IAAA,IAAI;AACA,QAAA,MAAM,EAAE,KAAK,EAAE,GAAG,MAAM,CAAC;AACzB,QAAA,OAAO,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;KAC7B;IAAC,OAAO,KAAK,EAAE;AACZ,QAAA,OAAO,EAAE,CAAC;KACb;AACL,CAAC;AAED;;;AAGG;AACH,SAAS,YAAY,CAAC,aAAkB,EAAE,MAAqB,EAAE,MAAyB,EAAA;;AAEtF,IAAA,MAAM,CAAC,cAAc,CAAC,aAAa,EAAE,QAAQ,EAAE;AAC3C,QAAA,KAAK,EAAE,MAAM;AACb,QAAA,QAAQ,EAAE,KAAK;AACf,QAAA,UAAU,EAAE,KAAK;AACjB,QAAA,YAAY,EAAE,KAAK;AACtB,KAAA,CAAC,CAAC;AAEH,IAAA,MAAM,CAAC,cAAc,CAAC,aAAa,EAAE,QAAQ,EAAE;AAC3C,QAAA,KAAK,EAAE,MAAM;AACb,QAAA,QAAQ,EAAE,IAAI;AACd,QAAA,UAAU,EAAE,KAAK;AACjB,QAAA,YAAY,EAAE,KAAK;AACtB,KAAA,CAAC,CAAC;;;IAIH,aAAa,CAAC,UAAU,GAAG,SAAS,UAAU,CAA4B,OAAe,EAAE,MAAU,EAAA;AACjG,QAAA,MAAM,EAAE,KAAK,EAAE,GAAG,IAAI,CAAC;;QAEvB,MAAM,UAAU,GAAG,eAAe,CAAC,KAAK,EAAE,OAAO,CAAC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SAAS;AAAE,gBAAA,OAAO,KAAK,CAAC;AAC7D,YAAA,IAAI,OAAO,EAAE,YAAY,IAAI,CAAC,CAAC,CAAC,UAAU;AAAE,gBAAA,OAAO,KAAK,CAAC;AACzD,YAAA,OAAO,IAAI,CAAC;AAChB,SAAC,CAAC,CAAC;AAEH,QAAA,OAAO,CAAC,GAAG,CAAC,yCAAyC,CAAC,CAAC;AACvD,QAAA,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,KAAI;YACnB,OAAO,CAAC,GAAG,CAAC,CAAA,EAAG,CAAC,CAAC,UAAU,CAAG,CAAA,CAAA,CAAC,CAAC;YAChC,OAAO,CAAC,GAAG,CAAC,CAAA,gBAAA,EAAmB,CAAC,CAAC,SAAS,CAAE,CAAA,CAAC,CAAC;YAC9C,OAAO,CAAC,GAAG,CAAC,CAAA,cAAA,EAAiB,CAAC,CAAC,UAAU,CAAE,CAAA,CAAC,CAAC;YAC7C,OAAO,CAAC,GAAG,CAAC,CAAA,cAAA,EAAiB,CAAC,CAAC,UAAU,CAAE,CAAA,CAAC,CAAC;YAC7C,IAAI,CAAC,CAAC,UAAU,CAAC,MAAM,GAAG,CAAC,EAAE;AACzB,gBAAA,OAAO,CAAC,GAAG,CAAC,CAAA,YAAA,EAAe,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA,CAAE,CAAC,CAAC;aACzD;AACD,YAAA,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;AACpB,SAAC,CAAC,CAAC;KACN;AA1Be,IAAA,YAAA,CAAA,iBAAiB,oBA0BhC,CAAA;AACL,CAAC,EApEgB,YAAY,KAAZ,YAAY,GAoE5B,EAAA,CAAA,CAAA"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts Zod schemas to Moleculer parameter validation schemas
|
|
3
|
+
*
|
|
4
|
+
* This utility bridges the gap between Zod's rich type system and Moleculer's
|
|
5
|
+
* validator format, allowing us to use TypeScript-derived Zod schemas for
|
|
6
|
+
* runtime parameter validation in Moleculer services.
|
|
7
|
+
*/
|
|
8
|
+
import { z } from 'zod';
|
|
9
|
+
/**
|
|
10
|
+
* Converts a Zod schema object to Moleculer parameter schema format
|
|
11
|
+
*
|
|
12
|
+
* @param zodSchema - Zod object schema with parameter definitions
|
|
13
|
+
* @returns Moleculer-compatible parameter schema
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const zodSchema = z.object({
|
|
18
|
+
* email: z.string(),
|
|
19
|
+
* age: z.number().optional(),
|
|
20
|
+
* });
|
|
21
|
+
*
|
|
22
|
+
* const moleculerParams = zodToMoleculerParams(zodSchema);
|
|
23
|
+
* // Returns: { email: 'string', age: { type: 'number', optional: true } }
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export declare function zodToMoleculerParams(zodSchema: z.ZodObject<any>): Record<string, any>;
|
|
27
|
+
/**
|
|
28
|
+
* Batch converts multiple Zod schemas to Moleculer parameter schemas
|
|
29
|
+
*
|
|
30
|
+
* @param schemas - Object mapping method names to Zod schemas
|
|
31
|
+
* @returns Object mapping method names to Moleculer parameter schemas
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* const schemas = {
|
|
36
|
+
* findById: z.object({ id: z.string() }),
|
|
37
|
+
* update: z.object({ id: z.string(), data: z.object({}) }),
|
|
38
|
+
* };
|
|
39
|
+
*
|
|
40
|
+
* const moleculerSchemas = zodSchemasToMoleculer(schemas);
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export declare function zodSchemasToMoleculer(schemas: Record<string, z.ZodObject<any>>): Record<string, Record<string, any>>;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import {z}from'zod';// from package: store-mongo
|
|
2
|
+
/**
|
|
3
|
+
* Converts Zod schemas to Moleculer parameter validation schemas
|
|
4
|
+
*
|
|
5
|
+
* This utility bridges the gap between Zod's rich type system and Moleculer's
|
|
6
|
+
* validator format, allowing us to use TypeScript-derived Zod schemas for
|
|
7
|
+
* runtime parameter validation in Moleculer services.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Converts a Zod schema object to Moleculer parameter schema format
|
|
11
|
+
*
|
|
12
|
+
* @param zodSchema - Zod object schema with parameter definitions
|
|
13
|
+
* @returns Moleculer-compatible parameter schema
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const zodSchema = z.object({
|
|
18
|
+
* email: z.string(),
|
|
19
|
+
* age: z.number().optional(),
|
|
20
|
+
* });
|
|
21
|
+
*
|
|
22
|
+
* const moleculerParams = zodToMoleculerParams(zodSchema);
|
|
23
|
+
* // Returns: { email: 'string', age: { type: 'number', optional: true } }
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
function zodToMoleculerParams(zodSchema) {
|
|
27
|
+
const { shape } = zodSchema;
|
|
28
|
+
const result = {};
|
|
29
|
+
for (const [key, value] of Object.entries(shape)) {
|
|
30
|
+
result[key] = zodTypeToMoleculer(value);
|
|
31
|
+
}
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Converts a single Zod type to Moleculer validator format
|
|
36
|
+
*/
|
|
37
|
+
function zodTypeToMoleculer(zodType) {
|
|
38
|
+
// Handle optional types
|
|
39
|
+
if (zodType instanceof z.ZodOptional) {
|
|
40
|
+
const innerType = zodTypeToMoleculer(zodType.def.innerType);
|
|
41
|
+
if (typeof innerType === 'string') {
|
|
42
|
+
return { type: innerType, optional: true };
|
|
43
|
+
}
|
|
44
|
+
return { ...innerType, optional: true };
|
|
45
|
+
}
|
|
46
|
+
// Handle nullable types
|
|
47
|
+
if (zodType instanceof z.ZodNullable) {
|
|
48
|
+
return zodTypeToMoleculer(zodType.def.innerType);
|
|
49
|
+
}
|
|
50
|
+
// Primitive types
|
|
51
|
+
if (zodType instanceof z.ZodString) {
|
|
52
|
+
return 'string';
|
|
53
|
+
}
|
|
54
|
+
if (zodType instanceof z.ZodNumber) {
|
|
55
|
+
return 'number';
|
|
56
|
+
}
|
|
57
|
+
if (zodType instanceof z.ZodBoolean) {
|
|
58
|
+
return 'boolean';
|
|
59
|
+
}
|
|
60
|
+
if (zodType instanceof z.ZodDate) {
|
|
61
|
+
return 'date';
|
|
62
|
+
}
|
|
63
|
+
// Array types
|
|
64
|
+
if (zodType instanceof z.ZodArray) {
|
|
65
|
+
return 'array';
|
|
66
|
+
}
|
|
67
|
+
// Enum types
|
|
68
|
+
if (zodType instanceof z.ZodEnum || zodType.type === 'enum') {
|
|
69
|
+
return 'string'; // Moleculer doesn't have enum type, use string
|
|
70
|
+
}
|
|
71
|
+
// Object types
|
|
72
|
+
if (zodType instanceof z.ZodObject) {
|
|
73
|
+
return 'object';
|
|
74
|
+
}
|
|
75
|
+
// Union types - use the first non-undefined/null type
|
|
76
|
+
if (zodType instanceof z.ZodUnion) {
|
|
77
|
+
const { options } = zodType.def;
|
|
78
|
+
for (const option of options) {
|
|
79
|
+
if (!(option instanceof z.ZodUndefined) && !(option instanceof z.ZodNull)) {
|
|
80
|
+
return zodTypeToMoleculer(option);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return 'any';
|
|
84
|
+
}
|
|
85
|
+
// Any type
|
|
86
|
+
if (zodType instanceof z.ZodAny) {
|
|
87
|
+
return 'any';
|
|
88
|
+
}
|
|
89
|
+
// Default to object for complex types
|
|
90
|
+
return 'object';
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Batch converts multiple Zod schemas to Moleculer parameter schemas
|
|
94
|
+
*
|
|
95
|
+
* @param schemas - Object mapping method names to Zod schemas
|
|
96
|
+
* @returns Object mapping method names to Moleculer parameter schemas
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```typescript
|
|
100
|
+
* const schemas = {
|
|
101
|
+
* findById: z.object({ id: z.string() }),
|
|
102
|
+
* update: z.object({ id: z.string(), data: z.object({}) }),
|
|
103
|
+
* };
|
|
104
|
+
*
|
|
105
|
+
* const moleculerSchemas = zodSchemasToMoleculer(schemas);
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
function zodSchemasToMoleculer(schemas) {
|
|
109
|
+
const result = {};
|
|
110
|
+
for (const [methodName, schema] of Object.entries(schemas)) {
|
|
111
|
+
result[methodName] = zodToMoleculerParams(schema);
|
|
112
|
+
}
|
|
113
|
+
return result;
|
|
114
|
+
}export{zodSchemasToMoleculer,zodToMoleculerParams};//# sourceMappingURL=zodToMoleculer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zodToMoleculer.js","sources":["../../src/moleculer-generation/zodToMoleculer.ts"],"sourcesContent":[null],"names":[],"mappings":"oBAAA;AACA;;;;;;AAMG;AAIH;;;;;;;;;;;;;;;;AAgBG;AACG,SAAU,oBAAoB,CAAC,SAA2B,EAAA;AAC5D,IAAA,MAAM,EAAE,KAAK,EAAE,GAAG,SAAS,CAAC;IAC5B,MAAM,MAAM,GAAwB,EAAE,CAAC;AAEvC,IAAA,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE;QAC9C,MAAM,CAAC,GAAG,CAAC,GAAG,kBAAkB,CAAC,KAAqB,CAAC,CAAC;KAC3D;AAED,IAAA,OAAO,MAAM,CAAC;AAClB,CAAC;AAED;;AAEG;AACH,SAAS,kBAAkB,CAAC,OAAqB,EAAA;;AAE7C,IAAA,IAAI,OAAO,YAAY,CAAC,CAAC,WAAW,EAAE;QAClC,MAAM,SAAS,GAAG,kBAAkB,CAAC,OAAO,CAAC,GAAG,CAAC,SAAyB,CAAC,CAAC;AAC5E,QAAA,IAAI,OAAO,SAAS,KAAK,QAAQ,EAAE;YAC/B,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;SA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;AACD,QAAA,OAAO,KAAK,CAAC;KAChB;;AAGD,IAAA,IAAI,OAAO,YAAY,CAAC,CAAC,MAAM,EAAE;AAC7B,QAAA,OAAO,KAAK,CAAC;KAChB;;AAGD,IAAA,OAAO,QAAQ,CAAC;AACpB,CAAC;AAED;;;;;;;;;;;;;;;AAeG;AACG,SAAU,qBAAqB,CACjC,OAAyC,EAAA;IAEzC,MAAM,MAAM,GAAwC,EAAE,CAAC;AAEvD,IAAA,KAAK,MAAM,CAAC,UAAU,EAAE,MAAM,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE;QACxD,MAAM,CAAC,UAAU,CAAC,GAAG,oBAAoB,CAAC,MAAM,CAAC,CAAC;KACrD;AAED,IAAA,OAAO,MAAM,CAAC;AAClB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@common-stack/server-core",
|
|
3
|
-
"version": "7.2.1-alpha.
|
|
3
|
+
"version": "7.2.1-alpha.33",
|
|
4
4
|
"description": "common core for higher packages to depend on",
|
|
5
5
|
"license": "ISC",
|
|
6
6
|
"author": "CDMBase LLC",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"publishConfig": {
|
|
34
34
|
"access": "public"
|
|
35
35
|
},
|
|
36
|
-
"gitHead": "
|
|
36
|
+
"gitHead": "a3dd530984e7d5e278d5ee6867afc5df393eb1de",
|
|
37
37
|
"typescript": {
|
|
38
38
|
"definition": "lib/index.d.ts"
|
|
39
39
|
}
|