T
TanStack13mo ago
harsh-harlequin

Add file type to API route

I am trying to create an API route that generates custom meta images. I have successfully done this. But I am unable to add the .png file extension for the path. How can I create an API route like '/api/images/meta/$id.png'?
4 Replies
conscious-sapphire
conscious-sapphire13mo ago
I've been trying to figure it out as well. In Remix I could name the route like 'images.$id[.]png' to kind of escape the "." but I haven't found the equivalent in Tanstack Start. I'll let you know if I find a solution.
xenial-black
xenial-black13mo ago
we don't have a way to escape anything right now can you please create a github issue as a feature request?
harsh-harlequin
harsh-harlequinOP13mo ago
Will do. Thanks.
conscious-sapphire
conscious-sapphire13mo ago
An alternative, which opens up some nice possibilities to output different image types from the same route ''/api/images/meta/$id', is to think of $id as the filename + extension and then do something like this in your route:
import path from "node:path";

const { name, ext } = path.parse(params.id);
import path from "node:path";

const { name, ext } = path.parse(params.id);
Then you can handle requests for both /api/images/meta/test.jpg and /api/images/meta/test.png from the same route and return an error if you don't support the extension or file type.

Did you find this page helpful?