@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/CHANGELOG.md +38 -0
- package/dist/index.cjs +208 -205
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +196 -193
- package/dist/index.js.map +1 -1
- package/package.json +13 -13
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
|
-
|
|
9
|
-
|
|
10
|
-
(
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
-
|
|
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
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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
|
|
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
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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
|
-
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
|
|
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.
|
|
102
|
-
|
|
103
|
-
"
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
"
|
|
111
|
-
"
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
"
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
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
|
-
|
|
92
|
+
formErrors.push(mapper(sub));
|
|
138
93
|
}
|
|
139
|
-
this.name = "ZodError";
|
|
140
|
-
this.issues = issues;
|
|
141
94
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
const
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
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
|
-
|
|
184
|
-
|
|
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
|
-
|
|
187
|
-
|
|
146
|
+
clear() {
|
|
147
|
+
this._map = /* @__PURE__ */ new WeakMap();
|
|
148
|
+
this._idmap = /* @__PURE__ */ new Map();
|
|
149
|
+
return this;
|
|
188
150
|
}
|
|
189
|
-
|
|
190
|
-
|
|
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
|
-
|
|
193
|
-
const
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
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
|
|
167
|
+
return this._map.get(schema);
|
|
205
168
|
}
|
|
206
|
-
|
|
207
|
-
return this.
|
|
169
|
+
has(schema) {
|
|
170
|
+
return this._map.has(schema);
|
|
208
171
|
}
|
|
209
172
|
};
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
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
|
-
|
|
635
|
-
ctx.
|
|
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
|
-
|
|
655
|
-
ctx.
|
|
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
|
-
|
|
675
|
-
ctx.
|
|
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
|
|
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: `${
|
|
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} ${
|
|
860
|
+
this.logger[level](`${method} ${path} ${status} ${duration2}ms`, logData);
|
|
858
861
|
});
|
|
859
862
|
}
|
|
860
863
|
};
|