what url
A whatwg url builder for
deno
┌────────────────────────────────────────────────────────────────────────────────────────────────┐
│ href │
├──────────┬──┬─────────────────────┬────────────────────────┬───────────────────────────┬───────┤
│ protocol │ │ auth │ host │ path │ hash │
│ │ │ ├─────────────────┬──────┼──────────┬────────────────┤ │
│ │ │ │ hostname │ port │ pathname │ search │ │
│ │ │ │ │ │ ├─┬──────────────┤ │
│ │ │ │ │ │ │ │ query │ │
" https: // user : pass @ sub.example.com : 8080 /p/a/t/h ? query=string #hash "
│ │ │ │ │ hostname │ port │ │ │ │
│ │ │ │ ├─────────────────┴──────┤ │ │ │
│ protocol │ │ username │ password │ host │ │ │ │
├──────────┴──┼──────────┴──────────┼────────────────────────┤ │ │ │
│ origin │ │ origin │ pathname │ search │ hash │
├─────────────┴─────────────────────┴────────────────────────┴──────────┴────────────────┴───────┤
│ href │
└────────────────────────────────────────────────────────────────────────────────────────────────┘
Import
import {
QueryParam,
QueryParams,
WhatUrl,
} from "https://github.com/codearoni/what_url/blob/master/mod.ts";
Usage
Basic url building:
const url = new WhatUrl()
.setProtocol("https:")
.setHostname("deno.land")
.setPathname("/path/to/endpoint")
.build();
console.log(url.href);
Build a WhatUrl given an existing string
const url = new WhatUrl(
"https://what:1234@deno.land:8080/path/to/endpoint?x=hello_world&y=&z=true#asdf",
).build();
console.log(url.protocol);
console.log(url.user);
console.log(url.password);
console.log(url.hostname);
console.log(url.port);
console.log(url.pathname);
console.log(url.query);
console.log(url.search);
console.log(url.path);
console.log(url.hash);
console.log(url.origin);
console.log(url.auth);
console.log(url.host);
console.log(url.href);
Build a WhatUrl based on an existing WhatUrl
const urlA = new WhatUrl()
.setProtocol("https:")
.setHostname("subdomain.example.com")
.setPort(3000)
.build();
const urlB = new WhatUrl(urlA)
.addParam("x", 1)
.addParam("y", 2)
.addParam("z", 3)
.build();
console.log(urlB.href);
Parse a given url and retrieve a parameter
const url = new WhatUrl(
"https://what:1234@deno.land:8080/path/to/endpoint?x=hello_world&y=check123&z=true#asdf",
).build();
const param = url.getParam("y");
console.log(param);
Create a WhatUrl using supported datatypes for QueryParam
const numParam: QueryParam = 1;
const stringParam: QueryParam = "hello_world";
const boolParam: QueryParam = true;
const nullParam: QueryParam = null;
const urlA = new WhatUrl()
.setProtocol("https:")
.setHostname("subdomain.example.com")
.setPort(3000)
.addParam("a", numParam)
.addParam("b", stringParam)
.addParam("c", boolParam)
.addParam("d", nullParam)
.build();
console.log(urlA.href);
Create different urls with the same query string
const queryMap: QueryParams = new Map();
queryMap.set("a", "1");
queryMap.set("b", "asdf");
queryMap.set("c", "");
const urlA = new WhatUrl()
.setProtocol("https:")
.setHostname("subdomain.example.com")
.setPort(3000)
.setQuery(queryMap)
.build();
const urlB = new WhatUrl()
.setProtocol("https:")
.setHostname("deno.land")
.setPort(4000)
.setQuery(queryMap)
.build();
const urlC = new WhatUrl()
.setProtocol("https:")
.setHostname("some.site.net")
.setPort(5000)
.setQuery(queryMap)
.build();
console.log(urlA.href);
console.log(urlB.href);
console.log(urlC.href);
Add a given url to the query string of another url
const baseUrl = new WhatUrl(
"https://what:1234@deno.land:8080",
).build();
const urlInParams = "https://some.url.example.com/path/to/endpoint?a=1&b=2&c=3";
const url = new WhatUrl(baseUrl)
.addParam("embedded", urlInParams)
.build();
console.log(url.href);
Add a WhatUrl to the query string of another url
const urlInParams = new WhatUrl()
.setProtocol("https:")
.setHostname("subdomain.example.com")
.setPort(3000)
.addParam("x", null)
.addParam("y", 500)
.addParam("z", "hello_world")
.build();
const baseUrl = new WhatUrl(
"https://what:1234@deno.land:8080",
)
.addParam("embedded", urlInParams)
.build();
console.log(baseUrl.href);