mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-08 11:27:47 +00:00
Move nock to dev deps and load it lazily
# Conflicts: # backend/src/server/routes/v1/bdd-nock-router.ts
This commit is contained in:
@@ -1,20 +1,30 @@
|
|||||||
// import { z } from "zod";
|
import type { Definition } from "nock";
|
||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
// import { getConfig } from "@app/lib/config/env";
|
import { getConfig } from "@app/lib/config/env";
|
||||||
// import { ForbiddenRequestError } from "@app/lib/errors";
|
import { ForbiddenRequestError, InternalServerError } from "@app/lib/errors";
|
||||||
// import { logger } from "@app/lib/logger";
|
import { logger } from "@app/lib/logger";
|
||||||
// import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
|
import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
|
||||||
// import { AuthMode } from "@app/services/auth/auth-type";
|
import { AuthMode } from "@app/services/auth/auth-type";
|
||||||
|
|
||||||
// export const registerBddNockRouter = async (server: FastifyZodProvider) => {
|
export const registerBddNockRouter = async (server: FastifyZodProvider) => {
|
||||||
// const checkIfBddNockApiEnabled = () => {
|
const importNock = async () => {
|
||||||
// const appCfg = getConfig();
|
// Notice: it seems like importing nock somehow increase memory usage a lots, let's import it lazily.
|
||||||
// // Note: Please note that this API is only available in development mode and only for BDD tests.
|
const nock = await import("nock");
|
||||||
// // This endpoint should NEVER BE ENABLED IN PRODUCTION!
|
if (!nock) {
|
||||||
// if (appCfg.NODE_ENV !== "development" || !appCfg.isBddNockApiEnabled) {
|
throw new InternalServerError({ message: "Failed to import nock" });
|
||||||
// throw new ForbiddenRequestError({ message: "BDD Nock API is not enabled" });
|
}
|
||||||
// }
|
return nock;
|
||||||
// };
|
};
|
||||||
|
|
||||||
|
const checkIfBddNockApiEnabled = () => {
|
||||||
|
const appCfg = getConfig();
|
||||||
|
// Note: Please note that this API is only available in development mode and only for BDD tests.
|
||||||
|
// This endpoint should NEVER BE ENABLED IN PRODUCTION!
|
||||||
|
if (appCfg.NODE_ENV !== "development" || !appCfg.isBddNockApiEnabled) {
|
||||||
|
throw new ForbiddenRequestError({ message: "BDD Nock API is not enabled" });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// server.route({
|
// server.route({
|
||||||
// method: "POST",
|
// method: "POST",
|
||||||
@@ -42,46 +52,49 @@
|
|||||||
// } as Definition;
|
// } as Definition;
|
||||||
// });
|
// });
|
||||||
|
|
||||||
// nock.define(processedDefinitions);
|
const nock = await importNock();
|
||||||
// // Ensure we are activating the nocks, because we could have called `nock.restore()` before this call.
|
nock.define(processedDefinitions);
|
||||||
// if (!nock.isActive()) {
|
// Ensure we are activating the nocks, because we could have called `nock.restore()` before this call.
|
||||||
// nock.activate();
|
if (!nock.isActive()) {
|
||||||
// }
|
nock.activate();
|
||||||
// return { status: "ok" };
|
}
|
||||||
// }
|
return { status: "ok" };
|
||||||
// });
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// server.route({
|
server.route({
|
||||||
// method: "POST",
|
method: "POST",
|
||||||
// url: "/clean-all",
|
url: "/clean-all",
|
||||||
// schema: {
|
schema: {
|
||||||
// response: {
|
response: {
|
||||||
// 200: z.object({ status: z.string() })
|
200: z.object({ status: z.string() })
|
||||||
// }
|
}
|
||||||
// },
|
},
|
||||||
// onRequest: verifyAuth([AuthMode.JWT]),
|
onRequest: verifyAuth([AuthMode.JWT]),
|
||||||
// handler: async () => {
|
handler: async () => {
|
||||||
// checkIfBddNockApiEnabled();
|
checkIfBddNockApiEnabled();
|
||||||
// logger.info("Cleaning all nocks");
|
logger.info("Cleaning all nocks");
|
||||||
// nock.cleanAll();
|
const nock = await importNock();
|
||||||
// return { status: "ok" };
|
nock.cleanAll();
|
||||||
// }
|
return { status: "ok" };
|
||||||
// });
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// server.route({
|
server.route({
|
||||||
// method: "POST",
|
method: "POST",
|
||||||
// url: "/restore",
|
url: "/restore",
|
||||||
// schema: {
|
schema: {
|
||||||
// response: {
|
response: {
|
||||||
// 200: z.object({ status: z.string() })
|
200: z.object({ status: z.string() })
|
||||||
// }
|
}
|
||||||
// },
|
},
|
||||||
// onRequest: verifyAuth([AuthMode.JWT]),
|
onRequest: verifyAuth([AuthMode.JWT]),
|
||||||
// handler: async () => {
|
handler: async () => {
|
||||||
// checkIfBddNockApiEnabled();
|
checkIfBddNockApiEnabled();
|
||||||
// logger.info("Restore network requests from nock");
|
logger.info("Restore network requests from nock");
|
||||||
// nock.restore();
|
const nock = await importNock();
|
||||||
// return { status: "ok" };
|
nock.restore();
|
||||||
// }
|
return { status: "ok" };
|
||||||
// });
|
}
|
||||||
// };
|
});
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user