@jon49/sw 0.14.4 → 0.14.6
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/lib/routes.middleware.ts +26 -10
- package/package.json +1 -1
package/lib/routes.middleware.ts
CHANGED
|
@@ -138,12 +138,25 @@ async function executeHandler({ url, req, ctx }: ExectuteHandlerOptions): Promis
|
|
|
138
138
|
const data = await getData(req)
|
|
139
139
|
let query = searchParams<{ handler?: string }>(url)
|
|
140
140
|
let args = { req, data, query }
|
|
141
|
-
let result
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
141
|
+
let result
|
|
142
|
+
try {
|
|
143
|
+
result = await (
|
|
144
|
+
handlers instanceof Function
|
|
145
|
+
? handlers(args)
|
|
146
|
+
: (call(handlers[query.handler ?? ""], args)
|
|
147
|
+
|| call(handlers[method], args)
|
|
148
|
+
|| Promise.reject("I'm sorry, I didn't understand where to route your request.")))
|
|
149
|
+
} catch (e: any) {
|
|
150
|
+
if (e.reasons) {
|
|
151
|
+
ctx.messages =
|
|
152
|
+
e.reasons.map((x: any) => x.reason.reasons).flat().map((x: any) => x.reason).flat()
|
|
153
|
+
return {
|
|
154
|
+
status: 204,
|
|
155
|
+
}
|
|
156
|
+
} else {
|
|
157
|
+
throw e
|
|
158
|
+
}
|
|
159
|
+
}
|
|
147
160
|
|
|
148
161
|
if (!result) {
|
|
149
162
|
return isPost
|
|
@@ -160,7 +173,7 @@ async function executeHandler({ url, req, ctx }: ExectuteHandlerOptions): Promis
|
|
|
160
173
|
} else if (result.messages?.length > 0) {
|
|
161
174
|
messages.push(...result.messages)
|
|
162
175
|
}
|
|
163
|
-
ctx.messages = messages
|
|
176
|
+
ctx.messages = result.messages = messages
|
|
164
177
|
ctx.events = result.events
|
|
165
178
|
|
|
166
179
|
if (isHtml(result)) {
|
|
@@ -180,13 +193,17 @@ async function executeHandler({ url, req, ctx }: ExectuteHandlerOptions): Promis
|
|
|
180
193
|
result.headers = {
|
|
181
194
|
...result.headers,
|
|
182
195
|
}
|
|
196
|
+
} else if (result.messages?.length > 0) {
|
|
197
|
+
return {
|
|
198
|
+
...result,
|
|
199
|
+
status: result.status || 204,
|
|
200
|
+
}
|
|
183
201
|
}
|
|
184
202
|
return {
|
|
185
203
|
...result,
|
|
186
204
|
type: "application/json"
|
|
187
205
|
}
|
|
188
206
|
}
|
|
189
|
-
|
|
190
207
|
} catch (error) {
|
|
191
208
|
console.error(`"${method}" error:`, error, "\nURL:", url);
|
|
192
209
|
if (isPost) {
|
|
@@ -305,5 +322,4 @@ type RequireAtLeastOne<T, Keys extends keyof T = keyof T> =
|
|
|
305
322
|
}[Keys]
|
|
306
323
|
|
|
307
324
|
export type Route = RequireAtLeastOne<Route_, "file" | "get" | "post">
|
|
308
|
-
export type RoutePage = Pick<Route_, "get" | "post">
|
|
309
|
-
|
|
325
|
+
export type RoutePage = Pick<Route_, "get" | "post">
|