@ccci/micro-server 1.0.45 → 1.0.47

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -87526,7 +87526,7 @@ var logger = import_winston.default.createLogger({
87526
87526
  if (typeof info.message === "object") {
87527
87527
  info.message = JSON.stringify(info.message, null, 3);
87528
87528
  }
87529
- return `${info.timestamp} - [${info.level}]: ${info.message} ${JSON.stringify(info.metadata)}`;
87529
+ return `[${info.level}]${info.timestamp}: ${info.message}`;
87530
87530
  })),
87531
87531
  transports: [new import_winston.default.transports.Console]
87532
87532
  });
@@ -87637,32 +87637,35 @@ var ISSUER = process.env.JWT_ISSUER ? process.env.JWT_ISSUER : "*.service";
87637
87637
  var UNSECURED_DIR = ["dev", "public"];
87638
87638
 
87639
87639
  class RouterFactory {
87640
- constructor(app, pathName, folderName = "routes", context = "api") {
87641
- try {
87642
- Logger.info("Create API Routes...\n");
87643
- fs2.readdirSync(folderName).forEach((dir) => {
87644
- let isUnsecure = UNSECURED_DIR.findIndex((x) => x === dir) > -1;
87645
- var routerDir = path.join(folderName, dir);
87646
- fs2.readdirSync(routerDir).forEach((file) => {
87647
- let fullName = path.join(routerDir, file);
87648
- if (file.toLowerCase().indexOf(".js") || file.toLowerCase().indexOf(".ts")) {
87649
- let fileName = fullName.replace(folderName, context).replace(".js", "").replace(".ts", "").split("\\").join("/");
87650
- let endpoint = `/${fileName}`;
87651
- let routeFile = `${pathName}/${fullName}`;
87652
- import(routeFile).then((routerClass) => {
87653
- let router = new routerClass.default;
87654
- app.use(endpoint, (req, res, next) => this.authenticate(req, res, next, isUnsecure || router.grantPublicAccess), router.getRoutes());
87655
- Logger.info("Initializing endpoint:", endpoint);
87656
- });
87657
- }
87640
+ static async init(app, pathName, folderName = "routes", context = "api") {
87641
+ return new Promise((resolve, reject) => {
87642
+ try {
87643
+ Logger.info("Create API Routes...\n");
87644
+ fs2.readdirSync(folderName).forEach((dir) => {
87645
+ let isUnsecure = UNSECURED_DIR.findIndex((x) => x === dir) > -1;
87646
+ var routerDir = path.join(folderName, dir);
87647
+ fs2.readdirSync(routerDir).forEach((file) => {
87648
+ let fullName = path.join(routerDir, file);
87649
+ if (file.toLowerCase().indexOf(".js") || file.toLowerCase().indexOf(".ts")) {
87650
+ let fileName = fullName.replace(folderName, context).replace(".js", "").replace(".ts", "").split("\\").join("/");
87651
+ let endpoint = `/${fileName}`;
87652
+ let routeFile = `${pathName}/${fullName}`;
87653
+ import(routeFile).then((routerClass) => {
87654
+ let router = new routerClass.default;
87655
+ app.use(endpoint, (req, res, next) => RouterFactory.authenticate(req, res, next, isUnsecure || router.grantPublicAccess), router.getRoutes());
87656
+ Logger.info("Initializing endpoint:", endpoint);
87657
+ });
87658
+ }
87659
+ });
87660
+ resolve();
87658
87661
  });
87659
- });
87660
- } catch (error) {
87661
- Logger.error(error);
87662
- throw error;
87663
- }
87662
+ } catch (error) {
87663
+ Logger.error(error);
87664
+ reject(error);
87665
+ }
87666
+ });
87664
87667
  }
87665
- authenticate(req, res, next, grantPublicAccess = false) {
87668
+ static authenticate(req, res, next, grantPublicAccess = false) {
87666
87669
  try {
87667
87670
  let bearerToken = req.headers.authorization;
87668
87671
  if (bearerToken && bearerToken != null && bearerToken != "null") {
@@ -87741,7 +87744,6 @@ class ApplicationServer {
87741
87744
  static app;
87742
87745
  static async bootstrap(port, options) {
87743
87746
  ApplicationServer.app = import_express.default();
87744
- Logger.info("Setting up application middlewares...\n");
87745
87747
  ApplicationServer.app.use(import_cors.default());
87746
87748
  ApplicationServer.app.use(helmet());
87747
87749
  ApplicationServer.app.use(import_express.default.json());
@@ -87752,13 +87754,13 @@ class ApplicationServer {
87752
87754
  ApplicationServer.app.get("/", (req, resp) => {
87753
87755
  return resp.sendFile(path3.resolve("index.html"));
87754
87756
  });
87755
- new RouterFactory(ApplicationServer.app, process.cwd(), "routes");
87757
+ RouterFactory.init(ApplicationServer.app, process.cwd(), "routes");
87756
87758
  const server = ApplicationServer.app.listen(port, () => {
87757
87759
  Logger.info(`API Sever ready at: http://127.0.0.1:${port}\n`);
87760
+ if (options?.enableWebSocket) {
87761
+ new WebSocketServer(options?.websocketPort, process.cwd(), options?.websocketDir);
87762
+ }
87758
87763
  });
87759
- if (options?.enableWebSocket) {
87760
- new WebSocketServer(options?.websocketPort, process.cwd(), options?.websocketDir);
87761
- }
87762
87764
  }
87763
87765
  getInstance() {
87764
87766
  return ApplicationServer.app;
@@ -6,7 +6,7 @@ export default class RouterFactory {
6
6
  * @param pathName root path of the application
7
7
  * @param folderName path of the routers (default: routes)
8
8
  */
9
- constructor(app: Application, pathName: string, folderName?: string, context?: string);
10
- authenticate(req: Request, res: Response, next: NextFunction, grantPublicAccess?: boolean): Response<any, Record<string, any>> | undefined;
9
+ static init(app: Application, pathName: string, folderName?: string, context?: string): Promise<void>;
10
+ static authenticate(req: Request, res: Response, next: NextFunction, grantPublicAccess?: boolean): Response<any, Record<string, any>> | undefined;
11
11
  }
12
12
  //# sourceMappingURL=RouterFactory.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ccci/micro-server",
3
- "version": "1.0.45",
3
+ "version": "1.0.47",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",