tom-microservice 3.5.1 → 3.5.3
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 +2 -2
- package/src/routes.js +29 -23
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.
|
|
5
|
+
"version": "3.5.3",
|
|
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",
|
package/src/routes.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const { get, eq, forEach } = require('lodash')
|
|
4
|
-
const
|
|
4
|
+
const { buffer, text } = require('http-body')
|
|
5
5
|
const requestIp = require('request-ip')
|
|
6
6
|
const toQuery = require('to-query')()
|
|
7
7
|
const Router = require('router-http')
|
|
@@ -12,12 +12,26 @@ const createTom = require('.')
|
|
|
12
12
|
|
|
13
13
|
const { TOM_API_KEY, TOM_ALLOWED_ORIGIN, NODE_ENV } = process.env
|
|
14
14
|
|
|
15
|
-
const
|
|
15
|
+
const UNAUTHENTICATED_PATHS = [
|
|
16
|
+
'/',
|
|
17
|
+
'/robots.txt',
|
|
18
|
+
'/favicon.ico',
|
|
19
|
+
'/ping',
|
|
20
|
+
'/payment/webhook'
|
|
21
|
+
]
|
|
22
|
+
|
|
23
|
+
const getBody = async req => {
|
|
24
|
+
if (req.path === '/payment/webhook') return buffer(req)
|
|
25
|
+
const body = await text(req)
|
|
26
|
+
if (body === '') return body
|
|
27
|
+
try {
|
|
28
|
+
return JSON.parse(body)
|
|
29
|
+
} catch (_) {
|
|
30
|
+
return body
|
|
31
|
+
}
|
|
32
|
+
}
|
|
16
33
|
|
|
17
|
-
const
|
|
18
|
-
const urlEncodedBodyParser = bodyParser.urlencoded({ extended: true })
|
|
19
|
-
const rawBodyParser = bodyParser.raw({ type: 'application/json' })
|
|
20
|
-
const isWebhook = req => req.path.endsWith('webhook')
|
|
34
|
+
const isTest = NODE_ENV === 'test'
|
|
21
35
|
|
|
22
36
|
const finalhandler = (error, req, res) => {
|
|
23
37
|
const hasError = error !== undefined
|
|
@@ -52,32 +66,18 @@ const createRouter = () => {
|
|
|
52
66
|
]
|
|
53
67
|
})
|
|
54
68
|
)
|
|
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) => {
|
|
69
|
+
.use(async (req, res, next) => {
|
|
65
70
|
req.query = toQuery(req.url)
|
|
66
71
|
req.ipAddress = requestIp.getClientIp(req)
|
|
72
|
+
req.body = await getBody(req)
|
|
67
73
|
next()
|
|
68
74
|
})
|
|
69
75
|
|
|
70
76
|
if (!isTest) router.use(require('morgan')('tiny'))
|
|
71
77
|
|
|
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
78
|
if (TOM_API_KEY) {
|
|
79
79
|
router.use((req, res, next) => {
|
|
80
|
-
if (req.path
|
|
80
|
+
if (UNAUTHENTICATED_PATHS.includes(req.path)) return next()
|
|
81
81
|
const apiKey = get(req, 'headers.x-api-key')
|
|
82
82
|
return eq(apiKey, TOM_API_KEY)
|
|
83
83
|
? next()
|
|
@@ -91,6 +91,12 @@ const createRouter = () => {
|
|
|
91
91
|
})
|
|
92
92
|
}
|
|
93
93
|
|
|
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'))
|
|
99
|
+
|
|
94
100
|
return router
|
|
95
101
|
}
|
|
96
102
|
|