Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make status customizable #168

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion packages/mock-addon/src/utils/faker.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,20 @@ export class Faker {

if (matched) {
const { response, status, delay = 0 } = matched;

return new Promise((resolve) => {
setTimeout(() => {
if (typeof response === 'function') {
resolve(new Response(url, status, response(request)));
const data = response(request);

let status = 200;
const customStatusCode = data?.header?.status;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yormy can you please help understand why we are passing status inside the header?

if (customStatusCode) {
status = customStatusCode;
}
delete data.header;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we pass the header?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to allow the status to be customizable inside the function.
And at the same time leaving the rest of the code alone. The status might be important to mock, the rest of the header not so.
This allows to set the status code based on conditionals in the response function. The current status in the main header is not passed in the request, so you cannot determine how to set anything at that level


resolve(new Response(url, status, data));
} else {
resolve(new Response(url, status, response));
}
Expand All @@ -141,6 +151,13 @@ export class Faker {
setTimeout(() => {
if (typeof response === 'function') {
const data = response(new Request(url, { method, body }));
let status = 200;
const customStatusCode = data?.header?.status;
if (customStatusCode) {
status = customStatusCode;
}
delete data.header;

request.respond(
+status,
defaultResponseHeaders,
Expand Down