Skip to content

Commit 1e22769

Browse files
committed
perf: optimized the code
1 parent 1eefc70 commit 1e22769

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

src/core/BaseService.ts

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -86,39 +86,34 @@ export default class BaseService {
8686

8787
$submit<T = any, F = any>(method: Method, param?: string | number, form?: F, config?: AxiosRequestConfig) {
8888
this.beforeSubmit()
89+
const formData = hasFiles(form) ? objectToFormData(form) : form
90+
const endpointPath = param ? `/${this.endpoint}/${param}` : `/${this.endpoint}`
91+
const endpoint = endpointPath.replace(/\/\//g, '/')
92+
const url = this.__getParameterString(endpoint)
93+
const axiosConfig = { url, data: formData, method, ...config }
8994
return new Promise<AxiosResponse<AxiosResponse<T>>>((resolve, reject) => {
90-
const formData = hasFiles(form) ? objectToFormData(form) : form
91-
const endpointPath = param ? `/${this.endpoint}/${param}` : `/${this.endpoint}`
92-
const endpoint = endpointPath.replace(/\/\//g, '/')
93-
const url = this.__getParameterString(endpoint)
94-
const axiosConfig = { url, data: formData, method, ...config }
95-
this.$http(axiosConfig)
95+
this.$http<T>(axiosConfig)
9696
.then((response) => {
9797
this.onSuccess()
98+
if (this.$resetParameter) this.removeParameters()
9899
resolve(response)
99100
})
100101
.catch((error: AxiosError<AxiosResponseData>) => {
101102
this.errors.processing = false
102103
validator.processing = false
103-
const { response } = error
104-
if (response && response.status === UNPROCESSABLE_ENTITY) {
105-
const { data } = response
104+
if (error.response && error.response.status === UNPROCESSABLE_ENTITY) {
106105
const validationErrors: SimpleObject<any> = {}
107-
Object.assign(validationErrors, data[this.$errorProperty])
106+
Object.assign(validationErrors, error.response.data[this.$errorProperty])
108107
this.onFail(validationErrors)
109108
}
110109
reject(error)
111110
})
112-
if (this.$resetParameter) this.removeParameters()
113111
})
114112
}
115113

116-
submit<T = any, F = any>(method: Method, url?: string | number, form?: F, config?: AxiosRequestConfig) {
117-
return new Promise<AxiosResponse<T>>((resolve, reject) => {
118-
this.$submit<T>(method, url, form, config)
119-
.then(({ data }) => resolve(data))
120-
.catch((err) => reject(err))
121-
})
114+
async submit<T = any, F = any>(method: Method, url?: string | number, form?: F, config?: AxiosRequestConfig) {
115+
const { data } = await this.$submit<T>(method, url, form, config)
116+
return data
122117
}
123118

124119
private __getParameterString(url: string) {

0 commit comments

Comments
 (0)