Skip to content

Commit 4fb5adb

Browse files
committed
chore(methods)!: remvoe redundant methods from base proxy class
1 parent 129b46d commit 4fb5adb

File tree

3 files changed

+17
-134
lines changed

3 files changed

+17
-134
lines changed

src/__tests__/base-proxy.test.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('BaseProxy', () => {
2424
it('check if http was installed', async () => {
2525
BaseProxy.$http = undefined as any
2626
try {
27-
await proxy.getMany()
27+
await proxy.all()
2828
} catch (e) {
2929
const { message } = e as any
3030
expect(message).toBe('Vue Axios Http, No http library provided.')
@@ -167,7 +167,7 @@ describe('BaseProxy', () => {
167167
it('it should find an item by id', async () => {
168168
const item = { first_name: 'Chantouch', last_name: 'Sek', id: 1 }
169169
mockAdapter.onGet('posts/1').reply(200, { data: item })
170-
const { data } = await proxy.getOne(1)
170+
const { data } = await proxy.find(1)
171171
expect(data).toEqual(item)
172172
})
173173

@@ -178,15 +178,6 @@ describe('BaseProxy', () => {
178178
expect(data).toEqual(item)
179179
})
180180

181-
it('it should create items with bulk', async () => {
182-
const item = {
183-
bulk: [{ first_name: 'Chantouch', last_name: 'Sek', id: 1 }],
184-
}
185-
mockAdapter.onPost('/posts/bulk').reply(201, item)
186-
const data = await proxy.createMany(item)
187-
expect(data).toEqual(item)
188-
})
189-
190181
it('transforms the data to a FormData object if there is a File', async () => {
191182
const file = new File(['hello world!'], 'myfile')
192183
const form: any = { field1: {}, field2: {}, files: [] }
@@ -308,7 +299,7 @@ describe('BaseProxy', () => {
308299
it('it should be able to put item', async () => {
309300
const item = { first_name: 'Chantouch', last_name: 'Sek', id: 1 }
310301
mockAdapter.onPut('posts/1').reply(200, { data: item })
311-
const { data } = await proxy.replace(item.id, item)
302+
const { data } = await proxy.put(item.id, item)
312303
expect(data).toEqual(item)
313304
})
314305

src/__tests__/post-proxy.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('PostProxy', () => {
2424
},
2525
}
2626
mockAdapter.onGet('/posts/1/tags').reply(200, items)
27-
const { data, meta } = await proxy.tags(1)
27+
const { data, meta } = await proxy.removeParameters([]).tags(1)
2828
const item = {
2929
items: data,
3030
pagination: meta.pagination,

src/core/BaseProxy.ts

Lines changed: 13 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -43,103 +43,34 @@ class BaseProxy {
4343
return BaseProxy.$parsedQs
4444
}
4545

46-
/**
47-
* Get all or by pagination
48-
*/
4946
all<T>() {
5047
return this.submit<T>('get')
5148
}
5249

53-
/**
54-
* Alternative of all method
55-
*/
56-
getMany<T>() {
57-
return this.all<T>()
58-
}
59-
60-
/**
61-
* Find a record by id
62-
* @param {string|number} id
63-
*/
6450
find<T>(id: number | string) {
6551
return this.submit<T>('get', id)
6652
}
6753

68-
/**
69-
* Alternative of find method
70-
* @param {string | number} id
71-
*/
72-
getOne<T>(id: number | string) {
73-
return this.find<T>(id)
74-
}
75-
76-
/**
77-
* Create record
78-
* @param {Object|string} payload
79-
* @param {AxiosRequestConfig} config
80-
*/
8154
post<T>(payload: any, config?: AxiosRequestConfig) {
8255
return this.submit<T>('post', '', payload, config)
8356
}
8457

85-
/**
86-
* Alternative of post method
87-
* @param payload
88-
* @param {AxiosRequestConfig} config
89-
*/
9058
store<T>(payload: any, config?: AxiosRequestConfig) {
9159
return this.post<T>(payload, config)
9260
}
9361

94-
/**
95-
* Alternative of store method
96-
* @param payload
97-
* @param {AxiosRequestConfig} config
98-
*/
9962
create<T>(payload: any, config?: AxiosRequestConfig) {
10063
return this.store<T>(payload, config)
10164
}
10265

103-
/**
104-
* Create many items
105-
* @param {Object} payload
106-
*/
107-
createMany<T>(payload: T) {
108-
return this.submit<T>('post', 'bulk', payload)
109-
}
110-
111-
/**
112-
* Update record by id using PUT method
113-
* @param {string|number} id
114-
* @param {Object|string} payload
115-
*/
11666
put<T>(id: string | number, payload: any) {
11767
return this.submit<T>('put', `/${id}`, payload)
11868
}
11969

120-
/**
121-
* Update record without ID parameter by using PUT method
122-
* @param {Object|string} payload
123-
*/
12470
putWithoutId<T>(payload: any) {
12571
return this.submit<T>('put', '', payload)
12672
}
12773

128-
/**
129-
* Alternative of put method
130-
* @param {string|number} id
131-
* @param {Object|string} payload
132-
*/
133-
replace<T>(id: string | number, payload: any) {
134-
return this.put<T>(id, payload)
135-
}
136-
137-
/**
138-
* This method helpful for laravel developer
139-
* @param {string|number} id
140-
* @param {Object} payload
141-
* @param config
142-
*/
14374
putWithFile<T>(
14475
id: string | number,
14576
payload: any,
@@ -149,47 +80,35 @@ class BaseProxy {
14980
return this.submit<T>('post', `/${id}`, payload, config)
15081
}
15182

152-
/**
153-
* Update record by id
154-
* @param id
155-
* @param payload
156-
*/
15783
patch<T>(id: string | number, payload: any) {
15884
return this.submit<T>('patch', `/${id}`, payload)
15985
}
16086

161-
/**
162-
* Alternative of path method
163-
* @param {string|number} id
164-
* @param {Object|string} payload
165-
*/
16687
update<T>(id: string | number, payload: any) {
16788
return this.patch<T>(id, payload)
16889
}
16990

170-
/**
171-
* Delete record by id
172-
* @param {string|number} id
173-
*/
17491
delete<T>(id: string | number) {
17592
return this.submit<T>('delete', `/${id}`)
17693
}
17794

178-
/**
179-
* Alternative of delete method
180-
* @param {string|number} id
181-
*/
18295
remove<T>(id: string | number) {
18396
return this.delete<T>(id)
18497
}
18598

186-
/**
187-
* Main endpoint method to handle requests
188-
* @param requestType
189-
* @param {string} parameter
190-
* @param {Object|string} form
191-
* @param {AxiosRequestConfig} config
192-
*/
99+
submit<T = any>(requestType: Method): Promise<T>
100+
submit<T = any>(requestType: Method, parameter?: string | number): Promise<T>
101+
submit<T = any>(
102+
requestType: Method,
103+
parameter?: string | number,
104+
form?: T,
105+
): Promise<T>
106+
submit<T = any>(
107+
requestType: Method,
108+
parameter?: string | number,
109+
form?: T,
110+
config?: AxiosRequestConfig,
111+
): Promise<T>
193112
submit<T = any>(
194113
requestType: Method,
195114
parameter?: string | number,
@@ -260,22 +179,13 @@ class BaseProxy {
260179
return requestType
261180
}
262181

263-
/**
264-
* Set parameters by keys
265-
* @param {Object} parameters
266-
*/
267182
setParameters(parameters: Record<string, any>): this {
268183
Object.keys(parameters).forEach((key) => {
269184
this.parameters[key] = parameters[key]
270185
})
271186
return this
272187
}
273188

274-
/**
275-
* Set parameters by key
276-
* @param {string} parameter
277-
* @param {Object|string|Array} value
278-
*/
279189
setParameter(parameter: string, value?: any): this {
280190
if (!value) {
281191
const options: IParseOptions = Object.assign({}, this.$parsedQs, {
@@ -290,10 +200,6 @@ class BaseProxy {
290200
return this
291201
}
292202

293-
/**
294-
* Remove parameters by keys
295-
* @param {Array<Object>>} parameters
296-
*/
297203
removeParameters(parameters = [] as any[]): this {
298204
if (!parameters.length) {
299205
this.parameters = []
@@ -305,27 +211,16 @@ class BaseProxy {
305211
return this
306212
}
307213

308-
/**
309-
* Remove parameters
310-
* @param {string} parameter
311-
*/
312214
removeParameter(parameter: string): this {
313215
delete this.parameters[parameter]
314216
return this
315217
}
316218

317-
/**
318-
* Fill errors on fails passed
319-
* @param {Object} errors
320-
*/
321219
onFail(errors: Record<string, any>) {
322220
this.errors.fill(errors)
323221
validator.fill(errors)
324222
}
325223

326-
/**
327-
* Clean up errors and set processing
328-
*/
329224
beforeSubmit() {
330225
if (!this.$http) {
331226
throw new Error('Vue Axios Http, No http library provided.')
@@ -338,9 +233,6 @@ class BaseProxy {
338233
validator.successful = false
339234
}
340235

341-
/**
342-
* Clean up errors and set success on after request
343-
*/
344236
onSuccess() {
345237
this.errors.processing = false
346238
this.errors.successful = true

0 commit comments

Comments
 (0)