Skip to content

Commit fc9e787

Browse files
committed
Move nock to dev deps and load it lazily
# Conflicts: # backend/src/server/routes/v1/bdd-nock-router.ts
1 parent 624c268 commit fc9e787

File tree

1 file changed

+69
-56
lines changed

1 file changed

+69
-56
lines changed
Lines changed: 69 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
1-
// import { z } from "zod";
1+
import type { Definition } from "nock";
2+
import { z } from "zod";
23

3-
// import { getConfig } from "@app/lib/config/env";
4-
// import { ForbiddenRequestError } from "@app/lib/errors";
5-
// import { logger } from "@app/lib/logger";
6-
// import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
7-
// import { AuthMode } from "@app/services/auth/auth-type";
4+
import { getConfig } from "@app/lib/config/env";
5+
import { ForbiddenRequestError, InternalServerError } from "@app/lib/errors";
6+
import { logger } from "@app/lib/logger";
7+
import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
8+
import { AuthMode } from "@app/services/auth/auth-type";
89

9-
// export const registerBddNockRouter = async (server: FastifyZodProvider) => {
10-
// const checkIfBddNockApiEnabled = () => {
11-
// const appCfg = getConfig();
12-
// // Note: Please note that this API is only available in development mode and only for BDD tests.
13-
// // This endpoint should NEVER BE ENABLED IN PRODUCTION!
14-
// if (appCfg.NODE_ENV !== "development" || !appCfg.isBddNockApiEnabled) {
15-
// throw new ForbiddenRequestError({ message: "BDD Nock API is not enabled" });
16-
// }
17-
// };
10+
export const registerBddNockRouter = async (server: FastifyZodProvider) => {
11+
const importNock = async () => {
12+
// Notice: it seems like importing nock somehow increase memory usage a lots, let's import it lazily.
13+
const nock = await import("nock");
14+
if (!nock) {
15+
throw new InternalServerError({ message: "Failed to import nock" });
16+
}
17+
return nock;
18+
};
19+
20+
const checkIfBddNockApiEnabled = () => {
21+
const appCfg = getConfig();
22+
// Note: Please note that this API is only available in development mode and only for BDD tests.
23+
// This endpoint should NEVER BE ENABLED IN PRODUCTION!
24+
if (appCfg.NODE_ENV !== "development" || !appCfg.isBddNockApiEnabled) {
25+
throw new ForbiddenRequestError({ message: "BDD Nock API is not enabled" });
26+
}
27+
};
1828

1929
// server.route({
2030
// method: "POST",
@@ -42,46 +52,49 @@
4252
// } as Definition;
4353
// });
4454

45-
// nock.define(processedDefinitions);
46-
// // Ensure we are activating the nocks, because we could have called `nock.restore()` before this call.
47-
// if (!nock.isActive()) {
48-
// nock.activate();
49-
// }
50-
// return { status: "ok" };
51-
// }
52-
// });
55+
const nock = await importNock();
56+
nock.define(processedDefinitions);
57+
// Ensure we are activating the nocks, because we could have called `nock.restore()` before this call.
58+
if (!nock.isActive()) {
59+
nock.activate();
60+
}
61+
return { status: "ok" };
62+
}
63+
});
5364

54-
// server.route({
55-
// method: "POST",
56-
// url: "/clean-all",
57-
// schema: {
58-
// response: {
59-
// 200: z.object({ status: z.string() })
60-
// }
61-
// },
62-
// onRequest: verifyAuth([AuthMode.JWT]),
63-
// handler: async () => {
64-
// checkIfBddNockApiEnabled();
65-
// logger.info("Cleaning all nocks");
66-
// nock.cleanAll();
67-
// return { status: "ok" };
68-
// }
69-
// });
65+
server.route({
66+
method: "POST",
67+
url: "/clean-all",
68+
schema: {
69+
response: {
70+
200: z.object({ status: z.string() })
71+
}
72+
},
73+
onRequest: verifyAuth([AuthMode.JWT]),
74+
handler: async () => {
75+
checkIfBddNockApiEnabled();
76+
logger.info("Cleaning all nocks");
77+
const nock = await importNock();
78+
nock.cleanAll();
79+
return { status: "ok" };
80+
}
81+
});
7082

71-
// server.route({
72-
// method: "POST",
73-
// url: "/restore",
74-
// schema: {
75-
// response: {
76-
// 200: z.object({ status: z.string() })
77-
// }
78-
// },
79-
// onRequest: verifyAuth([AuthMode.JWT]),
80-
// handler: async () => {
81-
// checkIfBddNockApiEnabled();
82-
// logger.info("Restore network requests from nock");
83-
// nock.restore();
84-
// return { status: "ok" };
85-
// }
86-
// });
87-
// };
83+
server.route({
84+
method: "POST",
85+
url: "/restore",
86+
schema: {
87+
response: {
88+
200: z.object({ status: z.string() })
89+
}
90+
},
91+
onRequest: verifyAuth([AuthMode.JWT]),
92+
handler: async () => {
93+
checkIfBddNockApiEnabled();
94+
logger.info("Restore network requests from nock");
95+
const nock = await importNock();
96+
nock.restore();
97+
return { status: "ok" };
98+
}
99+
});
100+
};

0 commit comments

Comments
 (0)