Read and rebuild a URL
Split a URL into its parts, change one query parameter, rebuild it, and refuse a non-URL.
Apache-2.0Runs on this page
Package: @awacloud/fw
Split a URL into origin, path, query (as data) and fragment, set one query parameter and rebuild it, and report a string that is not a URL.
Uses @awacloud/fw (Apache-2.0). This example's code is Apache-2.0.
When to use it
Search pages, filters and shareable links: read the state a URL carries, change one parameter, and produce the link again with every value encoded correctly.
Copy-paste steps
npm install @awacloud/fw- Serve
node_modules/@awacloud/fw/src/at/vendor/fw/(node serve.jsfrom the examples root does this; any static server works). - Copy
index.htmlandmain.jsnext to each other and openindex.htmlthrough that server. The../../_style/examples.csslink is optional: copy that file along, or delete the link.
What it shows
url.parseURL(href, base?): the parts of a URL (a relative one needs abase); it throws on a string that is not a URL, and the page says so.- Its
query: a query from an HTML form encodes spaces as+, andparseURLreads them back as spaces (parseURL(href, base, { space: 'percent' })keeps a literal+). Repeated keys (tag=io&tag=url) become an array. url.buildURL({ ...parts, query }): the URL again, the query encoded (fw examples→fw%20examples).
Code
main.js
// SPDX-License-Identifier: Apache-2.0
import fw from '@awacloud/fw';
import { url } from '@awacloud/fw/io/codec/url.js';
fw.runtime.registerAllDeep([url]);
const URLS = fw.runtime.resolve('url');
/**
* The parts of a URL, its query as data (a `+` read as a space, as an HTML
* form encodes it).
* @param {string} href @param {string} [base] resolves a relative URL
* @throws {Error} when `href` is not a URL
*/
export const describe = (href, base) => URLS.parseURL(href, base);
/**
* The same URL with one query value set; the query is encoded again.
* @param {ReturnType<typeof describe>} parts @param {string} key @param {string} value
*/
export function withParam(parts, key, value) {
return URLS.buildURL({ ...parts, search: '', query: { ...parts.query, [key]: value } });
}
/** @param {string} id */
const field = (id) => /** @type {HTMLInputElement} */ (document.getElementById(id));
/** @param {string} id @param {string} value */
const show = (id, value) => { /** @type {HTMLElement} */ (document.getElementById(id)).textContent = value; };
function update() {
const href = field('url').value.trim();
try {
const parts = describe(href);
show('error', '');
show('origin', parts.origin);
show('path', parts.pathname);
show('query', JSON.stringify(parts.query));
show('hash', parts.hash);
show('rebuilt', field('page').value ? withParam(parts, 'page', field('page').value) : parts.href);
} catch {
show('error', `Not a URL: "${href}" (a relative one needs a base)`); // engines word this error differently
}
}
field('url').addEventListener('input', update);
field('page').addEventListener('input', update);
update();index.html
<!doctype html>
<!-- SPDX-License-Identifier: Apache-2.0 -->
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>fw · Read and rebuild a URL</title>
<link rel="stylesheet" href="../../_style/examples.css">
<script type="importmap">
{ "imports": {
"@awacloud/fw": "/vendor/fw/main.js", "@awacloud/fw/": "/vendor/fw/" } }
</script>
<script type="module" src="./main.js"></script>
</head>
<body>
<main>
<h1>Read and rebuild a URL</h1>
<label for="url">URL</label>
<input id="url" type="text"
value="https://example.org:8443/docs/search?q=fw+examples&tag=io&tag=url#results" spellcheck="false">
<p id="error" class="error" aria-live="polite"></p>
<dl>
<dt>Origin</dt><dd><output id="origin"></output></dd>
<dt>Path</dt><dd><output id="path"></output></dd>
<dt>Query</dt><dd><output id="query"></output></dd>
<dt>Fragment</dt><dd><output id="hash"></output></dd>
</dl>
<label for="page">Set <code>page</code> to</label>
<input id="page" type="number" min="1">
<p>URL: <output id="rebuilt" for="url page"></output></p>
</main>
</body>
</html>