sveltejs / kit

web development, streamlined
https://svelte.dev/docs/kit
MIT License
18.66k stars 1.93k forks source link

Add a way to access available param-matchers from inside a library #12094

Open LorisSigrist opened 6 months ago

LorisSigrist commented 6 months ago

Describe the problem

It's currently not possible to access the param-matcher-functions that are available in an App from inside a library. They need to be passed into the library by reference, one-by-one.

I have a library that requires access to the matcher-functions as it operates on routeIDs.

As discussed in: https://discord.com/channels/457912077277855764/1226858732223205497/1226858732223205497

Describe the proposed solution

A way to access the matcher-functions that are present in an app. For example an $app/matchers module where we can import the matcher functions from. Given that matchers are already shipped to the client this wouldn't make a meaningful bundle-size difference, it's just about exposing them.

Example:

import * as matchers from "$app/matchers"

matchers.int("123") // true
matchers.float("asd") //false

const availableMatchers = Object.keys(matchers) // ["int", "float", "uuid"]

Alternatives considered

Require library users to pass in matcher-functions one by one. This is awkward since they'er all exported as "match" and the matcher name is implicit from the filename.

import { init } from "some-library"
import { match as int } from "../params/int.js"
import { match as float } from "../params/float.js"

init({ matchers: { int, float }})

Importance

nice to have

Additional Information

No response

david-plugge commented 6 months ago

You can use globs here, but not in your library. The lib user has to glob all matchers and pass them into your entry function. Thats what im doing in such cases. Its not the optimal solution, but still better than manually having to add all matchers one by one.