@mastra/fastify 1.1.9 → 1.2.0-alpha.1

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,215 +1,215 @@
1
1
  import { Busboy } from '@fastify/busboy';
2
- import { formatZodError } from '@mastra/server/handlers/error';
3
2
  import { MastraServer as MastraServer$1, redactStreamChunk, normalizeQueryParams } from '@mastra/server/server-adapter';
4
3
 
5
4
  // src/index.ts
6
-
7
- // ../../node_modules/.pnpm/zod@3.25.76/node_modules/zod/v3/helpers/util.js
8
- var util;
9
- (function(util2) {
10
- util2.assertEqual = (_) => {
11
- };
12
- function assertIs(_arg) {
13
- }
14
- util2.assertIs = assertIs;
15
- function assertNever(_x) {
16
- throw new Error();
17
- }
18
- util2.assertNever = assertNever;
19
- util2.arrayToEnum = (items) => {
20
- const obj = {};
21
- for (const item of items) {
22
- obj[item] = item;
5
+ // @__NO_SIDE_EFFECTS__
6
+ function $constructor(name, initializer3, params) {
7
+ function init(inst, def) {
8
+ if (!inst._zod) {
9
+ Object.defineProperty(inst, "_zod", {
10
+ value: {
11
+ def,
12
+ constr: _,
13
+ traits: /* @__PURE__ */ new Set()
14
+ },
15
+ enumerable: false
16
+ });
23
17
  }
24
- return obj;
25
- };
26
- util2.getValidEnumValues = (obj) => {
27
- const validKeys = util2.objectKeys(obj).filter((k) => typeof obj[obj[k]] !== "number");
28
- const filtered = {};
29
- for (const k of validKeys) {
30
- filtered[k] = obj[k];
18
+ if (inst._zod.traits.has(name)) {
19
+ return;
31
20
  }
32
- return util2.objectValues(filtered);
33
- };
34
- util2.objectValues = (obj) => {
35
- return util2.objectKeys(obj).map(function(e) {
36
- return obj[e];
37
- });
38
- };
39
- util2.objectKeys = typeof Object.keys === "function" ? (obj) => Object.keys(obj) : (object) => {
40
- const keys = [];
41
- for (const key in object) {
42
- if (Object.prototype.hasOwnProperty.call(object, key)) {
43
- keys.push(key);
21
+ inst._zod.traits.add(name);
22
+ initializer3(inst, def);
23
+ const proto = _.prototype;
24
+ const keys = Object.keys(proto);
25
+ for (let i = 0; i < keys.length; i++) {
26
+ const k = keys[i];
27
+ if (!(k in inst)) {
28
+ inst[k] = proto[k].bind(inst);
44
29
  }
45
30
  }
46
- return keys;
47
- };
48
- util2.find = (arr, checker) => {
49
- for (const item of arr) {
50
- if (checker(item))
51
- return item;
31
+ }
32
+ const Parent = params?.Parent ?? Object;
33
+ class Definition extends Parent {
34
+ }
35
+ Object.defineProperty(Definition, "name", { value: name });
36
+ function _(def) {
37
+ var _a2;
38
+ const inst = params?.Parent ? new Definition() : this;
39
+ init(inst, def);
40
+ (_a2 = inst._zod).deferred ?? (_a2.deferred = []);
41
+ for (const fn of inst._zod.deferred) {
42
+ fn();
52
43
  }
53
- return void 0;
54
- };
55
- util2.isInteger = typeof Number.isInteger === "function" ? (val) => Number.isInteger(val) : (val) => typeof val === "number" && Number.isFinite(val) && Math.floor(val) === val;
56
- function joinValues(array, separator = " | ") {
57
- return array.map((val) => typeof val === "string" ? `'${val}'` : val).join(separator);
44
+ return inst;
58
45
  }
59
- util2.joinValues = joinValues;
60
- util2.jsonStringifyReplacer = (_, value) => {
61
- if (typeof value === "bigint") {
62
- return value.toString();
46
+ Object.defineProperty(_, "init", { value: init });
47
+ Object.defineProperty(_, Symbol.hasInstance, {
48
+ value: (inst) => {
49
+ if (params?.Parent && inst instanceof params.Parent)
50
+ return true;
51
+ return inst?._zod?.traits?.has(name);
63
52
  }
64
- return value;
65
- };
66
- })(util || (util = {}));
67
- var objectUtil;
68
- (function(objectUtil2) {
69
- objectUtil2.mergeShapes = (first, second) => {
70
- return {
71
- ...first,
72
- ...second
73
- // second overwrites first
74
- };
75
- };
76
- })(objectUtil || (objectUtil = {}));
77
- util.arrayToEnum([
78
- "string",
79
- "nan",
80
- "number",
81
- "integer",
82
- "float",
83
- "boolean",
84
- "date",
85
- "bigint",
86
- "symbol",
87
- "function",
88
- "undefined",
89
- "null",
90
- "array",
91
- "object",
92
- "unknown",
93
- "promise",
94
- "void",
95
- "never",
96
- "map",
97
- "set"
98
- ]);
53
+ });
54
+ Object.defineProperty(_, "name", { value: name });
55
+ return _;
56
+ }
99
57
 
100
- // ../../node_modules/.pnpm/zod@3.25.76/node_modules/zod/v3/ZodError.js
101
- util.arrayToEnum([
102
- "invalid_type",
103
- "invalid_literal",
104
- "custom",
105
- "invalid_union",
106
- "invalid_union_discriminator",
107
- "invalid_enum_value",
108
- "unrecognized_keys",
109
- "invalid_arguments",
110
- "invalid_return_type",
111
- "invalid_date",
112
- "invalid_string",
113
- "too_small",
114
- "too_big",
115
- "invalid_intersection_types",
116
- "not_multiple_of",
117
- "not_finite"
118
- ]);
119
- var ZodError = class _ZodError extends Error {
120
- get errors() {
121
- return this.issues;
122
- }
123
- constructor(issues) {
124
- super();
125
- this.issues = [];
126
- this.addIssue = (sub) => {
127
- this.issues = [...this.issues, sub];
128
- };
129
- this.addIssues = (subs = []) => {
130
- this.issues = [...this.issues, ...subs];
131
- };
132
- const actualProto = new.target.prototype;
133
- if (Object.setPrototypeOf) {
134
- Object.setPrototypeOf(this, actualProto);
58
+ // ../../node_modules/.pnpm/zod@4.3.6/node_modules/zod/v4/core/util.js
59
+ function jsonStringifyReplacer(_, value) {
60
+ if (typeof value === "bigint")
61
+ return value.toString();
62
+ return value;
63
+ }
64
+
65
+ // ../../node_modules/.pnpm/zod@4.3.6/node_modules/zod/v4/core/errors.js
66
+ var initializer = (inst, def) => {
67
+ inst.name = "$ZodError";
68
+ Object.defineProperty(inst, "_zod", {
69
+ value: inst._zod,
70
+ enumerable: false
71
+ });
72
+ Object.defineProperty(inst, "issues", {
73
+ value: def,
74
+ enumerable: false
75
+ });
76
+ inst.message = JSON.stringify(def, jsonStringifyReplacer, 2);
77
+ Object.defineProperty(inst, "toString", {
78
+ value: () => inst.message,
79
+ enumerable: false
80
+ });
81
+ };
82
+ var $ZodError = $constructor("$ZodError", initializer);
83
+ function flattenError(error, mapper = (issue2) => issue2.message) {
84
+ const fieldErrors = {};
85
+ const formErrors = [];
86
+ for (const sub of error.issues) {
87
+ if (sub.path.length > 0) {
88
+ fieldErrors[sub.path[0]] = fieldErrors[sub.path[0]] || [];
89
+ fieldErrors[sub.path[0]].push(mapper(sub));
135
90
  } else {
136
- this.__proto__ = actualProto;
91
+ formErrors.push(mapper(sub));
137
92
  }
138
- this.name = "ZodError";
139
- this.issues = issues;
140
93
  }
141
- format(_mapper) {
142
- const mapper = _mapper || function(issue) {
143
- return issue.message;
144
- };
145
- const fieldErrors = { _errors: [] };
146
- const processError = (error) => {
147
- for (const issue of error.issues) {
148
- if (issue.code === "invalid_union") {
149
- issue.unionErrors.map(processError);
150
- } else if (issue.code === "invalid_return_type") {
151
- processError(issue.returnTypeError);
152
- } else if (issue.code === "invalid_arguments") {
153
- processError(issue.argumentsError);
154
- } else if (issue.path.length === 0) {
155
- fieldErrors._errors.push(mapper(issue));
156
- } else {
157
- let curr = fieldErrors;
158
- let i = 0;
159
- while (i < issue.path.length) {
160
- const el = issue.path[i];
161
- const terminal = i === issue.path.length - 1;
162
- if (!terminal) {
163
- curr[el] = curr[el] || { _errors: [] };
164
- } else {
165
- curr[el] = curr[el] || { _errors: [] };
166
- curr[el]._errors.push(mapper(issue));
167
- }
168
- curr = curr[el];
169
- i++;
94
+ return { formErrors, fieldErrors };
95
+ }
96
+ function formatError(error, mapper = (issue2) => issue2.message) {
97
+ const fieldErrors = { _errors: [] };
98
+ const processError = (error2) => {
99
+ for (const issue2 of error2.issues) {
100
+ if (issue2.code === "invalid_union" && issue2.errors.length) {
101
+ issue2.errors.map((issues) => processError({ issues }));
102
+ } else if (issue2.code === "invalid_key") {
103
+ processError({ issues: issue2.issues });
104
+ } else if (issue2.code === "invalid_element") {
105
+ processError({ issues: issue2.issues });
106
+ } else if (issue2.path.length === 0) {
107
+ fieldErrors._errors.push(mapper(issue2));
108
+ } else {
109
+ let curr = fieldErrors;
110
+ let i = 0;
111
+ while (i < issue2.path.length) {
112
+ const el = issue2.path[i];
113
+ const terminal = i === issue2.path.length - 1;
114
+ if (!terminal) {
115
+ curr[el] = curr[el] || { _errors: [] };
116
+ } else {
117
+ curr[el] = curr[el] || { _errors: [] };
118
+ curr[el]._errors.push(mapper(issue2));
170
119
  }
120
+ curr = curr[el];
121
+ i++;
171
122
  }
172
123
  }
173
- };
174
- processError(this);
175
- return fieldErrors;
176
- }
177
- static assert(value) {
178
- if (!(value instanceof _ZodError)) {
179
- throw new Error(`Not a ZodError: ${value}`);
180
124
  }
125
+ };
126
+ processError(error);
127
+ return fieldErrors;
128
+ }
129
+
130
+ // ../../node_modules/.pnpm/zod@4.3.6/node_modules/zod/v4/core/registries.js
131
+ var _a;
132
+ var $ZodRegistry = class {
133
+ constructor() {
134
+ this._map = /* @__PURE__ */ new WeakMap();
135
+ this._idmap = /* @__PURE__ */ new Map();
181
136
  }
182
- toString() {
183
- return this.message;
137
+ add(schema, ..._meta) {
138
+ const meta = _meta[0];
139
+ this._map.set(schema, meta);
140
+ if (meta && typeof meta === "object" && "id" in meta) {
141
+ this._idmap.set(meta.id, schema);
142
+ }
143
+ return this;
184
144
  }
185
- get message() {
186
- return JSON.stringify(this.issues, util.jsonStringifyReplacer, 2);
145
+ clear() {
146
+ this._map = /* @__PURE__ */ new WeakMap();
147
+ this._idmap = /* @__PURE__ */ new Map();
148
+ return this;
187
149
  }
188
- get isEmpty() {
189
- return this.issues.length === 0;
150
+ remove(schema) {
151
+ const meta = this._map.get(schema);
152
+ if (meta && typeof meta === "object" && "id" in meta) {
153
+ this._idmap.delete(meta.id);
154
+ }
155
+ this._map.delete(schema);
156
+ return this;
190
157
  }
191
- flatten(mapper = (issue) => issue.message) {
192
- const fieldErrors = {};
193
- const formErrors = [];
194
- for (const sub of this.issues) {
195
- if (sub.path.length > 0) {
196
- const firstEl = sub.path[0];
197
- fieldErrors[firstEl] = fieldErrors[firstEl] || [];
198
- fieldErrors[firstEl].push(mapper(sub));
199
- } else {
200
- formErrors.push(mapper(sub));
201
- }
158
+ get(schema) {
159
+ const p = schema._zod.parent;
160
+ if (p) {
161
+ const pm = { ...this.get(p) ?? {} };
162
+ delete pm.id;
163
+ const f = { ...pm, ...this._map.get(schema) };
164
+ return Object.keys(f).length ? f : void 0;
202
165
  }
203
- return { formErrors, fieldErrors };
166
+ return this._map.get(schema);
204
167
  }
205
- get formErrors() {
206
- return this.flatten();
168
+ has(schema) {
169
+ return this._map.has(schema);
207
170
  }
208
171
  };
209
- ZodError.create = (issues) => {
210
- const error = new ZodError(issues);
211
- return error;
172
+ function registry() {
173
+ return new $ZodRegistry();
174
+ }
175
+ (_a = globalThis).__zod_globalRegistry ?? (_a.__zod_globalRegistry = registry());
176
+
177
+ // ../../node_modules/.pnpm/zod@4.3.6/node_modules/zod/v4/classic/errors.js
178
+ var initializer2 = (inst, issues) => {
179
+ $ZodError.init(inst, issues);
180
+ inst.name = "ZodError";
181
+ Object.defineProperties(inst, {
182
+ format: {
183
+ value: (mapper) => formatError(inst, mapper)
184
+ // enumerable: false,
185
+ },
186
+ flatten: {
187
+ value: (mapper) => flattenError(inst, mapper)
188
+ // enumerable: false,
189
+ },
190
+ addIssue: {
191
+ value: (issue2) => {
192
+ inst.issues.push(issue2);
193
+ inst.message = JSON.stringify(inst.issues, jsonStringifyReplacer, 2);
194
+ }
195
+ // enumerable: false,
196
+ },
197
+ addIssues: {
198
+ value: (issues2) => {
199
+ inst.issues.push(...issues2);
200
+ inst.message = JSON.stringify(inst.issues, jsonStringifyReplacer, 2);
201
+ }
202
+ // enumerable: false,
203
+ },
204
+ isEmpty: {
205
+ get() {
206
+ return inst.issues.length === 0;
207
+ }
208
+ // enumerable: false,
209
+ }
210
+ });
212
211
  };
212
+ var ZodError = $constructor("ZodError", initializer2);
213
213
 
214
214
  // src/index.ts
215
215
  var _hasPermissionPromise;
@@ -538,7 +538,8 @@ var MastraServer = class extends MastraServer$1 {
538
538
  error: error instanceof Error ? { message: error.message, stack: error.stack } : error
539
539
  });
540
540
  if (error instanceof ZodError) {
541
- return reply.status(400).send(formatZodError(error, "query parameters"));
541
+ const { status, body } = this.resolveValidationError(route, error, "query");
542
+ return reply.status(status).send(body);
542
543
  }
543
544
  return reply.status(400).send({
544
545
  error: "Invalid query parameters",
@@ -554,7 +555,8 @@ var MastraServer = class extends MastraServer$1 {
554
555
  error: error instanceof Error ? { message: error.message, stack: error.stack } : error
555
556
  });
556
557
  if (error instanceof ZodError) {
557
- return reply.status(400).send(formatZodError(error, "request body"));
558
+ const { status, body } = this.resolveValidationError(route, error, "body");
559
+ return reply.status(status).send(body);
558
560
  }
559
561
  return reply.status(400).send({
560
562
  error: "Invalid request body",
@@ -570,7 +572,8 @@ var MastraServer = class extends MastraServer$1 {
570
572
  error: error instanceof Error ? { message: error.message, stack: error.stack } : error
571
573
  });
572
574
  if (error instanceof ZodError) {
573
- return reply.status(400).send(formatZodError(error, "path parameters"));
575
+ const { status, body } = this.resolveValidationError(route, error, "path");
576
+ return reply.status(status).send(body);
574
577
  }
575
578
  return reply.status(400).send({
576
579
  error: "Invalid path parameters",
@@ -625,7 +628,7 @@ var MastraServer = class extends MastraServer$1 {
625
628
  };
626
629
  const shouldApplyBodyLimit = this.bodyLimitOptions && ["POST", "PUT", "PATCH"].includes(route.method.toUpperCase());
627
630
  const maxSize = route.maxBodySize ?? this.bodyLimitOptions?.maxSize;
628
- const config = shouldApplyBodyLimit && maxSize ? { bodyLimit: maxSize } : void 0;
631
+ const config2 = shouldApplyBodyLimit && maxSize ? { bodyLimit: maxSize } : void 0;
629
632
  if (route.method.toUpperCase() === "ALL") {
630
633
  const methods = ["GET", "POST", "PUT", "DELETE", "PATCH"];
631
634
  for (const method of methods) {
@@ -634,7 +637,7 @@ var MastraServer = class extends MastraServer$1 {
634
637
  method,
635
638
  url: fastifyPath,
636
639
  handler,
637
- config
640
+ config: config2
638
641
  });
639
642
  } catch (err) {
640
643
  if (err instanceof Error && err.message.includes("already declared")) {
@@ -648,7 +651,7 @@ var MastraServer = class extends MastraServer$1 {
648
651
  method: route.method,
649
652
  url: fastifyPath,
650
653
  handler,
651
- config
654
+ config: config2
652
655
  });
653
656
  }
654
657
  }
@@ -760,14 +763,14 @@ var MastraServer = class extends MastraServer$1 {
760
763
  const method = request.method;
761
764
  const path = urlPath;
762
765
  reply.raw.once("finish", () => {
763
- const duration = Date.now() - start;
766
+ const duration2 = Date.now() - start;
764
767
  const status = reply.statusCode;
765
768
  const level = this.httpLoggingConfig?.level || "info";
766
769
  const logData = {
767
770
  method,
768
771
  path,
769
772
  status,
770
- duration: `${duration}ms`
773
+ duration: `${duration2}ms`
771
774
  };
772
775
  if (this.httpLoggingConfig?.includeQueryParams) {
773
776
  logData.query = request.query;
@@ -783,7 +786,7 @@ var MastraServer = class extends MastraServer$1 {
783
786
  });
784
787
  logData.headers = headers;
785
788
  }
786
- this.logger[level](`${method} ${path} ${status} ${duration}ms`, logData);
789
+ this.logger[level](`${method} ${path} ${status} ${duration2}ms`, logData);
787
790
  });
788
791
  });
789
792
  }