Skip to content

UrlParams

Models URL query parameters as ordered string pairs.

UrlParams is used for HTTP client query strings, URL-encoded form bodies, and server-side decoding. Values can be built from records, iterables, or native URLSearchParams, then updated, serialized, converted to a URL, or decoded with schemas.

27 exports Added in v4.0.0 Source

Combinators

append

Added in v4.0.0 Source

Appends a query parameter value without removing existing values for the key.

Signature

declare const append: {
  (key: string, value: Coercible): (self: UrlParams) => UrlParams;
  (self: UrlParams, key: string, value: Coercible): UrlParams;
};

appendAll

Added in v4.0.0 Source

Appends all query parameters produced from the supplied input.

Details

Existing parameters are preserved.

Signature

declare const appendAll: {
  (input: Input): (self: UrlParams) => UrlParams;
  (self: UrlParams, input: Input): UrlParams;
};

getAll

Added in v4.0.0 Source

Returns all values for a query parameter key in insertion order.

Details

Returns an empty array when the key is absent.

Signature

declare const getAll: {
  (key: string): (self: UrlParams) => readonly Array<string>;
  (self: UrlParams, key: string): readonly Array<string>;
}

getFirst

Added in v4.0.0 Source

Returns the first value for a query parameter key safely.

When to use

Use when duplicate query parameters are ordered and the first occurrence has precedence.

Details

Returns Option.none when the key is absent.

Signature

declare const getFirst: {
  (key: string): (self: UrlParams) => Option<string>;
  (self: UrlParams, key: string): Option<string>;
};

getLast

Added in v4.0.0 Source

Returns the last value for a query parameter key safely.

When to use

Use when duplicate query parameters are ordered and the last occurrence has precedence.

Details

Returns Option.none when the key is absent.

Signature

declare const getLast: {
  (key: string): (self: UrlParams) => Option<string>;
  (self: UrlParams, key: string): Option<string>;
};

remove

Added in v4.0.0 Source

Removes all query parameter values for the specified key.

Signature

declare const remove: {
  (key: string): (self: UrlParams) => UrlParams;
  (self: UrlParams, key: string): UrlParams;
};

set

Added in v4.0.0 Source

Sets a query parameter to a single value.

Details

Existing values for the same key are removed, and the new value is appended to the end.

Signature

declare const set: {
  (key: string, value: Coercible): (self: UrlParams) => UrlParams;
  (self: UrlParams, key: string, value: Coercible): UrlParams;
};

setAll

Added in v4.0.0 Source

Sets multiple query parameters from input.

Details

Keys present in the input replace existing values for those keys, while unmentioned existing parameters are preserved.

Signature

declare const setAll: {
  (input: Input): (self: UrlParams) => UrlParams;
  (self: UrlParams, input: Input): UrlParams;
};

transform

Added in v4.0.0 Source

Transforms the underlying ordered key-value pairs of UrlParams.

Details

The result is wrapped in a new UrlParams value.

Signature

declare const transform: {
  (f: (params: readonly Array<readonly [string, string]>) => readonly Array<readonly [string, string]>): (self: UrlParams) => UrlParams;
  (self: UrlParams, f: (params: readonly Array<readonly [string, string]>) => readonly Array<readonly [string, string]>): UrlParams;
}

Constructors

empty

Added in v4.0.0 Source

An empty UrlParams value.

Signature

declare const empty: UrlParams;

fromInput

Added in v4.0.0 Source

Creates UrlParams from a supported input shape.

Details

Primitive values are converted to strings, arrays produce repeated parameters, nested records use bracket notation, and undefined values are omitted.

Signature

declare function fromInput(input: Input): UrlParams;

make

Added in v4.0.0 Source

Creates UrlParams from ordered string key-value pairs.

Details

The input pairs are used as-is and are not coerced or normalized.

Signature

declare function make(params: readonly Array<readonly [string, string]>): UrlParams

Converting

Builds a readonly record from UrlParams.

Details

Keys with one value map to a string, and keys with multiple values map to a non-empty readonly array of strings.

Signature

declare const toReadonlyRecord: (
  self: UrlParams,
) => ReadonlyRecord<string, string | Arr.NonEmptyReadonlyArray<string>>;

toRecord

Added in v4.0.0 Source

Builds a Record containing all the key-value pairs in the given UrlParams as string (if only one value for a key) or a NonEmptyArray<string> (when more than one value for a key)

Signature

declare function toRecord(self: UrlParams): Record<string, string | Arr.NonEmptyArray<string>>;

toString

Added in v4.0.0 Source

Serializes UrlParams to a URL query string without a leading question mark.

Signature

declare function toString(input: Input): string;

Guards

isUrlParams

Added in v4.0.0 Source

Returns true when a value is a UrlParams instance.

Signature

declare function isUrlParams(u: unknown): u is UrlParams;

Instances

Equivalence

Added in v4.0.0 Source

Provides an order-sensitive Equivalence instance for UrlParams.

Details

Two values are equivalent when they contain the same key-value pairs in the same order.

Signature

declare const Equivalence: Equ.Equivalence<UrlParams>;

Models

Coercible type

Added in v4.0.0 Source

Primitive value that can be converted into a URL parameter string.

Gotchas

undefined values are skipped when constructing from input.

Signature

type Coercible = string | number | bigint | boolean | null | undefined;

CoercibleRecord type

Added in v4.0.0 Source

Record input whose fields can be coerced into URL parameter values.

Details

Nested records are rendered using bracket notation, and arrays produce repeated parameters.

Signature

type CoercibleRecord<A extends object = any> = { [K in keyof A]: CoercibleRecordField<A[K]> };

Input type

Added in v4.0.0 Source

Input accepted when constructing UrlParams.

Details

Values can be provided as a coercible record, an iterable of key-value pairs, or a native URLSearchParams value.

Signature

type Input =
  | UrlParams
  | CoercibleRecordInput
  | Iterable<readonly [string, Coercible]>
  | URLSearchParams;

UrlParams interface

Added in v4.0.0 Source

Immutable collection of URL query parameters.

Details

Parameters are stored as ordered string key-value pairs and can contain multiple values for the same key.

Signature

interface UrlParams extends Pipeable, Inspectable, Iterable<readonly [string, string]> {
  readonly "~effect/http/UrlParams": "~effect/http/UrlParams";
  readonly params: readonly Array<readonly [string, string]>;
}

Schemas

Extracts a JSON value from the first occurrence of the given field in the UrlParams.

Signature

declare const schemaJsonField: (field: string) => schemaJsonField;

schemaJsonField interface

Added in v4.0.0 Source

Schema type for decoding one URL parameter field as JSON.

Signature

interface schemaJsonField extends decodeTo<Schema.fromJsonString<Schema.Unknown>, UrlParamsSchema> {
  constructor(_: never);
}

schemaRecord

Added in v4.0.0 Source

Schema that decodes UrlParams into a record of key-value pairs.

Details

Keys with one value decode to a string, and keys with multiple values decode to a non-empty readonly array of strings.

Signature

declare const schemaRecord: schemaRecord;

schemaRecord interface

Added in v4.0.0 Source

Extract a record of key-value pairs from the UrlParams.

Signature

interface schemaRecord extends decodeTo<
  Schema.$Record<
    Schema.String,
    Schema.Union<readonly [Schema.String, Schema.NonEmptyArray<Schema.String>]>
  >,
  UrlParamsSchema,
  never,
  never
> {
  constructor(_: never);
}

Schema for UrlParams.

Details

The encoded representation is an array of string key-value tuples.

Signature

declare const UrlParamsSchema: UrlParamsSchema;

UrlParamsSchema interface

Added in v4.0.0 Source

Schema type for UrlParams.

Signature

interface UrlParamsSchema extends declare<UrlParams, ReadonlyArray<readonly [string, string]>> {
  constructor(_: never);
}