Common utilities
These are the common utilities shipped with @rokoli/bnb.
createURLBuilder
A factory function that helps you build URLs. It takes the base path of your URL as the first argument.
js
import { createURLBuilder } from '@rokoli/bnb/common'
const buildURL = createURLBuilder('https://example.org/api')
// https://example.org/api/shows/
buildURL('shows')
// https://example.org/api/shows/1/
buildURL('shows', 1)
// https://example.org/api/articles/?query=rokoli
buildURL('articles', new URLSearchParams({ query: 'rokoli' }))
const buildShowUrls = buildURL.prefix('shows')
// https://example.org/api/shows/1/
buildShowUrls('shows', 1)
import { createURLBuilder } from '@rokoli/bnb/common'
const buildURL = createURLBuilder('https://example.org/api')
// https://example.org/api/shows/
buildURL('shows')
// https://example.org/api/shows/1/
buildURL('shows', 1)
// https://example.org/api/articles/?query=rokoli
buildURL('articles', new URLSearchParams({ query: 'rokoli' }))
const buildShowUrls = buildURL.prefix('shows')
// https://example.org/api/shows/1/
buildShowUrls('shows', 1)
By default, all generated URLs have a trailing slash. You can pass false
as second argument to createURLBuilder
in order to avoid that.
js
import { createURLBuilder } from '@rokoli/bnb/common'
const buildURL = createURLBuilder('https://example.org/api', false)
// https://example.org/api/shows
buildURL('shows')
import { createURLBuilder } from '@rokoli/bnb/common'
const buildURL = createURLBuilder('https://example.org/api', false)
// https://example.org/api/shows
buildURL('shows')