imean-service-engine 1.2.1 → 1.2.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.
- package/dist/mod.cjs +17 -10
- package/dist/mod.d.cts +4 -3
- package/dist/mod.d.ts +4 -3
- package/dist/mod.js +17 -10
- package/package.json +1 -1
package/dist/mod.cjs
CHANGED
@@ -284,6 +284,9 @@ function getZodTypeString(schema, defaultOptional = false) {
|
|
284
284
|
case "ZodObject": {
|
285
285
|
const shape = def.shape();
|
286
286
|
const props = Object.entries(shape).map(([key, value]) => {
|
287
|
+
if (key.includes("-")) {
|
288
|
+
key = `'${key}'`;
|
289
|
+
}
|
287
290
|
const fieldDef = value._def;
|
288
291
|
const isOptional = fieldDef.typeName === "ZodOptional";
|
289
292
|
const isDefault = defaultOptional && fieldDef.typeName === "ZodDefault";
|
@@ -310,10 +313,14 @@ function getZodTypeString(schema, defaultOptional = false) {
|
|
310
313
|
return "void";
|
311
314
|
}
|
312
315
|
case "ZodRecord": {
|
313
|
-
return `Record<${processType(def.keyType)}, ${processType(
|
316
|
+
return `Record<${processType(def.keyType)}, ${processType(
|
317
|
+
def.valueType
|
318
|
+
)}>`;
|
314
319
|
}
|
315
320
|
case "ZodMap": {
|
316
|
-
return `Map<${processType(def.keyType)}, ${processType(
|
321
|
+
return `Map<${processType(def.keyType)}, ${processType(
|
322
|
+
def.valueType
|
323
|
+
)}>`;
|
317
324
|
}
|
318
325
|
case "ZodAny": {
|
319
326
|
return "any";
|
@@ -354,7 +361,10 @@ async function generateClientCode(modules) {
|
|
354
361
|
}
|
355
362
|
const params = action.params.map((param, index) => {
|
356
363
|
const name2 = param.description || `arg${index}`;
|
357
|
-
return `${name2}${param.isOptional() ? "?" : ""}: ${getZodTypeString(
|
364
|
+
return `${name2}${param.isOptional() ? "?" : ""}: ${getZodTypeString(
|
365
|
+
param,
|
366
|
+
true
|
367
|
+
)}`;
|
358
368
|
}).join(", ");
|
359
369
|
const returnType = action.returns ? getZodTypeString(action.returns) : "void";
|
360
370
|
return `
|
@@ -378,11 +388,7 @@ async function generateClientCode(modules) {
|
|
378
388
|
});`;
|
379
389
|
}).join("\n\n ")}
|
380
390
|
}`;
|
381
|
-
return await formatCode([
|
382
|
-
imports,
|
383
|
-
interfaces,
|
384
|
-
clientClass
|
385
|
-
].join("\n\n"));
|
391
|
+
return await formatCode([imports, interfaces, clientClass].join("\n\n"));
|
386
392
|
}
|
387
393
|
function hashText(text) {
|
388
394
|
const hash = crypto2__default.default.createHash("sha256");
|
@@ -477,8 +483,10 @@ var ActionHandler = class {
|
|
477
483
|
async _handle(req) {
|
478
484
|
const span = api.trace.getActiveSpan();
|
479
485
|
const { args, requestHash } = await this._validate(req);
|
486
|
+
const cacheHash = typeof this.metadata.cache === "function" ? this.metadata.cache(...args) : requestHash;
|
487
|
+
const cacheKey = `${this.moduleName}.${this.actionName}.${hashText(ejson3__default.default.stringify(cacheHash))}`;
|
488
|
+
span?.setAttribute("args", JSON.stringify(args));
|
480
489
|
if (!this.metadata.stream && this.metadata.cache && !this.microservice.options.disableCache) {
|
481
|
-
const cacheKey = `${this.moduleName}.${this.actionName}.${requestHash}`;
|
482
490
|
const cached = await this.microservice.cache.get(cacheKey);
|
483
491
|
const now = Date.now();
|
484
492
|
if (cached !== null && (!this.metadata.ttl || cached?.expireAt > now)) {
|
@@ -524,7 +532,6 @@ var ActionHandler = class {
|
|
524
532
|
}
|
525
533
|
}
|
526
534
|
if (this.metadata.cache && !this.microservice.options.disableCache) {
|
527
|
-
const cacheKey = `${this.moduleName}.${this.actionName}.${requestHash}`;
|
528
535
|
const now = Date.now();
|
529
536
|
this.microservice.cache.set(
|
530
537
|
cacheKey,
|
package/dist/mod.d.cts
CHANGED
@@ -6,20 +6,21 @@ import { LRUCache } from 'lru-cache';
|
|
6
6
|
export { default as dayjs } from 'dayjs';
|
7
7
|
import winston from 'winston';
|
8
8
|
|
9
|
+
type CacheFn = (...args: any[]) => any;
|
9
10
|
interface ActionOptions {
|
10
11
|
params?: z.ZodType<any>[];
|
11
12
|
returns?: z.ZodType<any>;
|
12
13
|
idempotence?: boolean;
|
13
14
|
description?: string;
|
14
15
|
printError?: boolean;
|
15
|
-
cache?: boolean;
|
16
|
+
cache?: boolean | CacheFn;
|
16
17
|
ttl?: number;
|
17
18
|
stream?: boolean;
|
18
19
|
}
|
19
20
|
interface ActionMetadata extends ActionOptions {
|
20
21
|
name: string;
|
21
22
|
idempotence: boolean;
|
22
|
-
cache?: boolean;
|
23
|
+
cache?: boolean | CacheFn;
|
23
24
|
ttl?: number;
|
24
25
|
stream?: boolean;
|
25
26
|
}
|
@@ -258,4 +259,4 @@ declare function Schedule(options: ScheduleOptions): Function;
|
|
258
259
|
|
259
260
|
declare const logger: winston.Logger;
|
260
261
|
|
261
|
-
export { Action, type ActionErrorEvent, type ActionMetadata, type ActionOptions, type EtcdConfig, type EventServiceInfo, Microservice, type MicroserviceOptions, Module, type ModuleInfo, type ModuleMetadata, type ModuleOptions, type PreStartChecker, Schedule, type ScheduleMetadata, ScheduleMode, type ScheduleOptions, ServiceContext, type ServiceInfo, type ServiceStats, type StatisticsEvent, type StreamResponse, logger, startCheck };
|
262
|
+
export { Action, type ActionErrorEvent, type ActionMetadata, type ActionOptions, type CacheFn, type EtcdConfig, type EventServiceInfo, Microservice, type MicroserviceOptions, Module, type ModuleInfo, type ModuleMetadata, type ModuleOptions, type PreStartChecker, Schedule, type ScheduleMetadata, ScheduleMode, type ScheduleOptions, ServiceContext, type ServiceInfo, type ServiceStats, type StatisticsEvent, type StreamResponse, logger, startCheck };
|
package/dist/mod.d.ts
CHANGED
@@ -6,20 +6,21 @@ import { LRUCache } from 'lru-cache';
|
|
6
6
|
export { default as dayjs } from 'dayjs';
|
7
7
|
import winston from 'winston';
|
8
8
|
|
9
|
+
type CacheFn = (...args: any[]) => any;
|
9
10
|
interface ActionOptions {
|
10
11
|
params?: z.ZodType<any>[];
|
11
12
|
returns?: z.ZodType<any>;
|
12
13
|
idempotence?: boolean;
|
13
14
|
description?: string;
|
14
15
|
printError?: boolean;
|
15
|
-
cache?: boolean;
|
16
|
+
cache?: boolean | CacheFn;
|
16
17
|
ttl?: number;
|
17
18
|
stream?: boolean;
|
18
19
|
}
|
19
20
|
interface ActionMetadata extends ActionOptions {
|
20
21
|
name: string;
|
21
22
|
idempotence: boolean;
|
22
|
-
cache?: boolean;
|
23
|
+
cache?: boolean | CacheFn;
|
23
24
|
ttl?: number;
|
24
25
|
stream?: boolean;
|
25
26
|
}
|
@@ -258,4 +259,4 @@ declare function Schedule(options: ScheduleOptions): Function;
|
|
258
259
|
|
259
260
|
declare const logger: winston.Logger;
|
260
261
|
|
261
|
-
export { Action, type ActionErrorEvent, type ActionMetadata, type ActionOptions, type EtcdConfig, type EventServiceInfo, Microservice, type MicroserviceOptions, Module, type ModuleInfo, type ModuleMetadata, type ModuleOptions, type PreStartChecker, Schedule, type ScheduleMetadata, ScheduleMode, type ScheduleOptions, ServiceContext, type ServiceInfo, type ServiceStats, type StatisticsEvent, type StreamResponse, logger, startCheck };
|
262
|
+
export { Action, type ActionErrorEvent, type ActionMetadata, type ActionOptions, type CacheFn, type EtcdConfig, type EventServiceInfo, Microservice, type MicroserviceOptions, Module, type ModuleInfo, type ModuleMetadata, type ModuleOptions, type PreStartChecker, Schedule, type ScheduleMetadata, ScheduleMode, type ScheduleOptions, ServiceContext, type ServiceInfo, type ServiceStats, type StatisticsEvent, type StreamResponse, logger, startCheck };
|
package/dist/mod.js
CHANGED
@@ -275,6 +275,9 @@ function getZodTypeString(schema, defaultOptional = false) {
|
|
275
275
|
case "ZodObject": {
|
276
276
|
const shape = def.shape();
|
277
277
|
const props = Object.entries(shape).map(([key, value]) => {
|
278
|
+
if (key.includes("-")) {
|
279
|
+
key = `'${key}'`;
|
280
|
+
}
|
278
281
|
const fieldDef = value._def;
|
279
282
|
const isOptional = fieldDef.typeName === "ZodOptional";
|
280
283
|
const isDefault = defaultOptional && fieldDef.typeName === "ZodDefault";
|
@@ -301,10 +304,14 @@ function getZodTypeString(schema, defaultOptional = false) {
|
|
301
304
|
return "void";
|
302
305
|
}
|
303
306
|
case "ZodRecord": {
|
304
|
-
return `Record<${processType(def.keyType)}, ${processType(
|
307
|
+
return `Record<${processType(def.keyType)}, ${processType(
|
308
|
+
def.valueType
|
309
|
+
)}>`;
|
305
310
|
}
|
306
311
|
case "ZodMap": {
|
307
|
-
return `Map<${processType(def.keyType)}, ${processType(
|
312
|
+
return `Map<${processType(def.keyType)}, ${processType(
|
313
|
+
def.valueType
|
314
|
+
)}>`;
|
308
315
|
}
|
309
316
|
case "ZodAny": {
|
310
317
|
return "any";
|
@@ -345,7 +352,10 @@ async function generateClientCode(modules) {
|
|
345
352
|
}
|
346
353
|
const params = action.params.map((param, index) => {
|
347
354
|
const name2 = param.description || `arg${index}`;
|
348
|
-
return `${name2}${param.isOptional() ? "?" : ""}: ${getZodTypeString(
|
355
|
+
return `${name2}${param.isOptional() ? "?" : ""}: ${getZodTypeString(
|
356
|
+
param,
|
357
|
+
true
|
358
|
+
)}`;
|
349
359
|
}).join(", ");
|
350
360
|
const returnType = action.returns ? getZodTypeString(action.returns) : "void";
|
351
361
|
return `
|
@@ -369,11 +379,7 @@ async function generateClientCode(modules) {
|
|
369
379
|
});`;
|
370
380
|
}).join("\n\n ")}
|
371
381
|
}`;
|
372
|
-
return await formatCode([
|
373
|
-
imports,
|
374
|
-
interfaces,
|
375
|
-
clientClass
|
376
|
-
].join("\n\n"));
|
382
|
+
return await formatCode([imports, interfaces, clientClass].join("\n\n"));
|
377
383
|
}
|
378
384
|
function hashText(text) {
|
379
385
|
const hash = crypto2.createHash("sha256");
|
@@ -468,8 +474,10 @@ var ActionHandler = class {
|
|
468
474
|
async _handle(req) {
|
469
475
|
const span = trace.getActiveSpan();
|
470
476
|
const { args, requestHash } = await this._validate(req);
|
477
|
+
const cacheHash = typeof this.metadata.cache === "function" ? this.metadata.cache(...args) : requestHash;
|
478
|
+
const cacheKey = `${this.moduleName}.${this.actionName}.${hashText(ejson3.stringify(cacheHash))}`;
|
479
|
+
span?.setAttribute("args", JSON.stringify(args));
|
471
480
|
if (!this.metadata.stream && this.metadata.cache && !this.microservice.options.disableCache) {
|
472
|
-
const cacheKey = `${this.moduleName}.${this.actionName}.${requestHash}`;
|
473
481
|
const cached = await this.microservice.cache.get(cacheKey);
|
474
482
|
const now = Date.now();
|
475
483
|
if (cached !== null && (!this.metadata.ttl || cached?.expireAt > now)) {
|
@@ -515,7 +523,6 @@ var ActionHandler = class {
|
|
515
523
|
}
|
516
524
|
}
|
517
525
|
if (this.metadata.cache && !this.microservice.options.disableCache) {
|
518
|
-
const cacheKey = `${this.moduleName}.${this.actionName}.${requestHash}`;
|
519
526
|
const now = Date.now();
|
520
527
|
this.microservice.cache.set(
|
521
528
|
cacheKey,
|