Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const { me } = request.queryParams;
if (id) {
return schema.users.find(id); // users in the second case
}
// typically this check requires a cookie in the backend, but as mirage
// is run in the browser, and we use http-only cookies, we can't simulate
// this cookie behavior. using regular cookies makes us prone to memory
// leak issues. http-only cookies means the server is fully responsible
// for identity management
if (me) {
return schema.users.first();
}
return new Response(401, { some: 'header' }, { errors: ['Unauthorized'] });
});
this.get('/users/:id');
this.post('/document', function(schema, request) {
// requestBody should be a FormData object
const { requestBody } = request;
const success = requestBody.get('instanceId') && requestBody.get('entityName') && requestBody.get('file');
return success ? new Response(200) : new Response(400, {}, { errors: ['Bad Parameters'] });
});
server.post('/session', function () {
// Password sign-in
return new Response(201);
});
this.get(path, () => {
return new Response(200, {'Content-Type': 'text/http'}, body);
});
};
export function registrationDetail(this: HandlerContext, schema: Schema, request: Request) {
const { id } = request.params;
const registration = schema.registrations.find(id);
if (registration.embargoed && !registration.currentUserPermissions.length) {
return new Response(404, {}, {
meta: { version: '2.9' },
errors: [{ detail: 'Not found.' }],
});
}
const { data } = process(schema, request, this, [this.serialize(registration).data]);
return { data: data[0] };
}
export function institutionDelete(this: HandlerContext, schema: Schema, request: Request) {
const { id, parentID } = request.params;
const node = schema.nodes.find(parentID);
const institutionIds: Array = node.affiliatedInstitutionIds;
try {
institutionIds.splice(institutionIds.indexOf(id), 1);
} catch (e) {
return new Response(409, {}, {
meta: { version: '2.9' },
errors: [{ detail: 'Conflict.' }],
});
}
return institutionIds;
}
server.del('/users/:id/', function (db, request) {
db.users.remove(request.params.id);
return new Mirage.Response(204, {}, {});
});
this.post('/password/reset', function(schema) {
let { password, 'password-confirmation': passwordConfirmation, token } = this.normalizedRequestAttrs();
let [matchedUser] = schema.users.where({ token }).models;
if (password === passwordConfirmation) {
return new Mirage.Response(201, {}, {
email: matchedUser.email,
token,
user_id: matchedUser.id
});
} else {
return new Mirage.Response(422, {}, {
errors: [
{
detail: 'Password confirmation passwords do not match',
source: {
pointer: '/data/attributes/password-confirmation'
},
status: '422',
title: 'passwords do not match'
}
],
export function updatePassword(this: HandlerContext) {
const attrs = this.normalizedRequestAttrs('user-password');
const existingPassword = 'oldpassword';
if (attrs.existingPassword !== undefined) {
if (attrs.existingPassword === existingPassword) {
return new Response(204, undefined, undefined);
}
return new Response(409, { 'Content-Type': 'application/vnd.api+json' }, {
errors: [{
status: 409,
detail: 'Old password is invalid.',
}],
});
}
return new Response(400, { 'Content-Type': 'application/vnd.api+json' }, {
errors: [{
status: 400,
detail: 'Password must not be blank.',
}],
});
}
this.post('/gists/:id/forks', () => {
let gist = server.create('gist', { id: faker.random.uuid() });
return new Mirage.Response(200, {}, gist);
});