typed-bridge 2.0.12 → 2.0.14

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.
@@ -72,11 +72,11 @@ const createBridge = (bridge, port, path = '/bridge') => {
72
72
  app.use((req, res, next) => {
73
73
  setTimeout(next, __1.tbConfig.responseDelay);
74
74
  });
75
+ app.use(path, bridgeHandler(bridge));
75
76
  // Server health
76
- app.use(path_1.default.join(path, 'health'), (req, res) => {
77
- res.sendStatus(200);
77
+ app.get(path_1.default.join(path, 'health'), (req, res) => {
78
+ res.status(200).json({ status: 'OK' });
78
79
  });
79
- app.use(path, bridgeHandler(bridge));
80
80
  const server = app.listen(port, () => (0, helpers_1.printStartLogs)(port));
81
81
  let shuttingDown = false;
82
82
  const shutdown = () => {
@@ -93,9 +93,11 @@ const createBridge = (bridge, port, path = '/bridge') => {
93
93
  };
94
94
  exports.createBridge = createBridge;
95
95
  const bridgeHandler = (bridge) => async (req, res) => {
96
+ let args = {};
97
+ let context = {};
96
98
  try {
97
99
  const path = req.path.split('/').pop() || '';
98
- const args = req.body;
100
+ args = req.body;
99
101
  if (!path)
100
102
  throw new Error('Bridge not found!');
101
103
  const serverFunction = bridge[path];
@@ -105,7 +107,7 @@ const bridgeHandler = (bridge) => async (req, res) => {
105
107
  console.error(error);
106
108
  return res.status(404).json({ error });
107
109
  }
108
- let context = {};
110
+ context = {};
109
111
  for (const middleware of middlewares) {
110
112
  if ((0, helpers_1.matchesPattern)(path, middleware.pattern)) {
111
113
  const result = await middleware.handler(req, res);
@@ -118,13 +120,18 @@ const bridgeHandler = (bridge) => async (req, res) => {
118
120
  res.json((await serverFunction(args, context)) || {});
119
121
  }
120
122
  catch (error) {
123
+ const id = req.bind?.id;
124
+ if (__1.tbConfig.logs.argsOnError)
125
+ console.error(`ARGS | ${id} ::`, JSON.stringify(args, null, 2));
126
+ if (__1.tbConfig.logs.contextOnError)
127
+ console.error(`CONTEXT | ${id} ::`, JSON.stringify(context, null, 2));
121
128
  if (Array.isArray(error.errors)) {
122
129
  const keyPath = error.errors[0].path.join('/');
123
130
  const errorMessage = (keyPath ? keyPath + ': ' : '') + error.errors[0].message;
124
131
  return res.status(400).send(errorMessage);
125
132
  }
126
133
  if (__1.tbConfig.logs.error)
127
- console.error(error);
134
+ console.error(`ERROR | ${id} ::`, error);
128
135
  return res.status(500).json({ error: error.message });
129
136
  }
130
137
  };
@@ -3,6 +3,8 @@ interface config {
3
3
  request: boolean;
4
4
  response: boolean;
5
5
  error: boolean;
6
+ argsOnError: boolean;
7
+ contextOnError: boolean;
6
8
  };
7
9
  responseDelay: number;
8
10
  }
@@ -5,7 +5,9 @@ exports.config = {
5
5
  logs: {
6
6
  request: true,
7
7
  response: true,
8
- error: true
8
+ error: true,
9
+ argsOnError: true,
10
+ contextOnError: true
9
11
  },
10
12
  responseDelay: 0
11
13
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "typed-bridge",
3
3
  "description": "Strictly typed server functions for typescript apps",
4
- "version": "2.0.12",
4
+ "version": "2.0.14",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "author": "neilveil",