Compass helpers for browser platforms
    Preparing search index...

    Function replaceVarsInPath

    • Parameters

      • pathStr: string

        路径字符串

      • params: Record<string, VariableValue>

        参数对象,支持字符串、数字、布尔值、null、undefined

      • Optionaloptions: ReplaceVarsInPathOptions = {}

        配置选项

        • prefix

          变量前缀符号

        • strict

          是否严格模式,如果为true,未找到的变量会抛出错误

        • defaultValue

          未找到变量时的默认值,仅在非严格模式下生效

        • encodeValue

          是否对变量值进行URL编码

      Returns string

      替换路径字符串中的变量

      当输入参数类型无效时抛出错误

      当严格模式下存在未定义变量时抛出错误

      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'