Skip to content

Commit 1eefc70

Browse files
committed
feat: ✨ add support wildcard fields
1 parent e9ccb5b commit 1eefc70

File tree

4 files changed

+58
-75
lines changed

4 files changed

+58
-75
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
"nuxt"
8484
],
8585
"dependencies": {
86-
"axios": "^0.27.2",
86+
"axios": "^0.28.1",
8787
"lodash": "^4.17.21",
8888
"qs": "^6.12.1"
8989
},

src/core/BaseService.ts

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

8787
$submit<T = any, F = any>(method: Method, param?: string | number, form?: F, config?: AxiosRequestConfig) {
8888
this.beforeSubmit()
89-
return new Promise<AxiosResponse<T>>((resolve, reject) => {
89+
return new Promise<AxiosResponse<AxiosResponse<T>>>((resolve, reject) => {
9090
const formData = hasFiles(form) ? objectToFormData(form) : form
9191
const endpointPath = param ? `/${this.endpoint}/${param}` : `/${this.endpoint}`
9292
const endpoint = endpointPath.replace(/\/\//g, '/')
@@ -114,7 +114,7 @@ export default class BaseService {
114114
}
115115

116116
submit<T = any, F = any>(method: Method, url?: string | number, form?: F, config?: AxiosRequestConfig) {
117-
return new Promise<T>((resolve, reject) => {
117+
return new Promise<AxiosResponse<T>>((resolve, reject) => {
118118
this.$submit<T>(method, url, form, config)
119119
.then(({ data }) => resolve(data))
120120
.catch((err) => reject(err))
@@ -133,32 +133,27 @@ export default class BaseService {
133133
}
134134

135135
setParameters(parameters: SimpleObject<any>) {
136-
Object.keys(parameters).forEach((key) => {
137-
this.parameters[key] = parameters[key]
138-
})
136+
this.parameters = { ...this.parameters, ...parameters }
139137
return this
140138
}
141139

142140
setParameter(parameter: string, value?: any) {
143141
if (!value) {
144-
const options: IParseOptions = Object.assign({}, this.$parsedQs, {
145-
comma: true,
146-
allowDots: true,
147-
ignoreQueryPrefix: true,
148-
})
149-
const params = parse(parameter, options)
150-
return this.setParameters(params)
142+
return this.setParameters(
143+
parse(parameter, {
144+
...this.$parsedQs,
145+
comma: true,
146+
allowDots: true,
147+
ignoreQueryPrefix: true,
148+
}),
149+
)
151150
}
152151
this.parameters[parameter] = value
153152
return this
154153
}
155154

156155
removeParameters(parameters: string[] = []) {
157-
if (!parameters || !parameters.length) {
158-
this.parameters = {}
159-
} else if (Array.isArray(parameters)) {
160-
for (const parameter of parameters) delete this.parameters[parameter]
161-
}
156+
parameters.length ? parameters.forEach((param) => delete this.parameters[param]) : (this.parameters = {})
162157
return this
163158
}
164159

src/core/Validator.ts

Lines changed: 33 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { SimpleObject } from '../types'
2-
import { cloneDeep, get, has, omit } from 'lodash'
2+
import { castArray, cloneDeep, get, has, omit } from 'lodash'
33
import { is, toCamelCase, toSnakeCase } from '../util'
44

55
class Validator {
@@ -12,11 +12,10 @@ class Validator {
1212
}
1313

1414
add(field: string, message: string, forceUpdate?: boolean) {
15-
if (this.missed(field)) this.errors[field] = []
16-
if (!this.errors[field].includes(message)) this.errors[field].unshift(message)
17-
if (forceUpdate) {
18-
this.errors[field] = []
19-
this.errors[field].push(message)
15+
if (forceUpdate || this.missed(field)) {
16+
this.errors[field] = [message]
17+
} else if (!this.errors[field].includes(message)) {
18+
this.errors[field].unshift(message)
2019
}
2120
}
2221

@@ -26,32 +25,15 @@ class Validator {
2625
}
2726

2827
first(field: string | string[]): string | undefined {
29-
if (Array.isArray(field)) {
30-
const fields = this.fields(field)
31-
let fd = ''
32-
for (const f of fields) {
33-
if (has(this.errors, f)) {
34-
fd = f
35-
break
36-
}
37-
}
38-
return this.first(fd)
39-
} else {
40-
const value = this.get(field)
41-
if (Array.isArray(value)) return value[0]
42-
return value
43-
}
28+
const fields = this.fields(castArray(field))
29+
const foundField = fields.find((f) => has(this.errors, f)) ?? ''
30+
const value = this.get(foundField)
31+
return Array.isArray(value) ? value[0] : value
4432
}
4533

46-
firstBy(obj: SimpleObject<any>, field?: string) {
47-
let value: string
48-
if (!field) {
49-
value = obj[Object.keys(obj)[0]]
50-
} else {
51-
value = obj[field]
52-
}
53-
if (Array.isArray(value)) value = value[0]
54-
return value
34+
firstBy(obj: SimpleObject<any>, field: string = Object.keys(obj)[0]): string {
35+
const value: string = obj[field]
36+
return Array.isArray(value) ? value[0] : value
5537
}
5638

5739
missed(field: string | string[]) {
@@ -64,20 +46,17 @@ class Validator {
6446

6547
any(field: string[] = [], returnObject?: boolean) {
6648
const fields = this.fields(field)
67-
if (returnObject) {
68-
const errors: SimpleObject<any> = {}
69-
if (!fields.length) return {}
70-
for (const f of fields) {
71-
const val = this.get(f)
72-
if (!val.length) continue
73-
errors[f] = val
74-
}
75-
return errors
76-
}
77-
if (!fields.length) return Object.keys(this.errors).length > 0
7849
const errors: SimpleObject<any> = {}
79-
fields.forEach((key: string) => (errors[key] = this.get(key)))
80-
return Object.keys(errors).length > 0
50+
51+
if (!fields.length) return returnObject ? {} : Object.keys(this.errors).length > 0
52+
53+
fields.forEach((f: string) => {
54+
const val = this.get(f)
55+
if (returnObject && val.length) errors[f] = val
56+
else if (!returnObject) errors[f] = val
57+
})
58+
59+
return returnObject ? errors : Object.keys(errors).length > 0
8160
}
8261

8362
get(field: string): string | string[] {
@@ -117,15 +96,18 @@ class Validator {
11796
this.clear(names)
11897
}
11998

120-
fields(field: string | string[]): string[] {
121-
const fields: string[] = []
122-
if (Array.isArray(field)) {
123-
for (const f of field) {
124-
fields.push(toCamelCase(f), toSnakeCase(f))
125-
}
126-
} else {
127-
fields.push(toCamelCase(field), toSnakeCase(field))
99+
fields(field: string | string[]) {
100+
const processField = (f: string) => {
101+
if (f.includes('*')) {
102+
const regex = new RegExp(`^${f.replace('*', '.*')}$`, 'i')
103+
for (const key in this.errors) {
104+
if (regex.test(key)) fields.push(toCamelCase(key), toSnakeCase(key))
105+
}
106+
} else fields.push(toCamelCase(f), toSnakeCase(f))
128107
}
108+
109+
const fields: string[] = []
110+
Array.isArray(field) ? field.forEach(processField) : processField(field)
129111
return [...new Set(fields)].filter(Boolean)
130112
}
131113
}

yarn.lock

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3740,13 +3740,14 @@ axios-mock-adapter@^1.22.0:
37403740
fast-deep-equal "^3.1.3"
37413741
is-buffer "^2.0.5"
37423742

3743-
axios@^0.27.2:
3744-
version "0.27.2"
3745-
resolved "https://registry.yarnpkg.com/axios/-/axios-0.27.2.tgz#207658cc8621606e586c85db4b41a750e756d972"
3746-
integrity sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==
3743+
axios@^0.28.1:
3744+
version "0.28.1"
3745+
resolved "https://registry.yarnpkg.com/axios/-/axios-0.28.1.tgz#2a7bcd34a3837b71ee1a5ca3762214b86b703e70"
3746+
integrity sha512-iUcGA5a7p0mVb4Gm/sy+FSECNkPFT4y7wt6OM/CDpO/OnNCvSs3PoMG8ibrC9jRoGYU0gUK5pXVC4NPXq6lHRQ==
37473747
dependencies:
3748-
follow-redirects "^1.14.9"
3748+
follow-redirects "^1.15.0"
37493749
form-data "^4.0.0"
3750+
proxy-from-env "^1.1.0"
37503751

37513752
babel-loader@^8.3.0:
37523753
version "8.3.0"
@@ -6303,7 +6304,7 @@ flush-write-stream@^1.0.0:
63036304
inherits "^2.0.3"
63046305
readable-stream "^2.3.6"
63056306

6306-
follow-redirects@^1.14.9:
6307+
follow-redirects@^1.15.0:
63076308
version "1.15.6"
63086309
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.6.tgz#7f815c0cda4249c74ff09e95ef97c23b5fd0399b"
63096310
integrity sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==
@@ -10161,6 +10162,11 @@ protocols@^2.0.0, protocols@^2.0.1:
1016110162
resolved "https://registry.yarnpkg.com/protocols/-/protocols-2.0.1.tgz#8f155da3fc0f32644e83c5782c8e8212ccf70a86"
1016210163
integrity sha512-/XJ368cyBJ7fzLMwLKv1e4vLxOju2MNAIokcr7meSaNcVbWz/CPcW22cP04mwxOErdA5mwjA8Q6w/cdAQxVn7Q==
1016310164

10165+
proxy-from-env@^1.1.0:
10166+
version "1.1.0"
10167+
resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz#e102f16ca355424865755d2c9e8ea4f24d58c3e2"
10168+
integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==
10169+
1016410170
prr@~1.0.1:
1016510171
version "1.0.1"
1016610172
resolved "https://registry.yarnpkg.com/prr/-/prr-1.0.1.tgz#d3fc114ba06995a45ec6893f484ceb1d78f5f476"

0 commit comments

Comments
 (0)