name desc author 机智的小鱼君 <dragon-fish@qq.com>
 *  license export const name = 'genshin-kit' ; import { _getApiEndpoint } from './module/_getApiEndpoint' ; import { _getDS } from './module/_getDS' ; import { _getHttpHeaders } from './module/_getHttpHeaders' ; import { _getServer } from './module/_getServer' ; import { _hoyolabVersion } from './module/_hoyolabVersion' ; import { request } from './module/request' ; export * as util from './util' ; import { URLSearchParams } from 'url' ; import { deprecate } from 'util' ; import { Abyss , Activities , AppCache , Character , AppServerLocale , AppServerType , UserInfo , DailyNote , } from './types' ; export class GenshinKit { _cache ! : AppCache ; cookie ! : string ; serverType ! : AppServerType ; serverLocale ! : AppServerLocale ; _getApiEndpoint : typeof _getApiEndpoint ; _hoyolabVersion ! : typeof _hoyolabVersion ; _getHttpHeaders ! : typeof _getHttpHeaders ; _getDS ! : typeof _getDS ; _getServer ! : typeof _getServer ; request ! : typeof request ; getCharacters : ( uid : number , noCache ? : boolean ) => Promise < Character [ ] > ; getUserRoles : ( uid : number , noCache ? : boolean ) => Promise < Character [ ] > ; getAbyss : ( uid : number , type ? : 1 | 2 , noCache ? : boolean ) => Promise < Abyss > ; getCurAbyss : ( uid : number , noCache ? : boolean ) => Promise < Abyss > ; getPrevAbyss : ( uid : number , noCache ? : boolean ) => Promise < Abyss > ; setCookie : ( cookie : string ) => this ; constructor ( ) { this . _cache = { } ; this . cookie = '' ; this . _getApiEndpoint = _getApiEndpoint ; this . _getDS = _getDS ; this . _getHttpHeaders = _getHttpHeaders ; this . _getServer = _getServer ; this . _hoyolabVersion = _hoyolabVersion ; this . request = request ; this . serverType = 'cn' ; this . serverLocale = 'zh-cn' ; this . getCharacters = this . getAllCharacters ; this . getUserRoles = this . getAllCharacters ; this . getAbyss = this . getSpiralAbyss ; this . getCurAbyss = this . getCurrentAbyss ; this . getPrevAbyss = this . getPreviousAbyss ; this . setCookie = this . loginWithCookie ; } method param { String } cookie loginWithCookie ( cookie ) { this . clearCache ( ) ; this . cookie = cookie ; return this ; } method clearCache ( ) { this . _cache = { } ; return this ; } method param type setServerType ( type ) { this . serverType = type ; return this ; } method param locale setServerLocale ( locale ) { this . serverLocale = locale ; return this ; } function param { Number } uid returns { Promise < UserInfo > } async getUserInfo ( uid , noCache = false ) { const temp = this . _cache ?. [ uid ] ?. info ; if ( temp && ! noCache ) { return temp ; } const server = this . _getServer ( uid ) ; const data = await this . request ( 'get' , 'index' , { role_id : uid , server , } ) ; if ( data . retcode !== 0 || ! data . data ) { throw { code : data . retcode , message : data . message , } ; } this . _cache [ uid ] = { ... this . _cache [ uid ] , info : data . data , } ; return data . data ; } function param { Number } uid returns { Promise < Character [ ] > } async getAllCharacters ( uid , noCache = false ) { const temp = this . _cache ?. [ uid ] ?. roles ; if ( temp && ! noCache ) { return temp ; } const server = this . _getServer ( uid ) ; const userInfo = await this . getUserInfo ( uid ) ; const character_ids = userInfo . avatars . map ( ( item ) => { return item . id ; } ) ; const data = await this . request ( 'post' , 'character' , { character_ids , role_id : uid , server , } ) ; if ( data . retcode !== 0 || ! data . data ) { throw { code : data . retcode , message : data . message , } ; } else { this . _cache [ uid ] = { ... this . _cache [ uid ] , roles : data ?. data ?. avatars , } ; return data ?. data ?. avatars || [ ] ; } } getCharacterDetailsUrl ( uid , id ) { return deprecate ( ( ) => { const server = this . _getServer ( uid ) ; return `https://webstatic.mihoyo.com/app/community-game-records/index.html?${ new URLSearchParams ( { bbs_presentation_style : 'fullscreen' } ) }#/ys/role?${ new URLSearchParams ( { role_id : uid . toString ( ) , server : server , id : id . toString ( ) , } ) }` ; } , '`getCharacterDetailsUrl()` has been deprecated.' ) ( ) ; } function param { Number } uid param { 1 | 2 } type returns { Promise < Abyss > } async getSpiralAbyss ( uid , type = 1 , noCache = false ) { if ( type !== 1 && type !== 2 ) { throw { code : - 1 , message : 'Invalid abyss type' } ; } const temp = this . _cache ?. [ uid ] ?. abyss ?. [ type ] ; if ( temp && ! noCache ) { return temp ; } const server = this . _getServer ( uid ) ; const data = await this . request ( 'get' , 'spiralAbyss' , { role_id : uid , schedule_type : type , server , } ) ; if ( data . retcode !== 0 || ! data . data ) { throw { code : data . retcode , message : data . message } ; } else { this . _cache [ uid ] = this . _cache [ uid ] || { } ; this . _cache [ uid ] . abyss = { ... this . _cache [ uid ] . abyss , [ type ] : data . data , } ; return data . data ; } } method async getActivities ( uid ) { const server = this . _getServer ( uid ) ; const data = await this . request ( 'get' , 'activities' , { role_id : uid , server , } ) ; if ( data . retcode !== 0 || ! data . data ) { throw { code : data . retcode , message : data . message } ; } else { return data . data ; } } function async getCurrentAbyss ( uid , noCache ? ) { return this . getSpiralAbyss ( uid , 1 , noCache ) ; } function async getPreviousAbyss ( uid , noCache ? ) { return this . getSpiralAbyss ( uid , 2 , noCache ) ; } function param { Number } uid returns { Promise < DailyNote > } async getDailyNote ( uid , noCache = false ) { const temp = this . _cache ?. [ uid ] ?. dailyNote ; if ( temp && ! noCache ) { return temp ; } const server = this . _getServer ( uid ) ; const data = await this . request ( 'get' , 'dailyNote' , { role_id : uid , server , } ) ; if ( data . retcode !== 0 || ! data . data ) { throw { code : data . retcode , message : data . message , } ; } this . _cache [ uid ] = { ... this . _cache [ uid ] , dailyNote : data . data , } ; return data . data ; } }