jycouet / kitql

A set of standalone tools to SpeedRun WebApps!
https://kitql.dev
MIT License
405 stars 37 forks source link

One more way to format a route type with `vite-plugin-kit-routes` #666

Closed Evertt closed 1 month ago

Evertt commented 4 months ago

Is your feature request related to a problem? Please describe.

I just like concise code whenever possible. And currently, when I have these routes:

const PAGES = {
    "/blog": `/blog`,
    "/blog/[id=integer]": (params: {
        id: Parameters<typeof import("../params/integer.ts").match>[0]
    }) => {
        return `/blog/${params.id}`
    },
}

Then I'd need to use it like this in my svelte component:

<script lang="ts">
    import { route } from "$lib/ROUTES"
</script>

<a href={route("/blog/[id=integer]", { id: 1 })}>Go to post 1</a>

Describe the solution you'd like

Before I found this vite plugin I was working on my own version for this, and I generated my routes firstly as a type and then an object that would work like that type. Which allowed me to generate a route with the type /blog/${number}. So instead of needing to write:

<a href={route("/blog/[id=integer]", { id: 1 })}>Go to post 1</a>

I only needed to write:

<a href={route("/blog/1")}>Go to post 1</a>

And it would get correctly type-checked. If I had written route("/blog/some-slug"), typescript would warn me that that was invalid.

jycouet commented 4 months ago

This is working with numbers, but I think that it will not work with string as "everything" will be valid.

So I'm not sure what to suggest.

Did you try format_short that could be a good compromise ?

jycouet commented 2 months ago

Any update ?

jycouet commented 1 month ago

Feel free to re open if you have news 😉