-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathClient.ts
More file actions
90 lines (87 loc) · 2.97 KB
/
Copy pathClient.ts
File metadata and controls
90 lines (87 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
* Base client for API interaction.
* @description Provides session management and browser emulation.
*/
export default class BaseClient {
/** Standard browser headers for simulation */
protected readonly browserHeaders = {
Accept: 'application/json, text/plain, */*',
'Accept-Language': 'en-US,en;q=0.9,id;q=0.8',
Referer: 'https://www.idx.co.id/',
'Upgrade-Insecure-Requests': '1',
'User-Agent':
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36'
}
/** Cookies obtained from initialization */
protected sessionCookie = ''
/**
* Initialize session cookie.
* @description Fetches main page to obtain required cookies.
* @returns Promise resolving when session is ready
*/
async ensureSession(): Promise<void> {
if (this.sessionCookie) {
return
}
try {
const response = await this.fetcherUrl('https://www.idx.co.id/id')
this.sessionCookie = response.headers.getSetCookie().join('; ')
await this.wait(1000)
await response.body?.cancel()
const validationResponse = await this.fetcherUrl(
'https://www.idx.co.id/primary/home/GetIndexList'
)
await validationResponse.body?.cancel()
} catch (error) {
throw error
}
}
/**
* Universal fetcher with exponential retry.
* @description Handles network flakiness with automated retries.
* @param url - Target endpoint URI
* @param maxAttempts - Maximum execution retries
* @returns Promise resolving to response object
*/
async fetcherUrl(url: string, maxAttempts = 5): Promise<Response> {
const headers = {
...this.browserHeaders,
'X-Requested-With': 'XMLHttpRequest',
...(this.sessionCookie ? { Cookie: this.sessionCookie } : {})
}
const attemptFetch = async (attempt: number): Promise<Response> => {
try {
const response = await fetch(url, { headers })
if (!response.ok && response.status >= 500) {
await response.body?.cancel()
throw new Error(`Server returned ${response.status}: ${response.statusText}`)
}
return response
} catch (error) {
if (attempt >= maxAttempts) {
throw error
}
const delay = Math.min(1000 * Math.pow(2, attempt - 1), 15000)
console.warn(
`[Client] Fetch failed for ${url}. Retrying in ${
delay / 1000
}s (Attempt ${attempt}/${maxAttempts}). Error: ${
error instanceof Error ? error.message : String(error)
}`
)
await this.wait(delay)
return attemptFetch(attempt + 1)
}
}
return await attemptFetch(1)
}
/**
* Helper for execution delay.
* @description Pauses process for specified milliseconds.
* @param ms - Delay duration in milliseconds
* @returns Promise resolving after timeout
*/
protected wait(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms))
}
}