imean-service-engine 1.8.2 → 1.9.0

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.
Files changed (3) hide show
  1. package/dist/mod.cjs +30 -21
  2. package/dist/mod.js +30 -21
  3. package/package.json +1 -1
package/dist/mod.cjs CHANGED
@@ -72,11 +72,18 @@ function Action(options) {
72
72
  context.addInitializer(function() {
73
73
  const prototype = this.constructor.prototype;
74
74
  const func = target.toString().split("\n")[0];
75
- const params = func.slice(func.indexOf("(") + 1, func.indexOf(")")).split(",").map((p) => p.split("=").shift()).filter((param) => !!param?.trim());
76
- params.forEach((param, index) => {
77
- options.params[index] = options.params[index]?.describe(
78
- param.trim()
79
- );
75
+ const paramString = func.slice(func.indexOf("(") + 1, func.indexOf(")"));
76
+ const parsedParams = paramString.split(",").map((p) => {
77
+ const paramWithoutDefault = p.split("=")[0]?.trim();
78
+ return paramWithoutDefault;
79
+ }).filter((param) => !!param);
80
+ options.params.forEach((param, index) => {
81
+ if (index < parsedParams.length) {
82
+ const paramName = parsedParams[index].split(":")[0]?.trim();
83
+ if (paramName) {
84
+ options.params[index] = param.describe(paramName);
85
+ }
86
+ }
80
87
  });
81
88
  const existingMetadata = prototype[ACTION_METADATA] || {};
82
89
  existingMetadata[methodName] = {
@@ -397,7 +404,10 @@ async function generateClientCode(modules) {
397
404
  }
398
405
  const params = action.params.map((param, index) => {
399
406
  const name2 = param.description || `arg${index}`;
400
- return `${name2}${param.isOptional() ? "?" : ""}: ${getZodTypeString(
407
+ const paramDef = param._def;
408
+ const isOptional = param.isOptional();
409
+ const hasDefault = paramDef?.typeName === "ZodDefault";
410
+ return `${name2}${isOptional || hasDefault ? "?" : ""}: ${getZodTypeString(
401
411
  param,
402
412
  true
403
413
  )}`;
@@ -488,27 +498,25 @@ var ActionHandler = class {
488
498
  async _validate(req) {
489
499
  const span = tracer2.startSpan("validate");
490
500
  try {
491
- let args;
501
+ let paramsMap = [];
492
502
  if (typeof req === "string") {
493
503
  try {
494
- args = Object.values(ejson4__default.default.parse(req));
504
+ paramsMap = ejson4__default.default.parse(req);
495
505
  } catch (error) {
496
506
  throw new Error(`Invalid request body: ${error.message}`);
497
507
  }
498
508
  } else {
499
- args = req;
500
- }
501
- if (this.metadata.params) {
502
- args = args.map((arg, index) => {
503
- try {
504
- return this.metadata.params[index].parse(arg);
505
- } catch (error) {
506
- throw new Error(
507
- `Invalid argument ${index}: ${error.message}`
508
- );
509
- }
510
- });
509
+ paramsMap = req;
511
510
  }
511
+ const args = this.metadata.params ? this.metadata.params.map((param, index) => {
512
+ try {
513
+ return param.parse(paramsMap[index]);
514
+ } catch (error) {
515
+ throw new Error(
516
+ `Invalid argument ${index}: ${error.message}`
517
+ );
518
+ }
519
+ }) : [];
512
520
  const requestHash = hashText(ejson4__default.default.stringify(args));
513
521
  span.setAttribute("requestHash", requestHash);
514
522
  return { args, requestHash };
@@ -532,9 +540,10 @@ var ActionHandler = class {
532
540
  }
533
541
  }
534
542
  try {
543
+ const argsWithContext = [...args, ctx];
535
544
  const result = await this.moduleInstance[this.actionName].apply(
536
545
  this.moduleInstance,
537
- args.concat(ctx)
546
+ argsWithContext
538
547
  );
539
548
  if (this.metadata.stream) {
540
549
  span?.setAttribute("stream", true);
package/dist/mod.js CHANGED
@@ -63,11 +63,18 @@ function Action(options) {
63
63
  context.addInitializer(function() {
64
64
  const prototype = this.constructor.prototype;
65
65
  const func = target.toString().split("\n")[0];
66
- const params = func.slice(func.indexOf("(") + 1, func.indexOf(")")).split(",").map((p) => p.split("=").shift()).filter((param) => !!param?.trim());
67
- params.forEach((param, index) => {
68
- options.params[index] = options.params[index]?.describe(
69
- param.trim()
70
- );
66
+ const paramString = func.slice(func.indexOf("(") + 1, func.indexOf(")"));
67
+ const parsedParams = paramString.split(",").map((p) => {
68
+ const paramWithoutDefault = p.split("=")[0]?.trim();
69
+ return paramWithoutDefault;
70
+ }).filter((param) => !!param);
71
+ options.params.forEach((param, index) => {
72
+ if (index < parsedParams.length) {
73
+ const paramName = parsedParams[index].split(":")[0]?.trim();
74
+ if (paramName) {
75
+ options.params[index] = param.describe(paramName);
76
+ }
77
+ }
71
78
  });
72
79
  const existingMetadata = prototype[ACTION_METADATA] || {};
73
80
  existingMetadata[methodName] = {
@@ -388,7 +395,10 @@ async function generateClientCode(modules) {
388
395
  }
389
396
  const params = action.params.map((param, index) => {
390
397
  const name2 = param.description || `arg${index}`;
391
- return `${name2}${param.isOptional() ? "?" : ""}: ${getZodTypeString(
398
+ const paramDef = param._def;
399
+ const isOptional = param.isOptional();
400
+ const hasDefault = paramDef?.typeName === "ZodDefault";
401
+ return `${name2}${isOptional || hasDefault ? "?" : ""}: ${getZodTypeString(
392
402
  param,
393
403
  true
394
404
  )}`;
@@ -479,27 +489,25 @@ var ActionHandler = class {
479
489
  async _validate(req) {
480
490
  const span = tracer2.startSpan("validate");
481
491
  try {
482
- let args;
492
+ let paramsMap = [];
483
493
  if (typeof req === "string") {
484
494
  try {
485
- args = Object.values(ejson4.parse(req));
495
+ paramsMap = ejson4.parse(req);
486
496
  } catch (error) {
487
497
  throw new Error(`Invalid request body: ${error.message}`);
488
498
  }
489
499
  } else {
490
- args = req;
491
- }
492
- if (this.metadata.params) {
493
- args = args.map((arg, index) => {
494
- try {
495
- return this.metadata.params[index].parse(arg);
496
- } catch (error) {
497
- throw new Error(
498
- `Invalid argument ${index}: ${error.message}`
499
- );
500
- }
501
- });
500
+ paramsMap = req;
502
501
  }
502
+ const args = this.metadata.params ? this.metadata.params.map((param, index) => {
503
+ try {
504
+ return param.parse(paramsMap[index]);
505
+ } catch (error) {
506
+ throw new Error(
507
+ `Invalid argument ${index}: ${error.message}`
508
+ );
509
+ }
510
+ }) : [];
503
511
  const requestHash = hashText(ejson4.stringify(args));
504
512
  span.setAttribute("requestHash", requestHash);
505
513
  return { args, requestHash };
@@ -523,9 +531,10 @@ var ActionHandler = class {
523
531
  }
524
532
  }
525
533
  try {
534
+ const argsWithContext = [...args, ctx];
526
535
  const result = await this.moduleInstance[this.actionName].apply(
527
536
  this.moduleInstance,
528
- args.concat(ctx)
537
+ argsWithContext
529
538
  );
530
539
  if (this.metadata.stream) {
531
540
  span?.setAttribute("stream", true);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "imean-service-engine",
3
- "version": "1.8.2",
3
+ "version": "1.9.0",
4
4
  "description": "microservice engine",
5
5
  "keywords": [
6
6
  "microservice",