@mastra/koa 1.2.5-alpha.0 → 1.3.0-alpha.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.
package/dist/index.js CHANGED
@@ -1,216 +1,216 @@
1
1
  import { Busboy } from '@fastify/busboy';
2
2
  import { isProtectedCustomRoute } from '@mastra/server/auth';
3
- import { formatZodError } from '@mastra/server/handlers/error';
4
3
  import { MastraServer as MastraServer$1, redactStreamChunk, normalizeQueryParams } from '@mastra/server/server-adapter';
5
4
 
6
5
  // src/index.ts
7
-
8
- // ../../node_modules/.pnpm/zod@3.25.76/node_modules/zod/v3/helpers/util.js
9
- var util;
10
- (function(util2) {
11
- util2.assertEqual = (_) => {
12
- };
13
- function assertIs(_arg) {
14
- }
15
- util2.assertIs = assertIs;
16
- function assertNever(_x) {
17
- throw new Error();
18
- }
19
- util2.assertNever = assertNever;
20
- util2.arrayToEnum = (items) => {
21
- const obj = {};
22
- for (const item of items) {
23
- obj[item] = item;
6
+ // @__NO_SIDE_EFFECTS__
7
+ function $constructor(name, initializer3, params) {
8
+ function init(inst, def) {
9
+ if (!inst._zod) {
10
+ Object.defineProperty(inst, "_zod", {
11
+ value: {
12
+ def,
13
+ constr: _,
14
+ traits: /* @__PURE__ */ new Set()
15
+ },
16
+ enumerable: false
17
+ });
24
18
  }
25
- return obj;
26
- };
27
- util2.getValidEnumValues = (obj) => {
28
- const validKeys = util2.objectKeys(obj).filter((k) => typeof obj[obj[k]] !== "number");
29
- const filtered = {};
30
- for (const k of validKeys) {
31
- filtered[k] = obj[k];
19
+ if (inst._zod.traits.has(name)) {
20
+ return;
32
21
  }
33
- return util2.objectValues(filtered);
34
- };
35
- util2.objectValues = (obj) => {
36
- return util2.objectKeys(obj).map(function(e) {
37
- return obj[e];
38
- });
39
- };
40
- util2.objectKeys = typeof Object.keys === "function" ? (obj) => Object.keys(obj) : (object) => {
41
- const keys = [];
42
- for (const key in object) {
43
- if (Object.prototype.hasOwnProperty.call(object, key)) {
44
- keys.push(key);
22
+ inst._zod.traits.add(name);
23
+ initializer3(inst, def);
24
+ const proto = _.prototype;
25
+ const keys = Object.keys(proto);
26
+ for (let i = 0; i < keys.length; i++) {
27
+ const k = keys[i];
28
+ if (!(k in inst)) {
29
+ inst[k] = proto[k].bind(inst);
45
30
  }
46
31
  }
47
- return keys;
48
- };
49
- util2.find = (arr, checker) => {
50
- for (const item of arr) {
51
- if (checker(item))
52
- return item;
32
+ }
33
+ const Parent = params?.Parent ?? Object;
34
+ class Definition extends Parent {
35
+ }
36
+ Object.defineProperty(Definition, "name", { value: name });
37
+ function _(def) {
38
+ var _a2;
39
+ const inst = params?.Parent ? new Definition() : this;
40
+ init(inst, def);
41
+ (_a2 = inst._zod).deferred ?? (_a2.deferred = []);
42
+ for (const fn of inst._zod.deferred) {
43
+ fn();
53
44
  }
54
- return void 0;
55
- };
56
- util2.isInteger = typeof Number.isInteger === "function" ? (val) => Number.isInteger(val) : (val) => typeof val === "number" && Number.isFinite(val) && Math.floor(val) === val;
57
- function joinValues(array, separator = " | ") {
58
- return array.map((val) => typeof val === "string" ? `'${val}'` : val).join(separator);
45
+ return inst;
59
46
  }
60
- util2.joinValues = joinValues;
61
- util2.jsonStringifyReplacer = (_, value) => {
62
- if (typeof value === "bigint") {
63
- return value.toString();
47
+ Object.defineProperty(_, "init", { value: init });
48
+ Object.defineProperty(_, Symbol.hasInstance, {
49
+ value: (inst) => {
50
+ if (params?.Parent && inst instanceof params.Parent)
51
+ return true;
52
+ return inst?._zod?.traits?.has(name);
64
53
  }
65
- return value;
66
- };
67
- })(util || (util = {}));
68
- var objectUtil;
69
- (function(objectUtil2) {
70
- objectUtil2.mergeShapes = (first, second) => {
71
- return {
72
- ...first,
73
- ...second
74
- // second overwrites first
75
- };
76
- };
77
- })(objectUtil || (objectUtil = {}));
78
- util.arrayToEnum([
79
- "string",
80
- "nan",
81
- "number",
82
- "integer",
83
- "float",
84
- "boolean",
85
- "date",
86
- "bigint",
87
- "symbol",
88
- "function",
89
- "undefined",
90
- "null",
91
- "array",
92
- "object",
93
- "unknown",
94
- "promise",
95
- "void",
96
- "never",
97
- "map",
98
- "set"
99
- ]);
54
+ });
55
+ Object.defineProperty(_, "name", { value: name });
56
+ return _;
57
+ }
100
58
 
101
- // ../../node_modules/.pnpm/zod@3.25.76/node_modules/zod/v3/ZodError.js
102
- util.arrayToEnum([
103
- "invalid_type",
104
- "invalid_literal",
105
- "custom",
106
- "invalid_union",
107
- "invalid_union_discriminator",
108
- "invalid_enum_value",
109
- "unrecognized_keys",
110
- "invalid_arguments",
111
- "invalid_return_type",
112
- "invalid_date",
113
- "invalid_string",
114
- "too_small",
115
- "too_big",
116
- "invalid_intersection_types",
117
- "not_multiple_of",
118
- "not_finite"
119
- ]);
120
- var ZodError = class _ZodError extends Error {
121
- get errors() {
122
- return this.issues;
123
- }
124
- constructor(issues) {
125
- super();
126
- this.issues = [];
127
- this.addIssue = (sub) => {
128
- this.issues = [...this.issues, sub];
129
- };
130
- this.addIssues = (subs = []) => {
131
- this.issues = [...this.issues, ...subs];
132
- };
133
- const actualProto = new.target.prototype;
134
- if (Object.setPrototypeOf) {
135
- Object.setPrototypeOf(this, actualProto);
59
+ // ../../node_modules/.pnpm/zod@4.3.6/node_modules/zod/v4/core/util.js
60
+ function jsonStringifyReplacer(_, value) {
61
+ if (typeof value === "bigint")
62
+ return value.toString();
63
+ return value;
64
+ }
65
+
66
+ // ../../node_modules/.pnpm/zod@4.3.6/node_modules/zod/v4/core/errors.js
67
+ var initializer = (inst, def) => {
68
+ inst.name = "$ZodError";
69
+ Object.defineProperty(inst, "_zod", {
70
+ value: inst._zod,
71
+ enumerable: false
72
+ });
73
+ Object.defineProperty(inst, "issues", {
74
+ value: def,
75
+ enumerable: false
76
+ });
77
+ inst.message = JSON.stringify(def, jsonStringifyReplacer, 2);
78
+ Object.defineProperty(inst, "toString", {
79
+ value: () => inst.message,
80
+ enumerable: false
81
+ });
82
+ };
83
+ var $ZodError = $constructor("$ZodError", initializer);
84
+ function flattenError(error, mapper = (issue2) => issue2.message) {
85
+ const fieldErrors = {};
86
+ const formErrors = [];
87
+ for (const sub of error.issues) {
88
+ if (sub.path.length > 0) {
89
+ fieldErrors[sub.path[0]] = fieldErrors[sub.path[0]] || [];
90
+ fieldErrors[sub.path[0]].push(mapper(sub));
136
91
  } else {
137
- this.__proto__ = actualProto;
92
+ formErrors.push(mapper(sub));
138
93
  }
139
- this.name = "ZodError";
140
- this.issues = issues;
141
94
  }
142
- format(_mapper) {
143
- const mapper = _mapper || function(issue) {
144
- return issue.message;
145
- };
146
- const fieldErrors = { _errors: [] };
147
- const processError = (error) => {
148
- for (const issue of error.issues) {
149
- if (issue.code === "invalid_union") {
150
- issue.unionErrors.map(processError);
151
- } else if (issue.code === "invalid_return_type") {
152
- processError(issue.returnTypeError);
153
- } else if (issue.code === "invalid_arguments") {
154
- processError(issue.argumentsError);
155
- } else if (issue.path.length === 0) {
156
- fieldErrors._errors.push(mapper(issue));
157
- } else {
158
- let curr = fieldErrors;
159
- let i = 0;
160
- while (i < issue.path.length) {
161
- const el = issue.path[i];
162
- const terminal = i === issue.path.length - 1;
163
- if (!terminal) {
164
- curr[el] = curr[el] || { _errors: [] };
165
- } else {
166
- curr[el] = curr[el] || { _errors: [] };
167
- curr[el]._errors.push(mapper(issue));
168
- }
169
- curr = curr[el];
170
- i++;
95
+ return { formErrors, fieldErrors };
96
+ }
97
+ function formatError(error, mapper = (issue2) => issue2.message) {
98
+ const fieldErrors = { _errors: [] };
99
+ const processError = (error2) => {
100
+ for (const issue2 of error2.issues) {
101
+ if (issue2.code === "invalid_union" && issue2.errors.length) {
102
+ issue2.errors.map((issues) => processError({ issues }));
103
+ } else if (issue2.code === "invalid_key") {
104
+ processError({ issues: issue2.issues });
105
+ } else if (issue2.code === "invalid_element") {
106
+ processError({ issues: issue2.issues });
107
+ } else if (issue2.path.length === 0) {
108
+ fieldErrors._errors.push(mapper(issue2));
109
+ } else {
110
+ let curr = fieldErrors;
111
+ let i = 0;
112
+ while (i < issue2.path.length) {
113
+ const el = issue2.path[i];
114
+ const terminal = i === issue2.path.length - 1;
115
+ if (!terminal) {
116
+ curr[el] = curr[el] || { _errors: [] };
117
+ } else {
118
+ curr[el] = curr[el] || { _errors: [] };
119
+ curr[el]._errors.push(mapper(issue2));
171
120
  }
121
+ curr = curr[el];
122
+ i++;
172
123
  }
173
124
  }
174
- };
175
- processError(this);
176
- return fieldErrors;
177
- }
178
- static assert(value) {
179
- if (!(value instanceof _ZodError)) {
180
- throw new Error(`Not a ZodError: ${value}`);
181
125
  }
126
+ };
127
+ processError(error);
128
+ return fieldErrors;
129
+ }
130
+
131
+ // ../../node_modules/.pnpm/zod@4.3.6/node_modules/zod/v4/core/registries.js
132
+ var _a;
133
+ var $ZodRegistry = class {
134
+ constructor() {
135
+ this._map = /* @__PURE__ */ new WeakMap();
136
+ this._idmap = /* @__PURE__ */ new Map();
182
137
  }
183
- toString() {
184
- return this.message;
138
+ add(schema, ..._meta) {
139
+ const meta = _meta[0];
140
+ this._map.set(schema, meta);
141
+ if (meta && typeof meta === "object" && "id" in meta) {
142
+ this._idmap.set(meta.id, schema);
143
+ }
144
+ return this;
185
145
  }
186
- get message() {
187
- return JSON.stringify(this.issues, util.jsonStringifyReplacer, 2);
146
+ clear() {
147
+ this._map = /* @__PURE__ */ new WeakMap();
148
+ this._idmap = /* @__PURE__ */ new Map();
149
+ return this;
188
150
  }
189
- get isEmpty() {
190
- return this.issues.length === 0;
151
+ remove(schema) {
152
+ const meta = this._map.get(schema);
153
+ if (meta && typeof meta === "object" && "id" in meta) {
154
+ this._idmap.delete(meta.id);
155
+ }
156
+ this._map.delete(schema);
157
+ return this;
191
158
  }
192
- flatten(mapper = (issue) => issue.message) {
193
- const fieldErrors = {};
194
- const formErrors = [];
195
- for (const sub of this.issues) {
196
- if (sub.path.length > 0) {
197
- const firstEl = sub.path[0];
198
- fieldErrors[firstEl] = fieldErrors[firstEl] || [];
199
- fieldErrors[firstEl].push(mapper(sub));
200
- } else {
201
- formErrors.push(mapper(sub));
202
- }
159
+ get(schema) {
160
+ const p = schema._zod.parent;
161
+ if (p) {
162
+ const pm = { ...this.get(p) ?? {} };
163
+ delete pm.id;
164
+ const f = { ...pm, ...this._map.get(schema) };
165
+ return Object.keys(f).length ? f : void 0;
203
166
  }
204
- return { formErrors, fieldErrors };
167
+ return this._map.get(schema);
205
168
  }
206
- get formErrors() {
207
- return this.flatten();
169
+ has(schema) {
170
+ return this._map.has(schema);
208
171
  }
209
172
  };
210
- ZodError.create = (issues) => {
211
- const error = new ZodError(issues);
212
- return error;
173
+ function registry() {
174
+ return new $ZodRegistry();
175
+ }
176
+ (_a = globalThis).__zod_globalRegistry ?? (_a.__zod_globalRegistry = registry());
177
+
178
+ // ../../node_modules/.pnpm/zod@4.3.6/node_modules/zod/v4/classic/errors.js
179
+ var initializer2 = (inst, issues) => {
180
+ $ZodError.init(inst, issues);
181
+ inst.name = "ZodError";
182
+ Object.defineProperties(inst, {
183
+ format: {
184
+ value: (mapper) => formatError(inst, mapper)
185
+ // enumerable: false,
186
+ },
187
+ flatten: {
188
+ value: (mapper) => flattenError(inst, mapper)
189
+ // enumerable: false,
190
+ },
191
+ addIssue: {
192
+ value: (issue2) => {
193
+ inst.issues.push(issue2);
194
+ inst.message = JSON.stringify(inst.issues, jsonStringifyReplacer, 2);
195
+ }
196
+ // enumerable: false,
197
+ },
198
+ addIssues: {
199
+ value: (issues2) => {
200
+ inst.issues.push(...issues2);
201
+ inst.message = JSON.stringify(inst.issues, jsonStringifyReplacer, 2);
202
+ }
203
+ // enumerable: false,
204
+ },
205
+ isEmpty: {
206
+ get() {
207
+ return inst.issues.length === 0;
208
+ }
209
+ // enumerable: false,
210
+ }
211
+ });
213
212
  };
213
+ var ZodError = $constructor("ZodError", initializer2);
214
214
 
215
215
  // src/index.ts
216
216
  var _hasPermissionPromise;
@@ -631,8 +631,9 @@ var MastraServer = class extends MastraServer$1 {
631
631
  error: error instanceof Error ? { message: error.message, stack: error.stack } : error
632
632
  });
633
633
  if (error instanceof ZodError) {
634
- ctx.status = 400;
635
- ctx.body = formatZodError(error, "query parameters");
634
+ const resolved = this.resolveValidationError(route, error, "query");
635
+ ctx.status = resolved.status;
636
+ ctx.body = resolved.body;
636
637
  return;
637
638
  }
638
639
  ctx.status = 400;
@@ -651,8 +652,9 @@ var MastraServer = class extends MastraServer$1 {
651
652
  error: error instanceof Error ? { message: error.message, stack: error.stack } : error
652
653
  });
653
654
  if (error instanceof ZodError) {
654
- ctx.status = 400;
655
- ctx.body = formatZodError(error, "request body");
655
+ const resolved = this.resolveValidationError(route, error, "body");
656
+ ctx.status = resolved.status;
657
+ ctx.body = resolved.body;
656
658
  return;
657
659
  }
658
660
  ctx.status = 400;
@@ -671,8 +673,9 @@ var MastraServer = class extends MastraServer$1 {
671
673
  error: error instanceof Error ? { message: error.message, stack: error.stack } : error
672
674
  });
673
675
  if (error instanceof ZodError) {
674
- ctx.status = 400;
675
- ctx.body = formatZodError(error, "path parameters");
676
+ const resolved = this.resolveValidationError(route, error, "path");
677
+ ctx.status = resolved.status;
678
+ ctx.body = resolved.body;
676
679
  return;
677
680
  }
678
681
  ctx.status = 400;
@@ -831,14 +834,14 @@ var MastraServer = class extends MastraServer$1 {
831
834
  const method = ctx.method;
832
835
  const path = ctx.path;
833
836
  await next();
834
- const duration = Date.now() - start;
837
+ const duration2 = Date.now() - start;
835
838
  const status = ctx.status;
836
839
  const level = this.httpLoggingConfig?.level || "info";
837
840
  const logData = {
838
841
  method,
839
842
  path,
840
843
  status,
841
- duration: `${duration}ms`
844
+ duration: `${duration2}ms`
842
845
  };
843
846
  if (this.httpLoggingConfig?.includeQueryParams) {
844
847
  logData.query = ctx.query;
@@ -854,7 +857,7 @@ var MastraServer = class extends MastraServer$1 {
854
857
  });
855
858
  logData.headers = headers;
856
859
  }
857
- this.logger[level](`${method} ${path} ${status} ${duration}ms`, logData);
860
+ this.logger[level](`${method} ${path} ${status} ${duration2}ms`, logData);
858
861
  });
859
862
  }
860
863
  };