chore(product): Improve product normalization and fix http router with tracing (#11724)

**What**
- Improve product normalization and prevent over fetching data
- Fix HTTP router wrap handler with tracing enabled
This commit is contained in:
Adrien de Peretti
2025-03-05 14:04:25 +01:00
committed by GitHub
parent e81deb49f8
commit cc1309d370
15 changed files with 467 additions and 121 deletions

View File

@@ -0,0 +1,5 @@
import { Request, Response } from "express"
export function GET(req: Request, res: Response) {
throw new Error("Failed")
}

View File

@@ -36,6 +36,22 @@ describe("RoutesLoader", function () {
request = request_
})
it("should be handled by the error handler when a route handler fails", async function () {
const res = await request("GET", "/admin/fail", {
adminSession: {
jwt: {
userId: "admin_user",
},
},
})
expect(res.status).toBe(500)
console.log(res)
expect(res.text).toBe(
'{"code":"unknown_error","type":"unknown_error","message":"An unknown error occurred."}'
)
})
it("should return a status 200 on GET admin/order/:id", async function () {
const res = await request("GET", "/admin/orders/1000", {
adminSession: {

View File

@@ -9,6 +9,18 @@ describe("Routes loader", () => {
expect(loader.getRoutes()).toMatchInlineSnapshot(`
[
{
"absolutePath": "${BASE_DIR}/admin/fail/route.ts",
"handler": [Function],
"isRoute": true,
"matcher": "/admin/fail",
"method": "GET",
"optedOutOfAuth": false,
"relativePath": "/admin/fail/route.ts",
"shouldAppendAdminCors": true,
"shouldAppendAuthCors": false,
"shouldAppendStoreCors": false,
},
{
"absolutePath": "${BASE_DIR}/admin/orders/[id]/route.ts",
"handler": [Function],
@@ -205,6 +217,18 @@ describe("Routes loader", () => {
expect(loader.getRoutes()).toMatchInlineSnapshot(`
[
{
"absolutePath": "${BASE_DIR}/admin/fail/route.ts",
"handler": [Function],
"isRoute": true,
"matcher": "/admin/fail",
"method": "GET",
"optedOutOfAuth": false,
"relativePath": "/admin/fail/route.ts",
"shouldAppendAdminCors": true,
"shouldAppendAuthCors": false,
"shouldAppendStoreCors": false,
},
{
"absolutePath": "${BASE_DIR}/admin/orders/[id]/route.ts",
"handler": [Function],

View File

@@ -104,25 +104,25 @@ export class ApiLoader {
if ("isRoute" in route) {
logger.debug(`registering route ${route.method} ${route.matcher}`)
const handler = ApiLoader.traceRoute
? ApiLoader.traceRoute(wrapHandler(route.handler), {
? ApiLoader.traceRoute(route.handler, {
route: route.matcher,
method: route.method,
})
: wrapHandler(route.handler)
: route.handler
this.#app[route.method.toLowerCase()](route.matcher, handler)
this.#app[route.method.toLowerCase()](route.matcher, wrapHandler(handler))
return
}
if (!route.methods) {
logger.debug(`registering global middleware for ${route.matcher}`)
const handler = ApiLoader.traceMiddleware
? (ApiLoader.traceMiddleware(wrapHandler(route.handler), {
? (ApiLoader.traceMiddleware(route.handler, {
route: route.matcher,
}) as RequestHandler)
: (wrapHandler(route.handler) as RequestHandler)
: (route.handler as RequestHandler)
this.#app.use(route.matcher, handler)
this.#app.use(route.matcher, wrapHandler(handler))
return
}