tom-microservice 3.5.0 → 3.5.2

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/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.5.0",
5
+ "version": "3.5.2",
6
6
  "main": "src",
7
7
  "bin": {
8
8
  "tom": "bin/index.js"
@@ -41,12 +41,12 @@
41
41
  "dependencies": {
42
42
  "acho": "~4.0.6",
43
43
  "beauty-error": "~1.2.15",
44
- "body-parser": "~1.20.2",
45
44
  "cors": "~2.8.5",
46
45
  "country-vat": "~1.0.8",
47
46
  "emittery": "~0.13.1",
48
47
  "got": "~11.8.6",
49
48
  "helmet": "~6.1.2",
49
+ "http-body": "~1.0.4",
50
50
  "http-compression": "~1.0.5",
51
51
  "import-modules": "~2.1.0",
52
52
  "is-buffer": "~2.0.5",
@@ -36,7 +36,6 @@ module.exports = ({ config }) => {
36
36
 
37
37
  await stripe.customers.update(customerId, {
38
38
  metadata: { ...oldMetadata, ...newMetadata },
39
- default_source: paymentMethod,
40
39
  invoice_settings: { default_payment_method: paymentMethod }
41
40
  })
42
41
 
package/src/routes.js CHANGED
@@ -1,8 +1,8 @@
1
1
  'use strict'
2
2
 
3
3
  const { get, eq, forEach } = require('lodash')
4
- const bodyParser = require('body-parser')
5
4
  const requestIp = require('request-ip')
5
+ const { text } = require('http-body')
6
6
  const toQuery = require('to-query')()
7
7
  const Router = require('router-http')
8
8
  const send = require('./send')
@@ -12,12 +12,24 @@ const createTom = require('.')
12
12
 
13
13
  const { TOM_API_KEY, TOM_ALLOWED_ORIGIN, NODE_ENV } = process.env
14
14
 
15
- const isTest = NODE_ENV === 'test'
15
+ const UNAUTHENTICATED_PATHS = [
16
+ '/',
17
+ '/robots.txt',
18
+ '/favicon.ico',
19
+ '/ping',
20
+ '/payment/webhook'
21
+ ]
22
+
23
+ const getBody = async req => {
24
+ const string = await text(req)
25
+ try {
26
+ return JSON.parse(string)
27
+ } catch (_) {
28
+ return string
29
+ }
30
+ }
16
31
 
17
- const jsonBodyParser = bodyParser.json()
18
- const urlEncodedBodyParser = bodyParser.urlencoded({ extended: true })
19
- const rawBodyParser = bodyParser.raw({ type: 'application/json' })
20
- const isWebhook = req => req.path.endsWith('webhook')
32
+ const isTest = NODE_ENV === 'test'
21
33
 
22
34
  const finalhandler = (error, req, res) => {
23
35
  const hasError = error !== undefined
@@ -52,32 +64,18 @@ const createRouter = () => {
52
64
  ]
53
65
  })
54
66
  )
55
- .use((req, res, next) =>
56
- isWebhook(req) ? rawBodyParser(req, res, next) : next()
57
- )
58
- .use((req, res, next) =>
59
- isWebhook(req) ? next() : urlEncodedBodyParser(req, res, next)
60
- )
61
- .use((req, res, next) =>
62
- isWebhook(req) ? next() : jsonBodyParser(req, res, next)
63
- )
64
- .use((req, res, next) => {
67
+ .use(async (req, res, next) => {
65
68
  req.query = toQuery(req.url)
66
69
  req.ipAddress = requestIp.getClientIp(req)
70
+ req.body = await getBody(req)
67
71
  next()
68
72
  })
69
73
 
70
74
  if (!isTest) router.use(require('morgan')('tiny'))
71
75
 
72
- router
73
- .get('/', (req, res) => send(res, 204))
74
- .get('/robots.txt', (req, res) => send(res, 204))
75
- .get('/favicon.ico', (req, res) => send(res, 204))
76
- .get('/ping', (req, res) => send(res, 200, 'pong'))
77
-
78
76
  if (TOM_API_KEY) {
79
77
  router.use((req, res, next) => {
80
- if (req.path.endsWith('webhook')) return next()
78
+ if (UNAUTHENTICATED_PATHS.includes(req.path)) return next()
81
79
  const apiKey = get(req, 'headers.x-api-key')
82
80
  return eq(apiKey, TOM_API_KEY)
83
81
  ? next()
@@ -91,6 +89,12 @@ const createRouter = () => {
91
89
  })
92
90
  }
93
91
 
92
+ router
93
+ .get('/', (req, res) => send(res, 204))
94
+ .get('/robots.txt', (req, res) => send(res, 204))
95
+ .get('/favicon.ico', (req, res) => send(res, 204))
96
+ .get('/ping', (req, res) => send(res, 200, 'pong'))
97
+
94
98
  return router
95
99
  }
96
100