All files / src/cookie cookie.ts

100% Statements 112/112
100% Branches 24/24
100% Functions 2/2
100% Lines 112/112

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 1131x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 59x 59x 1x 1x 1x 1x 59x 59x 59x 59x 59x 59x 59x 22x 59x 4x 4x 10x 10x 10x 10x 10x 10x 10x 1x 10x 1x 1x 10x 10x 4x 4x 10x 10x 1x 1x 9x 9x 9x 1x 1x 1x 1x 1x 1x 1x 1x 64x 1x 1x 64x 64x 64x 1x 1x 1x 1x 447x 447x 447x 447x 447x 447x 447x 447x 447x 447x 318x 318x 447x 64x 64x 447x 64x 64x 64x 64x 1x  
import { ICookie } from './cookie.interface'
import { toSnakeCase, toCamelCase } from './cookie.helper'
import { Language } from '../language'
import { HoyoAPIError } from '../error'
 
/**
 * Represents a cookie object.
 *
 * @class
 * @category Main
 */
export class Cookie {
  /**
   * Parses a cookie string and returns a parsed ICookie object.
   *
   * @param cookieString - The cookie string to be parsed.
   * @returns {string} - A parsed ICookie object.
   * @throws {HoyoAPIError} when ltuid or ltoken keys are not found in the cookie string.
   */
  static parseCookieString(cookieString: string): ICookie {
    const cookies: Map<string, any> = new Map()
 
    const keys: string[] = [
      'ltoken',
      'ltuid',
      'account_id',
      'cookie_token',
      'account_id_v2',
      'account_mid_v2',
      'cookie_token_v2',
      'mi18nLang',
    ]
 
    cookieString.split('; ').forEach((cookie) => {
      const cookieSplited = cookie.trim().split(/=(?=.+)/)
 
      /* c8 ignore next 3 */
      if (keys.includes(cookieSplited[0]) === false) {
        return
      }
 
      const key = toCamelCase(cookieSplited[0]).trim()
      const val = decodeURIComponent(cookieSplited[1]).replace(';', '').trim()
 
      cookies.set(key, val)
 
      if (['ltuid', 'account_id', 'account_id_v2'].includes(cookieSplited[0])) {
        cookies.set(key, parseInt(cookies.get(key), 10))
      } else if (cookieSplited[0] === 'mi18nLang') {
        cookies.set(key, Language.parseLang(cookies.get(key)))
      }
    })
 
    const ltuid = cookies.get('ltuid')
    const accountId = cookies.get('accountId')
    const accountIdV2 = cookies.get('accountIdV2')
 
    if (ltuid && !accountId) {
      cookies.set('accountId', ltuid)
    } else if (!ltuid && accountId) {
      cookies.set('ltuid', accountId)
    }
 
    if (!accountIdV2 && (accountId || ltuid) !== null) {
      cookies.set('accountIdV2', accountId || ltuid)
    }
 
    if (!cookies.get('ltoken') || !cookies.get('ltuid')) {
      throw new HoyoAPIError('Cookie key ltuid or ltoken doesnt exist !')
    }
 
    return Object.fromEntries(cookies) as ICookie
  }
 
  /**
   * Converts an `ICookie` object into a cookie string.
   * @param {ICookie} cookie - The `ICookie` object to convert.
   * @returns {string} A string representing the cookie.
   * @throws {HoyoAPIError} If the `ltuid` or `ltoken` key is missing in the `ICookie` object.
   */
  static parseCookie(cookie: ICookie): string {
    if (!cookie.accountId) {
      cookie.accountId = cookie.ltuid
    }
 
    const cookies = Object.entries(cookie)
      .map(([key, value]) => {
        /* c8 ignore next 3 */
        if (!value) {
          return undefined
        }
 
        if (
          [
            'cookieToken',
            'accountId',
            'cookieTokenV2',
            'accountIdV2',
            'accountMidV2',
          ].includes(key)
        ) {
          key = toSnakeCase(key)
        }
        return `${key}=${value}`
      })
      .filter((val) => {
        return val !== undefined
      })
 
    return cookies.join('; ')
  }
}