Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for ltuid_v2 and ltoken_v2 #132

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
with:
envkey_DEBUG: true
envkey_LTOKEN: ${{ secrets.LTOKEN }}
envkey_LTUID: ${{ secrets.LTUID }}
envkey_LTUID_V2: ${{ secrets.LTUID_V2 }}
envkey_ACCOUNT_ID: ${{ secrets.ACCOUNT_ID }}
envkey_COOKIE_TOKEN: ${{ secrets.COOKIE_TOKEN }}
envkey_ACCOUNT_ID_V2: ${{ secrets.ACCOUNT_ID_V2 }}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ For Yarn <br/>
1. To begin, login with your [HoYoLab](https://www.hoyolab.com/home) Account or from [Battlepass](https://act.hoyolab.com/app/community-game-records-sea/index.html?bbs_presentation_style=fullscreen&bbs_auth_required=true&gid=2&user_id=122516750&utm_source=hoyolab&utm_medium=gamecard&bbs_theme=light&bbs_theme_device=1#/ys).
2. Type `java` in the address bar followed by the script down below.
3. ```javascript
script: (function(){if(document.cookie.includes('ltoken')&&document.cookie.includes('ltuid')){const e=document.createElement('input');e.value=document.cookie,document.body.appendChild(e),e.focus(),e.select();var t=document.execCommand('copy');document.body.removeChild(e),t?alert('HoYoLAB cookie copied to clipboard'):prompt('Failed to copy cookie. Manually copy the cookie below:\n\n',e.value)}else alert('Please logout and log back in. Cookie is expired/invalid!')})();
script: (function(){if(document.cookie.includes('ltoken_v2')&&document.cookie.includes('ltuid_v2')){const e=document.createElement('input');e.value=document.cookie,document.body.appendChild(e),e.focus(),e.select();var t=document.execCommand('copy');document.body.removeChild(e),t?alert('HoYoLAB cookie copied to clipboard'):prompt('Failed to copy cookie. Manually copy the cookie below:\n\n',e.value)}else alert('Please logout and log back in. Cookie is expired/invalid!')})();
```
4. Once you've successfully ran the script, click the Click here to copy! button to copy the cookie.
5. Finally, you can copy your cookie
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/guide/get-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ $ yarn add hoyoapi
2. Type `java` in the address bar followed by the script down below.
3. ```javascript
script: (function() {
if (document.cookie.includes('ltoken') && document.cookie.includes('ltuid')) {
if (document.cookie.includes('ltoken_v2') && document.cookie.includes('ltuid_v2')) {
const input = document.createElement('input');
input.value = document.cookie;
document.body.appendChild(input);
Expand Down
2 changes: 1 addition & 1 deletion src/client/hoyolab/hoyolab.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface IHoyolabOptions {
* The cookie used to authenticate the request. This can be either a string or an {@link ICookie} object.
*/
cookie:
| Pick<ICookie, 'ltoken' | 'ltuid' | 'cookieToken' | 'cookieTokenV2'>
| Pick<ICookie, 'ltokenV2' | 'ltuidV2' | 'cookieToken' | 'cookieTokenV2'>
| string

/**
Expand Down
6 changes: 3 additions & 3 deletions src/client/hoyolab/hoyolab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class Hoyolab {
*
* @constructor
* @param {IHoyolabOptions} options - The options to initialize the `Hoyolab` instance.
* @throws {HoyoAPIError} If `ltuid` or `ltoken` keys are missing in the `ICookie` object.
* @throws {HoyoAPIError} If `ltuidV2` or `ltokenV2` keys are missing in the `ICookie` object.
*
* @remarks
* Because CookieTokenV2 has a short expiration time and cannot be refreshed so far.
Expand Down Expand Up @@ -105,7 +105,7 @@ export class Hoyolab {
}

this.request.setQueryParams({
uid: this.cookie.ltuid,
uid: this.cookie.ltuidV2,
sLangKey: this.cookie.mi18nLang,
})
const {
Expand Down Expand Up @@ -173,7 +173,7 @@ export class Hoyolab {
/* c8 ignore start */
this.request.setQueryParams({
uid:
this.cookie.ltuid || this.cookie.accountId || this.cookie.accountIdV2,
this.cookie.ltuidV2 || this.cookie.accountId || this.cookie.accountIdV2,
})

const { response: res } = await this.request.send(GAME_RECORD_CARD_API)
Expand Down
8 changes: 4 additions & 4 deletions src/cookie/cookie.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import { LanguageEnum } from '../language'
*/
export interface ICookie {
/**
* The value of the "ltoken" cookie.
* The value of the "ltoken_v2" cookie.
*/
ltoken: string
ltokenV2: string

/**
* The value of the "ltuid" cookie.
* The value of the "ltuid_v2" cookie.
*/
ltuid: number
ltuidV2: number

/**
* The value of the "cookieToken" cookie, if it exists.
Expand Down
16 changes: 8 additions & 8 deletions src/cookie/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export class Cookie {
const cookies: Map<string, any> = new Map()

const keys: string[] = [
'ltoken',
'ltuid',
'ltoken_v2',
'ltuid_v2',
'account_id',
'cookie_token',
'account_id_v2',
Expand All @@ -44,28 +44,28 @@ export class Cookie {

cookies.set(key, val)

if (['ltuid', 'account_id', 'account_id_v2'].includes(cookieSplited[0])) {
if (['ltuid_v2', '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 ltuid = cookies.get('ltuidV2')
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)
cookies.set('ltuidV2', accountId)
}

if (!accountIdV2 && (accountId || ltuid) !== null) {
cookies.set('accountIdV2', accountId || ltuid)
}

if (!cookies.get('ltoken') || !cookies.get('ltuid')) {
if (!cookies.get('ltokenV2') || !cookies.get('ltuidV2')) {
throw new HoyoAPIError('Cookie key ltuid or ltoken doesnt exist !')
}

Expand All @@ -76,11 +76,11 @@ export class Cookie {
* 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.
* @throws {HoyoAPIError} If the `ltuidV2` or `ltokenV2` key is missing in the `ICookie` object.
*/
static parseCookie(cookie: ICookie): string {
if (!cookie.accountId) {
cookie.accountId = cookie.ltuid
cookie.accountId = cookie.ltuidV2
}

const cookies = Object.entries(cookie)
Expand Down
2 changes: 1 addition & 1 deletion src/request/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class HTTPRequest {
}

constructor(cookie?: string) {
if (cookie) this.headers.Cookie = cookie
if (cookie) this.headers.Cookie = cookie.replace("ltokenV2", "ltoken_v2").replace("ltuidV2", "ltuid_v2");
this.cache = new Cache()
}

Expand Down
24 changes: 12 additions & 12 deletions tests/cookie.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@ import { Cookie, HoyoAPIError } from '../src'

test('parseCookie return should be valid', (t) => {
const cookie = Cookie.parseCookie({
ltoken: 'ltoken',
ltuid: 1,
ltokenV2: 'ltoken_v2',
ltuidV2: 1,
cookieToken: 'cookieToken',
mi18nLang: 'id-id',
cookieTokenV2: 'cookieTokenV2',
})

t.deepEqual(
cookie,
'ltoken=ltoken; ltuid=1; cookie_token=cookieToken; mi18nLang=id-id; cookie_token_v2=cookieTokenV2; account_id=1',
'ltoken_v2=ltoken; ltuid_v2=1; cookie_token=cookieToken; mi18nLang=id-id; cookie_token_v2=cookieTokenV2; account_id=1',
)
})

test('parseCookieString return should be valid', (t) => {
const cookieString = Cookie.parseCookieString(
'ltoken=ltoken; ltuid=1; cookie_token=cookieToken; mi18nLang=id-id; cookie_token_v2=cookieTokenV2; account_id=1',
'ltoken_v2=ltokenV2; ltuid_v2=1; cookie_token=cookieToken; mi18nLang=id-id; cookie_token_v2=cookieTokenV2; account_id=1',
)

t.deepEqual(cookieString, {
ltoken: 'ltoken',
ltuid: 1,
ltokenV2: 'ltokenV2',
ltuidV2: 1,
cookieToken: 'cookieToken',
mi18nLang: 'id-id',
cookieTokenV2: 'cookieTokenV2',
Expand All @@ -34,12 +34,12 @@ test('parseCookieString return should be valid', (t) => {

test('parseCookieString return should be valid when account_id is null', (t) => {
const cookieString = Cookie.parseCookieString(
'ltoken=ltoken; ltuid=1; cookie_token=cookieToken; cookie_token_v2=cookieTokenV2; mi18nLang=id-id',
'ltoken_v2=ltoken; ltuid_v2=1; cookie_token=cookieToken; cookie_token_v2=cookieTokenV2; mi18nLang=id-id',
)

t.deepEqual(cookieString, {
ltoken: 'ltoken',
ltuid: 1,
ltokenV2: 'ltokenV2',
ltuidV2: 1,
cookieToken: 'cookieToken',
mi18nLang: 'id-id',
cookieTokenV2: 'cookieTokenV2',
Expand All @@ -48,13 +48,13 @@ test('parseCookieString return should be valid when account_id is null', (t) =>
})
})

test('parseCookieString return should be valid when ltuid is null', (t) => {
test('parseCookieString return should be valid when ltuidV2 is null', (t) => {
const cookieString = Cookie.parseCookieString(
'ltoken=ltoken; account_id=1; cookie_token_v2=cookieTokenV2; cookie_token=cookieToken; mi18nLang=id-id',
'ltoken_v2=ltokenV2; account_id=1; cookie_token_v2=cookieTokenV2; cookie_token=cookieToken; mi18nLang=id-id',
)

t.deepEqual(cookieString, {
ltoken: 'ltoken',
ltokenV2: 'ltokenV2',
ltuid: 1,
cookieToken: 'cookieToken',
mi18nLang: 'id-id',
Expand Down
4 changes: 2 additions & 2 deletions tests/genshin_impact/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ config()
export const cookie: ICookie = {
accountId: parseInt(process.env.ACCOUNT_ID ?? '0'),
cookieToken: process.env.COOKIE_TOKEN,
ltoken: process.env.LTOKEN ?? '',
ltuid: parseInt(process.env.LTUID ?? '0'),
ltokenV2: process.env.LTOKEN_V2 ?? '',
ltuidV2: parseInt(process.env.LTUID_V2 ?? '0'),
cookieTokenV2: process.env.COOKIE_TOKEN_V2 ?? '',
accountIdV2: parseInt(process.env.ACCOUNT_ID_V2 ?? '0'),
accountMidV2: process.env.ACCOUNT_MID_V2,
Expand Down
4 changes: 2 additions & 2 deletions tests/honkai_impact/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ config()
export const cookie: ICookie = {
accountId: parseInt(process.env.ACCOUNT_ID ?? '0'),
cookieToken: process.env.COOKIE_TOKEN,
ltoken: process.env.LTOKEN ?? '',
ltuid: parseInt(process.env.LTUID ?? '0'),
ltokenV2: process.env.LTOKEN_V2 ?? '',
ltuidV2: parseInt(process.env.LTUID_V2 ?? '0'),
cookieTokenV2: process.env.COOKIE_TOKEN_V2 ?? '',
accountIdV2: parseInt(process.env.ACCOUNT_ID_V2 ?? '0'),
accountMidV2: process.env.ACCOUNT_MID_V2,
Expand Down
4 changes: 2 additions & 2 deletions tests/honkai_starrail/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ config()
export const cookie: ICookie = {
accountId: parseInt(process.env.ACCOUNT_ID ?? '0' ?? '0'),
cookieToken: process.env.COOKIE_TOKEN,
ltoken: process.env.LTOKEN ?? '',
ltuid: parseInt(process.env.LTUID ?? '0'),
ltokenV2: process.env.LTOKEN_V2 ?? '',
ltuidV2: parseInt(process.env.LTUID_V2 ?? '0'),
cookieTokenV2: process.env.COOKIE_TOKEN_V2 ?? '',
accountIdV2: parseInt(process.env.ACCOUNT_ID_V2 ?? '0'),
accountMidV2: process.env.ACCOUNT_MID_V2,
Expand Down