@wooksjs/express-adapter 0.2.0 → 0.2.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/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/icon.png" height="156px"><br>
6
+ <img src="./docs/wooks-logo.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>
@@ -17,6 +17,10 @@ Want to use [@wooksjs/event-http](https://www.npmjs.com/package/@wooksjs/event-h
17
17
 
18
18
  ## Usage
19
19
 
20
+ There are two options to use express with wooks
21
+
22
+ ### 1. Adapter for express API:
23
+ This one will modify express `get`, `post`, ..., methods. Take this one if you want to keep using express app API.
20
24
  ```ts
21
25
  import express from 'express'
22
26
  import { applyExpressAdapter } from '@wooksjs/express-adapter'
@@ -44,3 +48,34 @@ app.get('/error', () => {
44
48
 
45
49
  app.listen(3000, () => console.log('listening 3000'))
46
50
  ```
51
+
52
+ ### 2. Adapter for WooksHttp API:
53
+ This one does not modify anything. It just applies express middleware and reroutes requests through wooks. Use this one if you want to use wooks app API (compatible with [@moostjs/event-http](https://www.npmjs.com/package/@moostjs/event-http))
54
+
55
+ ```ts
56
+ import express from 'express'
57
+ import { WooksExpress } from '@wooksjs/express-adapter'
58
+ import { useBody } from '@wooksjs/http-body'
59
+ import { HttpError } from '@wooksjs/event-http'
60
+ import { useRouteParams } from '@wooksjs/event-core'
61
+
62
+ const expressApp = Express()
63
+
64
+ const wooksApp = new WooksExpress(expressApp, { raise404: true })
65
+
66
+ wooksApp.get('/test/:param', () => {
67
+ const { get } = useRouteParams()
68
+ return { message: 'it works', param: get('param') }
69
+ })
70
+
71
+ wooksApp.post('/post', () => {
72
+ const { parseBody } = useBody()
73
+ return parseBody()
74
+ })
75
+
76
+ wooksApp.get('/error', () => {
77
+ throw new HttpError(400, 'test error')
78
+ })
79
+
80
+ wooksApp.listen(3000, () => console.log('listening 3000'))
81
+ ```
package/dist/index.cjs CHANGED
@@ -47,4 +47,47 @@ function wooksContext(req, res, next) {
47
47
  next();
48
48
  }
49
49
 
50
+ class WooksExpress extends eventHttp.WooksHttp {
51
+ constructor(expressApp, opts) {
52
+ super();
53
+ this.expressApp = expressApp;
54
+ this.opts = opts;
55
+ expressApp.use(this.getServerCb());
56
+ }
57
+ async listen(...args) {
58
+ const server = this.server = this.expressApp.listen(...args);
59
+ return new Promise((resolve, reject) => {
60
+ server.once("listening", resolve);
61
+ server.once("error", reject);
62
+ });
63
+ }
64
+ getServerCb() {
65
+ 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) {
69
+ try {
70
+ await this.processHandlers(handlers);
71
+ } catch (e) {
72
+ console.error("Internal error, please report: ", e);
73
+ if (e.stack) {
74
+ console.warn(e.stack);
75
+ }
76
+ restoreCtx();
77
+ this.respond(e);
78
+ clearCtx();
79
+ }
80
+ } else {
81
+ if (this.opts?.raise404) {
82
+ this.respond(new eventHttp.HttpError(404));
83
+ clearCtx();
84
+ } else if (next) {
85
+ next();
86
+ }
87
+ }
88
+ };
89
+ }
90
+ }
91
+
92
+ exports.WooksExpress = WooksExpress;
50
93
  exports.applyExpressAdapter = applyExpressAdapter;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,19 @@
1
- import { Express } from 'express';
1
+ import Express$1, { Express } from 'express';
2
+ import { WooksHttp } from '@wooksjs/event-http';
3
+ import { Server, IncomingMessage, ServerResponse } from 'http';
2
4
 
3
5
  declare function applyExpressAdapter(app: Express): void;
4
6
 
5
- export { applyExpressAdapter };
7
+ declare class WooksExpress extends WooksHttp {
8
+ protected expressApp: Express$1.Application;
9
+ protected opts?: {
10
+ raise404?: boolean | undefined;
11
+ } | undefined;
12
+ constructor(expressApp: Express$1.Application, opts?: {
13
+ raise404?: boolean | undefined;
14
+ } | undefined);
15
+ listen(...args: Parameters<Server['listen']>): Promise<unknown>;
16
+ getServerCb(): (req: IncomingMessage, res: ServerResponse, next?: Express$1.NextFunction) => Promise<void>;
17
+ }
18
+
19
+ export { WooksExpress, applyExpressAdapter };
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { createWooksResponder, createHttpContext, useHttpContext } from '@wooksjs/event-http';
1
+ import { createWooksResponder, createHttpContext, useHttpContext, WooksHttp, HttpError } from '@wooksjs/event-http';
2
2
 
3
3
  const methods = [
4
4
  "get",
@@ -45,4 +45,46 @@ function wooksContext(req, res, next) {
45
45
  next();
46
46
  }
47
47
 
48
- export { applyExpressAdapter };
48
+ class WooksExpress extends WooksHttp {
49
+ constructor(expressApp, opts) {
50
+ super();
51
+ this.expressApp = expressApp;
52
+ this.opts = opts;
53
+ expressApp.use(this.getServerCb());
54
+ }
55
+ async listen(...args) {
56
+ const server = this.server = this.expressApp.listen(...args);
57
+ return new Promise((resolve, reject) => {
58
+ server.once("listening", resolve);
59
+ server.once("error", reject);
60
+ });
61
+ }
62
+ getServerCb() {
63
+ 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) {
67
+ try {
68
+ await this.processHandlers(handlers);
69
+ } catch (e) {
70
+ console.error("Internal error, please report: ", e);
71
+ if (e.stack) {
72
+ console.warn(e.stack);
73
+ }
74
+ restoreCtx();
75
+ this.respond(e);
76
+ clearCtx();
77
+ }
78
+ } else {
79
+ if (this.opts?.raise404) {
80
+ this.respond(new HttpError(404));
81
+ clearCtx();
82
+ } else if (next) {
83
+ next();
84
+ }
85
+ }
86
+ };
87
+ }
88
+ }
89
+
90
+ export { WooksExpress, applyExpressAdapter };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wooksjs/express-adapter",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "Express Adapter for Wooks Composables",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.mjs",