Skip to content

Commit 267afba

Browse files
committed
feat: ✨ add remove param option
1 parent 1eba7d8 commit 267afba

File tree

3 files changed

+44
-38
lines changed

3 files changed

+44
-38
lines changed

src/core/BaseService.ts

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { ValidatorType } from './Validator'
21
import type { AxiosError, AxiosInstance, Method, AxiosRequestConfig, AxiosResponse } from 'axios'
32
import type { IParseOptions } from 'qs'
43
import { isObject, isArray } from 'lodash'
@@ -13,23 +12,18 @@ interface AxiosResponseData {
1312
[key: string | number]: any
1413
}
1514

16-
class BaseService {
17-
errors: ValidatorType
18-
parameters: Record<string, any>
19-
endpoint: string
15+
export default class BaseService {
16+
public errors = Validator
2017
static $http: AxiosInstance
2118
static $errorProperty = 'errors'
19+
static $removeParams? = false
2220
static $parsedQs: IParseOptions = {
2321
comma: true,
2422
allowDots: true,
2523
ignoreQueryPrefix: true,
2624
}
2725

28-
constructor(endpoint: string, parameters: Record<string, any>) {
29-
this.endpoint = endpoint
30-
this.parameters = parameters
31-
this.errors = Validator
32-
}
26+
constructor(readonly endpoint: string, public parameters: Record<string, any>) {}
3327

3428
get $http() {
3529
return BaseService.$http
@@ -39,6 +33,10 @@ class BaseService {
3933
return BaseService.$errorProperty
4034
}
4135

36+
get $removeParams() {
37+
return BaseService.$removeParams
38+
}
39+
4240
get $parsedQs() {
4341
return BaseService.$parsedQs
4442
}
@@ -85,15 +83,7 @@ class BaseService {
8583
return this.delete<T>(id)
8684
}
8785

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) {
86+
$submit<T = any, F = any>(method: Method, param?: string | number, form?: F, config?: AxiosRequestConfig) {
9787
this.beforeSubmit()
9888
return new Promise<AxiosResponse<T>>((resolve, reject) => {
9989
const data = hasFiles(form) ? objectToFormData(form) : form
@@ -117,6 +107,15 @@ class BaseService {
117107
}
118108
reject(error)
119109
})
110+
if (this.$removeParams) this.removeParameters()
111+
})
112+
}
113+
114+
submit<T = any, F = any>(method: Method, url?: string | number, form?: F, config?: AxiosRequestConfig) {
115+
return new Promise<T>((resolve, reject) => {
116+
this.$submit<T>(method, url, form, config)
117+
.then(({ data }) => resolve(data))
118+
.catch((err) => reject(err))
120119
})
121120
}
122121

@@ -125,14 +124,14 @@ class BaseService {
125124
return `${url}${query}`
126125
}
127126

128-
setParameters(parameters: Record<string, any>): this {
127+
setParameters(parameters: Record<string, any>) {
129128
Object.keys(parameters).forEach((key) => {
130129
this.parameters[key] = parameters[key]
131130
})
132131
return this
133132
}
134133

135-
setParameter(parameter: string, value?: any): this {
134+
setParameter(parameter: string, value?: any) {
136135
if (!value) {
137136
const options: IParseOptions = Object.assign({}, this.$parsedQs, {
138137
comma: true,
@@ -146,7 +145,7 @@ class BaseService {
146145
return this
147146
}
148147

149-
removeParameters(parameters = [] as any[]): this {
148+
removeParameters(parameters: string[] = []) {
150149
if (!parameters || !parameters.length) {
151150
this.parameters = []
152151
} else if (isArray(parameters)) {
@@ -155,7 +154,7 @@ class BaseService {
155154
return this
156155
}
157156

158-
removeParameter(parameter: string): this {
157+
removeParameter(parameter: string) {
159158
delete this.parameters[parameter]
160159
return this
161160
}
@@ -182,5 +181,3 @@ class BaseService {
182181
validator.successful = true
183182
}
184183
}
185-
186-
export default BaseService

src/core/Validator.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@ import { cloneDeep, get, has, omit } from 'lodash'
22
import { is, toCamelCase, toSnakeCase } from '../util'
33

44
class Validator {
5-
public errors: Record<string, any>
65
public successful: boolean
76
public processing: boolean
87

9-
constructor(errors: Record<string, any> = {}) {
8+
constructor(public errors: Record<string, any> = {}) {
109
this.processing = false
1110
this.successful = false
12-
this.errors = errors
1311
}
1412

1513
add(field: string, message: string, forceUpdate?: boolean) {

src/index.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,32 @@ import Validator from './core/Validator'
88
// augment typings of Vue.js
99
import './vue'
1010

11-
class AxiosHttp {
12-
installed = false
13-
parsedQs: IParseOptions = {
11+
interface ModuleOptions {
12+
removeParams?: boolean
13+
parsedQs: IParseOptions
14+
errorProperty?: string | 'errors' | 'message'
15+
}
16+
const optionDefault: ModuleOptions = {
17+
removeParams: false,
18+
parsedQs: {
1419
comma: true,
1520
allowDots: true,
1621
ignoreQueryPrefix: true,
17-
}
18-
install(Vue: typeof _Vue, options: Record<string, any> = {}) {
22+
},
23+
errorProperty: 'errors',
24+
}
25+
26+
class AxiosHttp {
27+
installed = false
28+
install(Vue: typeof _Vue, options: Partial<ModuleOptions> = {}) {
1929
if (this.installed) return
30+
2031
this.installed = true
21-
const defaultOption = merge({ parsedQs: this.parsedQs, errorProperty: 'errors' }, options)
22-
const { $axios, errorProperty, parsedQs } = defaultOption
23-
BaseService.$http = $axios
24-
BaseService.$errorProperty = errorProperty || 'errors'
32+
const { errorProperty, parsedQs, removeParams } = merge(optionDefault, options)
33+
2534
BaseService.$parsedQs = parsedQs
35+
BaseService.$removeParams = removeParams
36+
BaseService.$errorProperty = errorProperty || 'errors'
2637
Vue.mixin({
2738
beforeCreate() {
2839
this.$options.$errors = {} as never
@@ -41,6 +52,6 @@ class AxiosHttp {
4152
}
4253

4354
export * from './util'
44-
export type { ValidatorType }
55+
export type { ValidatorType, ModuleOptions }
4556
export { Validator, BaseService }
4657
export default new AxiosHttp()

0 commit comments

Comments
 (0)