Skip to content

Commit fc716dd

Browse files
Merge pull request #150 from robsontenorio/dev
Release v1.8.1
2 parents f3d4cbf + 80a7636 commit fc716dd

File tree

5 files changed

+71
-25
lines changed

5 files changed

+71
-25
lines changed

docs/content/en/api/model-options.md

+18
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,24 @@ parameterNames() {
6565
}
6666
```
6767

68+
### `formData`
69+
- Returns: `object`
70+
71+
This method can be overridden in the model to configure `object-to-formdata`.
72+
73+
See [object-to-formdata](https://github.com/therealparmesh/object-to-formdata#usage)
74+
75+
```js
76+
formData() {
77+
return {
78+
indices: false,
79+
nullsAsUndefineds: false,
80+
booleansAsIntegers: false,
81+
allowEmptyArrays: false,
82+
}
83+
}
84+
```
85+
6886
#### `include`
6987
- Default: `include`
7088
- Returns: `string`

docs/content/en/configuration.md

+44
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,47 @@ export default class Model extends BaseModel {
193193
}
194194
}
195195
```
196+
197+
## Configuring FormData
198+
199+
See the [API reference](/api/model-options#formdata) and [object-to-formdata](https://github.com/therealparmesh/object-to-formdata#usage)
200+
201+
When uploading files, the data of our request will be automatically converted to `FormData` using `object-to-formdata`.
202+
203+
If needed, we can easily configure the options by overriding the `formData` method.
204+
205+
We can globally configure this in the [Base Model](/configuration#creating-a-base-model):
206+
207+
```js{}[~/models/Model.js]
208+
import { Model as BaseModel } from 'vue-api-query'
209+
210+
export default class Model extends BaseModel {
211+
212+
// Define a base url for a REST API
213+
baseURL() {
214+
return 'http://my-api.com'
215+
}
216+
217+
// Implement a default request method
218+
request(config) {
219+
return this.$http.request(config)
220+
}
221+
222+
// Override default query parameter names
223+
parameterNames() {
224+
const defaultParams = super.parameterNames()
225+
const customParams = {
226+
include: 'include_custom'
227+
}
228+
229+
return { ...defaultParams, ...customParams }
230+
}
231+
232+
// Configure object-to-formadata
233+
formData() {
234+
return {
235+
indices: true
236+
}
237+
}
238+
}
239+
```

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"dependencies": {
6464
"dotprop": "^1.2.0",
6565
"dset": "^2.0.1",
66-
"object-to-formdata": "^4.1.0"
66+
"object-to-formdata": "^4.1.0",
67+
"qs": "^6.9.4"
6768
}
6869
}

src/Model.js

+2-24
Original file line numberDiff line numberDiff line change
@@ -42,30 +42,8 @@ export default class Model extends StaticModel {
4242
return this
4343
}
4444

45-
formData(options = {}) {
46-
const defaultOptions = {
47-
/**
48-
* Include array indices in FormData keys
49-
*/
50-
indices: false,
51-
52-
/**
53-
* Treat null values like undefined values and ignore them
54-
*/
55-
nullsAsUndefineds: false,
56-
57-
/**
58-
* Convert true or false to 1 or 0 respectively
59-
*/
60-
booleansAsIntegers: false,
61-
62-
/**
63-
* Store arrays even if they're empty
64-
*/
65-
allowEmptyArrays: false,
66-
}
67-
68-
return { ...defaultOptions, ...options }
45+
formData() {
46+
return {}
6947
}
7048

7149
resource() {

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -6284,6 +6284,11 @@ qrcode-terminal@^0.12.0:
62846284
resolved "https://registry.yarnpkg.com/qrcode-terminal/-/qrcode-terminal-0.12.0.tgz#bb5b699ef7f9f0505092a3748be4464fe71b5819"
62856285
integrity sha512-EXtzRZmC+YGmGlDFbXKxQiMZNwCLEO6BANKXG4iCtSIM0yqc/pappSx3RIKr4r0uh5JsBckOXeKrB3Iz7mdQpQ==
62866286

6287+
qs@^6.9.4:
6288+
version "6.9.4"
6289+
resolved "https://registry.yarnpkg.com/qs/-/qs-6.9.4.tgz#9090b290d1f91728d3c22e54843ca44aea5ab687"
6290+
integrity sha512-A1kFqHekCTM7cz0udomYUoYNWjBebHm/5wzU/XqrBRBNWectVH0QIiN+NEcZ0Dte5hvzHwbr8+XQmguPhJ6WdQ==
6291+
62876292
qs@~6.5.2:
62886293
version "6.5.2"
62896294
resolved "https://registry.yarnpkg.com/qs/-/qs-6.5.2.tgz#cb3ae806e8740444584ef154ce8ee98d403f3e36"

0 commit comments

Comments
 (0)