Skip to content

Commit e74204f

Browse files
committed
.
1 parent 970fbe5 commit e74204f

File tree

8 files changed

+19
-27
lines changed

8 files changed

+19
-27
lines changed

docs/mocks.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ MSW Auto-Mocker
88
Usage
99
- Vitest: tests initialize MSW in `src/mocks/test.setup.ts`. Run `pnpm test`.
1010
- Browser (optional): call `startWorker()` from `src/mocks/browser.ts` in your development entry point to mock requests in the browser.
11-
- Standalone server (dev): `pnpm mock:server` starts an HTTP mock server at `http://localhost:9090` (configurable with `MOCK_PORT`). Point API requests or Next.js rewrites to this origin to develop without a live backend.
11+
- Standalone server (dev): `pnpm mock:server` starts an HTTP mock server at `http://localhost:9090`. In dev, Next.js rewrites proxy `/registry/*` to this origin; use relative URLs like `/registry/v0.1/servers` from both client and server code.
1212

1313
Regeneration
1414
- Delete a fixture file to re-generate it on next request.
@@ -17,5 +17,6 @@ Failure behavior (always strict)
1717
- If a schema is missing or faker fails, the handler responds 500 and does not write a placeholder.
1818
- Invalid fixtures (including empty `{}` when the schema defines properties) respond 500.
1919

20-
Types (optional)
21-
- If you expose OpenAPI response types under `@api/types.gen`, set `USE_TYPES_FOR_FIXTURES = true` in `src/mocks/mocker.ts` to add a `satisfies` clause in generated fixtures.
20+
Types
21+
- Fixtures default to strict types. Generated modules import response types from `@api/types.gen` and use a `satisfies` clause to ensure compatibility.
22+
- Make sure `tsconfig.json` includes: `"paths": { "@api/*": ["./src/generated/*"] }`.

next.config.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { NextConfig } from "next";
22

33
const isDev = process.env.NODE_ENV !== "production";
4-
const mockOrigin = process.env.MOCK_SERVER_ORIGIN || "http://localhost:9090";
54

65
const nextConfig: NextConfig = {
76
/* config options here */
@@ -13,7 +12,7 @@ const nextConfig: NextConfig = {
1312
// Proxy registry API to the local MSW mock server in dev
1413
{
1514
source: "/registry/:path*",
16-
destination: `${mockOrigin}/registry/:path*`,
15+
destination: "http://localhost:9090/registry/:path*",
1716
},
1817
];
1918
},

src/app/catalog/page.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,15 @@ export default async function CatalogPage() {
1212
redirect("/signin");
1313
}
1414

15-
// Load server registry summary (SSR). In dev, target the mock server.
15+
// Load server registry summary (SSR). Use a relative URL so dev rewrites
16+
// proxy to the standalone mock server.
1617
let serversSummary: {
1718
count: number;
1819
titles: string[];
1920
sample: Array<{ title: string; name: string; version?: string }>;
2021
} = { count: 0, titles: [], sample: [] };
2122
try {
22-
const base =
23-
process.env.MOCK_SERVER_ORIGIN ||
24-
(process.env.NODE_ENV !== "production" ? "http://localhost:9090" : "");
25-
const url = base
26-
? `${base}/registry/v0.1/servers`
27-
: "/registry/v0.1/servers";
23+
const url = "/registry/v0.1/servers";
2824
const res = await fetch(url);
2925
if (process.env.NODE_ENV !== "production") {
3026
// eslint-disable-next-line no-console

src/app/page.tsx

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,14 @@ export default async function Home() {
1212
redirect("/signin");
1313
}
1414

15-
// Try to load servers list from the registry API (SSR).
16-
// In dev, prefer the standalone mock server to validate server-side fetches.
15+
// Try to load servers list from the registry API (SSR). Uses a relative URL
16+
// so Next.js dev rewrites can proxy to the standalone mock server.
1717
let serversSummary: { count: number; titles: string[] } = {
1818
count: 0,
1919
titles: [],
2020
};
2121
try {
22-
const base =
23-
process.env.MOCK_SERVER_ORIGIN ||
24-
(process.env.NODE_ENV !== "production" ? "http://localhost:9090" : "");
25-
const url = base
26-
? `${base}/registry/v0.1/servers`
27-
: "/registry/v0.1/servers";
22+
const url = "/registry/v0.1/servers";
2823
const res = await fetch(url);
2924
if (process.env.NODE_ENV !== "production") {
3025
// eslint-disable-next-line no-console

src/mocks/fixtures/README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ Fixtures are auto-generated on first run by the MSW Auto-Mocker.
77
Notes
88
- Non-schema mocks in `src/mocks/customHandlers` take precedence over schema-based mocks.
99
- To re-generate a fixture, delete the file; it will be recreated on next request.
10-
- If your project exposes OpenAPI response types via `@api/types.gen`, you can
11-
enable type enforcement by setting `USE_TYPES_FOR_FIXTURES = true` in
12-
`src/mocks/mocker.ts`.
10+
- Fixtures are type-checked against OpenAPI response types via `@api/types.gen` by default.
11+
- Ensure `tsconfig.json` defines: `"paths": { "@api/*": ["./src/generated/*"] }`.
1312
- Always strict: missing/failed generation returns 500 (no placeholder), and invalid fixtures return 500.

src/mocks/mocker.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import { buildMockModule } from "./mockTemplate";
1212
// ===== Config =====
1313
// Adjust the path of the OpenAPI JSON here if needed.
1414
// This repo keeps it at project root as `swagger.json`.
15-
const USE_TYPES_FOR_FIXTURES = false; // set true if you have @api/types.gen alias
15+
// Generate typed fixtures that satisfy OpenAPI response types under @api/types.gen
16+
const USE_TYPES_FOR_FIXTURES = true;
1617

1718
// Strip these noisy prefixes from generated fixture folder names.
1819
const PREFIXES_TO_STRIP = ["api_v1beta_", "api_v0_"];

src/mocks/server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import type { IncomingMessage, ServerResponse } from "node:http";
22
import { createServer } from "@mswjs/http-middleware";
33
import { handlers } from "./handlers";
44

5-
const DEFAULT_PORT = 9090;
6-
const port = Number(process.env.MOCK_PORT || DEFAULT_PORT);
5+
// Fixed port for the standalone mock server
6+
const port = 9090;
77

88
const httpServer = createServer(...handlers);
99

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
}
2020
],
2121
"paths": {
22-
"@/*": ["./src/*"]
22+
"@/*": ["./src/*"],
23+
"@api/*": ["./src/generated/*"]
2324
}
2425
},
2526
"include": [

0 commit comments

Comments
 (0)