[**Documentation**](../../../../README)

***

[Documentation](../../../../README) / [@zerotal/core](../../README) / [http](../README) / Uri

# Class: Uri

Defined in: [http/Uri.ts:150](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/http/Uri.ts#L150)

Fluent, immutable URI value object. Parse an absolute or relative URL, read its
parts, and rewrite scheme/host/path/query/fragment — every mutator returns a
NEW `Uri`, so instances are safe to share and reuse.

Construct via the [uri](../functions/uri) helper or the static factories ([Uri.of](#of),
[Uri.to](#to), [Uri.current](#current), [Uri.route](#route)). For app-aware URL
generation and signing, see the higher-level [Url](../variables/Url) / [url](#url) service.

## Example

```ts
Uri.of("https://example.com/users?page=1")
  .withQuery({ page: 2, sort: "name" })
  .withFragment("top")
  .value(); // "https://example.com/users?page=2&sort=name#top"
```

## Throws

from [Uri.url](#url) (and, transitively, [Uri.current](#current))
when called on a relative URI that has no host.

## Components

### scheme()

> **scheme**(): `string` \| `undefined`

Defined in: [http/Uri.ts:205](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/http/Uri.ts#L205)

The scheme (e.g. `"https"`), or undefined for a relative URI.

#### Returns

`string` \| `undefined`

***

### user()

> **user**(): `string` \| `undefined`

Defined in: [http/Uri.ts:212](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/http/Uri.ts#L212)

The decoded userinfo username, or undefined.

#### Returns

`string` \| `undefined`

***

### password()

> **password**(): `string` \| `undefined`

Defined in: [http/Uri.ts:219](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/http/Uri.ts#L219)

The decoded userinfo password, or undefined.

#### Returns

`string` \| `undefined`

***

### host()

> **host**(): `string` \| `undefined`

Defined in: [http/Uri.ts:226](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/http/Uri.ts#L226)

The host (without port), or undefined for a relative URI.

#### Returns

`string` \| `undefined`

***

### port()

> **port**(): `number` \| `undefined`

Defined in: [http/Uri.ts:233](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/http/Uri.ts#L233)

The port number, or undefined when none is set.

#### Returns

`number` \| `undefined`

***

### path()

> **path**(): `string`

Defined in: [http/Uri.ts:240](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/http/Uri.ts#L240)

The path component (may be empty).

#### Returns

`string`

***

### fragment()

> **fragment**(): `string` \| `undefined`

Defined in: [http/Uri.ts:247](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/http/Uri.ts#L247)

The fragment (without the leading `#`), or undefined.

#### Returns

`string` \| `undefined`

***

### withScheme()

> **withScheme**(`scheme`): `Uri`

Defined in: [http/Uri.ts:300](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/http/Uri.ts#L300)

Return a copy with the scheme replaced (trailing `://` is stripped).

#### Parameters

##### scheme

`string`

#### Returns

`Uri`

***

### withUser()

> **withUser**(`user`, `password?`): `Uri`

Defined in: [http/Uri.ts:307](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/http/Uri.ts#L307)

Return a copy with the userinfo username (and optional password) set.

#### Parameters

##### user

`string` \| `undefined`

##### password?

`string`

#### Returns

`Uri`

***

### withPassword()

> **withPassword**(`password`): `Uri`

Defined in: [http/Uri.ts:314](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/http/Uri.ts#L314)

Return a copy with the userinfo password set.

#### Parameters

##### password

`string` \| `undefined`

#### Returns

`Uri`

***

### withHost()

> **withHost**(`host`): `Uri`

Defined in: [http/Uri.ts:321](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/http/Uri.ts#L321)

Return a copy with the host replaced.

#### Parameters

##### host

`string`

#### Returns

`Uri`

***

### withPort()

> **withPort**(`port`): `Uri`

Defined in: [http/Uri.ts:328](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/http/Uri.ts#L328)

Return a copy with the port set (pass undefined to remove it).

#### Parameters

##### port

`number` \| `undefined`

#### Returns

`Uri`

***

### withPath()

> **withPath**(`path`): `Uri`

Defined in: [http/Uri.ts:335](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/http/Uri.ts#L335)

Return a copy with the path replaced; a non-empty path is given a leading slash.

#### Parameters

##### path

`string`

#### Returns

`Uri`

***

### withFragment()

> **withFragment**(`fragment`): `Uri`

Defined in: [http/Uri.ts:342](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/http/Uri.ts#L342)

Return a copy with the fragment set (pass undefined to remove it).

#### Parameters

##### fragment

`string` \| `undefined`

#### Returns

`Uri`

## Construction

### of()

> `static` **of**(`value`): `Uri`

Defined in: [http/Uri.ts:163](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/http/Uri.ts#L163)

Build a Uri from a string (or clone another Uri).

#### Parameters

##### value

`string` \| `Uri`

#### Returns

`Uri`

***

### to()

> `static` **to**(`path`): `Uri`

Defined in: [http/Uri.ts:172](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/http/Uri.ts#L172)

Build an absolute Uri for `path`, relative to the app URL (`config('app.url')`).

#### Parameters

##### path

`string`

#### Returns

`Uri`

***

### current()

> `static` **current**(): `Uri`

Defined in: [http/Uri.ts:183](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/http/Uri.ts#L183)

The current request's full URL.

#### Returns

`Uri`

#### Throws

when called outside an active HTTP request.

***

### route()

> `static` **route**(`name`, `params?`): `Uri`

Defined in: [http/Uri.ts:191](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/http/Uri.ts#L191)

Build a Uri from a named route + params.

#### Parameters

##### name

`string`

##### params?

`Record`\<`string`, `string` \| `number`\> = `{}`

#### Returns

`Uri`

***

### intended()

> **intended**(`fallback?`): `Uri`

Defined in: [http/Uri.ts:411](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/http/Uri.ts#L411)

Resolve the URL the user was heading to before authentication (the session's
`intended_url`, set by RequireAuth), falling back to `fallback`. Cross-origin stored URLs
are rejected (open-redirect guard). Returns a fresh Uri pointing at the resolved target.

#### Parameters

##### fallback?

`string` = `"/"`

#### Returns

`Uri`

#### Example

```ts
return uri().intended("/dashboard").redirect();
```

## Output

### url()

> **url**(): `string`

Defined in: [http/Uri.ts:255](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/http/Uri.ts#L255)

The assembled absolute URL string.

#### Returns

`string`

#### Throws

when the URI is relative (has no host).

***

### redirect()

> **redirect**(`status?`): [`ResponseBuilder`](../../index/classes/ResponseBuilder)

Defined in: [http/Uri.ts:427](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/http/Uri.ts#L427)

Issue a redirect to this URI, returning a [ResponseBuilder](../../index/classes/ResponseBuilder) for flash chaining.

#### Parameters

##### status?

`301` \| `302` \| `303` \| `307` \| `308`

#### Returns

[`ResponseBuilder`](../../index/classes/ResponseBuilder)

#### Throws

when called outside an active HTTP request.

***

### value()

> **value**(): `string`

Defined in: [http/Uri.ts:440](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/http/Uri.ts#L440)

The assembled URI string. Unlike [Uri.url](#url), this works for both
absolute and relative URIs.

#### Returns

`string`

***

### toString()

> **toString**(): `string`

Defined in: [http/Uri.ts:466](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/http/Uri.ts#L466)

Alias for [Uri.value](#value) — enables string coercion and template literals.

#### Returns

`string`

***

### toJSON()

> **toJSON**(): `string`

Defined in: [http/Uri.ts:474](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/http/Uri.ts#L474)

Serialize to the URI string when passed to `JSON.stringify`.

#### Returns

`string`

## Query

### query()

> **query**(): [`UriQueryString`](../interfaces/UriQueryString)

Defined in: [http/Uri.ts:277](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/http/Uri.ts#L277)

A read-only view over the query string.

#### Returns

[`UriQueryString`](../interfaces/UriQueryString)

***

### withQuery()

> **withQuery**(`query`): `Uri`

Defined in: [http/Uri.ts:350](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/http/Uri.ts#L350)

Merge the given parameters into the query string (overwriting existing keys).

#### Parameters

##### query

[`QueryInput`](../type-aliases/QueryInput)

#### Returns

`Uri`

***

### withQueryIfMissing()

> **withQueryIfMissing**(`query`): `Uri`

Defined in: [http/Uri.ts:358](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/http/Uri.ts#L358)

Add parameters only when their key is not already present.

#### Parameters

##### query

[`QueryInput`](../type-aliases/QueryInput)

#### Returns

`Uri`

***

### replaceQuery()

> **replaceQuery**(`query`): `Uri`

Defined in: [http/Uri.ts:370](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/http/Uri.ts#L370)

Replace the entire query string with the given parameters.

#### Parameters

##### query

[`QueryInput`](../type-aliases/QueryInput)

#### Returns

`Uri`

***

### pushOntoQuery()

> **pushOntoQuery**(`key`, `value`): `Uri`

Defined in: [http/Uri.ts:378](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/http/Uri.ts#L378)

Append a value to a key, turning it into a multi-value parameter.

#### Parameters

##### key

`string`

##### value

`string` \| `number`

#### Returns

`Uri`

***

### withoutQuery()

> **withoutQuery**(`keys`): `Uri`

Defined in: [http/Uri.ts:393](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/http/Uri.ts#L393)

Remove one or more keys from the query string.

#### Parameters

##### keys

`string`[]

#### Returns

`Uri`
