refactor: 리펙토링
This commit is contained in:
@@ -1,9 +1,5 @@
|
||||
import { delay, http, HttpResponse } from "msw";
|
||||
import { delay, http, HttpResponse, type JsonBodyType } from "msw";
|
||||
|
||||
import {
|
||||
failureEnvelope,
|
||||
successEnvelope,
|
||||
} from "../contracts/envelopes.ts";
|
||||
import type { HttpScenarioId } from "../scenarios/catalog.ts";
|
||||
import { assertOperationScenario } from "../scenarios/catalog.ts";
|
||||
|
||||
@@ -36,7 +32,7 @@ const DEFAULT_RESOURCE = Object.freeze({
|
||||
|
||||
async function scenarioResponse(
|
||||
scenario: HttpScenarioId,
|
||||
payload: unknown,
|
||||
payload: JsonBodyType,
|
||||
attempt: number,
|
||||
) {
|
||||
if (scenario === "slow") await delay(50);
|
||||
@@ -56,35 +52,33 @@ async function scenarioResponse(
|
||||
return HttpResponse.json({ data: payload });
|
||||
}
|
||||
if (scenario === "schema-mismatch") {
|
||||
return HttpResponse.json(successEnvelope({ unexpected: true }));
|
||||
return HttpResponse.json({ unexpected: true });
|
||||
}
|
||||
if (
|
||||
scenario === "auth-persistent-401" ||
|
||||
(scenario === "auth-recover-once" && attempt === 1)
|
||||
) {
|
||||
return HttpResponse.json(failureEnvelope("AUTH_REQUIRED"), {
|
||||
return HttpResponse.json(problem(401, "AUTH_REQUIRED"), {
|
||||
status: 401,
|
||||
});
|
||||
}
|
||||
if (scenario === "forbidden-403") {
|
||||
return HttpResponse.json(failureEnvelope("FORBIDDEN"), { status: 403 });
|
||||
return HttpResponse.json(problem(403, "FORBIDDEN"), { status: 403 });
|
||||
}
|
||||
if (scenario === "not-found-404") {
|
||||
return HttpResponse.json(failureEnvelope("NOT_FOUND"), { status: 404 });
|
||||
return HttpResponse.json(problem(404, "NOT_FOUND"), { status: 404 });
|
||||
}
|
||||
if (scenario === "conflict-409") {
|
||||
return HttpResponse.json(failureEnvelope("CONFLICT"), { status: 409 });
|
||||
return HttpResponse.json(problem(409, "CONFLICT"), { status: 409 });
|
||||
}
|
||||
if (scenario === "validation-422") {
|
||||
return HttpResponse.json(
|
||||
failureEnvelope("VALIDATION_REJECTED", {
|
||||
issues: [{ path: "name", code: "too_small" }],
|
||||
}),
|
||||
problem(422, "VALIDATION_REJECTED"),
|
||||
{ status: 422 },
|
||||
);
|
||||
}
|
||||
if (scenario === "rate-limited-429") {
|
||||
return HttpResponse.json(failureEnvelope("RATE_LIMITED"), {
|
||||
return HttpResponse.json(problem(429, "RATE_LIMITED"), {
|
||||
status: 429,
|
||||
headers: { "Retry-After": "1" },
|
||||
});
|
||||
@@ -93,11 +87,20 @@ async function scenarioResponse(
|
||||
scenario === "server-terminal-500" ||
|
||||
(scenario === "server-retry-success" && attempt === 1)
|
||||
) {
|
||||
return HttpResponse.json(failureEnvelope("SERVER_FAILURE"), {
|
||||
return HttpResponse.json(problem(503, "SERVER_FAILURE"), {
|
||||
status: 503,
|
||||
});
|
||||
}
|
||||
return HttpResponse.json(successEnvelope(payload));
|
||||
return HttpResponse.json(payload);
|
||||
}
|
||||
|
||||
function problem(status: number, code: string) {
|
||||
return Object.freeze({
|
||||
type: `https://api.test/problems/${code.toLowerCase()}`,
|
||||
title: code,
|
||||
status,
|
||||
code,
|
||||
});
|
||||
}
|
||||
|
||||
export function createReferenceScenarioHandlers(options: ScenarioOptions = {}) {
|
||||
|
||||
Reference in New Issue
Block a user