@relatecom/utils
v0.0.3
Published
utility functions for use with frontend services and components
Downloads
18
Readme
@relatecom/utils
Overview
This library provides a simple and efficient way to build URLs with query parameters. It allows you to construct URLs by specifying a root path, a path, and optional query parameters.
Installation
To install the library, run the following command:
npm install @relatecom/utilsor
yarn add @relatecom/utilsUsage
Here's an example of how to use the library:
import { UrlBuilder } from '@relatecom/utils';
const ROOT_PATH = 'https://example.com/api/users';
const { buildUrl } = new UrlBuilder(ROOT_PATH);
const url = buildUrl('/list', {
limit: 10,
offset: 20,
});
console.log(url); // Output: https://example.com/api/users/list?limit=10&offset=20API
UrlBuilder Class
Constructor
rootPath: The root path of the URL.
const urlBuilder = new UrlBuilder('https://example.com/api');Methods
buildUrl(path: string, queryParams?: object): Builds a URL from a path and query parameters. +path: The path of the URL. +queryParams: An object containing query parameters. + Returns: The built URL.
const url = urlBuilder.buildUrl('users', {
limit: 10,
offset: 20,
});Query Parameters
Query parameters can be passed as an object to the buildUrl method. The object can contain string, number, or boolean values.
const url = urlBuilder.buildUrl('users', {
limit: 10,
offset: 20,
isAdmin: true,
});Error Handling
If the path is empty, the buildUrl method will throw an error.
try {
const url = urlBuilder.buildUrl('');
} catch (error) {
console.error(error); // Output: Path cannot be empty
}Contributing
Contributions are welcome! If you have any suggestions or bug reports, please open an issue or submit a pull request.
License
This library is licensed under the MIT License.
