UNPKG

@newmo/graphql-fake-server

Version:

GraphQL fake server for testing

92 lines 3.72 kB
import path from "node:path"; import { pathToFileURL } from "node:url"; export const normalizeFakeServerConfig = (config) => { return { schemaFilePath: config.schemaFilePath, ports: { fakeServer: config.ports?.fakeServer ?? 4000, apolloServer: config.ports?.apolloServer ?? 4002, }, maxRegisteredSequences: config.maxRegisteredSequences ?? 1000, maxFieldRecursionDepth: config.maxFieldRecursionDepth ?? 9, maxQueryDepth: config.maxQueryDepth ?? 10, defaultValues: config.defaultValues ?? {}, logLevel: config.logLevel ?? "info", allowedCORSOrigins: config.allowedCORSOrigins ?? [], }; }; export const validateFakeServerConfig = (config) => { if (!config.schemaFilePath) { throw new Error("The schemaFilePath is required."); } if (typeof config.schemaFilePath !== "string") { throw new Error("The schemaPath must be a string."); } if (config.ports) { if (typeof config.ports !== "object") { throw new Error("The ports must be an object."); } if (config.ports.fakeServer && typeof config.ports.fakeServer !== "number") { throw new Error("The fakeServer port must be a number."); } if (config.ports.apolloServer && typeof config.ports.apolloServer !== "number") { throw new Error("The apolloServer port must be a number."); } } if (config.maxRegisteredSequences && typeof config.maxRegisteredSequences !== "number") { throw new Error("The maxRegisteredSequences must be a number."); } if (config.maxFieldRecursionDepth && typeof config.maxFieldRecursionDepth !== "number") { throw new Error("The maxFieldRecursionDepth must be a number."); } if (config.maxQueryDepth && typeof config.maxQueryDepth !== "number") { throw new Error("The maxQueryDepth must be a number."); } if (config.defaultValues) { if (typeof config.defaultValues !== "object") { throw new Error("The defaultValues must be an object."); } } // ["debug", "info", "warn", "error"].includes(logLevel) if (config.logLevel && !["debug", "info", "warn", "error"].includes(config.logLevel)) { throw new Error("The logLevel must be one of 'debug', 'info', 'warn', 'error'."); } if (config.allowedCORSOrigins) { if (!Array.isArray(config.allowedCORSOrigins)) { throw new Error("The allowedCORSOrigins must be an array."); } for (const origin of config.allowedCORSOrigins) { if (typeof origin !== "string") { throw new Error("Each allowedCORSOrigin must be a string."); } } } return config; }; /** * Load the fake server configuration from the file. * @param configPath */ export const loadConfig = async (cwd, configPath) => { const fileUrl = pathToFileURL(path.resolve(cwd, configPath)).href; const { default: config } = await import(fileUrl); const normalizedConfig = normalizeFakeServerConfig(config); validateFakeServerConfig(normalizedConfig); return normalizedConfig; }; /** * Load the fake server configuration from the CLI flags. * @param cliFlag */ export const loadFakeServerConfigFromCLI = ({ schemaFilePath, logLevel, }) => { if (!schemaFilePath) { throw new Error("The --schema is required. or pass --config ./fake-server.config.js to load the config file."); } const normalizedConfig = normalizeFakeServerConfig({ schemaFilePath, logLevel, }); validateFakeServerConfig(normalizedConfig); return normalizedConfig; }; //# sourceMappingURL=config.js.map