Sign in

    sw_router

    A fast and flexible URL router implementation for the MoonBit language.

    moonbit
    router
    Download zip
    Author
    Version
    0.2.0
    License
    Apache-2.0
    Last updated
    last year
    Downloads
    2K

    No dependencies

    #🚀 SW Router: A Lightweight MoonBit Router Library

    Build Status License codecov

    SW Router is a lightweight and efficient routing library for MoonBit applications. It provides a simple way to handle routing in your MoonBit projects with a clean and intuitive API.

    🚀 Key Features

    • đŸ› ī¸ Simple API - Easy to learn and use
    • 🚄 Fast Performance - Optimized for speed
    • đŸŽ¯ Pattern Matching - Support for route parameters
    • 🌟 MoonBit Native - Built specifically for MoonBit


    #đŸ“Ĩ Installation

    moon add ShellWen/sw-router

    #🚀 Usage Guide

    SW Router makes it simple to define and handle routes in your MoonBit applications.

    #🔍 Basic Usage

    fn main {
    let router = @sw_router.router?([
    ("/test", fn { _path, _query, _fragment => "main" }),
    (
    "/:foo/:bar",
    fn {
    path, query, fragment => {
    let foo = path.get("foo").unwrap()
    let bar = path.get("bar").unwrap()
    let flat_query = query.map(fn { (k, v) => k + "=" + v }).join("&")
    let fragment = fragment.or("")
    "FOO_\{foo}__BAR_\{bar}__QUERY_\{flat_query}__FRAGMENT_\{fragment}"
    }
    },
    ),
    ]).unwrap()
    let str = router.dispatch!("/test")
    assert_eq!(str, "main")
    let str = router.dispatch!("/hello/world?param=value&a=b#fragment")
    assert_eq!(str, "FOO_hello__BAR_world__QUERY_param=value&a=b__FRAGMENT_fragment")
    }

    #📚 Inspiration

    This project is inspired by modern routing libraries and aims to provide a similar experience for MoonBit developers.

    #📜 License

    This project is licensed under the Apache-2.0 License. See LICENSE for details.

    #đŸ“ĸ Contact & Support

    👋 If you like this project, give it a ⭐! Happy coding! 🚀

    AddRouteError

    pub suberror AddRouteError {
    ParseLocationError(LocationParseError)
    AlreadyExists(String)
    PathParamAlreadyExists(String)
    RouteShouldNotHaveQuery(String)
    RouteShouldNotHaveFragment(String)
    }
    Error types that may occur when adding routes ParseLocationError - Invalid location format in route pattern AlreadyExists - Route pattern conflicts with existing route PathParamAlreadyExists - Path parameter name is already used RouteShouldNotHaveQuery - Route pattern should not contain query string RouteShouldNotHaveFragment - Route pattern should not contain fragment
    impl Eq for AddRouteError

    DispatchError

    pub suberror DispatchError {
    InvalidLocation(LocationParseError)
    InvalidQuery(QueryParseError)
    RouteNotMatched(String)
    }
    Error types that may occur during route dispatch InvalidLocation - Failed to parse the request location InvalidQuery - Failed to parse query parameters RouteNotMatched - No matching route found for the location
    impl Eq for DispatchError

    LocationParseError

    pub suberror LocationParseError {
    ShouldStartWithSlash
    MoreThanOneQuery
    MoreThanOneFragment
    FragmentBeforeQuery
    EmptyLocation
    InvalidCharacter(Int)
    }

    QueryParseError

    pub suberror QueryParseError {
    InvalidCharacter(Int)
    }

    Router

    type Router[A]
    Router implementation Manages a collection of route configurations and handles dispatching requests to the appropriate handler based on the provided location

    Router::add_route

    fn[A] Router::add_route(self : Router[A], path : String, handler : (Map[String, String], Array[(String, String)], String?) -> A) -> Unit raise AddRouteError
    Adds a single route to the router Takes a path pattern and its handler function Path pattern must not contain query string or fragment Returns error if the route is invalid or conflicts with existing routes

    Router::add_routes

    fn[A] Router::add_routes(self : Router[A], routes : Array[(String, (Map[String, String], Array[(String, String)], String?) -> A)]) -> Unit raise AddRouteError
    Adds multiple routes to the router Takes an array of route configurations Returns error if any route is invalid or conflicts with existing routes

    Router::dispatch

    fn[A] Router::dispatch(self : Router[A], location : String) -> A raise DispatchError
    Dispatches a location to the appropriate handler Parses the provided location string, matches it against registered routes, and invokes the corresponding handler with extracted parameters

    router

    fn[A] router(configs : Array[(String, (Map[String, String], Array[(String, String)], String?) -> A)]) -> Router[A] raise AddRouteError
    Creates a new router instance Takes an array of route configurations and initializes the router Each configuration consists of a path pattern and its handler function Returns error if any route configuration is invalid

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    Š 2026 mooncakes.io