import {defineStore} from 'pinia' import {SimApiCore} from './simapi.core' import type {SimApiBaseResponse, SimApiOptions, SimApiVersions} from './types' // ============ Pinia Store ============ // 仅作为 core 的代理映射,不维护任何独立状态 export const useSimApi = defineStore('simapi', { state: () => ({ // 在 state 中实例化 core _core: new SimApiCore(), }), getters: { IsDebug: state => state._core.isDebug }, actions: { // 所有方法直接代理到 core async loadFromFile(file: string = '/config.json'): Promise { return this._core.loadFromFile(file) }, configure(options: SimApiOptions): void { this._core.configure(options) }, setDebug(debug: boolean): void { this._core.setDebug(debug); }, setEndpoints(endpoints: { [name: string]: string }): void { this._core.setEndpoints(endpoints) }, setBusinessCallback( code: number | string, callback: (data: SimApiBaseResponse) => void ): void { this._core.setBusinessCallback(code, callback) }, getToken(): string { return this._core.getToken() }, setToken(token: string): void { this._core.setToken(token) }, removeToken(): void { this._core.removeToken() }, async login(request: Record): Promise> { return this._core.login(request) }, async logout(url?: string | null): Promise { return this._core.logout(url) }, async checkLogin(url?: string | null): Promise { return this._core.checkLogin(url) }, async query( uri: string, params?: any, endpointKey?: string, extraHeaders?: Record, selfHandleError: boolean = false ): Promise> { return this._core.query(uri, params, endpointKey, extraHeaders, selfHandleError) }, getEndpoint(name?: string): string { return this._core.getEndpoint(name) }, async getConfig(reload = false, endpointName?: string): Promise> { return this._core.getConfig(reload, endpointName) }, }, })