Skip to content

Routing by Annotations

Thiago Santos edited this page Nov 17, 2024 · 2 revisions

A compile-time routes generator.

import dev.programadorthi.routing.generated.configure @Route(path = "/create") fun something() { // ... } val router = routing { configure() } router.call(uri = "/create")

Setup

Using ksp

settings.gradle

pluginManagement { repositories { gradlePluginPortal() mavenCentral() } }

build.gradle

plugins { kotlin("multiplatform") id("com.google.devtools.ksp") version "2.0.10-1.0.24" id("dev.programadorthi.routing") version <routing-version> } kotlin { sourceSets { commonMain { dependencies { implementation("dev.programadorthi.routing:ksp-core-annotations:<routing-version>") // other dependency as core, compose, voyager, etc. } } } }

Usage

Important

A function must be top-level

@Route(path = "/create") fun something() { // ... }

Path parameters

By default function parameters must match to path parameters

@Route(path = "/create/{id}") fun something(id: Int) { // ... }

A custom name can be used applying annotation @Path

import dev.programadorthi.routing.annotation.Path @Route(path = "/create/{id}") fun something(@Path("id") userId: Int) { // ... }

Optional path parameter

@Route(path = "/create/{id?}") fun something(id: Int?) { // ... }

Named route

@Route(path = "/path/{name}", name = "named") fun something(name: String) { //... }

Receive a body

import dev.programadorthi.routing.annotation.Body @Route("/with-body") fun something(@Body user: User) { //... }

Optional body

import dev.programadorthi.routing.annotation.Body @Route("/with-body") fun something(@Body user: User?) { //... }

More ways to use @Route annotation checkout the Routes sample.

Compose Integration

@Composable top-level function can be annotated as well to route to it.

build.gradle

kotlin { sourceSets { commonMain { dependencies { implementation("dev.programadorthi.routing:ksp-core-annotations:<routing-version>") implementation("dev.programadorthi.routing:compose:<routing-version>") } } } }

Important

A composable function must be top-level

@Route(path = "/create") @Composable fun something() { // ... }

More ways to use @Route annotation with @Composable functions checkout the Composables sample.

Voyager Integration

Voyager Screen can be annotated as well to route to it.

build.gradle

kotlin { sourceSets { commonMain { dependencies { implementation("dev.programadorthi.routing:ksp-core-annotations:<routing-version>") implementation("dev.programadorthi.routing:voyager:<routing-version>") } } } }

Just apply the annotation to a Screen to handle it:

@Route("/screen") class MyScreen : Screen { @Composable override fun Content() { //... } }

Multiple routes to the same Screen

@Route("/screen") class MyScreen : Screen { @Route("/screen/{id}") constructor(id: Int) : this() @Route("/screen/user") constructor(@Body user: User?) : this() @Composable override fun Content() { //... } }

More ways to use @Route annotation with Screen checkout the Screens sample.

Clone this wiki locally