-
-
Notifications
You must be signed in to change notification settings - Fork 359
Url
module
#4177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Url
module
#4177
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b0b4ed5
add Url module
KhraksMamtsov 367c553
rename arg
KhraksMamtsov 1b25b40
PR fixes
KhraksMamtsov 23f135e
cleanup
KhraksMamtsov 08688c0
add copy
KhraksMamtsov 4e2d863
simplify make ctor
KhraksMamtsov f1e7cc9
inline copy into immutableSetter for URL
KhraksMamtsov 52ae514
add Url.mutate
tim-smart a517364
wip
tim-smart File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,8 @@ | ||
--- | ||
"@effect/platform": patch | ||
--- | ||
|
||
`Url` module has been introduced: | ||
|
||
- immutable setters with dual-function api | ||
- integration with `UrlParams` |
This file contains hidden or 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,163 @@ | ||
/** | ||
* @since 1.0.0 | ||
*/ | ||
import * as Cause from "effect/Cause" | ||
import * as Either from "effect/Either" | ||
import { dual } from "effect/Function" | ||
import * as UrlParams from "./UrlParams.js" | ||
|
||
/** | ||
* @since 1.0.0 | ||
* @category constructors | ||
*/ | ||
export const fromString: { | ||
(url: string, base?: string | URL | undefined): Either.Either<URL, Cause.IllegalArgumentException> | ||
} = (url, base) => | ||
Either.try({ | ||
try: () => new URL(url, base), | ||
catch: (cause) => | ||
new Cause.IllegalArgumentException(cause instanceof globalThis.Error ? cause.message : "Invalid input") | ||
}) | ||
|
||
/** | ||
* @since 1.0.0 | ||
* @category utils | ||
*/ | ||
export const mutate: { | ||
(f: (url: URL) => void): (self: URL) => URL | ||
(self: URL, f: (url: URL) => void): URL | ||
} = dual(2, (self: URL, f: (url: URL) => void) => { | ||
const copy = new URL(self) | ||
f(copy) | ||
return copy | ||
}) | ||
|
||
/** @internal */ | ||
const immutableURLSetter = <P extends keyof URL>(property: P): { | ||
(value: URL[P]): (url: URL) => URL | ||
(url: URL, value: URL[P]): URL | ||
} => | ||
dual(2, (url: URL, value: URL[P]) => | ||
mutate(url, (url) => { | ||
url[property] = value | ||
})) | ||
|
||
/** | ||
* @since 1.0.0 | ||
* @category setters | ||
*/ | ||
export const setHash: { | ||
(hash: string): (url: URL) => URL | ||
(url: URL, hash: string): URL | ||
} = immutableURLSetter("hash") | ||
|
||
/** | ||
* @since 1.0.0 | ||
* @category setters | ||
*/ | ||
export const setHost: { | ||
(host: string): (url: URL) => URL | ||
(url: URL, host: string): URL | ||
} = immutableURLSetter("host") | ||
|
||
/** | ||
* @since 1.0.0 | ||
* @category setters | ||
*/ | ||
export const setHostname: { | ||
(hostname: string): (url: URL) => URL | ||
(url: URL, hostname: string): URL | ||
} = immutableURLSetter("hostname") | ||
/** | ||
* @since 1.0.0 | ||
* @category setters | ||
*/ | ||
export const setHref: { | ||
(href: string): (url: URL) => URL | ||
(url: URL, href: string): URL | ||
} = immutableURLSetter("href") | ||
|
||
/** | ||
* @since 1.0.0 | ||
* @category setters | ||
*/ | ||
export const setPassword: { | ||
(password: string): (url: URL) => URL | ||
(url: URL, password: string): URL | ||
} = immutableURLSetter("password") | ||
|
||
/** | ||
* @since 1.0.0 | ||
* @category setters | ||
*/ | ||
export const setPathname: { | ||
(pathname: string): (url: URL) => URL | ||
(url: URL, pathname: string): URL | ||
} = immutableURLSetter("pathname") | ||
|
||
/** | ||
* @since 1.0.0 | ||
* @category setters | ||
*/ | ||
export const setPort: { | ||
(port: string): (url: URL) => URL | ||
(url: URL, port: string): URL | ||
} = immutableURLSetter("port") | ||
|
||
/** | ||
* @since 1.0.0 | ||
* @category setters | ||
*/ | ||
export const setProtocol: { | ||
(protocol: string): (url: URL) => URL | ||
(url: URL, protocol: string): URL | ||
} = immutableURLSetter("protocol") | ||
|
||
/** | ||
* @since 1.0.0 | ||
* @category setters | ||
*/ | ||
export const setSearch: { | ||
(search: string): (url: URL) => URL | ||
(url: URL, search: string): URL | ||
} = immutableURLSetter("search") | ||
|
||
/** | ||
* @since 1.0.0 | ||
* @category setters | ||
*/ | ||
export const setUsername: { | ||
(username: string): (url: URL) => URL | ||
(url: URL, username: string): URL | ||
} = immutableURLSetter("username") | ||
|
||
/** | ||
* @since 1.0.0 | ||
* @category setters | ||
*/ | ||
export const setUrlParams: { | ||
(urlParams: UrlParams.UrlParams): (url: URL) => URL | ||
(url: URL, urlParams: UrlParams.UrlParams): URL | ||
} = dual(2, (url: URL, searchParams: UrlParams.UrlParams) => | ||
mutate(url, (url) => { | ||
url.search = UrlParams.toString(searchParams) | ||
})) | ||
|
||
/** | ||
* @since 1.0.0 | ||
* @category getters | ||
*/ | ||
export const urlParams = (url: URL): UrlParams.UrlParams => UrlParams.fromInput(url.searchParams) | ||
|
||
/** | ||
* @since 1.0.0 | ||
* @category utils | ||
*/ | ||
export const modifyUrlParams: { | ||
(f: (urlParams: UrlParams.UrlParams) => UrlParams.UrlParams): (url: URL) => URL | ||
(url: URL, f: (urlParams: UrlParams.UrlParams) => UrlParams.UrlParams): URL | ||
} = dual(2, (url: URL, f: (urlParams: UrlParams.UrlParams) => UrlParams.UrlParams) => | ||
mutate(url, (url) => { | ||
const params = f(UrlParams.fromInput(url.searchParams)) | ||
url.search = UrlParams.toString(params) | ||
})) |
This file contains hidden or 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 hidden or 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,39 @@ | ||
import * as Url from "@effect/platform/Url" | ||
import * as UrlParams from "@effect/platform/UrlParams" | ||
import { assert, describe, it } from "@effect/vitest" | ||
import { Cause, Effect } from "effect" | ||
import { constVoid } from "effect/Function" | ||
|
||
describe("Url", () => { | ||
const testURL = new URL("https://example.com/test") | ||
|
||
describe("mutate", () => { | ||
it.effect("immutable", () => | ||
Effect.gen(function*() { | ||
const url = Url.mutate(testURL, constVoid) | ||
assert.notStrictEqual(url, testURL) | ||
})) | ||
}) | ||
|
||
describe("fromString", () => { | ||
it.effect("fails on incorrect url", () => | ||
Effect.gen(function*() { | ||
const error = yield* Url.fromString("??").pipe(Effect.flip) | ||
assert.instanceOf(error, Cause.IllegalArgumentException) | ||
})) | ||
}) | ||
|
||
describe("setters", () => { | ||
it("immutable", () => { | ||
const hashUrl = Url.setHash(testURL, "test") | ||
assert.notStrictEqual(hashUrl, testURL) | ||
assert.strictEqual(hashUrl.toString(), "https://example.com/test#test") | ||
}) | ||
}) | ||
|
||
it("modifyUrlParams", () => { | ||
const paramsUrl = Url.modifyUrlParams(testURL, UrlParams.append("key", "value")) | ||
assert.notStrictEqual(paramsUrl, testURL) | ||
assert.strictEqual(paramsUrl.toString(), "https://example.com/test?key=value") | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.