Skip to content

URL API

Create helpers via createWURL(url: string).

When to use

  • Domain info: getMainDomain/getSubdomain/getOrigin/getProtocol/getPort
  • Query params: parseQueryParams/addParamsToURL/replaceParams/setParam/removeParam
  • Safe navigation: isSameOrigin(other)
  • Hash & path: getHash/getPathname
  • Client headers (approx): getClientRequestHeaders()

Import

ts
import { createWURL } from 'nex-lib'

Methods

ts
const u = createWURL('https://sub.example.com:8080/path?foo=bar')
u.getMainDomain()            // 'example.com'
u.getSubdomain()             // 'sub'
u.getHostname()              // 'sub.example.com'
u.getOrigin()                // 'https://sub.example.com:8080'
u.getProtocol()              // 'https'
u.parseQueryParams()         // { foo: 'bar' }
u.addParamsToURL({ q: 'x' }) // 'https://sub.example.com:8080/path?foo=bar&q=x'
u.isHttps()                  // true
u.getPathname()              // '/path'
u.getPort()                  // '8080'

Errors

Throws Error('Invalid URL format') when input is invalid.

Client headers (browser)

getClientRequestHeaders() returns approximated headers (not real request headers): User-Agent, Accept-Language, Referer, Origin, Host, Cookie.

Released under the ISC License.