@dtinsight/dt-utils
Version:
31 lines (30 loc) • 1.15 kB
TypeScript
declare type QueryValue = string | number | boolean | null | undefined;
declare type QueryParams = Record<string, QueryValue>;
/**
* 生成带查询参数的 URL
*
* @category Utils
* @description
* 将基础 URL 路径与查询参数组合,
* 生成一个完整的 URL 字符串。它会自动处理参数编码并移除无效值。
*
* @param {string} pathname - 基础 URL 路径(例如:'/api/users')
* @param {QueryParams} queryParams - 查询参数对象(例如:{ id: 123, name: 'john' })
* @returns {string} 完整的 URL 字符串
*
* @example
* ```typescript
* import { generateUrlWithQuery } from 'dt-utils';
*
* // 基础用法
* generateUrlWithQuery('/api/users', { id: 123 }) // => '/api/users?id=123'
*
* // 多个参数
* generateUrlWithQuery('/search', { q: 'test', page: 1, sort: 'desc' }) // => '/search?q=test&page=1&sort=desc'
*
* // 处理无效值
* generateUrlWithQuery('/api/data', { id: 123, name: null, status: undefined }) // => '/api/data?id=123'
* ```
*/
declare const generateUrlWithQuery: (pathname: string, queryParams?: QueryParams) => string;
export default generateUrlWithQuery;