tom-microservice 3.7.0 → 3.8.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.
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.0",
5
+ "version": "3.8.0",
6
6
  "main": "src",
7
7
  "bin": {
8
8
  "tom": "bin/index.js"
@@ -30,10 +30,22 @@ module.exports = ({ config }) => {
30
30
  stripe.setupIntents.retrieve(setupIntentId)
31
31
  ])
32
32
 
33
- await stripe.customers.update(customerId, {
34
- metadata: { ...oldMetadata, ...newMetadata },
35
- invoice_settings: { default_payment_method: paymentMethod }
36
- })
33
+ await Promise.all([
34
+ stripe.customers.update(customerId, {
35
+ metadata: { ...oldMetadata, ...newMetadata },
36
+ invoice_settings: { default_payment_method: paymentMethod }
37
+ }),
38
+ Promise.all(
39
+ (
40
+ await stripe.paymentMethods
41
+ .list({ customer: customerId, type: 'card' })
42
+ .then(({ data }) => data)
43
+ )
44
+ .map(({ id }) => id)
45
+ .filter(id => id !== paymentMethod)
46
+ .map(id => stripe.paymentMethods.detach(id))
47
+ )
48
+ ])
37
49
 
38
50
  ward(email, {
39
51
  label: 'email',
package/src/routes.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict'
2
2
 
3
- const { get, eq, forEach } = require('lodash')
3
+ const { get, 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')()
@@ -92,15 +92,15 @@ const createRouter = () => {
92
92
  }
93
93
 
94
94
  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'))
95
+ .get('/', (_, res) => send(res, 204))
96
+ .get('/robots.txt', (_, res) => send(res, 204))
97
+ .get('/favicon.ico', (_, res) => send(res, 204))
98
+ .get('/ping', (_, res) => send(res, 200, 'pong'))
99
99
 
100
100
  return router
101
101
  }
102
102
 
103
- module.exports = tomConfig => {
103
+ module.exports = (tomConfig, fn = noop) => {
104
104
  if (!tomConfig) throw TypeError('You need to provide tom configuration file.')
105
105
 
106
106
  const router = createRouter()
@@ -116,5 +116,7 @@ module.exports = tomConfig => {
116
116
  })
117
117
  })
118
118
 
119
+ fn({ tom, router, send })
120
+
119
121
  return router
120
122
  }