Skip to content

Commit a69a5ba

Browse files
committed
refactor!: renamed all proxy to service
1 parent 1e486c3 commit a69a5ba

File tree

3 files changed

+58
-55
lines changed

3 files changed

+58
-55
lines changed

README.md

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -127,18 +127,18 @@ It will create `$errors` object inside components.
127127

128128
1.Create **proxies** folder or your prefer folder name for this
129129

130-
`~/proxies/NewsProxy.js`
130+
`~/proxies/NewsService.js`
131131

132132
```js
133133
import { BaseService } from 'vue-axios-http'
134134

135-
class NewsProxy extends BaseService {
135+
class NewsService extends BaseService {
136136
constructor(parameters = {}) {
137137
super('news', parameters)
138138
}
139139
}
140140

141-
export default NewsProxy
141+
export default NewsService
142142
```
143143

144144
2.Store
@@ -157,19 +157,19 @@ actions.js
157157

158158
```js
159159
import { ALL } from './mutation-types'
160-
import { NewsProxy } from '~/proxies'
160+
import { NewsService } from '~/proxies'
161161
import { BaseTransformer, PaginationTransformer } from 'vue-axios-http'
162162
import { pagination, notify } from '~/utils'
163163

164-
const proxy = new NewsProxy()
164+
const service = new NewsService()
165165

166166
const all = async ({ commit, dispatch }, payload = {}) => {
167167
const { fn } = payload
168168
if (typeof fn === 'function') {
169-
await fn(proxy)
169+
await fn(service)
170170
}
171171
try {
172-
const { data, meta } = await proxy.all()
172+
const { data, meta } = await service.all()
173173
const all = {
174174
items: BaseTransformer.fetchCollection(data),
175175
pagination: PaginationTransformer.fetch(meta),
@@ -247,8 +247,8 @@ export default {
247247
async asyncData({ app, store }) {
248248
const { id = null } = app.$auth.user
249249
await store.dispatch('news/all', {
250-
fn: (proxy) => {
251-
proxy
250+
fn: (service) => {
251+
service
252252
.setParameters({
253253
userId: id,
254254
include: ['categories'],
@@ -267,8 +267,8 @@ export default {
267267
mounted() {
268268
const { id = null } = this.$auth.user
269269
this.$store.dispatch('news/all', {
270-
fn: (proxy) => {
271-
proxy
270+
fn: (service) => {
271+
service
272272
.setParameters({
273273
userId: id,
274274
include: ['categories'],
@@ -282,7 +282,7 @@ export default {
282282

283283
You can set or remove any parameters you like.
284284

285-
## Proxy's methods are available
285+
## Service's methods are available
286286

287287
| Method | Description |
288288
| ----------------------------------------------- | --------------------------- |
@@ -301,7 +301,7 @@ Set parameters with key/value.
301301
#### Example
302302

303303
```js
304-
const proxy = new ExampleProxy()
304+
const service = new ExampleService()
305305
const parameters = {
306306
search: {
307307
first_name: 'Sek',
@@ -317,7 +317,7 @@ const parameters = {
317317
},
318318
category_id: 6,
319319
}
320-
const { data } = proxy.setParameters(parameters).all()
320+
const { data } = service.setParameters(parameters).all()
321321
this.data = data
322322
```
323323

@@ -334,8 +334,8 @@ if setParameter that value is empty or null it will remove that param for query
334334
#### Example 1
335335

336336
```js
337-
const proxy = new ExampleProxy()
338-
const { data } = await proxy.setParameter('page', 1).all()
337+
const service = new ExampleService()
338+
const { data } = await service.setParameter('page', 1).all()
339339
this.data = data
340340
```
341341

@@ -350,9 +350,9 @@ Expected will be:
350350
#### Example 2
351351

352352
```js
353-
const proxy = new ExampleProxy()
353+
const service = new ExampleService()
354354
const queryString = 'limit=10&page=1&search[name]=hello'
355-
const { data } = await proxy.setParameter(queryString).all()
355+
const { data } = await service.setParameter(queryString).all()
356356
this.data = data
357357
```
358358

@@ -370,20 +370,20 @@ Expected will be:
370370

371371
Be sure to use only once in `mounted()` or `asyncData()` and `asyncData()` is only available in `NuxtJs`
372372

373-
## Use proxy in components
373+
## Use service in components
374374

375375
- news/\_id.vue pages
376376

377377
```js
378-
import { NewsProxy } from '~/proxies'
378+
import { NewsService } from '~/proxies'
379379

380-
const proxy = new NewsProxy()
380+
const service = new NewsService()
381381

382382
export default {
383383
methods: {
384384
async fetchNews(id) {
385385
try {
386-
const { data } = await proxy.find(id)
386+
const { data } = await service.find(id)
387387
this.detail = data
388388
} catch (e) {
389389
console.log(e)

src/__tests__/base-service.test.ts

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { ValidatorType } from '../core/Validator'
66
import Validator from '../core/Validator'
77
import { merge } from '../util'
88

9-
let proxy: PostService
9+
let service: PostService
1010
let mockAdapter: MockAdapter
1111
let validator: ValidatorType
1212

@@ -16,15 +16,15 @@ describe('BaseService', () => {
1616
const axios = Axios.create({ baseURL: 'https://mock-api.test' })
1717
BaseService.$http = axios
1818
BaseService.$errorProperty = 'message'
19-
proxy = new PostService()
19+
service = new PostService()
2020
mockAdapter = new MockAdapter(axios)
2121
mockAdapter.reset()
2222
})
2323

2424
it('check if http was installed', async () => {
2525
BaseService.$http = undefined as any
2626
try {
27-
await proxy.all()
27+
await service.all()
2828
} catch (e) {
2929
const { message } = e as any
3030
expect(message).toBe('Vue Axios Http, No http library provided.')
@@ -37,7 +37,7 @@ describe('BaseService', () => {
3737
pagination: { count: 1, page: 1, perPage: 20 },
3838
}
3939
mockAdapter.onGet('/posts').reply(200, items)
40-
const { data, pagination } = await proxy.removeParameters().all()
40+
const { data, pagination } = await service.removeParameters().all()
4141
const all = {
4242
pagination,
4343
items: data,
@@ -56,7 +56,7 @@ describe('BaseService', () => {
5656
},
5757
}
5858
mockAdapter.onGet('/posts').reply(200, items)
59-
const { data, meta } = await proxy.removeParameters().all()
59+
const { data, meta } = await service.removeParameters().all()
6060
const item = {
6161
items: data,
6262
pagination: meta.pagination,
@@ -69,14 +69,14 @@ describe('BaseService', () => {
6969
it('will fetch all records', async () => {
7070
const items = [{ first_name: 'Chantouch', last_name: 'Sek' }]
7171
mockAdapter.onGet('/posts').reply(200, { data: items })
72-
const { data } = await proxy.removeParameters().all()
72+
const { data } = await service.removeParameters().all()
7373
expect(data).toEqual(items)
7474
})
7575

7676
it('Check network server return 500', async () => {
7777
mockAdapter.onGet('/posts').networkError()
7878
try {
79-
await proxy.all()
79+
await service.all()
8080
} catch (e) {
8181
const { message } = e as any
8282
expect(message).toBe('Network Error')
@@ -89,7 +89,7 @@ describe('BaseService', () => {
8989
{ first_name: 'Chantouch', last_name: 'Sek', id: 2 },
9090
]
9191
mockAdapter.onGet('/posts?id=1&first_name=Dara').reply(200, { data: items })
92-
const { data } = await proxy
92+
const { data } = await service
9393
.setParameter('id', 1)
9494
.setParameters({ first_name: 'Dara' })
9595
.all()
@@ -113,11 +113,11 @@ describe('BaseService', () => {
113113
mockAdapter
114114
.onGet('/posts?id=1&last_name=Hok&search[name]=hello&first_name=Dara')
115115
.reply(200, { data: items })
116-
const { data } = await proxy
116+
const { data } = await service
117117
.setParameter('id=1&last_name=Hok&search[name]=hello')
118118
.setParameters({ first_name: 'Dara' })
119119
.all()
120-
expect(proxy.parameters).toEqual({
120+
expect(service.parameters).toEqual({
121121
id: '1',
122122
first_name: 'Dara',
123123
last_name: 'Hok',
@@ -134,7 +134,7 @@ describe('BaseService', () => {
134134
{ first_name: 'Chantouch', last_name: 'Sek', id: 2 },
135135
]
136136
mockAdapter.onGet('/posts?id=1').reply(200, { data: items })
137-
const { data } = await proxy
137+
const { data } = await service
138138
.setParameter('id', 1)
139139
.setParameters({ first_name: 'Dara', last_name: 'Sek' })
140140
.removeParameter('first_name')
@@ -156,25 +156,28 @@ describe('BaseService', () => {
156156
first_name: 'Dara',
157157
last_name: 'Hok',
158158
}
159-
const { data } = await proxy
159+
const { data } = await service
160160
.setParameters(params)
161161
.removeParameters(['last_name'])
162162
.all()
163163
expect(data).toEqual(items)
164-
expect(proxy.parameters).toEqual({ search: { id: 1 }, first_name: 'Dara' })
164+
expect(service.parameters).toEqual({
165+
search: { id: 1 },
166+
first_name: 'Dara',
167+
})
165168
})
166169

167170
it('it should find an item by id', async () => {
168171
const item = { first_name: 'Chantouch', last_name: 'Sek', id: 1 }
169172
mockAdapter.onGet('posts/1').reply(200, { data: item })
170-
const { data } = await proxy.find(1)
173+
const { data } = await service.find(1)
171174
expect(data).toEqual(item)
172175
})
173176

174177
it('it should create a item by post', async () => {
175178
const item = { first_name: 'Chantouch', last_name: 'Sek', id: 1 }
176179
mockAdapter.onPost('/posts').reply(201, { data: item })
177-
const { data } = await proxy.post(item)
180+
const { data } = await service.post(item)
178181
expect(data).toEqual(item)
179182
})
180183

@@ -209,7 +212,7 @@ describe('BaseService', () => {
209212
return [200, {}]
210213
})
211214

212-
await proxy.store(form)
215+
await service.store(form)
213216
})
214217

215218
it('transforms the data to a FormData object if there is a File with post', async () => {
@@ -241,7 +244,7 @@ describe('BaseService', () => {
241244
return [200, {}]
242245
})
243246

244-
await proxy.put(form.id, form)
247+
await service.put(form.id, form)
245248
})
246249

247250
it('transforms the boolean values in FormData object to "1" or "0"', async () => {
@@ -272,7 +275,7 @@ describe('BaseService', () => {
272275
return [200, {}]
273276
})
274277

275-
await proxy.post(form)
278+
await service.post(form)
276279
})
277280

278281
it('it should throw errors message when data is not valid', async () => {
@@ -281,7 +284,7 @@ describe('BaseService', () => {
281284
message: { first_name: 'The first name field is required' },
282285
})
283286
try {
284-
await proxy.post(item)
287+
await service.post(item)
285288
} catch (e) {
286289
const { message } = e as any
287290
expect(message).toBe('Request failed with status code 422')
@@ -292,65 +295,65 @@ describe('BaseService', () => {
292295
it('it should store the item', async () => {
293296
const item = { first_name: 'Chantouch', last_name: 'Sek', id: 1 }
294297
mockAdapter.onPost('/posts').reply(201, { data: item })
295-
const { data } = await proxy.store(item)
298+
const { data } = await service.store(item)
296299
expect(data).toEqual(item)
297300
})
298301

299302
it('it should be able to put item', async () => {
300303
const item = { first_name: 'Chantouch', last_name: 'Sek', id: 1 }
301304
mockAdapter.onPut('posts/1').reply(200, { data: item })
302-
const { data } = await proxy.put(item.id, item)
305+
const { data } = await service.put(item.id, item)
303306
expect(data).toEqual(item)
304307
})
305308

306309
it('it should be able to put item without id parameter', async () => {
307310
const item = { first_name: 'Chantouch', last_name: 'Sek', id: 1 }
308311
mockAdapter.onPut('posts').reply(200, { data: item })
309-
const { data } = await proxy.put(item)
312+
const { data } = await service.put(item)
310313
expect(data).toEqual(item)
311314
})
312315

313316
it('it should be able to patch item', async () => {
314317
const item = { first_name: 'Chantouch', last_name: 'Sek', id: 1 }
315318
mockAdapter.onPatch('posts/1').reply(200, { data: item })
316-
const { data } = await proxy.patch(item.id, item)
319+
const { data } = await service.patch(item.id, item)
317320
expect(data).toEqual(item)
318321
})
319322

320323
it('it should be able to patch item without id', async () => {
321324
const item = { first_name: 'Chantouch', last_name: 'Sek', id: 1 }
322325
mockAdapter.onPatch('posts').reply(200, { data: item })
323-
const { data } = await proxy.patch(item)
326+
const { data } = await service.patch(item)
324327
expect(data).toEqual(item)
325328
})
326329

327330
it('it should be able to update an item', async () => {
328331
const item = { first_name: 'Chantouch', last_name: 'Sek', id: 1 }
329332
mockAdapter.onPatch('posts/1').reply(200, { data: item })
330-
const { data } = await proxy.update(item.id, item)
333+
const { data } = await service.update(item.id, item)
331334
expect(data).toEqual(item)
332335
})
333336

334337
it('it should be able to delete an item', async () => {
335338
const item = { first_name: 'Chantouch', last_name: 'Sek', id: 1 }
336339
mockAdapter.onDelete('posts/1').reply(200, { data: item })
337-
const { data } = await proxy.delete(item.id)
340+
const { data } = await service.delete(item.id)
338341
expect(data).toEqual(item)
339342
})
340343

341344
it('it should be able to remove an item', async () => {
342345
const item = { first_name: 'Chantouch', last_name: 'Sek', id: 1 }
343346
mockAdapter.onDelete('posts/1').reply(200, { data: item })
344-
const { data } = await proxy.remove(item.id)
347+
const { data } = await service.remove(item.id)
345348
expect(data).toEqual(item)
346349
})
347350

348351
it('can accept a custom http instance in options', () => {
349352
BaseService.$http = Axios.create({ baseURL: 'https://another-example.com' })
350-
expect(proxy.$http.defaults.baseURL).toBe('https://another-example.com')
353+
expect(service.$http.defaults.baseURL).toBe('https://another-example.com')
351354

352355
BaseService.$http = Axios.create()
353-
expect(proxy.$http.defaults.baseURL).toBe(undefined)
356+
expect(service.$http.defaults.baseURL).toBe(undefined)
354357
})
355358
})
356359

0 commit comments

Comments
 (0)