bunsvr / router

A blazing-fast router for Stric

Home Page:https://stricjs.netlify.app/#/basic/routing/main

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Allow custom status codes in Router guard responses

brynne8 opened this issue · comments

Hello,

I've been using the @stricjs/router package and noticed that the current implementation of the .guard() method only allows returning null to trigger a 404 response. In some scenarios, it would be beneficial to return other status codes, such as 400, 403, etc., based on the guard's logic.

Example:

import { Router } from '@stricjs/router';

export default new Router()
    // Currently, this only triggers a 404 response
    .guard('/', req => verify(req))
    .get('/', () => new Response('Hi'))
    .use(404);

Suggestion:

It would be great if the .guard() method could be enhanced to support returning custom status codes or even custom responses. This would provide more flexibility in handling different scenarios.

Thank you for considering this enhancement!

commented

I can do something like this
Customize the 404 return for each path
Or maybe add another property to context
@brynne8

commented

Maybe a fail function should be good
app.reject('/', /* Handler here */)
I will implement that for 4.0.11

commented

@brynne8
Now you can register custom response when guard failed using app.reject with Stric 4.0.11
If you want it to be different please suggest me another approach
You can contact with me on Discord cuz I'm very active on it