H
Hono3w ago
Meexa

How to do filepath as a param in an RPC route?

Hey, I'm trying to fetch a file/folder with an RPC route and want to add the filepath to the URL as a param, but this doesn't work because then I get a 404. Example: /api/folders/:filepath -> /api/folders/foo/bar/baz This results in a 404 because of the extra slashes is my guess. I tried with a wildcard and that works when I use curl/fetch, but I don't know how to add the filepath to the url using RPC /api/folders/folder/* -> api.folders.folder['*'].$get() <- how to add /foo/bar/baz to this? So I don't think wildcard is the way to go here, but I can't find anything about catch-all params or anything in the docs.
4 Replies
Nico
Nico3w ago
Hi @Meexa you can achieve this by using regex. Here’s an example from the docs https://github.com/honojs/middleware
GitHub
GitHub - honojs/middleware: monorepo for Hono third-party middlewar...
monorepo for Hono third-party middleware/helpers/wrappers - honojs/middleware
Meexa
Meexa3w ago
@Nico Is that the correct link? Where do I look for the example?
Nico
Nico3w ago
My apologies, here’s the right link https://hono.dev/docs/api/routing#including-slashes
Routing - Hono
Ultrafast web framework for Cloudflare Workers, Fastly Compute, Deno, Bun, Vercel, Node.js, and others. Fast, but not only fast.
Meexa
Meexa3w ago
Thanks 🙏 Hey, I've had some time to look into this and I have this endpoint now .get('/:filepath{.+}?', ...) Then I can execute this function like so
function getFolders(filepath?: string) {
return api.folders[':filepath{.+}?'].$get({ param: { filepath } })
}
function getFolders(filepath?: string) {
return api.folders[':filepath{.+}?'].$get({ param: { filepath } })
}
The problem I have now is since filepath is optional, the filepath can be undefined. What happens then is that This request is made http://localhost:3000/api/folders/undefined? If I do this { filepath: filepath ?? '' } then the request is http://localhost:3000/api/folders/? Both requests fail. What am I doing wrong? { filepath: filepath ?? '/' } seems to work. http://localhost:3000/api/folders//? Not sure how to get rid of the ? though
Want results from more Discord servers?
Add your server
More Posts