fix versions and logger

This commit is contained in:
2022-07-15 19:40:28 +02:00
parent f8e2210cc0
commit 3597548b4d
18 changed files with 895 additions and 744 deletions

View File

@ -139,7 +139,7 @@
"private-static-method", "private-static-method",
"private-method" "private-method"
], ],
"order": "alphabetically" "order": "as-written"
} }
} }
], ],

6
.prettierrc Normal file
View File

@ -0,0 +1,6 @@
{
"trailingComma": "es5",
"tabWidth": 2,
"semi": true,
"singleQuote": false
}

View File

@ -5,7 +5,7 @@
"main": "server.ts", "main": "server.ts",
"type": "module", "type": "module",
"scripts": { "scripts": {
"start": "node ./dist/app.js", "start": "node --no-warnings --trace-warnings --es-module-specifier-resolution=node ./dist/app.js",
"dev": "cross-env TS_NODE_COMPILER=ttypescript node --no-warnings --loader ./src/myloader.mjs --es-module-specifier-resolution=node ./src/app.ts", "dev": "cross-env TS_NODE_COMPILER=ttypescript node --no-warnings --loader ./src/myloader.mjs --es-module-specifier-resolution=node ./src/app.ts",
"build": "ttsc -p tsconfig.json && tsc-alias -p tsconfig.json", "build": "ttsc -p tsconfig.json && tsc-alias -p tsconfig.json",
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
@ -25,42 +25,44 @@
"author": "Joshua Schnabel", "author": "Joshua Schnabel",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@automapper/classes": "^8.3.0", "@automapper/classes": "^8.5.0",
"@automapper/core": "^8.3.0", "@automapper/core": "^8.5.0",
"@gquittet/graceful-server": "^2.5.2", "@fastify/helmet": "9.1.0",
"@nestjs/common": "^8.4.3", "@fastify/sensible": "^5.1.0",
"@nestjs/core": "^8.4.3", "@gquittet/graceful-server": "^3.0.2",
"@nestjs/passport": "^8.2.1", "@nestjs/common": "^9.0.2",
"@nestjs/platform-fastify": "^8.4.3", "@nestjs/core": "^9.0.2",
"@nestjs/passport": "^9.0.0",
"@nestjs/platform-fastify": "^9.0.2",
"args-command-parser": "^1.2.4", "args-command-parser": "^1.2.4",
"cli-color": "^2.0.1", "cli-color": "^2.0.3",
"fastify": "^3.25.3", "fastify-helmet": "^7.1.0",
"fastify-plugin": "^3.0.0", "fastify-plugin": "^3.0.0",
"fastify-sensible": "^3.1.2", "passport": "^0.6.0",
"passport": "^0.5.2",
"passport-http": "^0.3.0", "passport-http": "^0.3.0",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
"rxjs": "^7.5.5", "rxjs": "^7.5.5",
"winston": "^3.4.0" "typescript-rtti": "^0.8.0",
"winston": "^3.8.1"
}, },
"devDependencies": { "devDependencies": {
"@tsconfig/node16": "^1.0.2", "@tsconfig/node16": "^1.0.3",
"@types/cli-color": "^2.0.2", "@types/cli-color": "^2.0.2",
"@types/node": "^17.0.9", "@types/node": "^18.0.3",
"@types/passport": "^1.0.7", "@types/passport": "^1.0.9",
"@types/passport-http": "^0.3.9", "@types/passport-http": "^0.3.9",
"@types/triple-beam": "^1.3.2", "@types/triple-beam": "^1.3.2",
"@typescript-eslint/eslint-plugin": "^5.10.0", "@typescript-eslint/eslint-plugin": "^5.30.5",
"@typescript-eslint/parser": "^5.10.0", "@typescript-eslint/parser": "^5.30.5",
"cross-env": "^7.0.3", "cross-env": "^7.0.3",
"eslint": "^8.7.0", "eslint": "^8.19.0",
"eslint-import-resolver-alias": "^1.1.2", "eslint-import-resolver-alias": "^1.1.2",
"eslint-plugin-import": "^2.25.4", "eslint-plugin-import": "^2.25.4",
"eslint-plugin-promise": "^6.0.0", "eslint-plugin-promise": "^6.0.0",
"eslint-plugin-unused-imports": "^2.0.0", "eslint-plugin-unused-imports": "^2.0.0",
"ts-node": "^10.4.0", "ts-node": "^10.8.2",
"tsc-alias": "^1.6.6", "tsc-alias": "^1.6.11",
"ttypescript": "^1.5.13", "ttypescript": "^1.5.13",
"typescript": "^4.5.4" "typescript": "^4.7.4"
} }
} }

View File

@ -1,11 +1,12 @@
/* eslint-disable @typescript-eslint/no-magic-numbers */ /* eslint-disable @typescript-eslint/no-magic-numbers */
/* eslint-disable no-console */ /* eslint-disable no-console */
import "reflect-metadata";
import clc from "cli-color"; import clc from "cli-color";
import App, { AppCommand } from "#app"; import App, { AppCommand } from "#app";
import ConfigurationDefinition, { DefinitionType } from "#configuration/configurationDefinition"; import ConfigurationDefinition, { DefinitionType } from "#configuration/configurationDefinition";
import definitionValidators from "#configuration/configurationValidatior"; import definitionValidators from "#configuration/configurationValidatior";
import Server from "#framework/server/server"; import Server from "#framework/server/server";
import getlogger, { defaultLogger } from "#logger"; import getlogger, { defaultLogger } from "#logger";
const app: App = new (class extends App { const app: App = new (class extends App {
public options (setDefinitions: (...definitions: ConfigurationDefinition[]) => void): void { public options (setDefinitions: (...definitions: ConfigurationDefinition[]) => void): void {

View File

@ -51,13 +51,6 @@ export default class Repo extends Aggregate<RepoId>{
// } // }
} }
export type RepoBuilder = {
[k in keyof Repo]-?: (arg: Repo[k]) => RepoBuilder
}
& {
build(): Repo;
};
export class RepoIdGenerator implements IdentifierGenerator<RepoId> { export class RepoIdGenerator implements IdentifierGenerator<RepoId> {
public generate (): RepoId { public generate (): RepoId {
return new RepoId(randomUUID()); return new RepoId(randomUUID());

View File

@ -1,13 +1,4 @@
import { Identifier, Aggregate} from "#ddd"; import { Identifier, Aggregate} from "#ddd";
import { ITest } from "#framework/hello/test";
interface YYYY {
}
export class XXX implements ITest, YYYY {
public xxx = "yyy";
}
export default class User extends Aggregate<UserId> { export default class User extends Aggregate<UserId> {
private readonly _id: UserId | undefined; private readonly _id: UserId | undefined;
@ -34,14 +25,6 @@ export default class User extends Aggregate<UserId> {
public get password (): string { public get password (): string {
return this._password; return this._password;
} }
public xxx (): XXX {
return new XXX();
}
// public static builder (): Test {
// return (<unknown> Builder(User)) as Test;
// }
} }
export class UserId extends Identifier<string> { export class UserId extends Identifier<string> {

View File

@ -1,10 +1,9 @@
import User, { UserId } from "#domain/user/user.entity"; import User, { UserId } from "#domain/user/user.entity";
import { UserRepository } from "#domain/user/user.repo"; import { UserRepository } from "#domain/user/user.repo";
import { Injectable } from "@nestjs/common"; import { Injectable } from "@nestjs/common";
import { ITest } from "#framework/hello/test";
@Injectable() @Injectable()
export default class UserFilePersistence implements UserRepository, ITest { export default class UserFilePersistence implements UserRepository {
public xxx = ""; public xxx = "";
private readonly values: Map<UserId, User> = new Map(); private readonly values: Map<UserId, User> = new Map();

View File

@ -7,17 +7,20 @@ import getLogger from "#logger";
import { Logger } from "winston"; import { Logger } from "winston";
import AppHelp from "./appHelp"; import AppHelp from "./appHelp";
// TODO: move to App
const ENV_PREFIX = "ANT";
export abstract class AppCommand { export abstract class AppCommand {
public abstract getDescriptions (): string; public abstract getDescriptions (): string;
public abstract getName (): string;
public abstract run (): void; public abstract run (): void;
public abstract getName (): string;
} }
export default abstract class App { export default abstract class App {
private readonly commands: Map<string, AppCommand> = new Map();
private readonly appHelp: AppHelp; private readonly appHelp: AppHelp;
private readonly commands: Map<string, AppCommand> = new Map();
private readonly logger: Logger; private readonly logger: Logger;
public constructor () { public constructor () {
@ -26,20 +29,23 @@ export default abstract class App {
this.appHelp = new AppHelp(); this.appHelp = new AppHelp();
this.logger = getLogger("app"); this.logger = getLogger("app");
this.addCommmand(new (class implements AppCommand { this.addCommmand(new (class implements AppCommand {
public getName (): string {
return "help";
}
public getDescriptions (): string { public getDescriptions (): string {
return "Show help for this application."; return "Show help for this application.";
} }
public getName (): string {
return "help";
}
public run (): void { public run (): void {
that.appHelp.printHelp(that.commands); that.appHelp.printHelp(that.commands, ENV_PREFIX);
} }
})); }));
} }
public abstract commmands (setCommands: (...commands: AppCommand[]) => void): void;
/** /**
* It executes the commands and options. * It executes the commands and options.
*/ */
@ -51,7 +57,7 @@ export default abstract class App {
console.log(); console.log();
}); });
this.options((...defs): void => { this.options((...defs): void => {
initConfiguration("XXX", ...defs); initConfiguration(ENV_PREFIX, ...defs);
}); });
this.printBanner(); this.printBanner();
configuration().validate(); configuration().validate();
@ -65,13 +71,12 @@ export default abstract class App {
} }
} }
public abstract options (setDefinitions: (...definitions: ConfigurationDefinition[]) => void): void;
public abstract printBanner (): void;
private addCommmand (command: AppCommand): void { private addCommmand (command: AppCommand): void {
this.commands.set(command.getName(), command); this.commands.set(command.getName(), command);
} }
public abstract options (setDefinitions: (...definitions: ConfigurationDefinition[]) => void): void;
public abstract commmands (setCommands: (...commands: AppCommand[]) => void): void;
public abstract printBanner (): void;
} }

View File

@ -13,60 +13,21 @@ export default class AppHelp {
* Prints the help message * Prints the help message
* @param commands - A map of all the commands that the program can run. * @param commands - A map of all the commands that the program can run.
*/ */
public printHelp (commands: Map<string, AppCommand>): void { public printHelp (commands: Map<string, AppCommand>, envPrefix: string): void {
const programm = process.argv.slice(0, 2).join(" "); const programm = process.argv.slice(0, 2).join(" ");
const underline = clc.underline; const underline = clc.underline;
console.log(underline("Usage")+"\n"); console.log(underline("Usage") + "\n");
console.log("\t"+ programm + " [COMMAND] [OPTIONS]"+"\n"); console.log("\t" + programm + " [COMMAND] [OPTIONS]" + "\n");
console.log(underline("Commands")+"\n"); console.log(underline("Commands") + "\n");
this.printCommands(commands); this.printCommands(commands);
console.log(); console.log();
console.log(underline("Options")); console.log(underline("Options"));
console.log(); console.log();
this.printOptions((def) => def.getCommands().length >= 1); this.printOptions(envPrefix, (def) => def.getCommands().length >= 1);
console.log(); console.log();
console.log(underline("Rules & Behavior")); console.log(underline("Rules & Behavior"));
console.log(); console.log();
this.printOptions((def) => def.getCommands().length == 0); this.printOptions(envPrefix, (def) => def.getCommands().length == 0);
}
/**
* Prints all commands and their descriptions
* @param commands - Map<string, AppCommand>
*/
private printCommands (commands: Map<string, AppCommand>): void {
commands.forEach((v, k, _m) => {
console.log("\t" + k + " - " + v.getDescriptions());
const paramaters: string[] = [];
// Find parameters for current command
for (const def of this.getConfigurationDefinitions()) {
if (def.getCommands().includes(k)) {
paramaters.push(this.generateParameters(def));
}
}
console.log("\t↳ " + k + " " + paramaters.join(" "));
});
}
/**
* Prints out all the options that are available to the user
* @param condition - Select witch options are printed.
*/
private printOptions (condition: (a: ConfigurationDefinition) => boolean): void {
let maxLength = 0;
/* Find longest String */
for (const def of this.getConfigurationDefinitions()) {
if (condition(def)) {
const option = " -" + def.getArgName().sort((a, b) => a.length - b.length).join(" -");
maxLength = Math.max(maxLength, option.length);
}
}
for (const def of this.getConfigurationDefinitions()) {
if (condition(def)) {
const option = " -" + def.getArgName().sort((a, b) => a.length - b.length).join(" -");
console.log("\t↳ " + (option.padEnd(maxLength, " ")) + " - " + def.getDescription());
}
}
} }
/** /**
@ -107,4 +68,46 @@ export default class AppHelp {
}); });
} }
/**
* Prints all commands and their descriptions
* @param commands - Map<string, AppCommand>
*/
private printCommands (commands: Map<string, AppCommand>): void {
commands.forEach((v, k, _m) => {
console.log("\t" + k + " - " + v.getDescriptions());
const paramaters: string[] = [];
// Find parameters for current command
for (const def of this.getConfigurationDefinitions()) {
if (def.getCommands().includes(k)) {
paramaters.push(this.generateParameters(def));
}
}
console.log("\t↳ " + k + " " + paramaters.join(" "));
});
}
/**
* Prints out all the options that are available to the user
* @param condition - Select witch options are printed.
*/
private printOptions (envPrefix: string, condition: (a: ConfigurationDefinition) => boolean): void {
let maxLength = 0;
/* Find longest String */
for (const def of this.getConfigurationDefinitions()) {
if (condition(def)) {
let option = " -" + def.getArgName().sort((a, b) => a.length - b.length).join(" -");
option += " (" + envPrefix + "_" + def.getEnvName().toUpperCase()+")";
maxLength =
Math.max(maxLength, option.length);
}
}
for (const def of this.getConfigurationDefinitions()) {
if (condition(def)) {
let option = " -" + def.getArgName().sort((a, b) => a.length - b.length).join(" -");
option += " (" + envPrefix + "_" + def.getEnvName().toUpperCase()+")";
console.log("\t↳ " + (option.padEnd(maxLength, " ")) + " - " + def.getDescription());
}
}
}
} }

View File

@ -0,0 +1,26 @@
import Repo from "#domain/repo/repo.entity";
import { reflect } from "typescript-rtti";
export type IBuilder<T> = {
[k in keyof T]-?: (arg: T[k]) => IBuilder<T>
}
& {
build(): T;
};
type Clazz<T> = new(...args: unknown[]) => T;
type Constructor<Params extends readonly any[] = readonly any[], Result = any> = new (...params: Params) => Result;
export function Builder<Type> (type: Constructor): IBuilder<Type> {
console.log(reflect(type).class.prototype);
const builder = new Proxy(
{},
{}
);
return builder as IBuilder<Type>;
}
const builder = Builder<Repo>(Repo);
builder.name("").build();

View File

@ -3,11 +3,11 @@ import {parser, ArgCollection} from "args-command-parser";
class Configuration { class Configuration {
private readonly definitions: Map<string, ConfigurationDefinition> = new Map();
private readonly values: Map<string, string[] | undefined> = new Map();
private readonly defaultCommand: string;
private readonly envPrefix: string;
private readonly arguments: ArgCollection; private readonly arguments: ArgCollection;
private readonly defaultCommand: string;
private readonly definitions: Map<string, ConfigurationDefinition> = new Map();
private readonly envPrefix: string;
private readonly values: Map<string, string[] | undefined> = new Map();
public constructor (defaultCommand: string, envPrefix: string, ...definitions: ConfigurationDefinition[]) { public constructor (defaultCommand: string, envPrefix: string, ...definitions: ConfigurationDefinition[]) {
this.arguments = parser(); this.arguments = parser();
for (const definition of definitions) { for (const definition of definitions) {
@ -18,23 +18,22 @@ class Configuration {
this.envPrefix = envPrefix; this.envPrefix = envPrefix;
} }
public validate (): void { /**
this.definitions.forEach((v, _k, _m) => { * Get the value of a boolean parameter
const def = v; * @param {string} name - The name of the parameter.
const values = this.values.get(def.getName()) ?? []; * @returns The value of the parameter.
if(!def.getMultiple() && values.length > 1) */
throw new Error("No more than one value may be assigned to parameter '" + def.getName() + "'."); public getBooleanValue (name: string): boolean | boolean[] | undefined {
if(def.getRequired() && (def.getCommands().length === 0 || def.getCommands().includes(this.getCommand())) && values.length === 0) const def = this.definitions.get(name);
throw new Error("Parameter '" + def.getName() + "' is required."); if(def?.getType() == DefinitionType.Boolean) {
for (const value of values) { const value = this.values.get(name) ?? [];
if(!def.getValidator().validate(value)) const result: boolean[] = this.toBoolean(value);
throw new Error("Parameter '" + def.getName() + "' does not meet the requirements: " + def.getValidator().description()); if(def.getMultiple()) {
return result;
} }
}); return result[0];
} }
return undefined;
public getDefinitions (): ConfigurationDefinition[] {
return Array.from(this.definitions.values());
} }
/** /**
@ -48,6 +47,35 @@ class Configuration {
return this.defaultCommand; return this.defaultCommand;
} }
public getDefinitions (): ConfigurationDefinition[] {
return Array.from(this.definitions.values());
}
public getNumberValue (name: string): number | number[] | undefined {
const def = this.definitions.get(name);
if(def?.getType() == DefinitionType.Number) {
const value = this.values.get(name) ?? [];
const result: number[] = this.toInteger(value);
if(def.getMultiple()) {
return result;
}
return result[0];
}
return undefined;
}
public getStringValue (name: string): string | string[] | undefined {
const def = this.definitions.get(name);
if(def?.getType() == DefinitionType.String) {
const value = this.values.get(name) ?? [];
if(def.getMultiple()) {
return value;
}
return value[0];
}
return undefined;
}
/** /**
* Get the value of a parameter * Get the value of a parameter
* @param {string} name - The name of the parameter. * @param {string} name - The name of the parameter.
@ -80,54 +108,56 @@ class Configuration {
} }
} }
/** public validate (): void {
* Get the value of a boolean parameter this.definitions.forEach((v, _k, _m) => {
* @param {string} name - The name of the parameter. const def = v;
* @returns The value of the parameter. const values = this.values.get(def.getName()) ?? [];
*/ if(!def.getMultiple() && values.length > 1)
public getBooleanValue (name: string): boolean | boolean[] | undefined { throw new Error("No more than one value may be assigned to parameter '" + def.getName() + "'.");
const def = this.definitions.get(name); if(def.getRequired() && (def.getCommands().length === 0 || def.getCommands().includes(this.getCommand())) && values.length === 0)
if(def?.getType() == DefinitionType.Boolean) { throw new Error("Parameter '" + def.getName() + "' is required.");
const value = this.values.get(name) ?? []; for (const value of values) {
const result: boolean[] = this.toBoolean(value); if(!def.getValidator().validate(value))
if(def.getMultiple()) { throw new Error("Parameter '" + def.getName() + "' does not meet the requirements: " + def.getValidator().description());
return result; }
});
}
private getArgValue (def: ConfigurationDefinition): string[] | undefined{
for (const arg of def.getArgName()) {
if(arg.length >= 2 && this.arguments.hasLongSwitch(arg)) {
return this.arguments.getLongSwitch(arg).values;
} else if (this.arguments.hasShortSwitch(arg)) {
return this.arguments.getShortSwitch(arg).values;
} }
return result[0];
} }
return undefined; return undefined;
} }
public getNumberValue (name: string): number | number[] | undefined {
const def = this.definitions.get(name);
if(def?.getType() == DefinitionType.Number) {
const value = this.values.get(name) ?? [];
const result: number[] = this.toInteger(value);
if(def.getMultiple()) {
return result;
}
return result[0];
}
return undefined;
}
public getStringValue (name: string): string | string[] | undefined {
const def = this.definitions.get(name);
if(def?.getType() == DefinitionType.String) {
const value = this.values.get(name) ?? [];
if(def.getMultiple()) {
return value;
}
return value[0];
}
return undefined;
}
/** /**
* Convert a string array to a boolean array * Convert a string array to a boolean array
* @param {string[]} value - The value of the parameter. * @param {string[]} value - The value of the parameter.
* @returns The `toBoolean` function returns an array of booleans. * @returns The `toBoolean` function returns an array of booleans.
*/ */
private getEnvValue (def: ConfigurationDefinition): string[] | undefined {
const value = process.env[this.envPrefix + "_" + def.getEnvName().toUpperCase()];
if(value !== undefined)
return [value];
else
return undefined;
}
private getInternalValue (def: ConfigurationDefinition): string[] | undefined {
let value = this.getArgValue(def);
value = (value === undefined)?this.getEnvValue(def):value;
value = (value === undefined)?def.getDefaultValue():value;
if(value !== undefined) {
return value;
}
return undefined;
}
private toBoolean (value: string[]): boolean[] { private toBoolean (value: string[]): boolean[] {
const result: boolean[] = []; const result: boolean[] = [];
for (const v of value) { for (const v of value) {
@ -150,34 +180,6 @@ class Configuration {
return result; return result;
} }
private getInternalValue (def: ConfigurationDefinition): string[] | undefined {
let value = this.getArgValue(def);
value = (value === undefined)?this.getEnvValue(def):value;
value = (value === undefined)?def.getDefaultValue():value;
if(value !== undefined) {
return value;
}
return undefined;
}
private getEnvValue (def: ConfigurationDefinition): string[] | undefined {
const value = process.env[this.envPrefix + "_" + def.getEnvName().toUpperCase()];
if(value !== undefined)
return [value];
else
return undefined;
}
private getArgValue (def: ConfigurationDefinition): string[] | undefined{
for (const arg of def.getArgName()) {
if(arg.length >= 2 && this.arguments.hasLongSwitch(arg)) {
return this.arguments.getLongSwitch(arg).values;
} else if (this.arguments.hasShortSwitch(arg)) {
return this.arguments.getShortSwitch(arg).values;
}
}
return undefined;
}
} }
let configInstance: Configuration; let configInstance: Configuration;

View File

@ -1,3 +0,0 @@
export interface ITest {
xxx: string;
}

View File

@ -1,55 +1,10 @@
/* eslint-disable @typescript-eslint/require-await */ /* eslint-disable @typescript-eslint/require-await */
import { FastifyLogFn, FastifyLoggerInstance, FastifyPluginAsync, FastifyRequest} from "fastify"; import { FastifyPluginAsync, FastifyRequest} from "fastify";
import fastifyPlugin from "fastify-plugin"; import fastifyPlugin from "fastify-plugin";
import { Bindings } from "fastify/types/logger";
import getlogger from "./logger";
import chalk from "chalk"; import chalk from "chalk";
import logadapter from "./pinoLoggerAdapter";
const logger = getlogger("fastify"); // const logger = getlogger("fastify");
const customLogger: FastifyLoggerInstance = {
info: function (obj: unknown, msg?: string, ...args: unknown[]): void {
if(typeof obj === "string")
logger.info(obj, msg, ...args);
else if(msg)
logger.info(msg, obj, ...args);
} as FastifyLogFn,
warn: function (obj: unknown, msg?: string, ...args: unknown[]): void {
if(typeof obj === "string")
logger.warning(obj, msg, ...args);
else if(msg)
logger.warning(msg, obj, ...args);
} as FastifyLogFn,
error: function (obj: unknown, msg?: string, ...args: unknown[]): void {
if(typeof obj === "string")
logger.error(obj, msg, ...args);
else if(msg)
logger.error(msg, obj, ...args);
} as FastifyLogFn,
fatal: function (obj: unknown, msg?: string, ...args: unknown[]): void {
if(typeof obj === "string")
logger.fatal(obj, msg, ...args);
else if(msg)
logger.fatal(msg, obj, ...args);
} as FastifyLogFn,
trace: function (obj: unknown, msg?: string, ...args: unknown[]): void {
if(typeof obj === "string")
logger.trace(obj, msg, ...args);
else if(msg)
logger.trace(msg, obj, ...args);
} as FastifyLogFn,
debug: function (obj: unknown, msg?: string, ...args: unknown[]): void {
if(typeof obj === "string")
logger.debug(obj, msg, ...args);
else if(msg)
logger.debug(msg, obj, ...args);
} as FastifyLogFn,
child: function (_bindings: Bindings): FastifyLoggerInstance {
return customLogger;
},
setBindings: function (_bindings: Bindings): void {
// OK
}
};
export type FastifyRequestLoggerOptions = { export type FastifyRequestLoggerOptions = {
logBody?: boolean; logBody?: boolean;
@ -153,15 +108,9 @@ export const plugin: FastifyPluginAsync<FastifyRequestLoggerOptions> = async (fa
}); });
}; };
declare module "fastify" {
interface FastifyLoggerInstance {
setBindings(bindings: Bindings): void;
}
}
export const fastifyRequestLogger = fastifyPlugin(plugin, { export const fastifyRequestLogger = fastifyPlugin(plugin, {
fastify: "3.x", fastify: "4.x",
name: "fastify-request-logger", name: "fastify-request-logger",
}); });
export const fastifyLogger = customLogger; export const fastifyLogger = logadapter;

View File

@ -1,5 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-explicit-any */
import { LoggerService } from "@nestjs/common"; import { LoggerService } from "@nestjs/common";
import getlogger from "./logger"; import getlogger from "./logger";

View File

@ -0,0 +1,37 @@
import { FastifyLogFn, FastifyLoggerInstance } from "fastify";
import getlogger from "./logger";
import pino, { ChildLoggerOptions, LoggerOptions } from "pino";
const pinoLogger = pino();
const logger = getlogger("fastify");
const customLogger: FastifyLoggerInstance = pinoLogger;
customLogger.info = function (obj: unknown, msg?: string, ...args: unknown[]): void {
if (typeof obj === "string") logger.info(obj, msg, ...args);
else if (msg) logger.info(msg, obj, ...args);
} as FastifyLogFn;
customLogger.warn = function (obj: unknown, msg?: string, ...args: unknown[]): void {
if (typeof obj === "string") logger.warning(obj, msg, ...args);
else if (msg) logger.warning(msg, obj, ...args);
} as FastifyLogFn;
customLogger.error = function (obj: unknown, msg?: string, ...args: unknown[]): void {
if (typeof obj === "string") logger.error(obj, msg, ...args);
else if (msg) logger.error(msg, obj, ...args);
} as FastifyLogFn;
customLogger.fatal = function (obj: unknown, msg?: string, ...args: unknown[]): void {
if (typeof obj === "string") logger.fatal(obj, msg, ...args);
else if (msg) logger.fatal(msg, obj, ...args);
} as FastifyLogFn;
customLogger.trace = function (obj: unknown, msg?: string, ...args: unknown[]): void {
if (typeof obj === "string") logger.trace(obj, msg, ...args);
else if (msg) logger.trace(msg, obj, ...args);
} as FastifyLogFn;
customLogger.debug = function (obj: unknown, msg?: string, ...args: unknown[]): void {
if (typeof obj === "string") logger.debug(obj, msg, ...args);
else if (msg) logger.debug(msg, obj, ...args);
} as FastifyLogFn;
customLogger.child = function <ChildOptions extends pino.ChildLoggerOptions>(_bindings: pino.Bindings, _options?: ChildLoggerOptions): pino.Logger<LoggerOptions & ChildOptions> {
return <pino.Logger<LoggerOptions & ChildOptions>>customLogger;
};
export default customLogger;

View File

@ -1,14 +1,20 @@
import fastifySensible from "fastify-sensible";
import GracefulServer from "@gquittet/graceful-server"; import GracefulServer from "@gquittet/graceful-server";
import getlogger from "#logger"; import getlogger from "#logger";
import {fastifyLogger, fastifyRequestLogger} from "#logger/fastifyLogger"; import {fastifyRequestLogger, fastifyLogger} from "#logger/fastifyLogger";
import { Logger } from "winston"; import { Logger } from "winston";
import IGracefulServer from "@gquittet/graceful-server/lib/types/interface/gracefulServer";
import configuration from "#configuration"; import configuration from "#configuration";
import { NestFactory } from "@nestjs/core"; import { NestFactory } from "@nestjs/core";
import { FastifyAdapter, NestFastifyApplication } from "@nestjs/platform-fastify"; import { FastifyAdapter, NestFastifyApplication } from "@nestjs/platform-fastify";
import { AppModule } from "../../app.module"; import { AppModule } from "../../app.module";
import NestLogger from "#framework/logger/nestLogger"; import NestLogger from "#framework/logger/nestLogger";
import { EventEmitter } from "events";
import fastifySensible from "@fastify/sensible";
interface IGracefulServer {
isReady: () => boolean;
setReady: () => void;
on: (name: string, callback: (...args: any[]) => void) => EventEmitter;
}
export default class Server { export default class Server {
private readonly logger: Logger; private readonly logger: Logger;
@ -40,7 +46,8 @@ export default class Server {
new FastifyAdapter({ new FastifyAdapter({
logger: fastifyLogger, logger: fastifyLogger,
disableRequestLogging: true disableRequestLogging: true
}), { }),
{
logger: new NestLogger(), logger: new NestLogger(),
} }
); );

View File

@ -24,8 +24,9 @@
"plugins": [ "plugins": [
{ {
"transform": "@automapper/classes/transformer-plugin", "transform": "@automapper/classes/transformer-plugin",
"modelFileNameSuffix": [".types2.js", ".entity2.ts", ".dto2.ts"] "modelFileNameSuffix": [".types.js", ".entity.ts", ".dto.ts"]
} },
{ "transform": "typescript-rtti/dist/transformer" }
] ]
}, },
"tsc-alias": { "tsc-alias": {

1107
yarn.lock

File diff suppressed because it is too large Load Diff