Huge Letters
Huge Letters
Explore posts from servers
HHono
Created by Huge Letters on 6/22/2024 in #help
How to resolve relative routes
how?( i've tried / but dont see the command
10 replies
HHono
Created by Huge Letters on 6/22/2024 in #help
How to resolve relative routes
but i kinda wish this was a built-in it makes sense I would want to declare relative routes which can then be resolved to the absolute ones - my router shouldnt know where and which route it was mounted to - and I should be able to just do resolveRoute("/") and it would give me the absolute route
10 replies
HHono
Created by Huge Letters on 6/22/2024 in #help
How to resolve relative routes
/**
* Wrap your router with this function and a `basePath` var will be exposed to all of your routes.
* Base path is whatever the full path this router has been assigned to.
*
* @example
* new Hono().route(
* '/a',
* new Hono().route(
* '/b',
* withBasePath(new Hono()).get('/c', (c) => c.var.basePath // "/a/b"
* )));
*/
export function withBasePath<THono extends Hono>(app: THono) {
return app
.use(
':*{.*}',
createMiddleware<{
Variables: {
/** This will be undefined on index routes because they do not trigger this middleware */
basePath?: string;
};
}>((c, next) => {
const rest = c.req.param('*');
const path = c.req.path;

const basePath = rest ? path.replace(new RegExp(`${rest}$`), '') : path;

c.set('basePath', basePath);
return next();
}),
)
.use<{ Variables: { basePath: string } }>((c, next) => {
c.set('basePath', c.var.basePath ?? c.req.path);
return next();
});
}
/**
* Wrap your router with this function and a `basePath` var will be exposed to all of your routes.
* Base path is whatever the full path this router has been assigned to.
*
* @example
* new Hono().route(
* '/a',
* new Hono().route(
* '/b',
* withBasePath(new Hono()).get('/c', (c) => c.var.basePath // "/a/b"
* )));
*/
export function withBasePath<THono extends Hono>(app: THono) {
return app
.use(
':*{.*}',
createMiddleware<{
Variables: {
/** This will be undefined on index routes because they do not trigger this middleware */
basePath?: string;
};
}>((c, next) => {
const rest = c.req.param('*');
const path = c.req.path;

const basePath = rest ? path.replace(new RegExp(`${rest}$`), '') : path;

c.set('basePath', basePath);
return next();
}),
)
.use<{ Variables: { basePath: string } }>((c, next) => {
c.set('basePath', c.var.basePath ?? c.req.path);
return next();
});
}
improved it a bit and turned into a more reusable middleware kinda
10 replies
HHono
Created by Huge Letters on 6/22/2024 in #help
How to resolve relative routes
well of course I would solve it after posting this
.get(':p{.*}', (c) => {
const url = c.req.path.replace(c.req.param('p') ?? '', '');
return c.render(
<div>
not found. <a href={url}>go to homepage</a>
</div>,
);
})
.get(':p{.*}', (c) => {
const url = c.req.path.replace(c.req.param('p') ?? '', '');
return c.render(
<div>
not found. <a href={url}>go to homepage</a>
</div>,
);
})
hope someone would enjoy the meme at least...
10 replies
HHono
Created by Huge Letters on 6/22/2024 in #help
How to resolve relative routes
hope that helps me get an answer faster...
10 replies