Skip to content

Class: CdnUrl ​

Defined in: builder/cdn-url.ts:95

Immutable, chainable facade over the functional core. Optional import — everything it does is possible with parseCdnUrl, serializeCdnUrl and plain array operations.

Every mutator returns a new instance; the original is never changed.

Example ​

ts
CdnUrl.parse(src).without(resize).with(preview(800, 600)).href

See ​

https://uploadcare.com/docs/cdn-operations/

Constructors ​

Constructor ​

ts
new CdnUrl(init): CdnUrl;

Defined in: builder/cdn-url.ts:98

Parameters ​

ParameterType
init| ParsedCdnUrl | CdnUrlInput

Returns ​

CdnUrl

Accessors ​

href ​

Get Signature ​

ts
get href(): string;

Defined in: builder/cdn-url.ts:125

The serialized URL string.

Returns ​

string


operations ​

Get Signature ​

ts
get operations(): CdnOperation[];

Defined in: builder/cdn-url.ts:120

A defensive copy of the operation chain (empty for group root urls).

Returns ​

CdnOperation[]

Methods ​

base() ​

ts
base(cdnBase): CdnUrl;

Defined in: builder/cdn-url.ts:233

Rebases the url onto another domain (trailing slash tolerated).

Parameters ​

ParameterType
cdnBasestring

Returns ​

CdnUrl


filename() ​

ts
filename(filename): CdnUrl;

Defined in: builder/cdn-url.ts:222

Sets or clears (null) the trailing filename.

Parameters ​

ParameterType
filenamestring | null

Returns ​

CdnUrl

Throws ​

TypeError on group root and proxy urls.


get() ​

ts
get(ref): CdnOperation | null;

Defined in: builder/cdn-url.ts:200

First matching operation (name, object or creator ref), or null.

Parameters ​

ParameterType
refOperationRef

Returns ​

CdnOperation | null


getAll() ​

ts
getAll(ref): CdnOperation[];

Defined in: builder/cdn-url.ts:213

Every matching operation, in chain order — the stackable-aware companion to CdnUrl.get. Returns a fresh array; empty when nothing matches.

Parameters ​

ParameterType
refOperationRef

Returns ​

CdnOperation[]

Example ​

ts
CdnUrl.parse(url).getAll(overlay) // → [{ name: 'overlay', … }, …]

has() ​

ts
has(ref): boolean;

Defined in: builder/cdn-url.ts:195

Whether a matching operation is present (name, object or creator ref).

Parameters ​

ParameterType
refOperationRef

Returns ​

boolean


replace() ​

ts
replace(operation): CdnUrl;

Defined in: builder/cdn-url.ts:171

Replaces the first matching operation in place, or appends it. Matching follows operationMatches, so counted operations are swapped rather than duplicated (thumbs~5 → thumbs~3).

Only the first match is touched. For a stackable operation that legitimately repeats (overlay, text, …) use CdnUrl.replaceAll instead.

Parameters ​

ParameterType
operationCdnOperation

Returns ​

CdnUrl

Example ​

ts
CdnUrl.parse(url).replace(resize({ width: 500 })).href

replaceAll() ​

ts
replaceAll(operation): CdnUrl;

Defined in: builder/cdn-url.ts:188

Collapses every matching operation into a single one, kept at the position of the first match; appends when nothing matches. The way to force a stackable operation to occur exactly once.

Parameters ​

ParameterType
operationCdnOperation

Returns ​

CdnUrl

Example ​

ts
// two overlays in, one overlay out
CdnUrl.parse(url).replaceAll(overlay(uuid, { size: ['50p', '50p'] })).href

toJSON() ​

ts
toJSON(): ParsedCdnUrl;

Defined in: builder/cdn-url.ts:135

The underlying ParsedCdnUrl, discriminated by kind.

Returns ​

ParsedCdnUrl


toString() ​

ts
toString(): string;

Defined in: builder/cdn-url.ts:130

Alias of CdnUrl.href for string coercion.

Returns ​

string


updateOperations() ​

ts
updateOperations(update): CdnUrl;

Defined in: builder/cdn-url.ts:261

The primitive every other operation mutator is sugar over: rewrites the whole chain through a callback. Reach for it when position matters and CdnUrl.replace / CdnUrl.replaceAll do not fit — replacing the nth stackable operation, inserting at an index, reordering, or matching on parameters rather than name.

The callback receives a defensive copy, so mutating it in place is safe; whatever it returns becomes the new chain.

Parameters ​

ParameterType
update(current) => readonly CdnOperation[]

Returns ​

CdnUrl

Throws ​

TypeError on group root urls, which cannot carry operations.

See ​

https://uploadcare.com/docs/transformations/image/

Example ​

ts
// replace the second overlay, leaving the others untouched
let seen = -1
url.updateOperations((ops) =>
  ops.map((op) => (operationMatches(op, overlay) && ++seen === 1 ? next : op))
)

with() ​

ts
with(...operations): CdnUrl;

Defined in: builder/cdn-url.ts:146

Appends operations to the chain.

Parameters ​

ParameterType
...operationsCdnOperation[]

Returns ​

CdnUrl

Throws ​

TypeError on group root urls, which cannot carry operations.


without() ​

ts
without(ref): CdnUrl;

Defined in: builder/cdn-url.ts:154

Removes every occurrence of an operation. Accepts the operation name, an operation object, or the creator itself: url.without(resize).

Parameters ​

ParameterType
refOperationRef

Returns ​

CdnUrl


parse() ​

ts
static parse(url): CdnUrl;

Defined in: builder/cdn-url.ts:107

Parses an existing CDN URL.

Parameters ​

ParameterType
urlstring

Returns ​

CdnUrl

Throws ​

TypeError when the URL cannot be interpreted as a CDN URL.