Compare commits

..

3 Commits

Author SHA1 Message Date
5c1525588b add builder 2022-07-18 20:57:37 +02:00
3597548b4d fix versions and logger 2022-07-15 19:40:28 +02:00
f8e2210cc0 save progress 2022-04-17 02:01:31 +02:00
23 changed files with 1124 additions and 807 deletions

View File

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

View File

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

View File

@ -15,17 +15,17 @@ export class RepoId extends Identifier<string> {
}
}
export default class Repo extends Aggregate<RepoId>{
private readonly _id?: RepoId;
private readonly _name: string;
private _name = "";
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
private readonly _count = 1;
private readonly _userAssignment: UserAssignment[] = [];
public constructor (id: RepoId| undefined, name: string, userAssignment: UserAssignment[]) {
public constructor (id: RepoId | undefined) {
super();
this._id = id;
this._name = name;
this._userAssignment = userAssignment;
}
public get id (): RepoId | undefined {
@ -36,25 +36,28 @@ export default class Repo extends Aggregate<RepoId>{
return this._name;
}
public set name (name: string) {
this._name = name;
}
public get path (): string {
return "/" + this._name;
}
public get userAssignment (): UserAssignment[] {
return this._userAssignment;
}
public addUserAssignment (userAssignment: UserAssignment): void {
this._userAssignment.push(userAssignment);
public addUserAssignment (ua: UserAssignment): UserAssignment[] {
this._userAssignment.push(ua);
return this._userAssignment;
}
}
public removeUserAssignment (userAssignment: UserAssignment): void {
this._userAssignment.filter(item => item !== userAssignment);
}
// public static builder (): IBuilder<Repo> {
// return <IBuilder<Repo>> <unknown>Builder(Repo);
// }
export interface IRepoBuider {
name: string;
id: RepoId;
addUserAssignment: UserAssignment;
}
export class RepoIdGenerator implements IdentifierGenerator<RepoId> {

View File

@ -2,7 +2,7 @@ import { ValueObject } from "#ddd";
import { UserId } from "../user/user.entity";
// import { Builder, IBuilder } from "builder-pattern";
enum UserRight {
export enum UserRight {
Read = "read",
AppendOnly = "append",
Write = "write"
@ -13,7 +13,7 @@ export class UserAssignment extends ValueObject {
private readonly _userId: UserId;
private constructor (userId: UserId, right: UserRight) {
public constructor (userId: UserId, right: UserRight) {
super();
this._userId = userId;
this._right = right;

View File

@ -1,6 +1,6 @@
import { Identifier, Aggregate} from "#ddd";
export default class User extends Aggregate<UserId>{
export default class User extends Aggregate<UserId> {
private readonly _id: UserId | undefined;
private readonly _name: string;
@ -25,8 +25,8 @@ export default class User extends Aggregate<UserId>{
public get password (): string {
return this._password;
}
}
export class UserId extends Identifier<string> {
private readonly _value: string;

View File

@ -1,5 +1,6 @@
import User, { UserId } from "./user.entity";
import {Repository} from "#ddd";
export const UserRepositoryToken = "UserRepository";
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export default interface UserRepository extends Repository<UserId, User> {}
export interface UserRepository extends Repository<UserId, User> {}

View File

@ -9,8 +9,8 @@ export default class RepoFilePersistence implements RepoRepository {
public constructor () {
const x = new RepoId("xxxx");
const y = Repo.instantiate(x, "/xxx");
this.values.set(x, y);
// const y = Repo.builder().id(x).name("dddd").build();
// this.values.set(x, y);
}
public deleteAggregate (aggregate: Repo): void {
@ -36,8 +36,9 @@ export default class RepoFilePersistence implements RepoRepository {
public storeAggregate (aggregate: Repo): Repo {
let id = aggregate.id;
if(id) {
id = new RepoId("");
aggregate = Repo.instantiate(id, aggregate.path);
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
id = new RepoId(Math.random().toString(16).substr(2, 8));
// aggregate = Repo.builder().id(id).name(aggregate.name).build();
}
this.values.set(id as RepoId, aggregate);
return aggregate;

View File

@ -0,0 +1,42 @@
import User, { UserId } from "#domain/user/user.entity";
import { UserRepository } from "#domain/user/user.repo";
import { Injectable } from "@nestjs/common";
@Injectable()
export default class UserFilePersistence implements UserRepository {
public xxx = "";
private readonly values: Map<UserId, User> = new Map();
public deleteAggregate (aggregate: User): void {
if(aggregate.id)
this.values.delete(aggregate.id);
}
public deleteAggregateById (identitiy: UserId): void {
this.values.delete(identitiy);
}
public getAggregate (identitiy: UserId): User {
const v: User | undefined = this.values.get(identitiy);
if(v !== undefined)
return v;
throw new Error();
}
public getAggregates (): User[] {
return Array.from(this.values.values());
}
public storeAggregate (aggregate: User): User {
let id = aggregate.id;
if(id) {
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
id = new UserId(Math.random().toString(16).substr(2, 8));
// aggregate = User.builder().id(id).name(aggregate.name).password(aggregate.password).build();
}
this.values.set(id as UserId, aggregate);
return aggregate;
}
}

View File

@ -4,7 +4,7 @@ import { mapper } from "#framework/mapper/mapper";
export class RepoDto {
public id!: string;
public path!: string;
public name!: string;
}
createMap(mapper, Repo, RepoDto, forMember(
@ -13,6 +13,6 @@ createMap(mapper, Repo, RepoDto, forMember(
createMap(mapper, RepoDto, Repo,
constructUsing((sourceObject, _destinationIdentifier): Repo => {
return new Repo(new RepoId(sourceObject.id), sourceObject.path);
return new Repo(new RepoId(sourceObject.id));
})
);

View File

@ -1,14 +1,20 @@
/* eslint-disable @typescript-eslint/no-extraneous-class */
import {RepoRepository} from "#domain/repo/repo.repo";
import {UserRepositoryToken} from "#domain/user/user.repo";
import { Module } from "@nestjs/common";
import RepoFilePersistence from "./infrastructure/persistence/file/repo.persistence";
import UserFilePersistence from "./infrastructure/persistence/file/user.persistence";
@Module({
providers: [
{
useClass: RepoFilePersistence,
provide: RepoRepository
},
{
useClass: UserFilePersistence,
provide: UserRepositoryToken
}
],
exports: [RepoRepository]

View File

@ -7,17 +7,20 @@ import getLogger from "#logger";
import { Logger } from "winston";
import AppHelp from "./appHelp";
// TODO: move to App
const ENV_PREFIX = "ANT";
export abstract class AppCommand {
public abstract getDescriptions (): string;
public abstract getName (): string;
public abstract run (): void;
public abstract getName (): string;
}
export default abstract class App {
private readonly commands: Map<string, AppCommand> = new Map();
private readonly appHelp: AppHelp;
private readonly commands: Map<string, AppCommand> = new Map();
private readonly logger: Logger;
public constructor () {
@ -26,19 +29,22 @@ export default abstract class App {
this.appHelp = new AppHelp();
this.logger = getLogger("app");
this.addCommmand(new (class implements AppCommand {
public getName (): string {
return "help";
}
public getDescriptions (): string {
return "Show help for this application.";
}
public getName (): string {
return "help";
}
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.
@ -51,7 +57,7 @@ export default abstract class App {
console.log();
});
this.options((...defs): void => {
initConfiguration("XXX", ...defs);
initConfiguration(ENV_PREFIX, ...defs);
});
this.printBanner();
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 {
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

@ -8,65 +8,26 @@ import { AppCommand } from "#app";
/* The `AppHelp` class is responsible for printing out the help message */
export default class AppHelp {
/**
* Prints the help message
* @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 underline = clc.underline;
console.log(underline("Usage")+"\n");
console.log("\t"+ programm + " [COMMAND] [OPTIONS]"+"\n");
console.log(underline("Commands")+"\n");
console.log(underline("Usage") + "\n");
console.log("\t" + programm + " [COMMAND] [OPTIONS]" + "\n");
console.log(underline("Commands") + "\n");
this.printCommands(commands);
console.log();
console.log(underline("Options"));
console.log();
this.printOptions((def) => def.getCommands().length >= 1);
this.printOptions(envPrefix, (def) => def.getCommands().length >= 1);
console.log();
console.log(underline("Rules & Behavior"));
console.log();
this.printOptions((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());
}
}
this.printOptions(envPrefix, (def) => def.getCommands().length == 0);
}
/**
@ -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,105 @@
import Repo, { IRepoBuider, RepoId } from "#domain/repo/repo.entity";
import { UserAssignment, UserRight } from "#domain/repo/userAssignment.entity";
import { UserId } from "#domain/user/user.entity";
import { reflect, ReflectedArrayRef, ReflectedClassRef, ReflectedUnionRef } 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;
interface Foo {
[key: string]: unknown;
}
export function Builder<Type> (type: Constructor): IBuilder<Type> {
const parameters: { name: string; class: any }[] = [];
const properties = new Map<string, { name: string; class: any, aggregation?: string, parameter: boolean }>();
for (const property of reflect(type).properties) {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (!property.name.startsWith("_") && property.class.super !== null) {
if (property.type instanceof ReflectedClassRef) {
properties.set(property.name, { name: property.name, class: property.type.class, parameter: false });
}
else if (property.type instanceof ReflectedUnionRef) {
properties.set(property.name, { name: property.name, class: (<ReflectedClassRef<unknown>>property.type.types.filter(t => t instanceof ReflectedClassRef)[0]).class, parameter: false });
}
else if (property.type instanceof ReflectedArrayRef) {
properties.set(property.name, { name: property.name, class: (<ReflectedClassRef<unknown>>property.type.elementType).class, aggregation: "array", parameter: false });
}
}
}
for (const parameter of reflect(type).parameters) {
const name = parameter.rawMetadata.n;
parameters.push({ name: name, class: properties.get(parameter.name) });
if (properties.get(name) !== undefined)
(properties.get(name) as { parameter: boolean }).parameter = true;
}
const values = new Map<string, unknown>();
const builder = new Proxy(
{},
{
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
get (_target, prop) {
if (typeof prop === "string" && prop.startsWith("add")) {
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
const tprop = prop.charAt(3).toLocaleLowerCase() + prop.substring(4);
return (x: unknown): unknown => {
if (properties.get(tprop)?.aggregation === "array") {
if (!values.has(tprop))
values.set(tprop, []);
(values.get(tprop) as Array<unknown>).push(x);
} else {
values.set(tprop, x);
}
return builder;
};
} else if ("build" === prop) {
const params: unknown[] = [];
for (const param of parameters) {
params.push(values.get(param.name));
}
const obj: Foo = new type(params);
for (const propertyname of properties.keys()) {
const prop = properties.get(propertyname);
if (values.has(propertyname) && !prop?.parameter) {
if (prop?.aggregation === "array") {
const desc = Object.getOwnPropertyDescriptor(obj, propertyname);
if (desc?.set === undefined) {
const methodeName = "add" + propertyname.charAt(0).toUpperCase() + propertyname.slice(1);
for (const value of (values.get(propertyname) as Array<unknown>)) {
(obj[methodeName] as ((t: unknown) => unknown))(value);
}
} else {
obj[propertyname] = values.get(propertyname);
}
} else {
obj[propertyname] = values.get(propertyname);
}
}
}
return () => obj;
}
return (x: unknown): unknown => {
values.set(prop.toString(), x);
return builder;
};
}
}
);
return builder as IBuilder<Type>;
}
const ua1 = new UserAssignment(new UserId("test"), UserRight.Read);
const ua2 = new UserAssignment(new UserId("test2"), UserRight.Read);
const builder = Builder<IRepoBuider>(Repo);
const x = builder.id(new RepoId("test")).name("name").addUserAssignment(ua1).addUserAssignment(ua2).build();
console.log(x);

View File

@ -2,12 +2,12 @@ import ConfigurationDefinition, {DefinitionType} from "./configurationDefinition
import {parser, ArgCollection} from "args-command-parser";
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 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[]) {
this.arguments = parser();
for (const definition of definitions) {
@ -18,23 +18,22 @@ class Configuration {
this.envPrefix = envPrefix;
}
public validate (): void {
this.definitions.forEach((v, _k, _m) => {
const def = v;
const values = this.values.get(def.getName()) ?? [];
if(!def.getMultiple() && values.length > 1)
throw new Error("No more than one value may be assigned to parameter '" + def.getName() + "'.");
if(def.getRequired() && (def.getCommands().length === 0 || def.getCommands().includes(this.getCommand())) && values.length === 0)
throw new Error("Parameter '" + def.getName() + "' is required.");
for (const value of values) {
if(!def.getValidator().validate(value))
throw new Error("Parameter '" + def.getName() + "' does not meet the requirements: " + def.getValidator().description());
/**
* Get the value of a boolean parameter
* @param {string} name - The name of the parameter.
* @returns The value of the parameter.
*/
public getBooleanValue (name: string): boolean | boolean[] | undefined {
const def = this.definitions.get(name);
if(def?.getType() == DefinitionType.Boolean) {
const value = this.values.get(name) ?? [];
const result: boolean[] = this.toBoolean(value);
if(def.getMultiple()) {
return result;
}
});
}
public getDefinitions (): ConfigurationDefinition[] {
return Array.from(this.definitions.values());
return result[0];
}
return undefined;
}
/**
@ -48,6 +47,35 @@ class Configuration {
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
* @param {string} name - The name of the parameter.
@ -80,54 +108,56 @@ class Configuration {
}
}
/**
* Get the value of a boolean parameter
* @param {string} name - The name of the parameter.
* @returns The value of the parameter.
*/
public getBooleanValue (name: string): boolean | boolean[] | undefined {
const def = this.definitions.get(name);
if(def?.getType() == DefinitionType.Boolean) {
const value = this.values.get(name) ?? [];
const result: boolean[] = this.toBoolean(value);
if(def.getMultiple()) {
return result;
public validate (): void {
this.definitions.forEach((v, _k, _m) => {
const def = v;
const values = this.values.get(def.getName()) ?? [];
if(!def.getMultiple() && values.length > 1)
throw new Error("No more than one value may be assigned to parameter '" + def.getName() + "'.");
if(def.getRequired() && (def.getCommands().length === 0 || def.getCommands().includes(this.getCommand())) && values.length === 0)
throw new Error("Parameter '" + def.getName() + "' is required.");
for (const value of values) {
if(!def.getValidator().validate(value))
throw new Error("Parameter '" + def.getName() + "' does not meet the requirements: " + def.getValidator().description());
}
});
}
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;
}
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
* @param {string[]} value - The value of the parameter.
* @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[] {
const result: boolean[] = [];
for (const v of value) {
@ -135,7 +165,7 @@ class Configuration {
}
return result;
}
/**
* It takes an array of strings and returns an array of numbers.
* @param {string[]} value - The value of the parameter.
@ -150,34 +180,6 @@ class Configuration {
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;

View File

@ -1,55 +1,10 @@
/* eslint-disable @typescript-eslint/require-await */
import { FastifyLogFn, FastifyLoggerInstance, FastifyPluginAsync, FastifyRequest} from "fastify";
import { FastifyPluginAsync, FastifyRequest} from "fastify";
import fastifyPlugin from "fastify-plugin";
import { Bindings } from "fastify/types/logger";
import getlogger from "./logger";
import chalk from "chalk";
import logadapter from "./pinoLoggerAdapter";
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
}
};
// const logger = getlogger("fastify");
export type FastifyRequestLoggerOptions = {
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, {
fastify: "3.x",
fastify: "4.x",
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 */
import { LoggerService } from "@nestjs/common";
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 getlogger from "#logger";
import {fastifyLogger, fastifyRequestLogger} from "#logger/fastifyLogger";
import {fastifyRequestLogger, fastifyLogger} from "#logger/fastifyLogger";
import { Logger } from "winston";
import IGracefulServer from "@gquittet/graceful-server/lib/types/interface/gracefulServer";
import configuration from "#configuration";
import { NestFactory } from "@nestjs/core";
import { FastifyAdapter, NestFastifyApplication } from "@nestjs/platform-fastify";
import { AppModule } from "../../app.module";
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 {
private readonly logger: Logger;
@ -40,7 +46,8 @@ export default class Server {
new FastifyAdapter({
logger: fastifyLogger,
disableRequestLogging: true
}), {
}),
{
logger: new NestLogger(),
}
);

View File

@ -1,4 +1,4 @@
import tsconfig from '../tsconfig.json';
import tsconfig from '../tsconfig.json' assert {type: "json"};;
import * as path from 'path';
import { fileURLToPath } from 'url'
import { createRequire } from 'module'

View File

@ -10,6 +10,7 @@
"baseUrl": "./src/",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"noImplicitAny": true,
"paths": {
"#ddd": ["framework/ddd/ddd.types"],
"#framework/*": ["framework/*"],
@ -24,7 +25,8 @@
{
"transform": "@automapper/classes/transformer-plugin",
"modelFileNameSuffix": [".types.js", ".entity.ts", ".dto.ts"]
}
},
{ "transform": "typescript-rtti/dist/transformer" }
]
},
"tsc-alias": {

1236
yarn.lock

File diff suppressed because it is too large Load Diff