Skip to content

Commit beb9478

Browse files
committed
chore: 🚚 :fixed can't over default errorproperty message key and some minor bug fixes
1 parent 480f170 commit beb9478

File tree

7 files changed

+34
-24
lines changed

7 files changed

+34
-24
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-api-queries",
3-
"version": "1.1.2",
3+
"version": "1.1.3",
44
"description": "Elegant and simple way to build requests for REST API",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
@@ -75,7 +75,6 @@
7575
],
7676
"dependencies": {
7777
"camelcase-keys": "^7.0.0",
78-
"lodash.merge": "^4.6.2",
7978
"qs": "^6.10.1",
8079
"snakecase-keys": "^5.0.0"
8180
}

src/__tests__/base-proxy.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ describe('BaseProxy', () => {
1717
validator = Validator
1818
const axios = Axios.create({ baseURL: 'https://mock-api.test' })
1919
BaseProxy.$http = axios
20+
BaseProxy.$errorProperty = 'message'
2021
proxy = new PostProxy()
2122
mockAdapter = new MockAdapter(axios)
2223
mockAdapter.reset()
@@ -304,7 +305,7 @@ describe('BaseProxy', () => {
304305
it('it should throw errors message when data is not valid', async () => {
305306
const item = { first_name: null, last_name: 'Sek', id: 1 }
306307
mockAdapter.onPost('/posts').replyOnce(422, {
307-
errors: { first_name: ['The first name field is required'] },
308+
message: { first_name: 'The first name field is required' },
308309
})
309310
try {
310311
await proxy.post(item)

src/__tests__/package.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ describe('Vue Api Queries', () => {
77
disableTransitions()
88
addElemWithDataAppToBody()
99
Vue.use(VueApiQueries, {
10-
errorProperty: 'errors',
10+
errorProperty: 'message',
1111
})
1212

1313
test('Get plugin installed', async () => {

src/index.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import BaseProxy from './core/BaseProxy'
33
import Validator from './core/Validator'
44
import BaseTransformer from './core/BaseTransformer'
55
import PaginationTransformer from './core/PaginationTransformer'
6-
import merge from 'lodash.merge'
6+
import { merge } from './util'
7+
import _Vue from 'vue'
78

89
// augment typings of Vue.js
910
import './vue'
@@ -13,29 +14,35 @@ export type { ValidatorType }
1314

1415
class VueApiQueries {
1516
installed = false
16-
install(Vue: any, options: any = {}) {
17+
parsedQs = {
18+
comma: true,
19+
allowDots: true,
20+
ignoreQueryPrefix: true,
21+
}
22+
install(Vue: typeof _Vue, options: any = {}) {
1723
if (this.installed) return
1824
this.installed = true
19-
const defaultOption = merge(options, {
20-
parsedQs: {
21-
comma: true,
22-
allowDots: true,
23-
ignoreQueryPrefix: true,
25+
const defaultOption = merge(
26+
{
27+
parsedQs: this.parsedQs,
28+
errorProperty: 'errors',
2429
},
25-
errorProperty: 'errors',
26-
})
30+
options,
31+
)
2732
const { axios, errorProperty, parsedQs } = defaultOption
2833
BaseProxy.$http = axios
2934
BaseProxy.$errorProperty = errorProperty || 'errors'
30-
BaseProxy.$parsedQs = parsedQs
35+
BaseProxy.$parsedQs = parsedQs || this.parsedQs
3136
Vue.mixin({
3237
beforeCreate() {
33-
this.$options.$errors = {}
34-
Vue.util.defineReactive(this.$options, '$errors', Validator)
38+
this.$options.$errors = {} as any
39+
Vue.set(this.$options, '$errors', Validator)
3540
if (!this.$options.computed) {
3641
this.$options.computed = {}
3742
}
3843
this.$options.computed.$errors = function () {
44+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
45+
// @ts-ignore
3946
return this.$options.$errors
4047
}
4148
},

src/util/objects.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ export function isArray(object: any): boolean {
22
return Object.prototype.toString.call(object) === '[object Array]'
33
}
44

5+
export function hasOwnProperty(obj: any, key: any) {
6+
if (!obj || !key) return false
7+
return Object.prototype.hasOwnProperty.call(obj, key)
8+
}
9+
510
export function isFile(object: any): boolean {
611
if (typeof window === 'undefined') {
712
return false
@@ -12,9 +17,9 @@ export function isFile(object: any): boolean {
1217
return object instanceof File || object instanceof FileList
1318
}
1419

15-
export function merge(a: any, b: any): string[] {
20+
export function merge(a: any, b: any): Record<any, any> {
1621
for (const key in b) {
17-
if (Object.prototype.hasOwnProperty.call(b, key)) {
22+
if (hasOwnProperty(b, key)) {
1823
a[key] = cloneDeep(b[key])
1924
}
2025
}
@@ -34,7 +39,7 @@ export function cloneDeep(object: any): any {
3439
const clone: any = []
3540

3641
for (const key in object) {
37-
if (Object.prototype.hasOwnProperty.call(object, key)) {
42+
if (hasOwnProperty(object, key)) {
3843
clone[key] = cloneDeep(object[key])
3944
}
4045
}
@@ -46,7 +51,7 @@ export function cloneDeep(object: any): any {
4651
const clone: any = {}
4752

4853
for (const key in object) {
49-
if (Object.prototype.hasOwnProperty.call(object, key)) {
54+
if (hasOwnProperty(object, key)) {
5055
clone[key] = cloneDeep(object[key])
5156
}
5257
}

src/util/promise.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
export function sleep(ms: number) {
2-
return new Promise((resolve) => {
3-
setTimeout(resolve, ms)
4-
})
2+
return new Promise((resolve) => setTimeout(resolve, ms))
53
}

src/vue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ declare module 'vue/types/vue' {
1717
declare module 'vue/types/options' {
1818
// eslint-disable-next-line @typescript-eslint/no-unused-vars
1919
interface ComponentOptions<V extends _Vue> {
20-
errors?: ValidatorType
20+
$errors?: ValidatorType
2121
}
2222
}

0 commit comments

Comments
 (0)