tom-microservice 3.7.1 → 3.9.0

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
@@ -62,6 +62,10 @@ To view details for a command at any time use `tom --help`
62
62
 
63
63
  ### from Node.js
64
64
 
65
+ #### as process
66
+
67
+ You can interact with **tom** 🐶 from Node.js user code:
68
+
65
69
  ```js
66
70
  // First of all you need to declare a configuration file.
67
71
  const config = {/* See configuration section */}
@@ -73,6 +77,37 @@ const tom = require('tom-microservice')(config)
73
77
  const { payment, email } = tom
74
78
  ```
75
79
 
80
+ #### as HTTP router
81
+
82
+ Additionally, you can get the **tom** 🐶 HTTP router in standalone way:
83
+
84
+ ```js
85
+ const { createRoutes } = require('tom-microservice')
86
+ const config = {/* See configuration section */}
87
+
88
+ const router = createRoutes(config, ({ tom, router, send }) => {
89
+ router
90
+ .get('/ping', (req, res) => send(res, 200, 'healthy'))
91
+ })
92
+ ```
93
+
94
+ #### as HTTP server
95
+
96
+ You can also intialize **tom** 🐶 HTTP server from code:
97
+
98
+ ```js
99
+ const { listen, createRoutes } = require('tom-microservice')
100
+
101
+ const config = {/* See configuration section */}
102
+
103
+ const router = createRoutes(config, ({ tom, router, send }) => {
104
+ router
105
+ .get('/ping', (req, res) => send(res, 200, 'healthy'))
106
+ })
107
+
108
+ listen(config, { routes })
109
+ ```
110
+
76
111
  ## Configuration
77
112
 
78
113
  !> Combine with [miconfig](https://www.npmjs.com/package/miconfig) for loading different settings based on environment.
@@ -431,7 +466,7 @@ See [cors](https://github.com/expressjs/cors#configuration-options) for more inf
431
466
  Type: `string` </br>
432
467
  Default: `undefined`
433
468
 
434
- When you provide it, all request to **tom** 🐶 needs to be authenticated using `x-api-key` header and the value provided.
469
+ When you provide it, all request to **tom** 🐶 needs to be authenticated using `req.headers[`x-api-key`] or `req.query.apiKey` and the value provided.
435
470
 
436
471
  You can use [randomkeygen.com](https://randomkeygen.com) for that.
437
472
 
package/bin/listen.js CHANGED
@@ -7,8 +7,9 @@ const { createServer } = require('http')
7
7
  const PORT =
8
8
  process.env.PORT || process.env.port || process.env.TOM_PORT || 3000
9
9
 
10
- module.exports = async (tomConfig, { port = PORT } = {}) => {
11
- const routes = require('../src/routes')(tomConfig)
10
+ module.exports = async (tomConfig, { port = PORT, routes } = {}) => {
11
+ if (!routes) routes = require('../src/routes')(tomConfig)
12
+
12
13
  const server = createServer(routes)
13
14
 
14
15
  server.listen(port, () => {
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "tom-microservice",
3
3
  "description": "Tom creates customers, subscriptions plans & send notifications.",
4
4
  "homepage": "https://tom.js.org",
5
- "version": "3.7.1",
5
+ "version": "3.9.0",
6
6
  "main": "src",
7
7
  "bin": {
8
8
  "tom": "bin/index.js"
package/src/routes.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict'
2
2
 
3
- const { get, eq, forEach } = require('lodash')
3
+ const { eq, forEach, noop } = require('lodash')
4
4
  const { buffer, text } = require('http-body')
5
5
  const requestIp = require('request-ip')
6
6
  const toQuery = require('to-query')()
@@ -78,7 +78,8 @@ const createRouter = () => {
78
78
  if (TOM_API_KEY) {
79
79
  router.use((req, res, next) => {
80
80
  if (UNAUTHENTICATED_PATHS.includes(req.path)) return next()
81
- const apiKey = get(req, 'headers.x-api-key')
81
+ const apiKey = req.headers['x-api-key'] ?? req.query.apiKey
82
+
82
83
  return eq(apiKey, TOM_API_KEY)
83
84
  ? next()
84
85
  : send.fail(
@@ -92,15 +93,15 @@ const createRouter = () => {
92
93
  }
93
94
 
94
95
  router
95
- .get('/', (req, res) => send(res, 204))
96
- .get('/robots.txt', (req, res) => send(res, 204))
97
- .get('/favicon.ico', (req, res) => send(res, 204))
98
- .get('/ping', (req, res) => send(res, 200, 'pong'))
96
+ .get('/', (_, res) => send(res, 204))
97
+ .get('/robots.txt', (_, res) => send(res, 204))
98
+ .get('/favicon.ico', (_, res) => send(res, 204))
99
+ .get('/ping', (_, res) => send(res, 200, 'pong'))
99
100
 
100
101
  return router
101
102
  }
102
103
 
103
- module.exports = tomConfig => {
104
+ module.exports = (tomConfig, fn = noop) => {
104
105
  if (!tomConfig) throw TypeError('You need to provide tom configuration file.')
105
106
 
106
107
  const router = createRouter()
@@ -116,5 +117,7 @@ module.exports = tomConfig => {
116
117
  })
117
118
  })
118
119
 
120
+ fn({ tom, router, send })
121
+
119
122
  return router
120
123
  }