Skip to content

Fix/889 return types #890

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/core/BaseService.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { SimpleObject } from '../types'
import type { AxiosError, AxiosInstance, Method, AxiosRequestConfig, AxiosResponse } from 'axios'
import type { IParseOptions } from 'qs'
import { isObject } from 'lodash'
Expand All @@ -23,7 +24,7 @@ export default class BaseService {
ignoreQueryPrefix: true,
}

constructor(readonly endpoint: string, public parameters: Record<string, any> = {}) {}
constructor(readonly endpoint: string, public parameters: SimpleObject<any> = {}) {}

get $http() {
return BaseService.$http
Expand Down Expand Up @@ -102,7 +103,7 @@ export default class BaseService {
const { response } = error
if (response && response.status === UNPROCESSABLE_ENTITY) {
const { data } = response
const validationErrors: Record<string, any> = {}
const validationErrors: SimpleObject<any> = {}
Object.assign(validationErrors, data[this.$errorProperty])
this.onFail(validationErrors)
}
Expand Down Expand Up @@ -131,7 +132,7 @@ export default class BaseService {
return `${url}${query}`
}

setParameters(parameters: Record<string, any>) {
setParameters(parameters: SimpleObject<any>) {
Object.keys(parameters).forEach((key) => {
this.parameters[key] = parameters[key]
})
Expand Down Expand Up @@ -166,7 +167,7 @@ export default class BaseService {
return this
}

onFail(errors: Record<string, any>) {
onFail(errors: SimpleObject<any>) {
this.errors.fill(errors)
validator.fill(errors)
}
Expand Down
13 changes: 7 additions & 6 deletions src/core/Validator.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { SimpleObject } from '../types'
import { cloneDeep, get, has, omit } from 'lodash'
import { is, toCamelCase, toSnakeCase } from '../util'

class Validator {
public successful: boolean
public processing: boolean

constructor(public errors: Record<string, any> = {}) {
constructor(public errors: SimpleObject<any> = {}) {
this.processing = false
this.successful = false
}
Expand All @@ -24,7 +25,7 @@ class Validator {
return is(Object.keys(this.errors), fields)
}

first(field: string | string[]): string | Record<string, any> | undefined {
first(field: string | string[]): string | undefined {
if (Array.isArray(field)) {
const fields = this.fields(field)
let fd = ''
Expand All @@ -42,7 +43,7 @@ class Validator {
}
}

firstBy(obj: Record<string, any>, field?: string) {
firstBy(obj: SimpleObject<any>, field?: string) {
let value: string
if (!field) {
value = obj[Object.keys(obj)[0]]
Expand All @@ -64,7 +65,7 @@ class Validator {
any(field: string[] = [], returnObject?: boolean) {
const fields = this.fields(field)
if (returnObject) {
const errors: Record<string, any> = {}
const errors: SimpleObject<any> = {}
if (!fields.length) return {}
for (const f of fields) {
const val = this.get(f)
Expand All @@ -74,7 +75,7 @@ class Validator {
return errors
}
if (!fields.length) return Object.keys(this.errors).length > 0
const errors: Record<string, any> = {}
const errors: SimpleObject<any> = {}
fields.forEach((key: string) => (errors[key] = this.get(key)))
return Object.keys(errors).length > 0
}
Expand All @@ -91,7 +92,7 @@ class Validator {
return Object.keys(this.errors).length
}

fill(errors: Record<string, any>) {
fill(errors: SimpleObject<any>) {
this.errors = errors
}

Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type SimpleObject<V> = Record<string, V>