Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F77444607
utils.js
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Wed, Aug 14, 13:11
Size
2 KB
Mime Type
text/x-java
Expires
Fri, Aug 16, 13:11 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
19870287
Attached To
R12546 Sesame - Configurator
utils.js
View Options
import
axios
from
'axios'
;
import
Papa
from
'papaparse'
;
const
genericFetch
=
(
url
,
state
)
=>
async
()
=>
{
try
{
const
{
data
}
=
await
axios
.
get
(
url
);
state
.
value
=
data
;
}
catch
(
error
)
{
handleHttpError
(
error
);
}
};
const
genericAdd
=
(
url
,
fetch
,
processProps
=
v
=>
v
)
=>
async
(
obj
)
=>
{
try
{
await
axios
.
post
(
url
,
await
processProps
(
obj
));
await
fetch
();
}
catch
(
error
)
{
handleHttpError
(
error
);
}
};
const
genericUpdate
=
(
url
,
fetch
,
processProps
=
v
=>
v
)
=>
async
(
obj
)
=>
{
try
{
await
axios
.
put
(
`
$
{
url
}
/
$
{
obj
.
id
}
`
,
await
processProps
(
obj
));
await
fetch
();
}
catch
(
error
)
{
handleHttpError
(
error
);
}
};
const
genericRemove
=
(
url
,
fetch
)
=>
async
({
id
})
=>
{
try
{
await
axios
.
delete
(
`
$
{
url
}
/
$
{
id
}
`
);
await
fetch
();
}
catch
(
error
)
{
handleHttpError
(
error
);
}
};
const
upload
=
async
(
endpoint
,
fieldName
,
img
)
=>
{
const
url
=
`
$
{
import
.
meta
.
env
.
VITE_API_URL
}
/
uploads
/
$
{
endpoint
}
`
;
const
formData
=
new
FormData
();
formData
.
append
(
fieldName
,
img
);
const
uploadRes
=
await
axios
.
post
(
url
,
formData
,
{
headers
:
{
'Content-Type'
:
'multipart/form-data'
}
});
return
'/uploads/'
+
uploadRes
.
data
.
filename
;
};
const
getUpload
=
async
(
relPath
)
=>
{
try
{
const
{
data
:
blob
}
=
await
axios
.
get
(
window
.
location
.
origin
+
relPath
,
{
responseType
:
'blob'
});
return
new
File
([
blob
],
relPath
.
split
(
'/'
).
pop
(),
{
type
:
blob
.
type
});
}
catch
(
error
)
{
console
.
log
(
error
);
handleHttpError
(
error
);
}
};
// wrap callback API with promise to be able to use async
const
parseCsv
=
(
relPath
)
=>
new
Promise
(
async
(
resolve
,
reject
)
=>
{
Papa
.
parse
(
await
getUpload
(
relPath
),
{
header
:
true
,
complete
:
res
=>
resolve
(
res
),
error
:
err
=>
reject
(
err
)
});
});
const
handleHttpError
=
(
error
)
=>
console
.
error
(
`
response
with
status
$
{
error
.
response
.
status
}
:
$
{
error
.
response
.
data
}
`
);
export
{
upload
,
genericFetch
,
genericAdd
,
genericUpdate
,
genericRemove
,
getUpload
,
parseCsv
};
Event Timeline
Log In to Comment