query 增加了自处理异常
Publish to npm / publish (push) Failing after 8s
Publish to npm / publish (push) Failing after 8s
This commit is contained in:
+8
-5
@@ -237,7 +237,8 @@ export class SimApiCore {
|
|||||||
uri: string,
|
uri: string,
|
||||||
params: any = {},
|
params: any = {},
|
||||||
endpointKey?: string,
|
endpointKey?: string,
|
||||||
extraHeaders?: Record<string, string>
|
extraHeaders?: Record<string, string>,
|
||||||
|
selfHandleError: boolean = false
|
||||||
): Promise<SimApiBaseResponse<T>> {
|
): Promise<SimApiBaseResponse<T>> {
|
||||||
const headers: Record<string, string> = {...extraHeaders, ...{}}
|
const headers: Record<string, string> = {...extraHeaders, ...{}}
|
||||||
const queryId = this.genS4()
|
const queryId = this.genS4()
|
||||||
@@ -271,10 +272,12 @@ export class SimApiCore {
|
|||||||
const processedData = this.api.responseCallback.success(respData) as SimApiBaseResponse<T>
|
const processedData = this.api.responseCallback.success(respData) as SimApiBaseResponse<T>
|
||||||
|
|
||||||
// 业务回调处理
|
// 业务回调处理
|
||||||
if (this.api.businessCallback.hasOwnProperty(processedData.code)) {
|
if (!selfHandleError) {
|
||||||
this.api.businessCallback[processedData.code](processedData)
|
if (this.api.businessCallback.hasOwnProperty(processedData.code)) {
|
||||||
} else if (this.api.businessCallback['common'] && processedData.code !== 200) {
|
this.api.businessCallback[processedData.code](processedData)
|
||||||
this.api.businessCallback['common'](processedData)
|
} else if (this.api.businessCallback['common'] && processedData.code !== 200) {
|
||||||
|
this.api.businessCallback['common'](processedData)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// code != 200 时抛出业务错误
|
// code != 200 时抛出业务错误
|
||||||
|
|||||||
+68
-67
@@ -1,91 +1,92 @@
|
|||||||
import { defineStore } from 'pinia'
|
import {defineStore} from 'pinia'
|
||||||
import { SimApiCore } from './simapi.core'
|
import {SimApiCore} from './simapi.core'
|
||||||
import type { SimApiBaseResponse, SimApiOptions, SimApiVersions } from './types'
|
import type {SimApiBaseResponse, SimApiOptions, SimApiVersions} from './types'
|
||||||
|
|
||||||
// ============ Pinia Store ============
|
// ============ Pinia Store ============
|
||||||
// 仅作为 core 的代理映射,不维护任何独立状态
|
// 仅作为 core 的代理映射,不维护任何独立状态
|
||||||
|
|
||||||
export const useSimApi = defineStore('simapi', {
|
export const useSimApi = defineStore('simapi', {
|
||||||
state: () => ({
|
state: () => ({
|
||||||
// 在 state 中实例化 core
|
// 在 state 中实例化 core
|
||||||
_core: new SimApiCore(),
|
_core: new SimApiCore(),
|
||||||
}),
|
}),
|
||||||
|
|
||||||
getters: {
|
getters: {
|
||||||
// 直接映射 core 的属性和方法
|
// 直接映射 core 的属性和方法
|
||||||
debug: (state) => state._core.debug,
|
debug: (state) => state._core.debug,
|
||||||
token: (state) => state._core.getToken(),
|
token: (state) => state._core.getToken(),
|
||||||
isLoggedIn: (state) => state._core.isLoggedIn,
|
isLoggedIn: (state) => state._core.isLoggedIn,
|
||||||
api: (state) => state._core.api,
|
api: (state) => state._core.api,
|
||||||
auth: (state) => state._core.auth,
|
auth: (state) => state._core.auth,
|
||||||
},
|
|
||||||
|
|
||||||
actions: {
|
|
||||||
// 所有方法直接代理到 core
|
|
||||||
autoInit(): void {
|
|
||||||
this._core.autoInit()
|
|
||||||
},
|
},
|
||||||
|
|
||||||
configure(options: SimApiOptions): void {
|
actions: {
|
||||||
this._core.configure(options)
|
// 所有方法直接代理到 core
|
||||||
},
|
autoInit(): void {
|
||||||
|
this._core.autoInit()
|
||||||
|
},
|
||||||
|
|
||||||
setDebug(debug: boolean): void {
|
configure(options: SimApiOptions): void {
|
||||||
this._core.debug = debug
|
this._core.configure(options)
|
||||||
},
|
},
|
||||||
|
|
||||||
setEndpoints(endpoints: { [name: string]: string }): void {
|
setDebug(debug: boolean): void {
|
||||||
this._core.setEndpoints(endpoints)
|
this._core.debug = debug
|
||||||
},
|
},
|
||||||
|
|
||||||
setBusinessCallback(
|
setEndpoints(endpoints: { [name: string]: string }): void {
|
||||||
code: number | string,
|
this._core.setEndpoints(endpoints)
|
||||||
callback: (data: SimApiBaseResponse) => void
|
},
|
||||||
): void {
|
|
||||||
this._core.setBusinessCallback(code, callback)
|
|
||||||
},
|
|
||||||
|
|
||||||
getToken(): string {
|
setBusinessCallback(
|
||||||
return this._core.getToken()
|
code: number | string,
|
||||||
},
|
callback: (data: SimApiBaseResponse) => void
|
||||||
|
): void {
|
||||||
|
this._core.setBusinessCallback(code, callback)
|
||||||
|
},
|
||||||
|
|
||||||
setToken(token: string): void {
|
getToken(): string {
|
||||||
this._core.setToken(token)
|
return this._core.getToken()
|
||||||
},
|
},
|
||||||
|
|
||||||
removeToken(): void {
|
setToken(token: string): void {
|
||||||
this._core.removeToken()
|
this._core.setToken(token)
|
||||||
},
|
},
|
||||||
|
|
||||||
async login(request: Record<string, any>): Promise<SimApiBaseResponse<string>> {
|
removeToken(): void {
|
||||||
return this._core.login(request)
|
this._core.removeToken()
|
||||||
},
|
},
|
||||||
|
|
||||||
async logout(url?: string | null): Promise<any> {
|
async login(request: Record<string, any>): Promise<SimApiBaseResponse<string>> {
|
||||||
return this._core.logout(url)
|
return this._core.login(request)
|
||||||
},
|
},
|
||||||
|
|
||||||
async checkLogin(url?: string | null): Promise<void> {
|
async logout(url?: string | null): Promise<any> {
|
||||||
return this._core.checkLogin(url)
|
return this._core.logout(url)
|
||||||
},
|
},
|
||||||
|
|
||||||
async query<T = any>(
|
async checkLogin(url?: string | null): Promise<void> {
|
||||||
uri: string,
|
return this._core.checkLogin(url)
|
||||||
params?: any,
|
},
|
||||||
endpointKey?: string,
|
|
||||||
extraHeaders?: Record<string, string>
|
|
||||||
): Promise<SimApiBaseResponse<T>> {
|
|
||||||
return this._core.query<T>(uri, params, endpointKey, extraHeaders)
|
|
||||||
},
|
|
||||||
|
|
||||||
getEndpoint(name?: string): string {
|
async query<T = any>(
|
||||||
return this._core.getEndpoint(name)
|
uri: string,
|
||||||
},
|
params?: any,
|
||||||
|
endpointKey?: string,
|
||||||
|
extraHeaders?: Record<string, string>,
|
||||||
|
selfHandleError: boolean = false
|
||||||
|
): Promise<SimApiBaseResponse<T>> {
|
||||||
|
return this._core.query<T>(uri, params, endpointKey, extraHeaders, selfHandleError)
|
||||||
|
},
|
||||||
|
|
||||||
async getVersion(endpointName?: string): Promise<SimApiVersions> {
|
getEndpoint(name?: string): string {
|
||||||
return this._core.getVersion(endpointName)
|
return this._core.getEndpoint(name)
|
||||||
|
},
|
||||||
|
|
||||||
|
async getVersion(endpointName?: string): Promise<SimApiVersions> {
|
||||||
|
return this._core.getVersion(endpointName)
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user