Skip to content

Commit

Permalink
fix(model): prevent request config from ditching private data (#185)
Browse files Browse the repository at this point in the history
Use `Object.defineProperty` to set `_config`. This way we don't need to ditch private data anymore.
  • Loading branch information
JoaoPedroAS51 committed May 25, 2021
1 parent d6943a0 commit 4b12b18
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 10 deletions.
8 changes: 2 additions & 6 deletions src/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ export default class Model extends StaticModel {
}

config(config) {
this._config = config
Object.defineProperty(this, '_config', { get: () => config })

return this
}

Expand Down Expand Up @@ -313,11 +314,6 @@ export default class Model extends StaticModel {

// Check if config has data
if ('data' in _config) {
// Ditch private data
_config.data = Object.fromEntries(
Object.entries(_config.data).filter(([key]) => !key.startsWith('_'))
)

const _hasFiles = Object.keys(_config.data).some((property) => {
if (Array.isArray(_config.data[property])) {
return _config.data[property].some((value) => value instanceof File)
Expand Down
4 changes: 0 additions & 4 deletions tests/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,6 @@ describe('Model methods', () => {

axiosMock.onAny().reply((config) => {
const _post = post
delete _post._config

expect(config.method).toEqual('patch')
expect(config.data).toEqual(JSON.stringify(_post))
Expand Down Expand Up @@ -452,7 +451,6 @@ describe('Model methods', () => {

axiosMock.onAny().reply((config) => {
const _post = post
delete _post._config

expect(config.method).toEqual('post')
expect(config.data).toEqual(JSON.stringify(_post))
Expand Down Expand Up @@ -572,7 +570,6 @@ describe('Model methods', () => {
axiosMock.onAny().reply((config) => {
let _data
const _post = post
delete _post._config

if (config.headers['Content-Type'] === 'multipart/form-data') {
_data = Object.fromEntries(config.data)
Expand Down Expand Up @@ -634,7 +631,6 @@ describe('Model methods', () => {
axiosMock.onAny().reply((config) => {
let _data
const _post = post
delete _post._config

if (config.headers['Content-Type'] === 'multipart/form-data') {
_data = JSON.stringify(Object.fromEntries(config.data))
Expand Down

0 comments on commit 4b12b18

Please sign in to comment.