1- import type { AxiosError , AxiosInstance , AxiosResponse , Method , AxiosRequestConfig } from 'axios'
2- import type { Errors } from '..'
3- import Validator from './Validator'
4- import { hasFiles , objectToFormData , removeDoubleSlash } from '../util'
1+ import type { ValidatorType } from './Validator'
2+ import type { AxiosError , AxiosInstance , Method , AxiosRequestConfig , AxiosResponse } from 'axios'
3+ import type { IParseOptions } from 'qs'
54import { isObject , isArray } from 'lodash'
6- import qs , { IParseOptions } from 'qs'
5+ import qs from 'qs'
6+ import Validator from './Validator'
7+ import { hasFiles , objectToFormData } from '../util'
78
89const validator = Validator
910const UNPROCESSABLE_ENTITY = 422
@@ -13,7 +14,7 @@ interface AxiosResponseData {
1314}
1415
1516class BaseService {
16- errors : Errors
17+ errors : ValidatorType
1718 parameters : Record < string , any >
1819 endpoint : string
1920 static $http : AxiosInstance
@@ -62,9 +63,7 @@ class BaseService {
6263 const parameter = id && ! isObject ( id ) ? `/${ id } ` : ''
6364 const body = isObject ( id ) ? id : payload
6465 const requestType : Method = hasFiles ( body ) ? 'post' : 'put'
65- if ( hasFiles ( body ) ) {
66- Object . assign ( body , { _method : 'put' } )
67- }
66+ if ( hasFiles ( body ) ) Object . assign ( body , { _method : 'put' } )
6867 return this . submit < T > ( requestType , parameter , body , config )
6968 }
7069
@@ -86,18 +85,25 @@ class BaseService {
8685 return this . delete < T > ( id )
8786 }
8887
89- submit < T = any > ( method : Method , parameter ?: string | number , form ?: T , config ?: AxiosRequestConfig ) : Promise < T > {
90- BaseService . __validateRequestType ( method )
88+ submit < T = any > ( method : Method , url ?: string | number , form ?: any , config ?: AxiosRequestConfig ) {
89+ return new Promise < T > ( ( resolve , reject ) => {
90+ this . $submit < T > ( method , url , form , config )
91+ . then ( ( { data } ) => resolve ( data ) )
92+ . catch ( ( err ) => reject ( err ) )
93+ } )
94+ }
95+
96+ $submit < T = any > ( method : Method , param ?: string | number , form ?: any , config ?: AxiosRequestConfig ) {
9197 this . beforeSubmit ( )
92- return new Promise ( ( resolve , reject ) => {
98+ return new Promise < AxiosResponse < T > > ( ( resolve , reject ) => {
9399 const data = hasFiles ( form ) ? objectToFormData ( form ) : form
94- const endpoint = parameter ? `/${ this . endpoint } /${ parameter } ` : `/${ this . endpoint } `
95- const url = this . __getParameterString ( removeDoubleSlash ( endpoint ) )
100+ const endpoint = param ? `/${ this . endpoint } /${ param } ` : `/${ this . endpoint } `
101+ const url = this . __getParameterString ( endpoint . replace ( / \/ \/ / g , '/' ) )
96102 config = Object . assign ( { } , config , { url, data, method } )
97103 this . $http ( config )
98- . then ( ( response : AxiosResponse ) => {
104+ . then ( ( response ) => {
99105 this . onSuccess ( )
100- resolve ( response . data )
106+ resolve ( response )
101107 } )
102108 . catch ( ( error : AxiosError < AxiosResponseData > ) => {
103109 this . errors . processing = false
@@ -115,39 +121,10 @@ class BaseService {
115121 }
116122
117123 private __getParameterString ( url : string ) {
118- const query = qs . stringify ( this . parameters , {
119- encode : false ,
120- skipNulls : true ,
121- addQueryPrefix : true ,
122- } )
124+ const query = qs . stringify ( this . parameters , { encode : false , skipNulls : true , addQueryPrefix : true } )
123125 return `${ url } ${ query } `
124126 }
125127
126- private static __validateRequestType ( requestType : Method ) {
127- const requestTypes : Method [ ] = [
128- 'get' ,
129- 'GET' ,
130- 'delete' ,
131- 'DELETE' ,
132- 'head' ,
133- 'HEAD' ,
134- 'options' ,
135- 'OPTIONS' ,
136- 'post' ,
137- 'POST' ,
138- 'put' ,
139- 'PUT' ,
140- 'patch' ,
141- 'PATCH' ,
142- ]
143- if ( ! requestTypes . includes ( requestType ) ) {
144- throw new Error (
145- `\`${ requestType } \` is not a valid request type, ` + `must be one of: \`${ requestTypes . join ( '`, `' ) } \`.` ,
146- )
147- }
148- return requestType
149- }
150-
151128 setParameters ( parameters : Record < string , any > ) : this {
152129 Object . keys ( parameters ) . forEach ( ( key ) => {
153130 this . parameters [ key ] = parameters [ key ]
@@ -173,9 +150,7 @@ class BaseService {
173150 if ( ! parameters || ! parameters . length ) {
174151 this . parameters = [ ]
175152 } else if ( isArray ( parameters ) ) {
176- for ( const parameter of parameters ) {
177- delete this . parameters [ parameter ]
178- }
153+ for ( const parameter of parameters ) delete this . parameters [ parameter ]
179154 }
180155 return this
181156 }
@@ -191,9 +166,7 @@ class BaseService {
191166 }
192167
193168 beforeSubmit ( ) {
194- if ( ! this . $http ) {
195- throw new Error ( 'Vue Axios Http, No http library provided.' )
196- }
169+ if ( ! this . $http ) throw new Error ( 'Vue Axios Http, No http library provided.' )
197170 this . errors . flush ( )
198171 this . errors . processing = true
199172 this . errors . successful = false
0 commit comments