Usage
Creating a Pathway Instance
To start, create a new instance of Pathway with the base URL.
js
import Pathway from '@fwdmo/pathway'
const url = new Pathway('https://example.com/api')Adding and Modifying URL Components
You can use various methods to modify the URL step by step.
Adding a Base URL
The withBase method allows you to prepend or modify the base URL.
js
const newUrl = new Pathway('https://example.com')
.withBase('/newPath')
console.log(newUrl.stringify())
// Outputs: https://example.com/newPathAdding Query Parameters
Use withQuery to add query parameters.
js
const urlWithQuery = new Pathway('https://example.com/api')
.withQuery({ key: 'value', page: 1 })
console.log(urlWithQuery.stringify())
// Outputs: https://example.com/api?key=value&page=1Removing Query Parameters
The withoutQuery method allows you to remove specified query parameters.
js
const urlWithoutQuery = new Pathway('https://example.com/api?key=value&page=1')
.withoutQuery('page')
console.log(urlWithoutQuery.stringify())
// Outputs: https://example.com/api?key=valueAdding Leading or Trailing Slash
You can use withLeadingSlash or withTrailingSlash to ensure slashes are correctly added or removed.
js
const urlWithTrailingSlash = new Pathway('https://example.com/api')
.withTrailingSlash()
console.log(urlWithTrailingSlash.stringify())
// Outputs: https://example.com/api/Parsing the URL
You can parse the URL into its individual components using the parse method.
js
const parsed = new Pathway('https://example.com/api?key=value')
.parse()
console.log(parsed)
// Outputs: Parsed URL components (protocol, host, pathname, etc.)Finalizing the URL
Once you're done building the URL, use stringify or toString to convert it into a string.
js
const finalUrl = new Pathway('https://example.com/api')
.withQuery({ key: 'value' })
.stringify()
console.log(finalUrl)
// Outputs: https://example.com/api?key=valueExample
js
import Pathway from '@fwdmo/pathway'
const url = new Pathway('https://example.com/api')
.withQuery({ key: 'value', page: 1 })
.withQuery({ filter: 'active' })
.withoutQuery('page')
.withTrailingSlash()
console.log(url.stringify())
// Outputs: https://example.com/api?key=value&filter=active/