@arkstack/driver-express 0.2.4 → 0.3.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Toneflix Technologies Limited
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { ErrorRequestHandler, Express, Handler } from "express";
2
- import { ArkstackKitDriver, PromiseOrValue } from "@arkstack/contract";
2
+ import { ArkstackKitDriver, ArkstackMiddlewareConfig, PromiseOrValue } from "@arkstack/contract";
3
3
 
4
4
  //#region src/index.d.ts
5
5
  interface ExpressDriverOptions {
@@ -13,7 +13,7 @@ declare class ExpressDriver extends ArkstackKitDriver<Express, Handler> {
13
13
  createApp(): Express;
14
14
  mountPublicAssets(app: Express, publicPath: string): void;
15
15
  bindRouter(app: Express): PromiseOrValue<void>;
16
- applyMiddleware(app: Express, middleware: Handler): void;
16
+ applyMiddleware(app: Express, middleware: Handler | ArkstackMiddlewareConfig<Handler>): void;
17
17
  registerErrorHandler(app: Express): void;
18
18
  start(app: Express, port: number): void;
19
19
  }
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import express from "express";
2
2
  import { ArkstackKitDriver } from "@arkstack/contract";
3
+ import { Logger } from "@arkstack/common";
3
4
 
4
5
  //#region src/index.ts
5
6
  /**
@@ -50,7 +51,17 @@ var ExpressDriver = class extends ArkstackKitDriver {
50
51
  * @param middleware
51
52
  */
52
53
  applyMiddleware(app, middleware) {
53
- app.use(middleware);
54
+ if (typeof middleware === "function") {
55
+ app.use(middleware);
56
+ return;
57
+ }
58
+ for (const [pos, entries] of Object.entries(middleware)) for (const entry of entries) if (pos === "after") app.use(async (req, res, next) => {
59
+ res.once("finish", async () => {
60
+ await entry(req, res, next);
61
+ });
62
+ next();
63
+ });
64
+ else app.use(entry);
54
65
  }
55
66
  /**
56
67
  * Registers an error handler middleware to the Express
@@ -69,7 +80,7 @@ var ExpressDriver = class extends ArkstackKitDriver {
69
80
  */
70
81
  start(app, port) {
71
82
  app.listen(port, () => {
72
- console.log(`Server is running on http://localhost:${port}`);
83
+ Logger.log([["Server is running on", "white"], [`http://localhost:${port}`, "cyan"]], " ");
73
84
  });
74
85
  }
75
86
  };
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["import express, { type ErrorRequestHandler, type Express, type Handler } from 'express'\n\nimport { ArkstackKitDriver, PromiseOrValue } from '@arkstack/contract'\n\nexport interface ExpressDriverOptions {\n bindRouter: (app: Express) => PromiseOrValue<void>;\n errorHandler?: ErrorRequestHandler | Handler;\n}\n\n/**\n * The ExpressDriver class implements the ArkstackKitDriver \n * contract for the Express framework.\n */\nexport class ExpressDriver extends ArkstackKitDriver<Express, Handler> {\n readonly name = 'express'\n private readonly options: ExpressDriverOptions\n\n /**\n * Creates an instance of ExpressDriver.\n * \n * @param options \n */\n constructor(options: ExpressDriverOptions) {\n super()\n this.options = options\n }\n\n /**\n * Creates an Express application instance.\n * \n * @returns \n */\n createApp (): Express {\n return express()\n }\n\n /**\n * Mounts static assets from the specified public path to the Express application.\n * \n * @param app \n * @param publicPath \n */\n mountPublicAssets (app: Express, publicPath: string): void {\n app.use(express.static(publicPath))\n }\n\n /**\n * Binds the router to the Express application using the provided bindRouter function.\n * \n * @param app \n */\n bindRouter (app: Express): PromiseOrValue<void> {\n return this.options.bindRouter(app)\n }\n\n /**\n * Applies middleware to the Express application.\n * \n * @param app \n * @param middleware \n */\n applyMiddleware (app: Express, middleware: Handler): void {\n app.use(middleware)\n }\n\n /**\n * Registers an error handler middleware to the Express \n * application if provided in the options.\n * \n * @param app \n */\n registerErrorHandler (app: Express): void {\n if (this.options.errorHandler) {\n app.use(this.options.errorHandler as ErrorRequestHandler)\n }\n }\n\n /**\n * Starts the Express server on the specified port.\n * \n * @param app \n * @param port \n */\n start (app: Express, port: number): void {\n app.listen(port, () => {\n console.log(`Server is running on http://localhost:${port}`)\n })\n }\n}\n"],"mappings":";;;;;;;;AAaA,IAAa,gBAAb,cAAmC,kBAAoC;CACnE,AAAS,OAAO;CAChB,AAAiB;;;;;;CAOjB,YAAY,SAA+B;AACvC,SAAO;AACP,OAAK,UAAU;;;;;;;CAQnB,YAAsB;AAClB,SAAO,SAAS;;;;;;;;CASpB,kBAAmB,KAAc,YAA0B;AACvD,MAAI,IAAI,QAAQ,OAAO,WAAW,CAAC;;;;;;;CAQvC,WAAY,KAAoC;AAC5C,SAAO,KAAK,QAAQ,WAAW,IAAI;;;;;;;;CASvC,gBAAiB,KAAc,YAA2B;AACtD,MAAI,IAAI,WAAW;;;;;;;;CASvB,qBAAsB,KAAoB;AACtC,MAAI,KAAK,QAAQ,aACb,KAAI,IAAI,KAAK,QAAQ,aAAoC;;;;;;;;CAUjE,MAAO,KAAc,MAAoB;AACrC,MAAI,OAAO,YAAY;AACnB,WAAQ,IAAI,yCAAyC,OAAO;IAC9D"}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["import express, { type ErrorRequestHandler, type Express, type Handler } from 'express'\n\nimport { ArkstackKitDriver, ArkstackMiddlewareConfig, PromiseOrValue } from '@arkstack/contract'\nimport { Logger } from '@arkstack/common'\n\nexport interface ExpressDriverOptions {\n bindRouter: (app: Express) => PromiseOrValue<void>;\n errorHandler?: ErrorRequestHandler | Handler;\n}\n\n/**\n * The ExpressDriver class implements the ArkstackKitDriver \n * contract for the Express framework.\n */\nexport class ExpressDriver extends ArkstackKitDriver<Express, Handler> {\n readonly name = 'express'\n private readonly options: ExpressDriverOptions\n\n /**\n * Creates an instance of ExpressDriver.\n * \n * @param options \n */\n constructor(options: ExpressDriverOptions) {\n super()\n this.options = options\n }\n\n /**\n * Creates an Express application instance.\n * \n * @returns \n */\n createApp (): Express {\n return express()\n }\n\n /**\n * Mounts static assets from the specified public path to the Express application.\n * \n * @param app \n * @param publicPath \n */\n mountPublicAssets (app: Express, publicPath: string): void {\n app.use(express.static(publicPath))\n }\n\n /**\n * Binds the router to the Express application using the provided bindRouter function.\n * \n * @param app \n */\n bindRouter (app: Express): PromiseOrValue<void> {\n return this.options.bindRouter(app)\n }\n\n /**\n * Applies middleware to the Express application.\n * \n * @param app \n * @param middleware \n */\n applyMiddleware (\n app: Express,\n middleware: Handler | ArkstackMiddlewareConfig<Handler>,\n ): void {\n if (typeof middleware === 'function') {\n app.use(middleware)\n\n return\n }\n\n for (const [pos, entries] of Object.entries(middleware) as [string, Handler[]][]) {\n for (const entry of entries) {\n if (pos === 'after') {\n app.use(async (req, res, next) => {\n res.once('finish', async () => {\n await entry(req, res, next)\n })\n next()\n })\n } else {\n app.use(entry)\n }\n }\n }\n }\n\n /**\n * Registers an error handler middleware to the Express \n * application if provided in the options.\n * \n * @param app \n */\n registerErrorHandler (app: Express): void {\n if (this.options.errorHandler) {\n app.use(this.options.errorHandler as ErrorRequestHandler)\n }\n }\n\n /**\n * Starts the Express server on the specified port.\n * \n * @param app \n * @param port \n */\n start (app: Express, port: number): void {\n app.listen(port, () => {\n Logger.log([\n ['Server is running on', 'white'],\n [`http://localhost:${port}`, 'cyan']\n ], ' ')\n })\n }\n}\n"],"mappings":";;;;;;;;;AAcA,IAAa,gBAAb,cAAmC,kBAAoC;CACnE,AAAS,OAAO;CAChB,AAAiB;;;;;;CAOjB,YAAY,SAA+B;AACvC,SAAO;AACP,OAAK,UAAU;;;;;;;CAQnB,YAAsB;AAClB,SAAO,SAAS;;;;;;;;CASpB,kBAAmB,KAAc,YAA0B;AACvD,MAAI,IAAI,QAAQ,OAAO,WAAW,CAAC;;;;;;;CAQvC,WAAY,KAAoC;AAC5C,SAAO,KAAK,QAAQ,WAAW,IAAI;;;;;;;;CASvC,gBACI,KACA,YACI;AACJ,MAAI,OAAO,eAAe,YAAY;AAClC,OAAI,IAAI,WAAW;AAEnB;;AAGJ,OAAK,MAAM,CAAC,KAAK,YAAY,OAAO,QAAQ,WAAW,CACnD,MAAK,MAAM,SAAS,QAChB,KAAI,QAAQ,QACR,KAAI,IAAI,OAAO,KAAK,KAAK,SAAS;AAC9B,OAAI,KAAK,UAAU,YAAY;AAC3B,UAAM,MAAM,KAAK,KAAK,KAAK;KAC7B;AACF,SAAM;IACR;MAEF,KAAI,IAAI,MAAM;;;;;;;;CAY9B,qBAAsB,KAAoB;AACtC,MAAI,KAAK,QAAQ,aACb,KAAI,IAAI,KAAK,QAAQ,aAAoC;;;;;;;;CAUjE,MAAO,KAAc,MAAoB;AACrC,MAAI,OAAO,YAAY;AACnB,UAAO,IAAI,CACP,CAAC,wBAAwB,QAAQ,EACjC,CAAC,oBAAoB,QAAQ,OAAO,CACvC,EAAE,IAAI;IACT"}
@@ -0,0 +1,11 @@
1
+ import { NextFunction, Request, Response } from "express";
2
+
3
+ //#region src/middlewares/request-logger.d.ts
4
+ declare const requestLogger: ({
5
+ allowInProduction
6
+ }?: {
7
+ allowInProduction?: boolean;
8
+ }) => (req: Request, res: Response, next: NextFunction) => Promise<void>;
9
+ //#endregion
10
+ export { requestLogger };
11
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,34 @@
1
+ import { Logger, nodeEnv } from "@arkstack/common";
2
+
3
+ //#region src/middlewares/request-logger.ts
4
+ const colors = {
5
+ GET: "green",
6
+ POST: "blue",
7
+ PUT: "yellow",
8
+ DELETE: "red",
9
+ PATCH: "cyan"
10
+ };
11
+ /**
12
+ * Middleware to log incoming requests and their response times.
13
+ *
14
+ * @param config Configuration options for the request logger middleware.
15
+ * @param config.allowInProduction If true, the logger will also log requests in production environment. Default is false.
16
+ * @returns
17
+ */
18
+ const requestLogger = ({ allowInProduction = false } = {}) => async (req, res, next) => {
19
+ if (nodeEnv() === "prod" && !allowInProduction) return next();
20
+ const start = Date.now();
21
+ const status = res.statusCode || 200;
22
+ const duration = Date.now() - start;
23
+ Logger.log([
24
+ [`[${req.method}]`, colors[req.method] || "white"],
25
+ [req.url, "cyan"],
26
+ [status.toString(), status >= 500 ? "red" : status >= 400 ? "yellow" : "green"],
27
+ [`- ${duration}ms`, "dim"]
28
+ ], " ");
29
+ next();
30
+ };
31
+
32
+ //#endregion
33
+ export { requestLogger };
34
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../../src/middlewares/request-logger.ts"],"sourcesContent":["import { Logger, nodeEnv } from '@arkstack/common'\nimport { NextFunction, Request, Response } from 'express'\n\nconst colors: Record<string, 'green' | 'blue' | 'yellow' | 'red' | 'cyan'> = {\n GET: 'green',\n POST: 'blue',\n PUT: 'yellow',\n DELETE: 'red',\n PATCH: 'cyan',\n}\n\n/**\n * Middleware to log incoming requests and their response times.\n * \n * @param config Configuration options for the request logger middleware.\n * @param config.allowInProduction If true, the logger will also log requests in production environment. Default is false. \n * @returns \n */\nexport const requestLogger = ({\n allowInProduction = false,\n}: {\n allowInProduction?: boolean\n} = {}) => async (req: Request, res: Response, next: NextFunction) => {\n if (nodeEnv() === 'prod' && !allowInProduction) return next()\n\n const start = Date.now()\n\n const status = res.statusCode || 200\n const duration = Date.now() - start\n Logger.log([\n [`[${req.method}]`, colors[req.method] || 'white'],\n [req.url, 'cyan'],\n [status.toString(), status >= 500 ? 'red' : status >= 400 ? 'yellow' : 'green'],\n [`- ${duration}ms`, 'dim']\n ], ' ')\n\n next()\n}"],"mappings":";;;AAGA,MAAM,SAAuE;CACzE,KAAK;CACL,MAAM;CACN,KAAK;CACL,QAAQ;CACR,OAAO;CACV;;;;;;;;AASD,MAAa,iBAAiB,EAC1B,oBAAoB,UAGpB,EAAE,KAAK,OAAO,KAAc,KAAe,SAAuB;AAClE,KAAI,SAAS,KAAK,UAAU,CAAC,kBAAmB,QAAO,MAAM;CAE7D,MAAM,QAAQ,KAAK,KAAK;CAExB,MAAM,SAAS,IAAI,cAAc;CACjC,MAAM,WAAW,KAAK,KAAK,GAAG;AAC9B,QAAO,IAAI;EACP,CAAC,IAAI,IAAI,OAAO,IAAI,OAAO,IAAI,WAAW,QAAQ;EAClD,CAAC,IAAI,KAAK,OAAO;EACjB,CAAC,OAAO,UAAU,EAAE,UAAU,MAAM,QAAQ,UAAU,MAAM,WAAW,QAAQ;EAC/E,CAAC,KAAK,SAAS,KAAK,MAAM;EAC7B,EAAE,IAAI;AAEP,OAAM"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arkstack/driver-express",
3
- "version": "0.2.4",
3
+ "version": "0.3.1",
4
4
  "type": "module",
5
5
  "description": "Express driver package for Arkstack providing Express-specific implementations of core Arkstack features such as routing, middleware, and database integration.",
6
6
  "homepage": "https://arkstack.toneflix.net",
@@ -32,13 +32,15 @@
32
32
  },
33
33
  "exports": {
34
34
  ".": "./dist/index.js",
35
+ "./middlewares": "./dist/middlewares/index.js",
35
36
  "./package.json": "./package.json"
36
37
  },
37
38
  "dependencies": {
38
- "@arkstack/contract": "^0.2.4"
39
+ "@arkstack/contract": "^0.3.1"
39
40
  },
40
41
  "peerDependencies": {
41
- "express": "^5.2.1"
42
+ "express": "^5.2.1",
43
+ "@arkstack/common": "^0.3.1"
42
44
  },
43
45
  "devDependencies": {
44
46
  "@types/express": "^5.0.6"