next-rest-framework
Version:
Next REST Framework - write type-safe, self-documenting REST APIs in Next.js
101 lines (100 loc) • 4.63 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.logNextRestFrameworkError = exports.handleReservedPathWarnings = exports.logIgnoredPaths = exports.warnAboutDirNotFound = exports.warnAboutReservedPath = exports.logReservedPaths = exports.logInitInfo = void 0;
const chalk_1 = __importDefault(require("chalk"));
const lodash_1 = require("lodash");
const logInitInfo = ({ config }) => {
const configsEqual = (0, lodash_1.isEqualWith)(global.nextRestFrameworkConfig, config, (val1, val2) => {
if (typeof val1 === 'function' && typeof val2 === 'function') {
return val1.toString() === val2.toString();
}
});
if (!global.nextRestFrameworkConfig) {
global.nextRestFrameworkConfig = config;
console.info(chalk_1.default.green('Next REST Framework initialized! 🚀'));
}
else if (!configsEqual) {
console.info(chalk_1.default.green('Next REST Framework config changed, re-initializing!'));
global.nextRestFrameworkConfig = config;
global.reservedPathsLogged = false;
}
};
exports.logInitInfo = logInitInfo;
const logReservedPaths = ({ config, baseUrl }) => {
if (config.exposeOpenApiSpec) {
console.info(chalk_1.default.yellowBright(`Swagger UI: ${baseUrl}${config.swaggerUiPath}
OpenAPI JSON: ${baseUrl}${config.openApiJsonPath}
OpenAPI YAML: ${baseUrl}${config.openApiYamlPath}`));
}
else {
console.info(chalk_1.default.yellowBright(`OpenAPI spec is not exposed. To expose it, set ${chalk_1.default.bold('exposeOpenApiSpec')} to ${chalk_1.default.bold('true')} in the Next REST Framework config.`));
}
global.reservedPathsLogged = true;
};
exports.logReservedPaths = logReservedPaths;
const warnAboutReservedPath = ({ path, name, configName }) => {
console.warn(chalk_1.default.yellowBright(`Warning: ${chalk_1.default.bold(path)} is reserved for ${name}. Update ${chalk_1.default.bold(configName)} in your Next REST Framework config to use this path for other purposes.`));
switch (configName) {
case 'openApiJsonPath': {
global.reservedOpenApiJsonPathWarningLogged = true;
break;
}
case 'openApiYamlPath': {
global.reservedOpenApiYamlPathWarningLogged = true;
break;
}
case 'swaggerUiPath': {
global.reservedSwaggerUiPathWarningLogged = true;
break;
}
}
};
exports.warnAboutReservedPath = warnAboutReservedPath;
const warnAboutDirNotFound = ({ configName, path }) => {
console.warn(chalk_1.default.yellowBright(`Warning: You have enabled the ${chalk_1.default.bold(configName)} option in your Next REST Framework config, but the directory does not exist at ${chalk_1.default.bold(path)}.`));
};
exports.warnAboutDirNotFound = warnAboutDirNotFound;
const logIgnoredPaths = (paths) => {
console.info(chalk_1.default.yellowBright(`The following paths are ignored by Next REST Framework: ${chalk_1.default.bold(paths.map((p) => `\n- ${p}`))}`));
};
exports.logIgnoredPaths = logIgnoredPaths;
const handleReservedPathWarnings = ({ pathname, config: { openApiJsonPath, openApiYamlPath, swaggerUiPath } }) => {
if (pathname === openApiJsonPath &&
!global.reservedOpenApiJsonPathWarningLogged) {
(0, exports.warnAboutReservedPath)({
path: openApiJsonPath,
name: 'OpenAPI JSON spec',
configName: 'openApiJsonPath'
});
}
if (pathname === openApiYamlPath &&
!global.reservedOpenApiYamlPathWarningLogged) {
(0, exports.warnAboutReservedPath)({
path: openApiYamlPath,
name: 'OpenAPI YAML spec',
configName: 'openApiYamlPath'
});
}
if (pathname === swaggerUiPath &&
!global.reservedSwaggerUiPathWarningLogged) {
(0, exports.warnAboutReservedPath)({
path: swaggerUiPath,
name: 'Swagger UI',
configName: 'swaggerUiPath'
});
}
};
exports.handleReservedPathWarnings = handleReservedPathWarnings;
const logNextRestFrameworkError = ({ error }) => {
if (process.env.NODE_ENV !== 'production') {
console.error(chalk_1.default.red(`Next REST Framework encountered an error:
${error}`));
}
else {
console.error(chalk_1.default.red('Next REST Framework encountered an error - suppressed in production mode.'));
}
};
exports.logNextRestFrameworkError = logNextRestFrameworkError;