https://github.com/jakecyr/node-url-path
Simple utility class to help join URL parts and add query parameters.
https://github.com/jakecyr/node-url-path
node nodejs pathlib paths urls utility
Last synced: about 1 year ago
JSON representation
Simple utility class to help join URL parts and add query parameters.
- Host: GitHub
- URL: https://github.com/jakecyr/node-url-path
- Owner: jakecyr
- Created: 2023-08-23T04:04:47.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-08-23T04:27:13.000Z (over 2 years ago)
- Last Synced: 2025-01-17T09:35:46.404Z (about 1 year ago)
- Topics: node, nodejs, pathlib, paths, urls, utility
- Language: JavaScript
- Homepage:
- Size: 45.9 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Node URL Path
Simple utility class to help join URLs and add query parameters to URLs similar to how Python's pathlib Path object works with file paths.
## Installation
Install the latest version using `npm` with:
```bash
npm install node-url-path
```
## Usage
Using the `URLPath` class makes working with URLs simple. You can add paths to a URL that already has query parameters added to it and add additional query parameters if needed with the call of a method.
All `URLPath` methods return a `URLPath` instance so you can chain them together.
Get the final URL from a `URLPath` instance by retrieving the public `href` property.
```typescript
import { URLPath } from 'node-url-path';
const urlPath = new URLPath('https://google.com/api/')
.joinPath('/query')
.addQueryParams({
query: 'How to make URLs easier when working with Node',
apiKey: '1234567890',
});
console.log(urlPath.href);
// Outputs:
// https://google.com/api/query?query=How%20to%20make%20URLs%20easier%20when%20working%20with%20Node&apiKey=1234567890
```
or a more complex example:
```typescript
import { URLPath } from 'node-url-path';
const urlPath = new URLPath('https://google.com/api/?someExistingParams=1')
.joinPath('/query')
.addQueryParams({
query: 'How to make URLs easier when working with Node',
apiKey: '1234567890',
});
console.log(urlPath.href);
// Outputs:
// https://google.com/api/query?someExistingParams=1&query=How%20to%20make%20URLs%20easier%20when%20working%20with%20Node&apiKey=1234567890
```