@wooksjs/express-adapter 0.2.1 → 0.4.8

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/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  **!!! This is work-in-progress library, breaking changes are expected !!!**
4
4
 
5
5
  <p align="center">
6
- <img src="./docs/wooks-logo.png" height="156px"><br>
6
+ <img src="./docs/wooksjs-express.png" height="156px"><br>
7
7
  <a href="https://github.com/wooksjs/express-adapter/blob/main/LICENSE">
8
8
  <img src="https://img.shields.io/badge/License-MIT-green?style=for-the-badge" />
9
9
  </a>
@@ -59,7 +59,7 @@ import { useBody } from '@wooksjs/http-body'
59
59
  import { HttpError } from '@wooksjs/event-http'
60
60
  import { useRouteParams } from '@wooksjs/event-core'
61
61
 
62
- const expressApp = Express()
62
+ const expressApp = express()
63
63
 
64
64
  const wooksApp = new WooksExpress(expressApp, { raise404: true })
65
65
 
@@ -79,3 +79,6 @@ wooksApp.get('/error', () => {
79
79
 
80
80
  wooksApp.listen(3000, () => console.log('listening 3000'))
81
81
  ```
82
+
83
+
84
+ ⚠️ Check out official [wooks documentation](https://wooksjs.org/guide/http/express.html).
package/dist/index.cjs CHANGED
@@ -12,7 +12,7 @@ const methods = [
12
12
  "head",
13
13
  "all"
14
14
  ];
15
- function applyExpressAdapter(app) {
15
+ function applyExpressAdapter(app, eventOptions) {
16
16
  const responder = eventHttp.createWooksResponder();
17
17
  function useWooksDecorator(fn) {
18
18
  return async () => {
@@ -28,7 +28,7 @@ function applyExpressAdapter(app) {
28
28
  clearCtx();
29
29
  };
30
30
  }
31
- app.use(wooksContext);
31
+ app.use(wooksContext(eventOptions));
32
32
  for (const m of methods) {
33
33
  const defFn = app[m].bind(app);
34
34
  const newFn = ((...args) => {
@@ -37,24 +37,26 @@ function applyExpressAdapter(app) {
37
37
  Object.defineProperty(app, m, { value: newFn });
38
38
  }
39
39
  }
40
- function wooksContext(req, res, next) {
41
- const { store } = eventHttp.createHttpContext({ req, res });
42
- store("routeParams").value = new Proxy({}, {
43
- get(target, prop, receiver) {
44
- return req.params && req.params[prop];
45
- }
46
- });
47
- next();
40
+ function wooksContext(eventOptions) {
41
+ return (req, res, next) => {
42
+ const { store } = eventHttp.createHttpContext({ req, res }, eventOptions || {});
43
+ store("routeParams").value = new Proxy({}, {
44
+ get(target, prop, receiver) {
45
+ return req.params && req.params[prop];
46
+ }
47
+ });
48
+ next();
49
+ };
48
50
  }
49
51
 
50
52
  class WooksExpress extends eventHttp.WooksHttp {
51
53
  constructor(expressApp, opts) {
52
- super();
54
+ super(opts);
53
55
  this.expressApp = expressApp;
54
56
  this.opts = opts;
55
57
  expressApp.use(this.getServerCb());
56
58
  }
57
- async listen(...args) {
59
+ listen(...args) {
58
60
  const server = this.server = this.expressApp.listen(...args);
59
61
  return new Promise((resolve, reject) => {
60
62
  server.once("listening", resolve);
@@ -63,11 +65,14 @@ class WooksExpress extends eventHttp.WooksHttp {
63
65
  }
64
66
  getServerCb() {
65
67
  return async (req, res, next) => {
66
- const { restoreCtx, clearCtx } = eventHttp.createHttpContext({ req, res });
67
- const handlers = this.wooks.lookup(req.method, req.url);
68
- if (handlers) {
68
+ const { restoreCtx, clearCtx } = eventHttp.createHttpContext(
69
+ { req, res },
70
+ this.mergeEventOptions(this.opts?.eventOptions)
71
+ );
72
+ const { handlers } = this.wooks.lookup(req.method, req.url);
73
+ if (handlers || this.opts?.onNotFound) {
69
74
  try {
70
- await this.processHandlers(handlers);
75
+ await this.processHandlers(handlers || [this.opts?.onNotFound]);
71
76
  } catch (e) {
72
77
  console.error("Internal error, please report: ", e);
73
78
  if (e.stack) {
@@ -78,6 +83,9 @@ class WooksExpress extends eventHttp.WooksHttp {
78
83
  clearCtx();
79
84
  }
80
85
  } else {
86
+ this.logger.debug(
87
+ `404 Not found (${req.method})${req.url}`
88
+ );
81
89
  if (this.opts?.raise404) {
82
90
  this.respond(new eventHttp.HttpError(404));
83
91
  clearCtx();
package/dist/index.d.ts CHANGED
@@ -1,17 +1,17 @@
1
+ import { TWooksHttpOptions, WooksHttp } from '@wooksjs/event-http';
1
2
  import Express$1, { Express } from 'express';
2
- import { WooksHttp } from '@wooksjs/event-http';
3
3
  import { Server, IncomingMessage, ServerResponse } from 'http';
4
4
 
5
- declare function applyExpressAdapter(app: Express): void;
5
+ declare function applyExpressAdapter(app: Express, eventOptions?: TWooksHttpOptions['eventOptions']): void;
6
6
 
7
7
  declare class WooksExpress extends WooksHttp {
8
8
  protected expressApp: Express$1.Application;
9
- protected opts?: {
9
+ protected opts?: (TWooksHttpOptions & {
10
10
  raise404?: boolean | undefined;
11
- } | undefined;
12
- constructor(expressApp: Express$1.Application, opts?: {
11
+ }) | undefined;
12
+ constructor(expressApp: Express$1.Application, opts?: (TWooksHttpOptions & {
13
13
  raise404?: boolean | undefined;
14
- } | undefined);
14
+ }) | undefined);
15
15
  listen(...args: Parameters<Server['listen']>): Promise<unknown>;
16
16
  getServerCb(): (req: IncomingMessage, res: ServerResponse, next?: Express$1.NextFunction) => Promise<void>;
17
17
  }
package/dist/index.mjs CHANGED
@@ -10,7 +10,7 @@ const methods = [
10
10
  "head",
11
11
  "all"
12
12
  ];
13
- function applyExpressAdapter(app) {
13
+ function applyExpressAdapter(app, eventOptions) {
14
14
  const responder = createWooksResponder();
15
15
  function useWooksDecorator(fn) {
16
16
  return async () => {
@@ -26,7 +26,7 @@ function applyExpressAdapter(app) {
26
26
  clearCtx();
27
27
  };
28
28
  }
29
- app.use(wooksContext);
29
+ app.use(wooksContext(eventOptions));
30
30
  for (const m of methods) {
31
31
  const defFn = app[m].bind(app);
32
32
  const newFn = ((...args) => {
@@ -35,24 +35,26 @@ function applyExpressAdapter(app) {
35
35
  Object.defineProperty(app, m, { value: newFn });
36
36
  }
37
37
  }
38
- function wooksContext(req, res, next) {
39
- const { store } = createHttpContext({ req, res });
40
- store("routeParams").value = new Proxy({}, {
41
- get(target, prop, receiver) {
42
- return req.params && req.params[prop];
43
- }
44
- });
45
- next();
38
+ function wooksContext(eventOptions) {
39
+ return (req, res, next) => {
40
+ const { store } = createHttpContext({ req, res }, eventOptions || {});
41
+ store("routeParams").value = new Proxy({}, {
42
+ get(target, prop, receiver) {
43
+ return req.params && req.params[prop];
44
+ }
45
+ });
46
+ next();
47
+ };
46
48
  }
47
49
 
48
50
  class WooksExpress extends WooksHttp {
49
51
  constructor(expressApp, opts) {
50
- super();
52
+ super(opts);
51
53
  this.expressApp = expressApp;
52
54
  this.opts = opts;
53
55
  expressApp.use(this.getServerCb());
54
56
  }
55
- async listen(...args) {
57
+ listen(...args) {
56
58
  const server = this.server = this.expressApp.listen(...args);
57
59
  return new Promise((resolve, reject) => {
58
60
  server.once("listening", resolve);
@@ -61,11 +63,14 @@ class WooksExpress extends WooksHttp {
61
63
  }
62
64
  getServerCb() {
63
65
  return async (req, res, next) => {
64
- const { restoreCtx, clearCtx } = createHttpContext({ req, res });
65
- const handlers = this.wooks.lookup(req.method, req.url);
66
- if (handlers) {
66
+ const { restoreCtx, clearCtx } = createHttpContext(
67
+ { req, res },
68
+ this.mergeEventOptions(this.opts?.eventOptions)
69
+ );
70
+ const { handlers } = this.wooks.lookup(req.method, req.url);
71
+ if (handlers || this.opts?.onNotFound) {
67
72
  try {
68
- await this.processHandlers(handlers);
73
+ await this.processHandlers(handlers || [this.opts?.onNotFound]);
69
74
  } catch (e) {
70
75
  console.error("Internal error, please report: ", e);
71
76
  if (e.stack) {
@@ -76,6 +81,9 @@ class WooksExpress extends WooksHttp {
76
81
  clearCtx();
77
82
  }
78
83
  } else {
84
+ this.logger.debug(
85
+ `404 Not found (${req.method})${req.url}`
86
+ );
79
87
  if (this.opts?.raise404) {
80
88
  this.respond(new HttpError(404));
81
89
  clearCtx();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wooksjs/express-adapter",
3
- "version": "0.2.1",
3
+ "version": "0.4.8",
4
4
  "description": "Express Adapter for Wooks Composables",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.mjs",
@@ -59,7 +59,8 @@
59
59
  },
60
60
  "homepage": "https://github.com/wooksjs/express-adapter#readme",
61
61
  "peerDependencies": {
62
- "@wooksjs/event-http": "^0.2.9",
62
+ "@wooksjs/event-http": "^0.4.8",
63
+ "wooks": "^0.4.8",
63
64
  "express": "^4.0.0"
64
65
  },
65
66
  "devDependencies": {
@@ -69,8 +70,8 @@
69
70
  "@types/node": "^18.11.0",
70
71
  "@typescript-eslint/eslint-plugin": "^5.42.0",
71
72
  "@typescript-eslint/parser": "^5.0.0",
72
- "@wooksjs/event-http": "^0.2.9",
73
- "@wooksjs/http-body": "^0.2.9",
73
+ "@wooksjs/event-http": "^0.4.8",
74
+ "@wooksjs/http-body": "^0.4.8",
74
75
  "conventional-changelog": "^3.1.24",
75
76
  "conventional-changelog-cli": "^2.1.1",
76
77
  "enquirer": "^2.3.6",