Skip to content

Commit c9b30ad

Browse files
committed
feat: 🚀 added ability to use custom
1 parent 8f8820c commit c9b30ad

File tree

5 files changed

+20
-11
lines changed

5 files changed

+20
-11
lines changed

nuxt/templates/plugin.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import Vue from 'vue'
22
import VueApiQueries, { Validator, BaseProxy } from 'vue-api-queries'
33

4+
const errorsKeyName = '<%= options.errorsKeyName %>'
5+
46
export default function (ctx, inject) {
57
Vue.use(VueApiQueries)
68
ctx.$errors = Validator
79
BaseProxy.$http = ctx.$axios
10+
BaseProxy.$errorsKeyName = errorsKeyName || 'errors'
811
inject('queries', BaseProxy)
912
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-api-queries",
3-
"version": "0.0.10-alpha.2",
3+
"version": "0.0.11",
44
"description": "Elegant and simple way to build requests for REST API",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/core/BaseProxy.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import { objectToFormData } from '../util/formData'
77
const validator = Validator
88

99
class BaseProxy {
10+
public errors: Errors
1011
public parameters: any | any[]
1112
public readonly endpoint: string
1213
public static $http: AxiosInstance
13-
public errors: Errors
14+
public static $errorsKeyName = 'errors'
1415

1516
constructor(endpoint: string, parameters?: any | any[]) {
1617
this.endpoint = endpoint
@@ -22,6 +23,10 @@ class BaseProxy {
2223
return <AxiosInstance>BaseProxy.$http
2324
}
2425

26+
get $errorsKeyName(): string {
27+
return BaseProxy.$errorsKeyName
28+
}
29+
2530
all(): Promise<any> {
2631
return this.submit('get', `/${this.endpoint}`)
2732
}
@@ -73,7 +78,8 @@ class BaseProxy {
7378
if (response) {
7479
const { data = {}, status } = response
7580
if (status === 422) {
76-
const { errors } = data
81+
const errors = {}
82+
Object.assign(errors, data[this.$errorsKeyName])
7783
this.onFail(errors)
7884
validator.fill(errors)
7985
}

src/core/Validator.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { is, isArray } from '../util/objects'
22

3+
export interface ErrorOptions {
4+
[key: string]: string | string[]
5+
}
6+
37
class Validator {
48
public errors: any
59
public successful: boolean
@@ -128,10 +132,6 @@ class Validator {
128132
}
129133
}
130134

131-
export interface ErrorOptions {
132-
[key: string]: any | any[]
133-
}
134-
135135
export type { Validator as ValidatorType }
136136

137137
export default new Validator()

src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import type { ValidatorType } from './core/Validator'
2+
import BaseProxy from './core/BaseProxy'
23
import Validator from './core/Validator'
4+
import BaseTransformer from './core/BaseTransformer'
5+
import PaginationTransformer from './core/PaginationTransformer'
36

47
// augment typings of Vue.js
58
import './vue'
@@ -23,8 +26,5 @@ class VueApiQueries {
2326
})
2427
}
2528
}
26-
export { default as Validator } from './core/Validator'
27-
export { default as BaseProxy } from './core/BaseProxy'
28-
export { default as BaseTransformer } from './core/BaseTransformer'
29-
export { default as PaginationTransformer } from './core/PaginationTransformer'
29+
export { BaseProxy, BaseTransformer, PaginationTransformer, Validator }
3030
export default new VueApiQueries()

0 commit comments

Comments
 (0)