Url
The Url module is one of the core building blocks of fx-fetch.
The module contains functions and types to create and manipulate URLs.
The Url Type
Section titled “The Url Type”A Url represents immutable and clonable HTTP URL data. It is a base element of the Url module.
import { import Url
Url } from 'fx-fetch';
type type Type = Url.Url
Type = import Url
Url.export Url
Represents immutable URL.
Url;The Input Type
Section titled “The Input Type”A Url.Input represents all the possible inputs that can be used to create a Url.
Concrete types that make up the Input union:
import { import Url
Url } from 'fx-fetch';
type type Type = string | Url.Url | Url.Url.Parts | Url.Url.Options | URL
Type = import Url
Url.export Url
Represents immutable URL.
Url.type Url.Input = string | Url.Url | Url.Url.Parts | Url.Url.Options | URL
Input;string— a simple string URLglobalThis.URL— the built-in JavaScript URL classUrl.Url.Parts— an object containing individual parts of a URLUrl.Url.Options— an object containing a URL string and optional search parametersUrl.Url— existing Url can be used too
Constructors
Section titled “Constructors”unsafeMake
Section titled “unsafeMake”Creates a Url from any of Url.Input.
Throws an IllegalArgumentException if the provided input is invalid.
import { import Url
Url } from 'fx-fetch';
// ┌─── Url.Url// ▼const const url: Url.Url
url = import Url
Url.function unsafeMake(input: Url.Url.Input): Url.Urlexport unsafeMake
Creates an immutable Url object. Throws an error if the input is invalid.
unsafeMake('https://example.com/');Similar to unsafeMake, but returns an Option instead of throwing an error if the input is invalid.
If the input is invalid, it returns None. If valid, it returns Some containing the Url.
import { import Option
Option } from 'effect';import { import Url
Url } from 'fx-fetch';
// ┌─── Option.Option<Url.Url>// ▼const const url: Option.Option<Url.Url>
url = import Url
Url.function make(input: Url.Url.Input): Option.Option<Url.Url>export make
Creates an immutable Url object. Returns Option.none() if the input is invalid.
make('https://example.com/');from Url.Parts
Section titled “from Url.Parts”For creating a Url from an object Url.Parts use same functions make or unsafeMake.
The Parts type is super primitive. It’s basically an object with properties similar to the built-in JavaScript URL object, but with better DX.
Everything except the hostname is optional.
import { import Url
Url } from 'fx-fetch';
const const parts: Url.Url.Parts
parts: import Url
Url.export Url
Represents immutable URL.
Url.type Url.Parts = { readonly hash?: string; readonly hostname: string; readonly password?: string; readonly pathname?: string; readonly port?: string | number; readonly protocol: string; readonly searchParams?: SearchParamsInput; readonly username?: string;}
Parts = { protocol: string
protocol: 'https:', username?: string
username: 'admin', password?: string
password: '123456', hostname: string
hostname: 'example.com', // ◀︎── required property pathname?: string
pathname: '/api/v1/resource', port?: string | number
port: 8080, searchParams?: SearchParamsInput
searchParams: { key: string
key: 'value', ReadonlyArray<readonly [key: string, value: SearchParamValueInput]>.filter<S>(predicate: (value: readonly [key: string, value: SearchParamValueInput], index: number, array: readonly (readonly [key: string, value: SearchParamValueInput])[]) => value is S, thisArg?: any): S[] (+1 overload)
Returns the elements of an array that meet the condition specified in a callback function.
filter: ['a', 'b'], id: number
id: 123, }, hash?: string
hash: '#section',};
const const url: Url.Url
url = import Url
Url.function unsafeMake(input: Url.Url.Input): Url.Urlexport unsafeMake
Creates an immutable Url object. Throws an error if the input is invalid.
unsafeMake(const parts: Url.Url.Parts
parts);All properties are designed for better DX. Here is a brief description of each:
protocol— The protocol scheme of the URL (e.g.,"http:","https://","ftp")username— The username of credentials, automatically percent-encodedpassword— The password of credentials, automatically percent-encodedhostname— The domain name or IP address of the URL (e.g.,"example.com","example.net/")pathname— The path component of the URL (e.g.,"/path/to/","foo/bar")port— The port number of the URL (e.g.,80,"443")hash— The fragment identifier of the URL (e.g.,"#section1","top")searchParams— Query parameters with various supported types:- instance of
globalThis.URLSearchParams - object with
string,number,undefinedor array ofstring | number | undefinedvalues - instance of
Map - array of tuples
- instance of
from Url.Options
Section titled “from Url.Options”For creating a Url from an object Url.Options use same functions make or unsafeMake.
Reason for having this variant is your comfort.
import { import Url
Url } from 'fx-fetch';
const const options: Url.Url.Options
options: import Url
Url.export Url
Represents immutable URL.
Url.type Url.Options = { readonly url: URL | string; readonly searchParams?: SearchParamsInput;}
Options = { url: string | URL
url: 'https://example.com', // ◀︎── required property searchParams?: SearchParamsInput
searchParams: { q: string
q: 'lorem ipsum', page: number
page: 1, ReadonlyArray<readonly [key: string, value: SearchParamValueInput]>.filter<S>(predicate: (value: readonly [key: string, value: SearchParamValueInput], index: number, array: readonly (readonly [key: string, value: SearchParamValueInput])[]) => value is S, thisArg?: any): S[] (+1 overload)
Returns the elements of an array that meet the condition specified in a callback function.
filter: ['active', 'new'], },};
const const url: Url.Url
url = import Url
Url.function unsafeMake(input: Url.Url.Input): Url.Urlexport unsafeMake
Creates an immutable Url object. Throws an error if the input is invalid.
unsafeMake(const options: Url.Url.Options
options);Conversions
Section titled “Conversions”format
Section titled “format”Formats the Url into a string representation.
import { import Url
Url } from 'fx-fetch';
const const url: Url.Url
url = import Url
Url.function unsafeMake(input: Url.Url.Input): Url.Urlexport unsafeMake
Creates an immutable Url object. Throws an error if the input is invalid.
unsafeMake({ url: string | URL
url: 'https://example.com', searchParams: { q: string; page: number; filter: string[]; category: undefined;}
searchParams: { q: string
q: 'lorem ipsum', page: number
page: 1, ReadonlyArray<readonly [key: string, value: SearchParamValueInput]>.filter<S>(predicate: (value: readonly [key: string, value: SearchParamValueInput], index: number, array: readonly (readonly [key: string, value: SearchParamValueInput])[]) => value is S, thisArg?: any): S[] (+1 overload)
Returns the elements of an array that meet the condition specified in a callback function.
filter: ['active', 'new'], category: undefined
category: var undefined
undefined, },});
import Url
Url.function format(url: Url.Url): stringexport format
Converts a Url.Url to a string.
format(const url: Url.Url
url); // 'https://example.com?q=lorem+ipsum&page=1&filter=active&filter=new'Combinators
Section titled “Combinators”appendSearchParam
Section titled “appendSearchParam”The appendSearchParam function appends a new search parameter to the existing URL’s query string.
import { import Url
Url } from 'fx-fetch';
const const url: Url.Url
url = import Url
Url.function unsafeMake(input: Url.Url.Input): Url.Urlexport unsafeMake
Creates an immutable Url object. Throws an error if the input is invalid.
unsafeMake({ url: string | URL
url: 'https://example.com', searchParams: { tag: string;}
searchParams: { tag: string
tag: "new" },}); // 'https://example.com?tag=new'
const const updatedUrl: Url.Url
updatedUrl = import Url
Url.function appendSearchParam(url: Url.Url, key: string, value: SearchParamValueInput): Url.Url (+1 overload)export appendSearchParam
Appends a search parameter to a Url.
appendSearchParam(const url: Url.Url
url, 'tag', 'active'); // 'https://example.com?tag=new&tag=active'setSearchParam
Section titled “setSearchParam”The setSearchParam function sets or updates a search parameter in the existing URL’s query string.
If the value is undefined, the parameter is removed.
import { import Url
Url } from 'fx-fetch';
const const url: Url.Url
url = import Url
Url.function unsafeMake(input: Url.Url.Input): Url.Urlexport unsafeMake
Creates an immutable Url object. Throws an error if the input is invalid.
unsafeMake({ url: string | URL
url: 'https://example.com', searchParams: { tag: string;}
searchParams: { tag: string
tag: "new" },}); // 'https://example.com?tag=new'
const const updatedUrl: Url.Url
updatedUrl = import Url
Url.function setSearchParam(url: Url.Url, key: string, value: SearchParamValueInput): Url.Url (+1 overload)export setSearchParam
Sets a search parameter on a Url. If the value is undefined, the parameter is removed.
setSearchParam(const url: Url.Url
url, 'tag', 'active'); // 'https://example.com?tag=active'setSearchParams
Section titled “setSearchParams”The setSearchParams function sets or updates multiple search parameters in the existing URL’s query string.
import { import Url
Url } from 'fx-fetch';
const const url: Url.Url
url = import Url
Url.function unsafeMake(input: Url.Url.Input): Url.Urlexport unsafeMake
Creates an immutable Url object. Throws an error if the input is invalid.
unsafeMake({ url: string | URL
url: 'https://example.com', searchParams: { tag: string[];}
searchParams: { tag: string[]
tag: ['new', 'sale'], },});
import Url
Url.function format(url: Url.Url): stringexport format
Converts a Url.Url to a string.
format(const url: Url.Url
url); // 'https://example.com?tag=new&tag=sale'
// Set 'tag' parameter to 'active', replacing existing valuesconst url: Url.Url
url.Pipeable.pipe<Url.Url, Url.Url, string>(this: Url.Url, ab: (_: Url.Url) => Url.Url, bc: (_: Url.Url) => string): string (+21 overloads)
pipe( import Url
Url.function setSearchParams(params: SearchParamsInput): (url: Url.Url) => Url.Url (+1 overload)export setSearchParams
Sets or updates multiple search parameters in the existing URL's query string.
setSearchParams({ "tag": "active" }), import Url
Url.function format(url: Url.Url): stringexport format
Converts a Url.Url to a string.
format // 'https://example.com?tag=active');
// Add new 'q' parameter without removing existing onesconst url: Url.Url
url.Pipeable.pipe<Url.Url, Url.Url, string>(this: Url.Url, ab: (_: Url.Url) => Url.Url, bc: (_: Url.Url) => string): string (+21 overloads)
pipe( import Url
Url.function setSearchParams(params: SearchParamsInput): (url: Url.Url) => Url.Url (+1 overload)export setSearchParams
Sets or updates multiple search parameters in the existing URL's query string.
setSearchParams({ "q": "Lorem ipsum", }), import Url
Url.function format(url: Url.Url): stringexport format
Converts a Url.Url to a string.
format // 'https://example.com?tag=new&tag=sale&q=Lorem+ipsum');deleteSearchParam
Section titled “deleteSearchParam”The deleteSearchParam function removes a search parameter from the existing URL’s query string.
Can be used to remove a specific value for a parameter or all values associated with that parameter.
import { import Url
Url } from 'fx-fetch';
const const url: Url.Url
url = import Url
Url.function unsafeMake(input: Url.Url.Input): Url.Urlexport unsafeMake
Creates an immutable Url object. Throws an error if the input is invalid.
unsafeMake({ url: string | URL
url: 'https://example.com', searchParams: { tag: string[];}
searchParams: { tag: string[]
tag: ['new', 'sale'], },});
import Url
Url.function format(url: Url.Url): stringexport format
Converts a Url.Url to a string.
format(const url: Url.Url
url); // 'https://example.com?tag=new&tag=sale'
// Remove specific 'tag' parameterconst url: Url.Url
url.Pipeable.pipe<Url.Url, Url.Url, string>(this: Url.Url, ab: (_: Url.Url) => Url.Url, bc: (_: Url.Url) => string): string (+21 overloads)
pipe( import Url
Url.function deleteSearchParam(key: string, value?: SearchParamValueInput): (url: Url.Url) => Url.Url (+1 overload)export deleteSearchParam
Deletes search parameters from a Url.
If a value is provided, only that value is removed; otherwise, all values for the key are removed.
deleteSearchParam('tag', 'sale'), import Url
Url.function format(url: Url.Url): stringexport format
Converts a Url.Url to a string.
format // 'https://example.com?tag=new');
// Remove all 'tag' parametersconst url: Url.Url
url.Pipeable.pipe<Url.Url, Url.Url, string>(this: Url.Url, ab: (_: Url.Url) => Url.Url, bc: (_: Url.Url) => string): string (+21 overloads)
pipe( import Url
Url.function deleteSearchParam(key: string, value?: SearchParamValueInput): (url: Url.Url) => Url.Url (+1 overload)export deleteSearchParam
Deletes search parameters from a Url.
If a value is provided, only that value is removed; otherwise, all values for the key are removed.
deleteSearchParam('tag', var undefined
undefined), import Url
Url.function format(url: Url.Url): stringexport format
Converts a Url.Url to a string.
format // 'https://example.com');