Skip to content

Commit ae8e3b1

Browse files
committed
chore: 📦 add putWithFile method to support upload file on put method
1 parent 3e0319f commit ae8e3b1

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
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.8",
3+
"version": "0.0.9",
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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,38 @@ describe('BaseProxy', () => {
108108
await proxy.post(form)
109109
})
110110

111+
it('transforms the data to a FormData object if there is a File with post', async () => {
112+
const file = new File(['hello world!'], 'my-file')
113+
const form = { id: 1, user: {}, file }
114+
form.user = {
115+
id: 1,
116+
name: 'testFoo',
117+
villages: ['testBar1', 'testBar2'],
118+
date: new Date(Date.UTC(2012, 3, 13, 2, 12)),
119+
}
120+
mockAdapter.onPost('/posts/' + 1).reply((request) => {
121+
expect(request.data).toBeInstanceOf(FormData)
122+
expect(request.data.get('user[name]')).toBe('testFoo')
123+
expect(request.data.get('user[villages][0]')).toBe('testBar1')
124+
expect(request.data.get('user[villages][1]')).toBe('testBar2')
125+
expect(request.data.get('user[date]')).toBe('2012-04-13T02:12:00.000Z')
126+
expect(request.data.get('file')).toEqual(file)
127+
expect(getFormDataKeys(request.data)).toEqual([
128+
'id',
129+
'user[id]',
130+
'user[name]',
131+
'user[villages][0]',
132+
'user[villages][1]',
133+
'user[date]',
134+
'file',
135+
'_method',
136+
])
137+
return [200, {}]
138+
})
139+
140+
await proxy.putWithFile(form.id, form)
141+
})
142+
111143
it('transforms the boolean values in FormData object to "1" or "0"', async () => {
112144
const file = new File(['hello world!'], 'myfile')
113145
const form = { field1: {}, field2: null }

src/core/BaseProxy.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ class BaseProxy {
4242
return this.submit('put', `/${this.endpoint}/${id}`, payload)
4343
}
4444

45+
putWithFile(id: string | number, payload: any): Promise<any> {
46+
payload['_method'] = 'put'
47+
return this.submit('post', `/${this.endpoint}/${id}`, payload)
48+
}
49+
4550
patch(id: string | number, payload: any): Promise<any> {
4651
return this.submit('patch', `/${this.endpoint}/${id}`, payload)
4752
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Validator from './core/Validator'
55
import './vue'
66

77
export type Errors = ValidatorType
8+
export type { ValidatorType }
89

910
class VueApiQueries {
1011
install(Vue: any) {

0 commit comments

Comments
 (0)