(url: string, currentNode?: string)
| 2 | import { handleAuthResponseCode, handleAuthResponseStatus } from '@/utils/auth-response'; |
| 3 | |
| 4 | export const checkStreamAuth = async (url: string, currentNode?: string) => { |
| 5 | const { currentNode: storeCurrentNode, language } = useGlobalStore(); |
| 6 | const controller = new AbortController(); |
| 7 | const timeout = window.setTimeout(() => controller.abort(), 5000); |
| 8 | try { |
| 9 | const res = await fetch(url.replace(/^ws/, 'http'), { |
| 10 | credentials: 'include', |
| 11 | headers: { |
| 12 | 'Accept-Language': language.value, |
| 13 | CurrentNode: encodeURIComponent(currentNode || storeCurrentNode.value), |
| 14 | }, |
| 15 | signal: controller.signal, |
| 16 | }); |
| 17 | const statusResult = handleAuthResponseStatus(res.status); |
| 18 | if (statusResult.handled) { |
| 19 | return statusResult.message; |
| 20 | } |
| 21 | const contentType = res.headers.get('content-type') || ''; |
| 22 | if (!contentType.includes('application/json')) { |
| 23 | await res.body?.cancel(); |
| 24 | return ''; |
| 25 | } |
| 26 | const data = await res.json(); |
| 27 | const codeResult = handleAuthResponseCode(data); |
| 28 | if (codeResult.handled) { |
| 29 | return codeResult.message; |
| 30 | } |
| 31 | return ''; |
| 32 | } catch { |
| 33 | return ''; |
| 34 | } finally { |
| 35 | window.clearTimeout(timeout); |
| 36 | } |
| 37 | }; |
no test coverage detected