All files / src/request request.ts

95.34% Statements 307/322
97.67% Branches 42/43
100% Functions 9/9
95.34% Lines 307/322

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 3231x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 61x 61x 61x 72x 72x 72x 72x 72x 72x 72x 72x 4x 4x 4x 72x 72x 72x 72x 72x 72x 72x 72x 61x 61x 61x 61x 72x 72x 72x 72x 72x 72x 72x 72x 82x 82x 82x 82x 72x 72x 72x 72x 72x 72x 72x 72x 39x 39x 39x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 1x 1x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x                               67x 67x 67x 1x 67x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 67x 67x 67x 67x 78x 67x 67x 67x 67x 67x 67x 67x 67x 30x 64x 1x 37x 1x 1x 67x 67x 67x 67x 67x 67x 67x 66x 66x 66x 66x 66x 66x 66x 66x 66x 1x 1x 66x 66x 66x 66x 66x 66x 66x 1x 1x 1x 1x 67x 1x 1x 1x 1x 1x 1x 67x 67x 67x 1x 1x 67x 67x 67x 67x 6x 6x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 1x 1x 1x 1x 1x 67x 67x 38x 38x 67x 67x 65x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import type { IncomingMessage, RequestOptions } from 'http'
import { request } from 'https'
import { brotliDecompressSync, gunzipSync, inflateSync } from 'zlib'
import type {
  HTTPBody,
  HTTPHeaders,
  HTTPQueryParams,
  HTTPServerResponse,
} from './request.inteface'
import { HoyoAPIError } from '../error'
import { delay, generateDS } from './request.helper'
import { Cache } from '../cache'
import { createHash } from 'crypto'
import { Language } from '../language'
 
/**
 * Class for handling HTTP requests with customizable headers, body, and parameters.
 *
 * @class
 * @internal
 * @category Internal
 */
export class HTTPRequest {
  /**
   * Query parameters for the request.
   */
  private params: HTTPQueryParams = {}
 
  /**
   * Body of the request.
   */
  private body: HTTPBody = {}
 
  /**
   * The cache used for the request
   */
  private cache: Cache
 
  /*
   * Headers for the request.
   */
  private headers: HTTPHeaders = {
    Accept: 'application/json, text/plain, */*',
    'Content-Type': 'application/json',
    'Accept-Encoding': 'gzip, deflate, br',
    'sec-ch-ua':
      '"Chromium";v="112", "Microsoft Edge";v="112", "Not:A-Brand";v="99"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-site',
    'user-agent':
      'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 Edg/112.0.1722.46',
    'x-rpc-app_version': '1.5.0',
    'x-rpc-client_type': '5',
    'x-rpc-language': 'en-us',
  }
 
  /**
   * Flag indicating whether Dynamic Security is used.
   */
  private ds = false
 
  /**
   * The number of request attempts made.
   */
  private retries = 1
 
  public http?: {
    response?: object
    request?: object
    code?: number
  }
 
  constructor(cookie?: string) {
    if (cookie) this.headers.Cookie = cookie
    this.cache = new Cache()
  }
 
  /**
   * Sets search parameters or query parameter.
   *
   * @param params - An object of query parameter to be set.
   * @returns Returns this Request object.
   */
  setQueryParams(params: HTTPQueryParams) {
    this.params = { ...this.params, ...params }
    return this
  }
 
  /**
   * Set Body Parameter
   *
   * @param body - RequestBodyType as object containing the body parameters.
   * @returns This instance of Request object.
   */
  setBody(data: HTTPBody) {
    this.body = { ...this.body, ...data }
    return this
  }
 
  /**
   * Set Referer Headers
   *
   * @param url - The URL string of referer
   * @returns The updated Request instance.
   */
  setReferer(url: string | URL) {
    this.headers.Referer = url.toString()
    this.headers.Origin = url.toString()
    return this
  }
 
  /**
   * Set Language
   *
   * @param lang Language Language that used for return of API (default: Language.ENGLISH).
   * @returns {this}
   */
  setLang(lang: string): this {
    this.headers['x-rpc-language'] = Language.parseLang(lang)
 
    return this
  }
 
  /**
   * Set to used Dynamic Security or not
   *
   * @param flag boolean Flag indicating whether to use dynamic security or not (default: true).
   * @returns {this} The current Request instance.
   */
  setDs(flag = true): this {
    this.ds = flag
    return this
  }
 
  /**
   * Send the HTTP request.
   *
   * @param url - The URL to send the request to.
   * @param method - The HTTP method to use. Defaults to 'GET'.
   * @param ttl - The TTL value for the cached data in seconds.
   * @returns A Promise that resolves with the response data, or rejects with a HoyoAPIError if an error occurs.
   * @throws {HoyoAPIError} if an error occurs rejects with a HoyoAPIError
   */
  async send(
    url: string,
    method: 'GET' | 'POST' = 'GET',
    ttl = 60,
  ): Promise<HTTPServerResponse> {
    // Internal NodeJS Fetch
    const fetch = (url: string, method: string) => {
      return new Promise<HTTPServerResponse>((resolve, reject) => {
        const hostname = new URL(url)
        const queryParams = new URLSearchParams(hostname.searchParams)
 
        Object.keys(this.params).forEach((val) => {
          /* c8 ignore next */
          queryParams.append(val, this.params[val]?.toString() ?? '')
        })
 
        hostname.search = queryParams.toString()
 
        const options: RequestOptions = {
          method,
          headers: this.headers,
        }
 
        const client = request(hostname, options, (res: IncomingMessage) => {
          if (res.statusCode === 429) {
            // If the status code is 429, return a resolved promise with a response object
            return resolve({
              response: {
                data: null,
                message: 'Too Many Request',
                retcode: 429,
              },
              status: {
                code: 429,
                message: 'Too Many Request',
              },
              headers: res.headers,
              body: this.body,
              params: this.params,
            })
          } else if (
            res.statusCode &&
            res.statusCode >= 400 &&
            res.statusCode < 600
          ) {
            // If the status code is between 400 and 599 (inclusive), reject the promise with an HoyoAPIError
            reject(
              new HoyoAPIError(
                `HTTP ${res.statusCode}: ${res.statusMessage}`,
                res.statusCode,
                {
                  response: res.statusMessage,
                  request: {
                    params: this.params,
                    body: this.body,
                    headers: this.headers,
                  },
                },
              ),
            )
          }
 
          const stream: Buffer[] = []
 
          res.on('data', (chunk: Buffer) => {
            stream.push(chunk)
          })
 
          res.on('end', () => {
            let buffer = Buffer.concat(stream)
 
            // Handling content compression
            const encoding = res.headers['content-encoding']
            if (encoding === 'gzip') {
              buffer = gunzipSync(buffer)
            } else if (encoding === 'deflate') {
              buffer = inflateSync(buffer)
            } else if (encoding === 'br') {
              buffer = brotliDecompressSync(buffer)
            }
 
            // Parse to UTF-8
            const responseString = buffer.toString('utf8')
 
            let response: any
            // Parse body to JSON
            if (res.headers['content-type'] === 'application/json') {
              try {
                response = JSON.parse(responseString)
                resolve({
                  response: {
                    data: response?.data ?? null,
                    message: response?.message ?? '',
                    retcode: response?.retcode ?? -1,
                  },
                  status: {
                    /* c8 ignore next */
                    code: res.statusCode ?? -1,
                    message: res.statusMessage,
                  },
                  headers: res.headers,
                  body: this.body,
                  params: this.params,
                })
              } catch (error) {
                reject(
                  new HoyoAPIError('Failed to parse response body as JSON'),
                )
              }
            } else {
              reject(
                new HoyoAPIError(
                  'Response Content-Type is not application/json',
                ),
              )
            }
          })
 
          res.on('error', (err: Error) => {
            /* c8 ignore next */
            reject(new HoyoAPIError(err.message))
          })
        })
 
        if (method === 'POST') {
          client.write(JSON.stringify(this.body))
        }
 
        client.end()
      })
    }
 
    const cacheKey = createHash('md5')
      .update(
        JSON.stringify({
          url,
          method,
          body: this.body,
          params: this.params,
        }),
      )
      .digest('hex')
 
    const cachedResult = this.cache.get(cacheKey)
 
    /* c8 ignore start */
    if (cachedResult) {
      return cachedResult as HTTPServerResponse
    }
    /* c8 ignore stop */
 
    if (this.ds) {
      this.headers.DS = generateDS()
    }
 
    const req = await fetch(url, method)
 
    /* c8 ignore start */
    if (
      [-1004, -2016, -500_004, 429].includes(req.response.retcode) &&
      this.retries <= 120
    ) {
      this.retries++
      await delay(1)
      return this.send(url, method)
    }
    /* c8 ignore start */
 
    this.retries = 1
    this.body = {}
    this.params = {}
 
    this.cache.set(cacheKey, req, ttl)
    return req
  }
}