forked from VoyagerScientific/react-jsonschema-form-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
36 lines (34 loc) · 1.17 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { createServer } from "miragejs";
import _ from "lodash";
const toDataURI = (file) => {
const reader = new FileReader();
return new Promise((resolve, reject) => {
reader.onload = (e) => resolve(e.target.result);
reader.readAsDataURL(file);
})
}
export default () => {
let server = createServer({
routes() {
this.post("/api/users", async (server, request) => {
const attachments = request.requestBody.getAll("attachments");
if (_.isArray(attachments)) {
const promises = _.map(attachments, async (attachment) => {
const url = await toDataURI(attachment);
const filename = attachment.name;
return { url, filename };
});
const responseData = await Promise.all(promises);
return responseData;
}
const dataURI = await toDataURI(attachments);
return { filename: attachments.name, url: dataURI };
})
this.passthrough()
this.passthrough("https://5fe385bb8bf8af001766e7a1.mockapi.io/**")
this.passthrough("https://api.airtable.com/**")
this.passthrough("https://cors-anywhere.herokuapp.com/**")
}
})
return server;
}