Skip to content

Commit

Permalink
tweaking RequestUrl and UrlString, tweaking code styling
Browse files Browse the repository at this point in the history
  • Loading branch information
yurique committed Jun 6, 2021
1 parent eca7b34 commit 7f77ff5
Show file tree
Hide file tree
Showing 13 changed files with 124 additions and 60 deletions.
33 changes: 33 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
# Changelog

### 0.13.5

##### `fetch` module: `RequestUrl`

* `hostname` renamed into `host` to reflect that it can contain the port
* `path` renamed into `segments` to better reflect what it is
* `uri` function renamed to `url`
* `RequestUrl` now makes sure to not contain empty segments (thus it is no longer possible,
accidentally or otherwise, to have a double `/` in the request URL)
* new `RequestUrl.fromLocation(location: dom.raw.Location)` builder
* Breaking: `addPath(path: String*)` renamed to `addSegments(path: String*)`
* New: `withParams(params: Map[String, Seq[String]])`
* New: `withSegments(segments: String*)`

`*path` methods will split the argument by `/` into a `Seq[String]`, ensuring there are
no empty segments.

`url`, `RequestUrl.apply` and `RequestUrl.fromLocation` ensure there are no empty segments as well.


##### `util` module: new utilities in the

* `UrlString` object
* `parse` – parse a string into a `dom.raw.Location`
```scala
val myUrl = "https://laminext.dev/path?param=1"
val location: dom.raw.Location = UrlString.parse(myUrl)
console.print(location.origin)
```
* `UrlUtils` object
* `encodeSearchParams(params: Map[String, Seq[String]]): String` – encode the given parameters into a URL search string (`?param=1&...`)
* `decodeSearchParams(search: String): Map[String, Seq[String]]` – decode a URL search string into a map of parameters

### 0.13.4

Sourcemaps are now pointing to GitHub.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ trait FetchSyntax {
absolute: String,
params: Map[String, Seq[String]] = Map.empty
): RequestUrl =
absolute match {
case UrlString(location) => RequestUrl.fromLocation(location).addParams(params)
case other => throw new IllegalArgumentException(s"Invalid URL: $other")
}
RequestUrl.fromLocation(UrlString.parse(absolute)).addParams(params)

implicit def syntaxFetchEventStreamBuilder(
underlying: FetchEventStreamBuilder
Expand Down
18 changes: 9 additions & 9 deletions modules/fetch/src/main/scala/io/laminext/fetch/RequestUrl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,33 @@ import scala.scalajs.js.URIUtils.encodeURIComponent
/**
* @param protocol http or https
* @param host hostname, optionally with port
* @param path a Seq of path segments
* @param segments a Seq of path segments
* @param params a Map of query search parameters
*/
case class RequestUrl private (
protocol: String,
host: String,
path: Seq[String] = Seq.empty,
params: Map[String, Seq[String]] = Map.empty
segments: Seq[String],
params: Map[String, Seq[String]]
) extends ToRequestUrl {

def encode: String = s"$protocol//$host/${path.map(encodeURIComponent).mkString("/")}${UrlUtils.encodeSearchParams(params)}"
def encode: String = s"$protocol//$host/${segments.map(encodeURIComponent).mkString("/")}${UrlUtils.encodeSearchParams(params)}"

override def apply(): String = encode

@inline def withHost(host: String): RequestUrl = this.copy(host = host)

@inline def withSegments(segments: String*): RequestUrl =
this.copy(
path = segments.filterNot(_.isEmpty)
segments = segments.filterNot(_.isEmpty)
)

@inline def withPath(path: String): RequestUrl =
this.withSegments(ArraySeq.unsafeWrapArray(path.split('/')): _*)

@inline def addSegments(segments: String*): RequestUrl =
this.withSegments(
this.path ++ segments: _*
this.segments ++ segments: _*
)

@inline def appendPath(path: String): RequestUrl =
Expand Down Expand Up @@ -63,13 +63,13 @@ object RequestUrl {
def apply(
protocol: String = "https",
host: String,
path: Seq[String] = Seq.empty,
segments: Seq[String] = Seq.empty,
params: Map[String, Seq[String]] = Map.empty
): RequestUrl =
new RequestUrl(
protocol = protocol,
host = host,
path = path.filterNot(_.isEmpty),
segments = segments.filterNot(_.isEmpty),
params = params
)

Expand All @@ -82,7 +82,7 @@ object RequestUrl {
RequestUrl(
protocol = location.protocol,
host = location.host,
path = segments,
segments = segments,
params = UrlUtils.decodeSearchParams(location.search)
)
}
Expand Down
28 changes: 13 additions & 15 deletions modules/util/src/main/scala/io/laminext/util/UrlString.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,21 @@ import scala.scalajs.js

object UrlString {

def unapply(url: String): Some[Location] = {
def parse(url: String): Location = {
val l = dom.document.createElement("a").asInstanceOf[HTMLAnchorElement]
l.href = url
Some(
js.Dynamic
.literal(
hash = l.hash,
protocol = l.protocol,
search = l.search,
href = l.href,
hostname = l.hostname,
port = l.port,
pathname = l.pathname,
host = l.host
)
.asInstanceOf[Location]
)
js.Dynamic
.literal(
hash = l.hash,
protocol = l.protocol,
search = l.search,
href = l.href,
hostname = l.hostname,
port = l.port,
pathname = l.pathname,
host = l.host
)
.asInstanceOf[Location]
}

}
2 changes: 1 addition & 1 deletion website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"private": true,
"license": "UNLICENSED",
"dependencies": {
"highlight.js": "^11.0.0",
"highlight.js": "^11.0.1",
"marked": "2.0.7"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion website/src/main/resources/doc/fetch/request-method.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ It provides functions to specify the HTTP method you want to use.
These functions return a `FetchEventStreamBuilder` which allows to specify the rest of the request settings.

Note that the `FetchEventStreamBuilder` is not immutable. It can be safely re-used, but calls to all configuration
methods do modify the instance.
methods **do** modify the instance.

```scala
import io.laminext.fetch.Fetch
Expand Down
1 change: 1 addition & 0 deletions website/src/main/resources/doc/news/index.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* [v0.13.5 release](/news/v0-13-5) (06.06.2021)
* [v0.13.4 release](/news/v0-13-4) (04.06.2021)
* [v0.13.3 release](/news/v0-13-3) (27.05.2021)
* [v0.13.2 release](/news/v0-13-2) (23.05.2021)
Expand Down
35 changes: 35 additions & 0 deletions website/src/main/resources/doc/news/v0.13.5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# v0.13.5 release

* 06.06.2021

### `fetch` module: `RequestUrl`

* `hostname` renamed into `host` to reflect that it can contain the port
* `path` renamed into `segments` to better reflect what it is
* `uri` function renamed to `url`
* `RequestUrl` now makes sure to not contain empty segments (thus it is no longer possible,
accidentally or otherwise, to have a double `/` in the request URL)
* new `RequestUrl.fromLocation(location: dom.raw.Location)` builder
* Breaking: `addPath(path: String*)` renamed to `addSegments(path: String*)`
* New: `withParams(params: Map[String, Seq[String]])`
* New: `withSegments(segments: String*)`

`*path` methods will split the argument by `/` into a `Seq[String]`, ensuring there are
no empty segments.

`url`, `RequestUrl.apply` and `RequestUrl.fromLocation` ensure there are no empty segments as well.


### `util` module: new utilities in the

* `UrlString` object
* `parse` – parse a string into a `dom.raw.Location`
```scala
val myUrl = "https://laminext.dev/path?param=1"
val location: dom.raw.Location = UrlString.parse(myUrl)
console.print(location.origin)
```
* `UrlUtils` object
* `encodeSearchParams(params: Map[String, Seq[String]]): String` – encode the given parameters into a URL search string (`?param=1&...`)
* `decodeSearchParams(search: String): Map[String, Seq[String]]` – decode a URL search string into a map of parameters

1 change: 1 addition & 0 deletions website/src/main/scala/io/laminext/site/Site.scala
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ object Site {
path = "news",
index = docPage("", "News", FileAsString("/doc/news/index.md")),
"" -> Seq(
docPage("v0-13-5", "v0.13.5", FileAsString("/doc/news/v0.13.5.md")),
docPage("v0-13-4", "v0.13.4", FileAsString("/doc/news/v0.13.4.md")),
docPage("v0-13-3", "v0.13.3", FileAsString("/doc/news/v0.13.3.md")),
docPage("v0-13-2", "v0.13.2", FileAsString("/doc/news/v0.13.2.md")),
Expand Down
4 changes: 2 additions & 2 deletions website/src/main/scala/io/laminext/site/TemplateVars.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package io.laminext.site
object TemplateVars {

val vars = Seq(
"laminextVersion" -> "0.13.4"
"laminextVersion" -> "0.13.5"
)

def apply(s: String): String =
vars.foldLeft(s) { case (acc, (varName, varValue)) =>
acc.replace(s"{{${varName}}}", varValue)
acc.replace(s"{{$varName}}", varValue)
}

}
32 changes: 13 additions & 19 deletions website/src/main/static/stylesheets/markdown.css
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
.prose {
& code:not(.hljs) {
@apply text-gray-900;
@apply font-medium;
/*@apply text-sm;*/
@apply bg-gray-200;
.prose code:not(.hljs) {
@apply bg-blue-50 !important;
@apply hover:bg-blue-200 !important;
@apply text-blue-900 !important;
@apply px-1 !important;
@apply rounded !important;
}

&::before, &::after {
content: " ";
}
}
.prose code:not(.hljs):before {
content: "" !important;
}

& h1, & h2, & h3, & h4, & h5 {
& code:not(.hljs) {
@apply bg-white;
@apply text-blue-700;
&::before, &::after {
content: "";
}
}
}
.prose code:not(.hljs):after {
content: "" !important;
}

15 changes: 10 additions & 5 deletions website/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const scalaOutputPath = path.resolve(__dirname, `./target/scala-${scalaVersion}`
const devServerHost = '127.0.0.1';
const devServerPort = 30088;

const devServer = _.mergeWith(
const devServer =
{
hot: true,
injectHot: true,
Expand All @@ -24,10 +24,15 @@ const devServer = _.mergeWith(
host: devServerHost,
historyApiFallback: {
index: ''
}
},
require('./devserver.config.js')
)
},
public: `${devServerHost}:${devServerPort}`,
firewall: false,
client: {
host: devServerHost,
port: devServerPort
},

}


function common(mode) {
Expand Down
8 changes: 4 additions & 4 deletions website/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2170,10 +2170,10 @@ hex-color-regex@^1.1.0:
resolved "https://registry.yarnpkg.com/hex-color-regex/-/hex-color-regex-1.1.0.tgz#4c06fccb4602fe2602b3c93df82d7e7dbf1a8a8e"
integrity sha512-l9sfDFsuqtOqKDsQdqrMRk0U85RZc0RtOR9yPI7mRVOa4FsR/BVnZ0shmQRM96Ji99kYZP/7hn1cedc1+ApsTQ==

highlight.js@^11.0.0:
version "11.0.0"
resolved "https://registry.yarnpkg.com/highlight.js/-/highlight.js-11.0.0.tgz#e22ac9ca45edc4f87a2187685d591a108ceb8449"
integrity sha512-ByaTMfsSuoqerTwemOgpIhfULEIaK52JJYhky/sK7/Yqc0+t7Uh5DHay9vIC94YXSupnQ1Vqfc9VXrYP4eXW3Q==
highlight.js@^11.0.1:
version "11.0.1"
resolved "https://registry.yarnpkg.com/highlight.js/-/highlight.js-11.0.1.tgz#a78bafccd9aa297978799fe5eed9beb7ee1ef887"
integrity sha512-EqYpWyTF2s8nMfttfBA2yLKPNoZCO33pLS4MnbXQ4hECf1TKujCt1Kq7QAdrio7roL4+CqsfjqwYj4tYgq0pJQ==

hosted-git-info@^2.1.4:
version "2.8.8"
Expand Down

0 comments on commit 7f77ff5

Please sign in to comment.