In this section, we are going to create the HTTP PATCH method that will be mocked by the MirageJS server. Follow these steps to create it:
- For the PATCH methods, we need to create a new file called patch.js in the src/server folder.
- For this recipe, we will make a generic patchFrom 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 updates a specific object with the id property that was passed along with the data. Using the key argument, the schema knows which table we are handling:
export const patchFrom = key => (schema, request) => {
const { data } = typeof request.requestBody === 'string'
? JSON.parse(request.requestBody)
: request.requestBody;
return schema.db[key].update(data.id, data);
};
export default {
patchFrom,
};