import { Express } from 'express'; import ApiLog from './src/db/ApiLog'; type StaticOrigin = boolean | string | RegExp | (string | RegExp)[]; export type CustomOrigin = (requestOrigin: string, callback: (err: Error | null, origin?: StaticOrigin) => void) => void; export interface CorsOptionsCallback { (error: Error, options: CorsOptions): void; } export interface CorsOptionsDelegate { (req: T, cb: CorsOptionsCallback): void; } export interface CorsOptions { /** * Configures the `Access-Control-Allow-Origins` CORS header. See [here for more detail.](https://github.com/expressjs/cors#configuration-options) */ origin?: StaticOrigin | CustomOrigin; /** * Configures the Access-Control-Allow-Methods CORS header. */ methods?: string | string[]; /** * Configures the Access-Control-Allow-Headers CORS header. */ allowedHeaders?: string | string[]; /** * Configures the Access-Control-Expose-Headers CORS header. */ exposedHeaders?: string | string[]; /** * Configures the Access-Control-Allow-Credentials CORS header. */ credentials?: boolean; /** * Configures the Access-Control-Max-Age CORS header. */ maxAge?: number; /** * Whether to pass the CORS preflight response to the next handler. */ preflightContinue?: boolean; /** * Provides a status code to use for successful OPTIONS requests. */ optionsSuccessStatus?: number; } export interface AuthOptions { /** * 密码 */ password: string; /** * 登录后会话有效期,单位为秒,默认 3600 秒(1 小时) */ exp?: number; /** * 加密密钥 */ secret: string; } /** * 日志系统配置接口 * * 可以自己定义请求id,在req中添加一个属性__requestId,后台会自动使用这个值(如果存在重复id则当前id会失效) */ export interface ApiLoggerOptions { /** * API 日志查询路由的路径前缀,默认为 '/api-logs' */ routePrefix?: string; /** * UI使用的服务端地址默认使用相对路径,可以通过修改这个值来配置不同的API路径 */ uiService?: string; /** * 是否自动同步数据库模型,默认为 true */ syncDatabase?: boolean; /** * 是否启用日志记录,默认为 true */ enabled?: boolean; /** * SQLite 数据库文件的自定义路径 * 如果路径不存在会自动创建相应的目录和文件 */ dbPath?: string; /** * 是否在响应头上添加请求ID,默认为 false * 可在request中通过 req.headers['X-Request-Id'] 获取 */ includeIdInHeader?: boolean; /** * 响应头中的ID字段名称,默认为 'X-Request-Id' */ requestIdHeaderName?: string; /** * 是否记录请求和响应体,默认为 true */ logRequestResponse?: boolean; /** * 是否记录API调用栈信息,默认为 false * 开启后同时需要在req 中添加一个属性__stackTrace * 将栈信息传入,后台会自动过滤掉不必要的堆栈信息并保存 */ logStackTrace?: boolean; /** * 从堆栈跟踪文件路径中删除给定的基路径,从而有效地将绝对路径转换为相对路径。 * * `'/Users/sindresorhus/dev/clean-stack/'` as `basePath`: * * `/Users/sindresorhus/dev/clean-stack/unicorn.js:2:15` → `unicorn.js:2:15` * * 默认展示全路径 */ filterStackBasePath?: string; /** * API 白名单,这些路径的请求将不会被记录 * 可以使用字符串数组或正则表达式数组 */ whitelistPaths?: (string | RegExp)[]; /** * 是否将API记录系统自身的API路径添加到白名单中,默认为 true */ excludeSystemApis?: boolean; /** * 日志最大保存条数,超出后自动删除最旧的10%(0为不限制) */ maxRecords?: number; /** * 日志最大保存天数,超期自动删除(0为不限制) */ maxDays?: number; /** * 日志清理检测间隔(单位:分钟,默认60分钟) */ cleanupInterval?: number; /** * 过滤请求类型 * 例如:['GET', 'POST'] */ filterRequestMethods?: string[]; /** * 跨域配置 * nestjs 当外层设置的跨域配置无效时可以配置这个 */ cors?: boolean | CorsOptions | CorsOptionsDelegate; /** * 访问UI文档鉴权 为了保护日志数据的安全性,可以设置一个UI文档密码 */ auth?: AuthOptions; /** * UI文档标题 */ title?: string; } /** * 初始化 API 日志系统 * @param app Express 应用实例 * @param options 配置选项 */ export declare function initApiLogger(app: Express, options?: ApiLoggerOptions): Promise; export { ApiLog }; export { print } from './src/utils/print'; export default initApiLogger;