In this section, we are going to create the HTTP POST method, that will be mocked by the MirageJS server. Follow these steps to create it:
- For the POST methods, we need to create a new file called post.js in the src/server folder.
- For this recipe, we will make a generic postFrom function that receives a key as an argument and returns a function. This returned function will parse the data property of the HTTP request body and returns an internal function of the server schema that inserts the data inside the database. Using the key argument, the schema knows which table we are handling:
export const postFrom = key => (schema, request) => {
const { data } = typeof request.requestBody === 'string'
? JSON.parse(request.requestBody)
: request.requestBody;
return schema.db[key].insert(data);
};
export default {
postFrom,
};