Feature: #107104 - Introduce UrlFactory JavaScript module
See forge#107104
Description
TYPO3 already uses the native
URL and
URLSearch objects when
working with URLs. The module
@typo3/ has been
introduced to provide a consistent and convenient way to create and manage these
objects.
Impact
URL and
URLSearch objects can now be created by the factory's
create and
create methods.
The method
create creates a full
URL object and automatically
sets its base. It accepts the following arguments:
url- stringparameters- mixed
If provided, the
parameters value is passed to
create (described below).
The method
create creates a
URLSearch object and
accepts the following argument:
parameters- mixed
Parameters can be passed as plain string values or nested objects. Passing a plain array is not supported.
Note
When a nested object is passed, values of type
null or
undefined are discarded.
Examples
The following examples assume the existence of
TYPO3., pointing to the route
/custom_, while being on https:// for documentation
purposes.
import { UrlFactory } from '@typo3/core/factory/url-factory.js';
const url = UrlFactory.createUrl(
TYPO3.settings.ajaxUrls.my_dedicated_endpoint
);
console.log(url.toString());
// https://localhost/custom_endpoint
import { UrlFactory } from '@typo3/core/factory/url-factory.js';
const url = UrlFactory.createUrl(
TYPO3.settings.ajaxUrls.my_dedicated_endpoint,
{
foo: 'bar',
baz: {
hello: 'world',
},
}
);
console.log(url.toString());
// https://localhost/custom_endpoint?foo=bar&baz[hello]=world
import { UrlFactory } from '@typo3/core/factory/url-factory.js';
const urlSearchParams = UrlFactory.createSearchParams(
'foo=bar&baz=bencer'
);
console.log(urlSearchParams.toString());
// foo=bar&baz=bencer
import { UrlFactory } from '@typo3/core/factory/url-factory.js';
const urlSearchParams = UrlFactory.createSearchParams({
foo: 'bar',
baz: 'bencer',
});
console.log(urlSearchParams.toString());
// foo=bar&baz=bencer
Note
Unlike the native
URLSearch constructor, the
Url method does not support JavaScript
entries objects directly. If needed, entries can be converted into an
object by using
Object..