Package Exports
- build-url-ts
- build-url-ts/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (build-url-ts) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
build-url-ts
A small, fast library for building URLs with a fluent API. Fully typed for TypeScript and works in Node.js and browsers.
Features
- π Small & Fast - Minimal footprint with zero dependencies
- π¦ TypeScript Support - Full TypeScript definitions included
- π Universal - Works in Node.js and all modern browsers
- π§ Flexible - Multiple ways to handle array query parameters
- β¨ Clean API - Simple and intuitive interface
- π‘οΈ Safe - Properly encodes URLs and handles edge cases
Installation
# npm
npm install build-url-ts
# yarn
yarn add build-url-ts
# pnpm
pnpm add build-url-tsQuick Start
import { buildUrl } from 'build-url-ts';
const url = buildUrl('https://api.example.com', {
path: 'users/123',
queryParams: {
tab: 'profile',
limit: 10
}
});
// Result: https://api.example.com/users/123?tab=profile&limit=10API Reference
buildUrl(baseUrl?, options?)
Builds a complete URL from components.
Parameters
baseUrl(optional):string | null- The base URLoptions(optional):IBuildUrlOptions- URL building options
Options
interface IBuildUrlOptions {
path?: string | number; // Path to append
queryParams?: IQueryParams; // Query parameters object
hash?: string | number; // Hash/fragment identifier
lowerCase?: boolean; // Convert to lowercase
disableCSV?: boolean | IDisableCsvType; // Array handling
}Usage Examples
Basic URL Building
import { buildUrl } from 'build-url-ts';
// Simple URL with path
buildUrl('https://example.com', {
path: 'about'
});
// β https://example.com/about
// With query parameters
buildUrl('https://example.com', {
path: 'search',
queryParams: {
q: 'typescript',
category: 'tutorials'
}
});
// β https://example.com/search?q=typescript&category=tutorials
// With hash
buildUrl('https://example.com', {
path: 'docs',
hash: 'installation'
});
// β https://example.com/docs#installation
// All combined
buildUrl('https://api.example.com', {
path: 'v1/users',
queryParams: {
role: 'admin',
active: true
},
hash: 'summary'
});
// β https://api.example.com/v1/users?role=admin&active=true#summaryWorking with Arrays
// Default: Arrays as comma-separated values
buildUrl('https://api.example.com', {
queryParams: {
ids: [1, 2, 3]
}
});
// β https://api.example.com?ids=1,2,3
// Arrays as repeated parameters
buildUrl('https://api.example.com', {
queryParams: {
id: [1, 2, 3]
},
disableCSV: true
});
// β https://api.example.com?id=1&id=2&id=3
// Arrays with bracket notation
buildUrl('https://api.example.com', {
queryParams: {
id: [1, 2, 3]
},
disableCSV: 'array'
});
// β https://api.example.com?id[]=1&id[]=2&id[]=3
// Arrays with indexed notation (ascending)
buildUrl('https://api.example.com', {
queryParams: {
id: [1, 2, 3]
},
disableCSV: 'order_asc'
});
// β https://api.example.com?id[0]=1&id[1]=2&id[2]=3
// Arrays with indexed notation (descending)
buildUrl('https://api.example.com', {
queryParams: {
id: [1, 2, 3]
},
disableCSV: 'order_desc'
});
// β https://api.example.com?id[2]=1&id[1]=2&id[0]=3Case Transformation
// Convert to lowercase
buildUrl('https://example.com', {
path: 'About',
hash: 'Contact',
queryParams: {
Filter: 'NEW'
},
lowerCase: true
});
// β https://example.com/about?filter=new#contactBuilding Partial URLs
// Query string only
buildUrl(null, {
queryParams: {
page: 1,
limit: 20
}
});
// β ?page=1&limit=20
// Path only
buildUrl(null, {
path: 'users/profile'
});
// β /users/profile
// Hash only
buildUrl(null, {
hash: 'top'
});
// β #top
// Using options as first parameter
buildUrl({
path: 'api/v2',
queryParams: {
format: 'json'
}
});
// β /api/v2?format=jsonHandling Special Values
// Null values become empty strings
buildUrl('https://api.example.com', {
queryParams: {
name: 'John',
age: null
}
});
// β https://api.example.com?name=John&age=
// Undefined values are omitted
buildUrl('https://api.example.com', {
queryParams: {
name: 'John',
age: undefined
}
});
// β https://api.example.com?name=John
// Number values
buildUrl('https://api.example.com', {
path: 404,
queryParams: {
code: 0,
retry: 3
}
});
// β https://api.example.com/404?code=0&retry=3
// Boolean values
buildUrl('https://api.example.com', {
queryParams: {
active: true,
deleted: false
}
});
// β https://api.example.com?active=true&deleted=falseAdvanced Usage
Using Individual Functions
The library also exports individual functions for more granular control:
import {
buildQueryString,
appendPath,
buildHash
} from 'build-url-ts';
// Build query string only
const qs = buildQueryString({
search: 'typescript',
limit: 10
});
// β ?search=typescript&limit=10
// Append path to URL
const urlWithPath = appendPath('users/123', 'https://api.example.com');
// β https://api.example.com/users/123
// Build hash fragment
const hash = buildHash('section-2');
// β #section-2TypeScript Types
import type {
IQueryParams,
IBuildUrlOptions,
IDisableCsvType
} from 'build-url-ts';
// Custom query params type
interface MyParams extends IQueryParams {
userId: number;
tags?: string[];
active?: boolean;
}
const options: IBuildUrlOptions = {
path: 'api/users',
queryParams: {
userId: 123,
tags: ['admin', 'verified']
} as MyParams
};URL Encoding
All values are properly encoded for URLs:
buildUrl('https://example.com', {
queryParams: {
name: 'John Doe',
email: 'john@example.com',
message: 'Hello & goodbye!',
unicode: 'δ½ ε₯½δΈη'
}
});
// β https://example.com?name=John%20Doe&email=john%40example.com&message=Hello%20%26%20goodbye!&unicode=%E4%BD%A0%E5%A5%BD%E4%B8%96%E7%95%8CEdge Cases
The library handles various edge cases gracefully:
// Empty or missing base URL
buildUrl('', { path: 'api' }); // β /api
buildUrl(null, { path: 'api' }); // β /api
buildUrl(undefined, { path: 'api' }); // β /api
// Trailing slashes
buildUrl('https://example.com/', { path: '/users' });
// β https://example.com/users (no double slash)
// Empty values
buildUrl('https://example.com', {
path: '', // ignored
hash: '', // ignored
queryParams: {
empty: '', // included as empty
zero: 0, // included as "0"
false: false // included as "false"
}
});
// β https://example.com?empty=&zero=0&false=falseMigration Guide
From build-url
This library is a TypeScript fork of the original build-url package with improvements:
// Before (build-url)
var buildUrl = require('build-url');
// After (build-url-ts)
import { buildUrl } from 'build-url-ts';The API remains fully compatible, so you can simply replace the import.
Browser Support
- Chrome/Edge: Latest 2 versions
- Firefox: Latest 2 versions
- Safari: Latest 2 versions
- Node.js: 14+
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Running Tests
# Run tests
npm test
# Run tests in watch mode
npm run test-watch
# Build the library
npm run build
# Run linting
npm run lintLicense
MIT Β© Mohamed Meabed
Acknowledgments
This is a TypeScript enhancement of the original build-url library by Steve Rydz.