Skip to content

Commit d42c3b4

Browse files
committed
feat(update): 🚀 accepted mssage as string value
1 parent c9b30ad commit d42c3b4

File tree

5 files changed

+90
-40
lines changed

5 files changed

+90
-40
lines changed

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.11",
3+
"version": "0.0.12",
44
"description": "Elegant and simple way to build requests for REST API",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/__tests__/base-proxy.test.ts

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import MockAdapter from 'axios-mock-adapter'
44
import PostProxy from '../core/PostPorxy'
55
import type { ValidatorType } from '../core/Validator'
66
import Validator from '../core/Validator'
7+
import BaseTransformer from '../core/BaseTransformer'
8+
import PaginationTransformer from '../core/PaginationTransformer'
79

810
let proxy: PostProxy
911
let mockAdapter
@@ -17,15 +19,47 @@ describe('BaseProxy', () => {
1719
proxy = new PostProxy()
1820
mockAdapter = new MockAdapter(axios)
1921
})
22+
23+
it('it should fetch items with pagination', async () => {
24+
const items = {
25+
data: [{ first_name: 'Chantouch', last_name: 'Sek' }],
26+
pagination: { count: 1, page: 1, perPage: 20 },
27+
}
28+
mockAdapter.onGet('/posts').reply(200, items)
29+
const { data, pagination = {} } = await proxy.removeParameters().all()
30+
const all = {
31+
items: BaseTransformer.fetchCollection(data),
32+
pagination: PaginationTransformer.fetch(pagination),
33+
}
34+
expect(all).toHaveProperty('pagination')
35+
expect(data).toEqual(all.items)
36+
expect(all.pagination.page).toEqual(1)
37+
})
38+
39+
it('it should fetch items with meta that has pagination', async () => {
40+
const items = {
41+
data: [{ first_name: 'Chantouch', last_name: 'Sek' }],
42+
meta: {
43+
pagination: { count: 1, current_page: 1, perPage: 20 },
44+
include: [],
45+
},
46+
}
47+
mockAdapter.onGet('/posts').reply(200, items)
48+
const { data, meta = {} } = await proxy.removeParameters().all()
49+
const all = {
50+
items: BaseTransformer.fetchCollection(data),
51+
pagination: PaginationTransformer.fetch(meta),
52+
}
53+
expect(meta).toHaveProperty('pagination')
54+
expect(data.length).toEqual(1)
55+
expect(all.pagination.page).toEqual(1)
56+
})
57+
2058
it('will fetch all records', async () => {
2159
const items = [{ first_name: 'Chantouch', last_name: 'Sek' }]
2260
mockAdapter.onGet('/posts').reply(200, { data: items })
23-
try {
24-
const { data } = await proxy.removeParameters().all()
25-
expect(data).toEqual(items)
26-
} catch (e) {
27-
console.log('all:', e)
28-
}
61+
const { data } = await proxy.removeParameters().all()
62+
expect(data).toEqual(items)
2963
})
3064

3165
it('will fetch all records with query params', async () => {

src/__tests__/validator.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,18 @@ describe('Validator', () => {
99
afterEach(() => {
1010
validator.flush()
1111
})
12-
test('Add error', () => {
12+
test('Add an error', () => {
1313
validator.add('name', 'The name field is required.')
1414
expect(validator.any()).toBeTruthy()
1515
})
16+
test('Add an error message as string', () => {
17+
const errors = {
18+
name: 'The name field is required.',
19+
}
20+
validator.fill(errors)
21+
expect(validator.any()).toBeTruthy()
22+
expect(validator.first('name')).toEqual('The name field is required.')
23+
})
1624
test('Check if error has "name" key.', () => {
1725
validator.add('name', 'The name field is required.')
1826
expect(validator.has('name')).toBeTruthy()

src/core/PaginationTransformer.ts

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,32 @@
11
import BaseTransformer from './BaseTransformer'
2+
import snakecaseKeys from 'snakecase-keys'
3+
4+
export interface PaginationOptions {
5+
per_page?: string | number
6+
total_pages?: string | number
7+
current_page?: string | number
8+
total?: string | number
9+
links?: string | number
10+
count?: string | number
11+
page?: string | number
12+
items_per_page?: string | number
13+
page_count?: string | number
14+
items_length?: string | number
15+
page_start?: string | number
16+
page_stop?: string | number
17+
include?: string | string[]
18+
}
19+
20+
export interface MetaOptions {
21+
pagination?: PaginationOptions
22+
include?: string | string[]
23+
}
224

325
class PaginationTransformer extends BaseTransformer {
4-
static fetch(meta: MetaOptions) {
26+
static fetch(meta: MetaOptions | any) {
27+
if (!meta.hasOwnProperty('pagination')) {
28+
return snakecaseKeys(meta, { deep: true })
29+
}
530
const { pagination = {}, include } = meta
631
return {
732
perPage: pagination.per_page,
@@ -22,24 +47,3 @@ class PaginationTransformer extends BaseTransformer {
2247
}
2348

2449
export default PaginationTransformer
25-
26-
export interface PaginationOptions {
27-
per_page?: string | number
28-
total_pages?: string | number
29-
current_page?: string | number
30-
total?: string | number
31-
links?: string | number
32-
count?: string | number
33-
page?: string | number
34-
items_per_page?: string | number
35-
page_count?: string | number
36-
items_length?: string | number
37-
page_start?: string | number
38-
page_stop?: string | number
39-
include?: string | string[]
40-
}
41-
42-
export interface MetaOptions {
43-
pagination?: PaginationOptions
44-
include?: string | string[]
45-
}

src/core/Validator.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import { is, isArray } from '../util/objects'
2-
3-
export interface ErrorOptions {
4-
[key: string]: string | string[]
5-
}
1+
import { is } from '../util/objects'
62

73
class Validator {
84
public errors: any
@@ -25,7 +21,7 @@ class Validator {
2521
}
2622

2723
has(field: any | any[]): boolean {
28-
if (isArray(field)) {
24+
if (field instanceof Array) {
2925
return is(Object.keys(this.errors), field)
3026
}
3127
let hasError = this.errors.hasOwnProperty(field)
@@ -39,7 +35,7 @@ class Validator {
3935
}
4036

4137
first(field: any | any[]): string {
42-
if (isArray(field)) {
38+
if (field instanceof Array) {
4339
for (let i = 0; i < field.length; i++) {
4440
if (!this.errors.hasOwnProperty(field[i])) {
4541
continue
@@ -83,20 +79,28 @@ class Validator {
8379
return this.errors
8480
}
8581

86-
fill(errors: ErrorOptions): void {
82+
fill(errors: any): void {
83+
for (const error in errors) {
84+
if (!errors.hasOwnProperty(error)) {
85+
continue
86+
}
87+
if (!(errors[error] instanceof Array)) {
88+
errors[error] = [errors[error]]
89+
}
90+
}
8791
this.errors = errors
8892
}
8993

9094
flush(): void {
9195
this.errors = {}
9296
}
9397

94-
clear(attribute?: any | any[]): void {
98+
clear(attribute?: string | string[]): void {
9599
if (!attribute) {
96100
return this.flush()
97101
}
98102
const errors = Object.assign({}, this.errors)
99-
if (isArray(attribute)) {
103+
if (attribute instanceof Array) {
100104
attribute.map((field: string) => {
101105
Object.keys(errors)
102106
.filter(

0 commit comments

Comments
 (0)