Skip to content

Commit

Permalink
feat: add uri.Port, uri.IsDefaultPort and fix uri.Host to retur…
Browse files Browse the repository at this point in the history
…n the host name without the port

Fix #3888
  • Loading branch information
MangelMaxime committed Sep 19, 2024
1 parent d61c993 commit 15878c6
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/Fable.Cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

* [JS/TS] Add support for `OrdinalIgnoreCase` overload for `String.EndsWith` (#3892) (by @goswinr)
* [JS/TS] Add `uri.Port`, `uri.IsDefaultPort` (by @MangelMaxime)

### Changed

Expand All @@ -18,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

* [JS/TS] Fix escaping of `{` and `}` in FormattableString (#3890) (by @roboz0r)
* [JS/TS] Fix `uri.Host` to return the host name without the port (by @MangelMaxime)

## 4.20.0 - 2024-09-04

Expand Down
2 changes: 2 additions & 0 deletions src/Fable.Transforms/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3621,11 +3621,13 @@ let uris
| "get_IsAbsoluteUri"
| "get_Scheme"
| "get_Host"
| "get_Port"
| "get_AbsolutePath"
| "get_AbsoluteUri"
| "get_PathAndQuery"
| "get_Query"
| "get_Fragment"
| "get_IsDefaultPort"
| "get_OriginalString" ->
Naming.removeGetSetPrefix i.CompiledName
|> Naming.lowerFirst
Expand Down
2 changes: 2 additions & 0 deletions src/fable-library-ts/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

* [JS/TS] Add support for `OrdinalIgnoreCase` overload for `String.EndsWith` (#3892) (by @goswinr)
* [JS/TS] Add `uri.Port`, `uri.IsDefaultPort` (by @MangelMaxime)

### Fixed

* [JS/TS] Fix escaping of `{` and `}` in FormattableString (#3890) (by @roboz0r)
* [JS/TS] Fix `uri.Host` to return the host name without the port (by @MangelMaxime)

## 1.4.3 - 2024-09-04

Expand Down
22 changes: 21 additions & 1 deletion src/fable-library-ts/Uri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,13 @@ export class Uri {
}

get host() {
return this.asUrl().host;
const host = this.asUrl().host;

if (host.includes(":")) {
return host.split(":")[0];
} else {
return host;
}
}

get absolutePath() {
Expand All @@ -188,6 +194,20 @@ export class Uri {
return this.asUrl().search;
}

get isDefaultPort() {
return this.port === 80;
}

get port() {
const port = this.asUrl().port;

if (port === "") {
return 80;
} else {
return parseInt(port);
}
}

get pathAndQuery() {
const url = this.asUrl();
return url.pathname + url.search;
Expand Down
26 changes: 26 additions & 0 deletions tests/Js/Main/UriTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,32 @@ let tests =
equal "#c" uri.Fragment
equal "http://www.test1.com/hello?a=b#c" uri.AbsoluteUri

testCase "Host from uri with port works" <| fun _ ->
let uri = Uri("http://www.test2.com:8080/hello?a=b#c")
equal "www.test2.com" uri.Host

testCase "Port default to 80 if no port is specified" <| fun _ ->
let uri = Uri("http://www.test3.com/hello?a=b#c")
equal 80 uri.Port

testCase "Port returns specified port" <| fun _ ->
let uri = Uri("http://www.test4.com:80/hello?a=b#c")
equal 80 uri.Port
let uri = Uri("http://www.test5.com:8080/hello?a=b#c")
equal 8080 uri.Port

testCase "IsDefaultPort returns true if no port is specified" <| fun _ ->
let uri = Uri("http://www.test3.com/hello?a=b#c")
equal true uri.IsDefaultPort

testCase "IsDefaultPort returns true if specified port is default" <| fun _ ->
let uri = Uri("http://www.test4.com:80/hello?a=b#c")
equal true uri.IsDefaultPort

testCase "IsDefaultPort returns false if specified port is not default" <| fun _ ->
let uri = Uri("http://www.test5.com:8080/hello?a=b#c")
equal false uri.IsDefaultPort

testCase "Uri from relative uri string works" <| fun _ ->
let uri = Uri("/hello.html", UriKind.Relative)
equal false uri.IsAbsoluteUri
Expand Down

0 comments on commit 15878c6

Please sign in to comment.