mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-27 04:31:27 +08:00
91 lines
3.3 KiB
TypeScript
91 lines
3.3 KiB
TypeScript
export const defaultAppHeaders = () => {
|
|
return {
|
|
'HTTP-Referer': 'https://cherry-ai.com',
|
|
'X-Title': 'Cherry Studio'
|
|
}
|
|
}
|
|
|
|
// Following two function are not being used for now.
|
|
// I may use them in the future, so just keep them commented. - by eurfelux
|
|
|
|
/**
|
|
* Converts an `undefined` value to `null`, otherwise returns the value as-is.
|
|
* @param value - The value to check
|
|
* @returns `null` if the input is `undefined`; otherwise the input value
|
|
*/
|
|
|
|
// export function toNullIfUndefined<T>(value: T | undefined): T | null {
|
|
// if (value === undefined) {
|
|
// return null
|
|
// } else {
|
|
// return value
|
|
// }
|
|
// }
|
|
|
|
/**
|
|
* Converts a `null` value to `undefined`, otherwise returns the value as-is.
|
|
* @param value - The value to check
|
|
* @returns `undefined` if the input is `null`; otherwise the input value
|
|
*/
|
|
|
|
// export function toUndefinedIfNull<T>(value: T | null): T | undefined {
|
|
// if (value === null) {
|
|
// return undefined
|
|
// } else {
|
|
// return value
|
|
// }
|
|
// }
|
|
|
|
/**
|
|
* Extracts the trailing API version segment from a URL path.
|
|
*
|
|
* This function extracts API version patterns (e.g., `v1`, `v2beta`) from the end of a URL.
|
|
* Only versions at the end of the path are extracted, not versions in the middle.
|
|
* The returned version string does not include leading or trailing slashes.
|
|
*
|
|
* @param {string} url - The URL string to parse.
|
|
* @returns {string | undefined} The trailing API version found (e.g., 'v1', 'v2beta'), or undefined if none found.
|
|
*
|
|
* @example
|
|
* getTrailingApiVersion('https://api.example.com/v1') // 'v1'
|
|
* getTrailingApiVersion('https://api.example.com/v2beta/') // 'v2beta'
|
|
* getTrailingApiVersion('https://api.example.com/v1/chat') // undefined (version not at end)
|
|
* getTrailingApiVersion('https://gateway.ai.cloudflare.com/v1/xxx/v1beta') // 'v1beta'
|
|
* getTrailingApiVersion('https://api.example.com') // undefined
|
|
*/
|
|
export function getTrailingApiVersion(url: string): string | undefined {
|
|
const match = url.match(TRAILING_VERSION_REGEX)
|
|
|
|
if (match) {
|
|
// Extract version without leading slash and trailing slash
|
|
return match[0].replace(/^\//, '').replace(/\/$/, '')
|
|
}
|
|
|
|
return undefined
|
|
}
|
|
|
|
/**
|
|
* Matches an API version at the end of a URL (with optional trailing slash).
|
|
* Used to detect and extract versions only from the trailing position.
|
|
*/
|
|
const TRAILING_VERSION_REGEX = /\/v\d+(?:alpha|beta)?\/?$/i
|
|
|
|
/**
|
|
* Removes the trailing API version segment from a URL path.
|
|
*
|
|
* This function removes API version patterns (e.g., `/v1`, `/v2beta`) from the end of a URL.
|
|
* Only versions at the end of the path are removed, not versions in the middle.
|
|
*
|
|
* @param {string} url - The URL string to process.
|
|
* @returns {string} The URL with the trailing API version removed, or the original URL if no trailing version found.
|
|
*
|
|
* @example
|
|
* withoutTrailingApiVersion('https://api.example.com/v1') // 'https://api.example.com'
|
|
* withoutTrailingApiVersion('https://api.example.com/v2beta/') // 'https://api.example.com'
|
|
* withoutTrailingApiVersion('https://api.example.com/v1/chat') // 'https://api.example.com/v1/chat' (no change)
|
|
* withoutTrailingApiVersion('https://api.example.com') // 'https://api.example.com'
|
|
*/
|
|
export function withoutTrailingApiVersion(url: string): string {
|
|
return url.replace(TRAILING_VERSION_REGEX, '')
|
|
}
|