参数对象,支持字符串、数字、布尔值、null、undefined
Optional
options: ReplaceVarsInPathOptions = {}配置选项
变量前缀符号
是否严格模式,如果为true,未找到的变量会抛出错误
未找到变量时的默认值,仅在非严格模式下生效
是否对变量值进行URL编码
import { replaceVarsInPath } from '@compass-aiden/utils';
// 基础用法
replaceVarsInPath('/api/user/:id', { id: '123' }); // '/api/user/123'
// 支持多种数据类型
replaceVarsInPath('/api/user/:id/active/:status', {
id: 123,
status: true
}); // '/api/user/123/active/true'
// 自定义前缀
replaceVarsInPath('/api/user/{id}', { id: '123' }, {
prefix: '{'
}); // '/api/user/123}'
// URL编码
replaceVarsInPath('/search/:query', { query: 'hello world' }, {
encodeValue: true
}); // '/search/hello%20world'
// 严格模式
replaceVarsInPath('/api/user/:id', {}, { strict: true }); // 抛出错误
// 默认值
replaceVarsInPath('/api/user/:id', {}, {
defaultValue: 'unknown'
}); // '/api/user/unknown'
路径字符串