mirror of
https://github.com/wu736139669/hapi.git
synced 2026-08-05 06:24:37 +00:00
refactor: Add Telegram binding storage and auth
This commit is contained in:
+47
-1
@@ -18,6 +18,33 @@ type ApiClientOptions = {
|
||||
onUnauthorized?: () => Promise<string | null>
|
||||
}
|
||||
|
||||
type ErrorPayload = {
|
||||
error?: unknown
|
||||
}
|
||||
|
||||
function parseErrorCode(bodyText: string): string | undefined {
|
||||
try {
|
||||
const parsed = JSON.parse(bodyText) as ErrorPayload
|
||||
return typeof parsed.error === 'string' ? parsed.error : undefined
|
||||
} catch {
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
export class ApiError extends Error {
|
||||
status: number
|
||||
code?: string
|
||||
body?: string
|
||||
|
||||
constructor(message: string, status: number, code?: string, body?: string) {
|
||||
super(message)
|
||||
this.name = 'ApiError'
|
||||
this.status = status
|
||||
this.code = code
|
||||
this.body = body
|
||||
}
|
||||
}
|
||||
|
||||
export class ApiClient {
|
||||
private token: string
|
||||
private readonly baseUrl: string | null
|
||||
@@ -93,7 +120,26 @@ export class ApiClient {
|
||||
|
||||
if (!res.ok) {
|
||||
const body = await res.text().catch(() => '')
|
||||
throw new Error(`Auth failed: HTTP ${res.status} ${res.statusText}: ${body}`)
|
||||
const code = parseErrorCode(body)
|
||||
const detail = body ? `: ${body}` : ''
|
||||
throw new ApiError(`Auth failed: HTTP ${res.status} ${res.statusText}${detail}`, res.status, code, body || undefined)
|
||||
}
|
||||
|
||||
return await res.json() as AuthResponse
|
||||
}
|
||||
|
||||
async bind(auth: { initData: string; accessToken: string }): Promise<AuthResponse> {
|
||||
const res = await fetch(this.buildUrl('/api/bind'), {
|
||||
method: 'POST',
|
||||
headers: { 'content-type': 'application/json' },
|
||||
body: JSON.stringify(auth)
|
||||
})
|
||||
|
||||
if (!res.ok) {
|
||||
const body = await res.text().catch(() => '')
|
||||
const code = parseErrorCode(body)
|
||||
const detail = body ? `: ${body}` : ''
|
||||
throw new ApiError(`Bind failed: HTTP ${res.status} ${res.statusText}${detail}`, res.status, code, body || undefined)
|
||||
}
|
||||
|
||||
return await res.json() as AuthResponse
|
||||
|
||||
Reference in New Issue
Block a user