-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'issue/krz-186_url_query_parameters' into 'main'
KRZ-186 URL Query Parameters See merge request reactivecore/kreuzberg!50
- Loading branch information
Showing
15 changed files
with
272 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
examples/shared/src/main/scala/kreuzberg/examples/showcase/pages/NotFoundPage.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
package kreuzberg.examples.showcase.pages | ||
|
||
import kreuzberg.* | ||
import kreuzberg.extras.UrlResource | ||
import kreuzberg.scalatags.* | ||
import kreuzberg.scalatags.all.* | ||
|
||
case class NotFoundPage(path: String) extends ComponentBase { | ||
case class NotFoundPage(resource: UrlResource) extends ComponentBase { | ||
|
||
override def assemble(using context: AssemblerContext): Assembly = { | ||
div( | ||
s"Path ${path} not found" | ||
s"Path ${resource} not found" | ||
) | ||
} | ||
} |
11 changes: 7 additions & 4 deletions
11
extras/js/src/main/scala/kreuzberg/extras/BrowserRouting.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package kreuzberg.extras | ||
|
||
object UriHelper { | ||
inline def encodeUriComponent(s: String): String = scalajs.js.URIUtils.encodeURIComponent(s) | ||
|
||
inline def decodeUriComponent(s: String): String = scalajs.js.URIUtils.decodeURIComponent(s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
extras/jvm/src/main/scala/kreuzberg/extras/UriHelper.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package kreuzberg.extras | ||
|
||
import java.net.{URLDecoder, URLEncoder} | ||
import java.nio.charset.StandardCharsets | ||
|
||
object UriHelper { | ||
inline def encodeUriComponent(s: String): String = URLEncoder.encode(s, StandardCharsets.UTF_8) | ||
|
||
inline def decodeUriComponent(s: String): String = URLDecoder.decode(s, StandardCharsets.UTF_8) | ||
} |
10 changes: 10 additions & 0 deletions
10
extras/native/src/main/scala/kreuzberg/extras/UriHelper.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package kreuzberg.extras | ||
|
||
import java.net.{URLDecoder, URLEncoder} | ||
|
||
object UriHelper { | ||
// Scala Native doesn't know the Charset here | ||
inline def encodeUriComponent(s: String): String = URLEncoder.encode(s, "UTF-8") | ||
|
||
inline def decodeUriComponent(s: String): String = URLDecoder.decode(s, "UTF-8") | ||
} |
68 changes: 49 additions & 19 deletions
68
extras/shared/src/main/scala/kreuzberg/extras/PathCodec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,89 @@ | ||
package kreuzberg.extras | ||
|
||
/** Helper for encoding/decoding paths. */ | ||
/** Helper for encoding/decoding [[UrlResource]]. */ | ||
trait PathCodec[S] { | ||
def handles(path: String): Boolean | ||
def handles(resource: UrlResource): Boolean | ||
|
||
def decode(path: String): Option[S] | ||
def decode(resource: UrlResource): Option[S] | ||
|
||
def forceDecode(path: String): S = decode(path).getOrElse { | ||
def forceDecode(resource: UrlResource): S = decode(resource).getOrElse { | ||
throw new IllegalStateException("Invalid path") | ||
} | ||
|
||
def encode(value: S): String | ||
def encode(value: S): UrlResource | ||
} | ||
|
||
object PathCodec { | ||
|
||
/** A Constant path. */ | ||
def const(constantPath: String): PathCodec[Unit] = new PathCodec[Unit] { | ||
def const(path: String): PathCodec[Unit] = new PathCodec[Unit] { | ||
|
||
override def handles(path: String): Boolean = path == constantPath | ||
override def handles(resource: UrlResource): Boolean = resource.path == path | ||
|
||
override def decode(path: String): Option[Unit] = if (path == constantPath) { | ||
override def decode(resource: UrlResource): Option[Unit] = if (path == resource.path) { | ||
Some(()) | ||
} else { | ||
None | ||
} | ||
|
||
override def encode(value: Unit): String = constantPath | ||
override def encode(value: Unit): UrlResource = UrlResource(path) | ||
} | ||
|
||
def constWithQueryParams(path: String, params: String*): PathCodec[Seq[String]] = new PathCodec[Seq[String]] { | ||
override def handles(resource: UrlResource): Boolean = { | ||
resource.path == path && { | ||
val queryArgs = resource.queryArgs | ||
params.forall(p => queryArgs.contains(p)) | ||
} | ||
} | ||
|
||
override def decode(resource: UrlResource): Option[Seq[String]] = { | ||
if (resource.path != path) { | ||
None | ||
} else { | ||
val builder = Seq.newBuilder[String] | ||
val queryArgs = resource.queryArgs | ||
val it = params.iterator | ||
while (it.hasNext) { | ||
queryArgs.get(it.next()) match { | ||
case None => return None | ||
case Some(v) => builder += v | ||
} | ||
} | ||
Some(builder.result()) | ||
} | ||
} | ||
|
||
override def encode(value: Seq[String]): UrlResource = { | ||
UrlResource.encodeWithArgs(path, params.zip(value)) | ||
} | ||
} | ||
|
||
/** A Simple Prefix. */ | ||
def prefix(prefix: String): PathCodec[String] = new PathCodec[String] { | ||
|
||
override def handles(path: String): Boolean = path.startsWith(prefix) | ||
override def handles(resource: UrlResource): Boolean = resource.path.startsWith(prefix) | ||
|
||
override def decode(path: String): Option[String] = { | ||
if (handles(prefix)) { | ||
Some(path.stripPrefix(prefix)) | ||
override def decode(resource: UrlResource): Option[String] = { | ||
if (handles(resource)) { | ||
Some(resource.path.stripPrefix(prefix)) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
override def encode(value: String): String = { | ||
prefix + value | ||
override def encode(value: String): UrlResource = { | ||
UrlResource(prefix + value) | ||
} | ||
} | ||
|
||
/** Collects all. */ | ||
def all: PathCodec[String] = new PathCodec[String] { | ||
def all: PathCodec[UrlResource] = new PathCodec[UrlResource] { | ||
|
||
override def handles(path: String): Boolean = true | ||
override def handles(path: UrlResource): Boolean = true | ||
|
||
override def decode(path: String): Option[String] = Some(path) | ||
override def decode(resource: UrlResource): Option[UrlResource] = Some(resource) | ||
|
||
override def encode(value: String): String = value | ||
override def encode(value: UrlResource): UrlResource = value | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.