t-comm
Version:
专业、稳定、纯粹的工具库
59 lines (58 loc) • 2.57 kB
TypeScript
export declare const instance: import("axios").AxiosStatic;
export declare const T_GIT_BASE_URL = "https://git.woa.com/api/v3";
/**
* 处理 baseUrl,去掉尾部斜杠,并在 path 以 /api 开头时去掉 baseUrl 末尾可能重复的 /api/v3 后缀,然后拼接请求路径。
*
* 使用场景:tgit 中所有请求统一通过此函数拼接完整 URL,防止 baseUrl 末尾带 /api/v3 导致路径重复。
*
* @param {string} baseUrl 基础路径,如 https://git.woa.com 或 https://git.woa.com/api/v3
* @param {string} path 请求路径,如 /api/v3/projects/xxx/merge_requests
* @returns {string} 拼接后的完整 URL
* @example
*
* resolveBaseUrl('https://git.woa.com', '/api/v3/projects/xxx')
* // => 'https://git.woa.com/api/v3/projects/xxx'
*
* resolveBaseUrl('https://git.woa.com/api/v3', '/api/v3/projects/xxx')
* // => 'https://git.woa.com/api/v3/projects/xxx'
*
* resolveBaseUrl('https://git.woa.com/api/v3/', '/api/v3/projects/xxx')
* // => 'https://git.woa.com/api/v3/projects/xxx'
*
* // path 不以 /api 开头时,保留 baseUrl 末尾的 /api/v3
* resolveBaseUrl('https://git.woa.com/api/v3', '/projects/xxx')
* // => 'https://git.woa.com/api/v3/projects/xxx'
*
* // 不传 baseUrl 时,使用统一默认值 T_GIT_BASE_URL
* resolveBaseUrl(undefined, '/api/v3/projects/xxx')
* // => 'https://git.woa.com/api/v3/projects/xxx'
*/
export declare function resolveBaseUrl(baseUrl?: string | undefined, path?: string): string;
/**
* 对 projectName(string | number)进行统一编码,用于拼接到 URL path 中。
*
* - 数字:直接转成字符串(项目 ID)
* - 字符串:按 path 段进行 URI 编码,例如 `group/sub/repo` -> `group%2Fsub%2Frepo`
*
* @param {string | number} projectName 项目名称或项目 ID
* @returns {string} 编码后的字符串
* @example
*
* encodeProject('group/sub/repo') // 'group%2Fsub%2Frepo'
* encodeProject(12345) // '12345'
*/
export declare function encodeProject(projectName: string | number): string;
/**
* 将 base64 字符串按 utf-8 解码(兼容 Node 与浏览器环境)。
*
* 工蜂 GET /repository/files 接口返回的 `content` 字段是 base64 编码的,
* 调用方通常需要解码后再使用,本函数将该逻辑内聚到一处。
*
* @param {string} content base64 内容
* @returns {string} utf-8 解码后的字符串
* @example
*
* base64DecodeUtf8('aGVsbG8='); // 'hello'
* base64DecodeUtf8('5L2g5aW9'); // '你好'
*/
export declare function base64DecodeUtf8(content: string): string;