Skip to content

Commit

Permalink
chore: ⚡ bump v1.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
chantouchsek committed Sep 20, 2021
1 parent e059ee4 commit 480f170
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 22 deletions.
10 changes: 4 additions & 6 deletions nuxt/templates/plugin.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import Vue from 'vue'
import VueApiQueries, { Validator, BaseProxy } from 'vue-api-queries'
import VueApiQueries, { Validator } from 'vue-api-queries'

const errorProperty = '<%= options.errorProperty %>'
const options = <%= serialize(options) %> || {}
const { errorProperty, parsedQs } = options

export default function (ctx) {
Vue.use(VueApiQueries)
Vue.use(VueApiQueries, { errorProperty, axios: ctx.$axios, parsedQs })
ctx.$errors = Validator
BaseProxy.$http = ctx.$axios
BaseProxy.$errorProperty = errorProperty || 'errors'
// inject('queries', BaseProxy)
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-api-queries",
"version": "1.1.1",
"version": "1.1.2",
"description": "Elegant and simple way to build requests for REST API",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/base-proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { ValidatorType } from '../core/Validator'
import Validator from '../core/Validator'
import BaseTransformer from '../core/BaseTransformer'
import PaginationTransformer from '../core/PaginationTransformer'
import { merge } from '../util/objects'
import { merge } from '../util'

let proxy: PostProxy
let mockAdapter: MockAdapter
Expand Down
26 changes: 18 additions & 8 deletions src/core/BaseProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
import type { Errors } from '..'
import Validator from './Validator'
import { hasFiles, objectToFormData, removeDoubleSlash } from '../util'
import qs, { ParsedQs } from 'qs'
import qs, { IParseOptions } from 'qs'
import merge from 'lodash.merge'

const validator = Validator
const UNPROCESSABLE_ENTITY = 422
Expand All @@ -21,8 +22,13 @@ class BaseProxy {
public errors: Errors
public parameters: any | any[]
public readonly endpoint: string
public static $http: AxiosInstance | undefined
public static $http?: AxiosInstance
public static $errorProperty = 'errors'
public static $parsedQs: IParseOptions = {
comma: true,
allowDots: true,
ignoreQueryPrefix: true,
}

constructor(endpoint: string, parameters?: ParametersType) {
this.endpoint = endpoint
Expand All @@ -38,6 +44,10 @@ class BaseProxy {
return BaseProxy.$errorProperty
}

get $parsedQs() {
return BaseProxy.$parsedQs
}

/**
* Get all or by pagination
*/
Expand Down Expand Up @@ -228,7 +238,7 @@ class BaseProxy {
}

private static __validateRequestType(requestType: Method): string {
const requestTypes: Array<string> = [
const requestTypes: string[] = [
'get',
'delete',
'head',
Expand Down Expand Up @@ -263,12 +273,12 @@ class BaseProxy {
*/
setParameter(parameter: string, value?: any): this {
if (!value) {
const options = {
const options: IParseOptions = merge(this.$parsedQs, {
comma: true,
allowDots: true,
ignoreQueryPrefix: true,
}
const params: ParsedQs = qs.parse(parameter, options)
})
const params = qs.parse(parameter, options)
return this.setParameters(params)
}
this.parameters[parameter] = value
Expand All @@ -283,9 +293,9 @@ class BaseProxy {
if (!parameters.length) {
this.parameters = []
} else {
parameters.forEach((parameter) => {
for (const parameter of parameters) {
delete this.parameters[parameter]
})
}
}
return this
}
Expand Down
10 changes: 5 additions & 5 deletions src/core/BaseTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import snakeCaseKeys from 'snakecase-keys'
import camelcaseKeys from 'camelcase-keys'

class BaseTransformer {
static fetchCollection<T>(items: T[], camelKey?: boolean): T[] {
static fetchCollection<T>(
items: T[],
camelKey?: boolean,
): (Record<string, any> | T)[] {
return items.map((item: T) => this.fetch(item, camelKey))
}

Expand All @@ -13,10 +16,7 @@ class BaseTransformer {
return items.map((item: T) => this.send(item, snakeKey))
}

static fetch<T extends Record<string, any>>(
item: T,
camelKey?: boolean,
): T | any {
static fetch<T>(item: T, camelKey?: boolean): T | Record<string, any> {
return camelKey ? camelcaseKeys(item, { deep: true }) : item
}

Expand Down
18 changes: 17 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import BaseProxy from './core/BaseProxy'
import Validator from './core/Validator'
import BaseTransformer from './core/BaseTransformer'
import PaginationTransformer from './core/PaginationTransformer'
import merge from 'lodash.merge'

// augment typings of Vue.js
import './vue'
Expand All @@ -11,7 +12,22 @@ export type Errors = ValidatorType
export type { ValidatorType }

class VueApiQueries {
install(Vue: any) {
installed = false
install(Vue: any, options: any = {}) {
if (this.installed) return
this.installed = true
const defaultOption = merge(options, {
parsedQs: {
comma: true,
allowDots: true,
ignoreQueryPrefix: true,
},
errorProperty: 'errors',
})
const { axios, errorProperty, parsedQs } = defaultOption
BaseProxy.$http = axios
BaseProxy.$errorProperty = errorProperty || 'errors'
BaseProxy.$parsedQs = parsedQs
Vue.mixin({
beforeCreate() {
this.$options.$errors = {}
Expand Down

0 comments on commit 480f170

Please sign in to comment.