11import type { AxiosError , AxiosInstance , AxiosResponse , Method } from 'axios'
2- import { isFile } from '../util/objects'
2+ import { isFile , isArray } from '../util/objects'
33import type { Errors } from '../'
44import Validator from './Validator'
55import { objectToFormData } from '../util/formData'
66
77const validator = Validator
8+ const UNPROCESSABLE_ENTITY = 422
9+ export interface ParametersType {
10+ [ key : string ] : any
11+ }
812
913class BaseProxy {
1014 public errors : Errors
@@ -68,16 +72,16 @@ class BaseProxy {
6872 this . $http [ requestType ] ( this . __getParameterString ( url ) , data )
6973 . then ( ( response : AxiosResponse ) => {
7074 this . onSuccess ( )
71- const { data = { } } = response
75+ const { data } = response
7276 resolve ( data )
7377 } )
7478 . catch ( ( error : AxiosError ) => {
7579 this . errors . processing = false
7680 validator . processing = false
7781 const { response } = error
7882 if ( response ) {
79- const { data = { } , status } = response
80- if ( status === 422 ) {
83+ const { data, status } = response
84+ if ( status === UNPROCESSABLE_ENTITY ) {
8185 const errors = { }
8286 Object . assign ( errors , data [ this . $errorsKeyName ] )
8387 this . onFail ( errors )
@@ -99,6 +103,16 @@ class BaseProxy {
99103 return parameters . length === 0 ? url : `${ url } ?${ parameters . join ( '&' ) } `
100104 }
101105
106+ __getQueryString ( parameter : string ) : string [ ] {
107+ const queries : string [ ] = parameter . split ( '&' )
108+ const obj : any = { }
109+ queries . forEach ( function ( property : string ) {
110+ const [ key = null , value = null ] : string [ ] = property . split ( '=' )
111+ obj [ key ] = value
112+ } )
113+ return obj
114+ }
115+
102116 __validateRequestType ( requestType : Method ) : void {
103117 const requestTypes : Array < string > = [
104118 'get' ,
@@ -146,7 +160,7 @@ class BaseProxy {
146160 }
147161 }
148162
149- if ( Array . isArray ( object ) ) {
163+ if ( isArray ( object ) ) {
150164 for ( const key in object ) {
151165 if ( object . hasOwnProperty ( key ) ) {
152166 return this . __hasFilesDeep ( object [ key ] )
@@ -164,7 +178,11 @@ class BaseProxy {
164178 return this
165179 }
166180
167- setParameter ( parameter : any , value : any ) : this {
181+ setParameter ( parameter : string , value ?: any ) : this {
182+ if ( ! value ) {
183+ this . parameters = this . __getQueryString ( parameter )
184+ return this
185+ }
168186 this . parameters [ parameter ] = value
169187 return this
170188 }
@@ -210,7 +228,3 @@ class BaseProxy {
210228}
211229
212230export default BaseProxy
213-
214- export interface ParametersType {
215- [ key : string ] : any
216- }
0 commit comments