Skip to content

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.

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.

@since0.1.0

Url
;

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.

@since0.1.0

Url
.
type Url.Input = string | Url.Url | Url.Url.Parts | Url.Url.Options | URL

@since0.1.0

Input
;
  • string — a simple string URL
  • globalThis.URL — the built-in JavaScript URL class
  • Url.Url.Parts — an object containing individual parts of a URL
  • Url.Url.Options — an object containing a URL string and optional search parameters
  • Url.Url — existing Url can be used too

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.Url
export unsafeMake

Creates an immutable Url object. Throws an error if the input is invalid.

@example

import { Url } from 'fx-fetch';
const url = Url.unsafeMake('https://api.example.com');

@since0.1.0

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

@since2.0.0

@since2.0.0

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.

@example

import { Url } from 'fx-fetch';
import { Option } from 'effect';
const urlOption = Url.make('https://api.example.com');
if (Option.isSome(urlOption)) {
const url = urlOption.value;
}

@since0.1.0

make
('https://example.com/');

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.

@since0.1.0

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;
}

@since0.1.0

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.

@parampredicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array.

@paramthisArg An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.

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.Url
export unsafeMake

Creates an immutable Url object. Throws an error if the input is invalid.

@example

import { Url } from 'fx-fetch';
const url = Url.unsafeMake('https://api.example.com');

@since0.1.0

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-encoded
  • password — The password of credentials, automatically percent-encoded
  • hostname — 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, undefined or array of string | number | undefined values
    • instance of Map
    • array of tuples

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.

@since0.1.0

Url
.
type Url.Options = {
readonly url: URL | string;
readonly searchParams?: SearchParamsInput;
}

@since0.1.0

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.

@parampredicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array.

@paramthisArg An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.

filter
: ['active', 'new'],
},
};
const
const url: Url.Url
url
=
import Url
Url
.
function unsafeMake(input: Url.Url.Input): Url.Url
export unsafeMake

Creates an immutable Url object. Throws an error if the input is invalid.

@example

import { Url } from 'fx-fetch';
const url = Url.unsafeMake('https://api.example.com');

@since0.1.0

unsafeMake
(
const options: Url.Url.Options
options
);

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.Url
export unsafeMake

Creates an immutable Url object. Throws an error if the input is invalid.

@example

import { Url } from 'fx-fetch';
const url = Url.unsafeMake('https://api.example.com');

@since0.1.0

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.

@parampredicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array.

@paramthisArg An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.

filter
: ['active', 'new'],
category: undefined
category
:
var undefined
undefined
,
},
});
import Url
Url
.
function format(url: Url.Url): string
export format

Converts a Url.Url to a string.

@example

import { Url } from 'fx-fetch';
const url = Url.make('https://api.example.com/users');
const urlString = Url.format(url); // 'https://api.example.com/users'

@since0.1.0

format
(
const url: Url.Url
url
); // 'https://example.com?q=lorem+ipsum&page=1&filter=active&filter=new'

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.Url
export unsafeMake

Creates an immutable Url object. Throws an error if the input is invalid.

@example

import { Url } from 'fx-fetch';
const url = Url.unsafeMake('https://api.example.com');

@since0.1.0

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.

@example

import { Url } from 'fx-fetch';
const url = Url.make('https://api.example.com');
const urlWithParam = Url.appendSearchParam(url, 'tag', 'news');

@since0.1.0

appendSearchParam
(
const url: Url.Url
url
, 'tag', 'active'); // 'https://example.com?tag=new&tag=active'

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.Url
export unsafeMake

Creates an immutable Url object. Throws an error if the input is invalid.

@example

import { Url } from 'fx-fetch';
const url = Url.unsafeMake('https://api.example.com');

@since0.1.0

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.

@example

import { Url } from 'fx-fetch';
const url = Url.unsafeMake({
url: 'https://example.com',
searchParams: {
tag: "new"
},
}); // 'https://example.com?tag=new'
const updatedUrl = Url.setSearchParam(url, 'tag', 'active'); // 'https://example.com?tag=active'

@since0.1.0

setSearchParam
(
const url: Url.Url
url
, 'tag', 'active'); // 'https://example.com?tag=active'

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.Url
export unsafeMake

Creates an immutable Url object. Throws an error if the input is invalid.

@example

import { Url } from 'fx-fetch';
const url = Url.unsafeMake('https://api.example.com');

@since0.1.0

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): string
export format

Converts a Url.Url to a string.

@example

import { Url } from 'fx-fetch';
const url = Url.make('https://api.example.com/users');
const urlString = Url.format(url); // 'https://api.example.com/users'

@since0.1.0

format
(
const url: Url.Url
url
); // 'https://example.com?tag=new&tag=sale'
// Set 'tag' parameter to 'active', replacing existing values
const 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.

@example

import { Url } from 'fx-fetch';
const url = Url.unsafeMake({
url: 'https://example.com',
searchParams: {
tag: ['new', 'sale'],
},
});
Url.format(url); // 'https://example.com?tag=new&tag=sale'
// Set 'tag' parameter to 'active', replacing existing values
url.pipe(
Url.setSearchParams({
"tag": "active"
}),
Url.format // 'https://example.com?tag=active'
);
// Add new 'q' parameter without removing existing ones
url.pipe(
Url.setSearchParams({
"q": "Lorem ipsum",
}),
Url.format // 'https://example.com?tag=new&tag=sale&q=Lorem+ipsum'
);

@since0.1.0

setSearchParams
({
"tag": "active"
}),
import Url
Url
.
function format(url: Url.Url): string
export format

Converts a Url.Url to a string.

@example

import { Url } from 'fx-fetch';
const url = Url.make('https://api.example.com/users');
const urlString = Url.format(url); // 'https://api.example.com/users'

@since0.1.0

format
// 'https://example.com?tag=active'
);
// Add new 'q' parameter without removing existing ones
const 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.

@example

import { Url } from 'fx-fetch';
const url = Url.unsafeMake({
url: 'https://example.com',
searchParams: {
tag: ['new', 'sale'],
},
});
Url.format(url); // 'https://example.com?tag=new&tag=sale'
// Set 'tag' parameter to 'active', replacing existing values
url.pipe(
Url.setSearchParams({
"tag": "active"
}),
Url.format // 'https://example.com?tag=active'
);
// Add new 'q' parameter without removing existing ones
url.pipe(
Url.setSearchParams({
"q": "Lorem ipsum",
}),
Url.format // 'https://example.com?tag=new&tag=sale&q=Lorem+ipsum'
);

@since0.1.0

setSearchParams
({
"q": "Lorem ipsum",
}),
import Url
Url
.
function format(url: Url.Url): string
export format

Converts a Url.Url to a string.

@example

import { Url } from 'fx-fetch';
const url = Url.make('https://api.example.com/users');
const urlString = Url.format(url); // 'https://api.example.com/users'

@since0.1.0

format
// 'https://example.com?tag=new&tag=sale&q=Lorem+ipsum'
);

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.Url
export unsafeMake

Creates an immutable Url object. Throws an error if the input is invalid.

@example

import { Url } from 'fx-fetch';
const url = Url.unsafeMake('https://api.example.com');

@since0.1.0

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): string
export format

Converts a Url.Url to a string.

@example

import { Url } from 'fx-fetch';
const url = Url.make('https://api.example.com/users');
const urlString = Url.format(url); // 'https://api.example.com/users'

@since0.1.0

format
(
const url: Url.Url
url
); // 'https://example.com?tag=new&tag=sale'
// Remove specific 'tag' parameter
const 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.

@example

import { Url } from 'fx-fetch';
const url = Url.unsafeMake({
url: 'https://example.com',
searchParams: {
tag: ['new', 'sale'],
},
});
Url.format(url); // 'https://example.com?tag=new&tag=sale'
// Remove specific 'tag' parameter
url.pipe(
Url.deleteSearchParam('tag', 'sale'),
Url.format // 'https://example.com?tag=new'
);
// Remove all 'tag' parameters
url.pipe(
Url.deleteSearchParam('tag'),
Url.format // 'https://example.com'
);

@since0.1.0

deleteSearchParam
('tag', 'sale'),
import Url
Url
.
function format(url: Url.Url): string
export format

Converts a Url.Url to a string.

@example

import { Url } from 'fx-fetch';
const url = Url.make('https://api.example.com/users');
const urlString = Url.format(url); // 'https://api.example.com/users'

@since0.1.0

format
// 'https://example.com?tag=new'
);
// Remove all 'tag' parameters
const 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.

@example

import { Url } from 'fx-fetch';
const url = Url.unsafeMake({
url: 'https://example.com',
searchParams: {
tag: ['new', 'sale'],
},
});
Url.format(url); // 'https://example.com?tag=new&tag=sale'
// Remove specific 'tag' parameter
url.pipe(
Url.deleteSearchParam('tag', 'sale'),
Url.format // 'https://example.com?tag=new'
);
// Remove all 'tag' parameters
url.pipe(
Url.deleteSearchParam('tag'),
Url.format // 'https://example.com'
);

@since0.1.0

deleteSearchParam
('tag',
var undefined
undefined
),
import Url
Url
.
function format(url: Url.Url): string
export format

Converts a Url.Url to a string.

@example

import { Url } from 'fx-fetch';
const url = Url.make('https://api.example.com/users');
const urlString = Url.format(url); // 'https://api.example.com/users'

@since0.1.0

format
// 'https://example.com'
);