tom-microservice 3.3.7 → 3.4.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/LICENSE +0 -0
- package/bin/listen.js +3 -7
- package/package.json +23 -23
- package/src/interface/route.js +20 -18
- package/src/routes.js +71 -54
package/LICENSE
CHANGED
|
File without changes
|
package/bin/listen.js
CHANGED
|
@@ -2,20 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
'use strict'
|
|
4
4
|
|
|
5
|
-
const
|
|
5
|
+
const { createServer } = require('http')
|
|
6
6
|
|
|
7
7
|
const PORT =
|
|
8
8
|
process.env.PORT || process.env.port || process.env.TOM_PORT || 3000
|
|
9
9
|
|
|
10
|
-
const createServer = routes =>
|
|
11
|
-
express()
|
|
12
|
-
.use(routes)
|
|
13
|
-
.disable('x-powered-by')
|
|
14
|
-
|
|
15
10
|
module.exports = async (tomConfig, { port = PORT } = {}) => {
|
|
16
11
|
const routes = require('../src/routes')(tomConfig)
|
|
12
|
+
const server = createServer(routes)
|
|
17
13
|
|
|
18
|
-
|
|
14
|
+
server.listen(port, () => {
|
|
19
15
|
console.log(
|
|
20
16
|
require('./logo')({
|
|
21
17
|
header: 'tom is running',
|
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
|
+
"version": "3.4.0",
|
|
6
6
|
"main": "src",
|
|
7
7
|
"bin": {
|
|
8
8
|
"tom": "bin/index.js"
|
|
@@ -46,14 +46,12 @@
|
|
|
46
46
|
"cors": "~2.8.5",
|
|
47
47
|
"country-vat": "~1.0.6",
|
|
48
48
|
"emittery": "~0.13.1",
|
|
49
|
-
"express": "~4.18.2",
|
|
50
49
|
"got": "~11.8.6",
|
|
51
50
|
"helmet": "~6.0.0",
|
|
52
51
|
"import-modules": "~2.1.0",
|
|
53
52
|
"is-buffer": "~2.0.5",
|
|
54
53
|
"is-european": "~1.0.4",
|
|
55
54
|
"joycon": "~3.1.1",
|
|
56
|
-
"jsendp": "~2.1.0",
|
|
57
55
|
"lodash": "~4.17.21",
|
|
58
56
|
"mailgen": "~2.0.27",
|
|
59
57
|
"map-values-deep": "~1.0.2",
|
|
@@ -68,6 +66,8 @@
|
|
|
68
66
|
"pretty-ms": "~7.0.1",
|
|
69
67
|
"req-country": "~1.2.6",
|
|
70
68
|
"request-ip": "~3.3.0",
|
|
69
|
+
"router-http": "0.1.2",
|
|
70
|
+
"send-http": "~1.0.2",
|
|
71
71
|
"stripe": "11",
|
|
72
72
|
"time-span": "~4.0.0",
|
|
73
73
|
"to-query": "~1.6.10",
|
|
@@ -113,6 +113,25 @@
|
|
|
113
113
|
"bin",
|
|
114
114
|
"src"
|
|
115
115
|
],
|
|
116
|
+
"scripts": {
|
|
117
|
+
"build": "gulp build && untracked > .nowignore",
|
|
118
|
+
"clean": "rm -rf node_modules",
|
|
119
|
+
"coverage": "c8 report --reporter=text-lcov | coveralls",
|
|
120
|
+
"dev": "svr src/router",
|
|
121
|
+
"dev:docs": "concurrently \"gulp\" \"npm run dev:docs:server\"",
|
|
122
|
+
"dev:docs:server": "browser-sync start --server --files \"index.html, README.md, static/**/*.(css|js)\"",
|
|
123
|
+
"lint": "standard-markdown README.md && standard",
|
|
124
|
+
"postrelease": "npm run release:tags && npm run release:github && (ci-publish || npm publish --access=public)",
|
|
125
|
+
"prerelease": "npm run update:check",
|
|
126
|
+
"pretest": "npm run lint",
|
|
127
|
+
"release": "git-authors-cli && git add package.json && standard-version -a",
|
|
128
|
+
"release:github": "conventional-github-releaser -p angular",
|
|
129
|
+
"release:tags": "git push --follow-tags origin HEAD:master",
|
|
130
|
+
"start": "tom",
|
|
131
|
+
"test": "NODE_ENV=test c8 ava",
|
|
132
|
+
"update": "ncu -i",
|
|
133
|
+
"update:check": "ncu -- --error-level 2"
|
|
134
|
+
},
|
|
116
135
|
"license": "MIT",
|
|
117
136
|
"ava": {
|
|
118
137
|
"files": [
|
|
@@ -144,24 +163,5 @@
|
|
|
144
163
|
"env": [
|
|
145
164
|
"mocha"
|
|
146
165
|
]
|
|
147
|
-
},
|
|
148
|
-
"scripts": {
|
|
149
|
-
"build": "gulp build && untracked > .nowignore",
|
|
150
|
-
"clean": "rm -rf node_modules",
|
|
151
|
-
"coverage": "c8 report --reporter=text-lcov | coveralls",
|
|
152
|
-
"dev": "svr src/router",
|
|
153
|
-
"dev:docs": "concurrently \"gulp\" \"npm run dev:docs:server\"",
|
|
154
|
-
"dev:docs:server": "browser-sync start --server --files \"index.html, README.md, static/**/*.(css|js)\"",
|
|
155
|
-
"lint": "standard-markdown README.md && standard",
|
|
156
|
-
"postrelease": "npm run release:tags && npm run release:github && (ci-publish || npm publish --access=public)",
|
|
157
|
-
"prerelease": "npm run update:check",
|
|
158
|
-
"pretest": "npm run lint",
|
|
159
|
-
"release": "git-authors-cli && git add package.json && standard-version -a",
|
|
160
|
-
"release:github": "conventional-github-releaser -p angular",
|
|
161
|
-
"release:tags": "git push --follow-tags origin HEAD:master",
|
|
162
|
-
"start": "tom",
|
|
163
|
-
"test": "NODE_ENV=test c8 ava",
|
|
164
|
-
"update": "ncu -i",
|
|
165
|
-
"update:check": "ncu -- --error-level 2"
|
|
166
166
|
}
|
|
167
|
-
}
|
|
167
|
+
}
|
package/src/interface/route.js
CHANGED
|
@@ -3,25 +3,27 @@
|
|
|
3
3
|
const { isArray, pick } = require('lodash')
|
|
4
4
|
const isBuffer = require('is-buffer')
|
|
5
5
|
|
|
6
|
-
module.exports =
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
module.exports =
|
|
7
|
+
({ fn, tom }) =>
|
|
8
|
+
async (req, res) => {
|
|
9
|
+
let status
|
|
10
|
+
const payload = {}
|
|
9
11
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
12
|
+
try {
|
|
13
|
+
const body = isBuffer(req.body) ? { body: req.body } : req.body
|
|
14
|
+
const props = pick(req, ['ipAddress', 'headers'])
|
|
13
15
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
const opts = isArray(body)
|
|
17
|
+
? { commands: body, ...props }
|
|
18
|
+
: { ...body, ...props }
|
|
17
19
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
20
|
+
const res = await fn(opts)
|
|
21
|
+
payload.data = res
|
|
22
|
+
status = 200
|
|
23
|
+
} catch (err) {
|
|
24
|
+
payload.message = err.message || err
|
|
25
|
+
status = 400
|
|
26
|
+
}
|
|
25
27
|
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
+
return res.status(status).send({ status, ...payload })
|
|
29
|
+
}
|
package/src/routes.js
CHANGED
|
@@ -4,7 +4,29 @@ const { get, eq, forEach } = require('lodash')
|
|
|
4
4
|
const bodyParser = require('body-parser')
|
|
5
5
|
const requestIp = require('request-ip')
|
|
6
6
|
const toQuery = require('to-query')()
|
|
7
|
-
const
|
|
7
|
+
const Router = require('router-http')
|
|
8
|
+
const send = require('send-http')
|
|
9
|
+
|
|
10
|
+
send.fail = (res, code = 400, data) => {
|
|
11
|
+
send(res, code, {
|
|
12
|
+
status: 'fail',
|
|
13
|
+
...data
|
|
14
|
+
})
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
send.error = (res, code = 500, data) => {
|
|
18
|
+
send(res, code, {
|
|
19
|
+
status: 'error',
|
|
20
|
+
...data
|
|
21
|
+
})
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
send.success = (res, code = 200, data) => {
|
|
25
|
+
send(res, code, {
|
|
26
|
+
status: 'success',
|
|
27
|
+
...data
|
|
28
|
+
})
|
|
29
|
+
}
|
|
8
30
|
|
|
9
31
|
const withRoute = require('./interface/route')
|
|
10
32
|
const createTom = require('.')
|
|
@@ -13,60 +35,63 @@ const { TOM_API_KEY, TOM_ALLOWED_ORIGIN, NODE_ENV } = process.env
|
|
|
13
35
|
|
|
14
36
|
const isTest = NODE_ENV === 'test'
|
|
15
37
|
|
|
16
|
-
const { Router } = express
|
|
17
|
-
|
|
18
38
|
const jsonBodyParser = bodyParser.json()
|
|
19
39
|
const urlEncodedBodyParser = bodyParser.urlencoded({ extended: true })
|
|
20
40
|
const rawBodyParser = bodyParser.raw({ type: 'application/json' })
|
|
21
41
|
const isWebhook = req => req.path.endsWith('webhook')
|
|
22
42
|
|
|
43
|
+
const finalhandler = (error, req, res) => {
|
|
44
|
+
const hasError = error !== undefined
|
|
45
|
+
// if (hasError) console.error(error)
|
|
46
|
+
return hasError
|
|
47
|
+
? send.error(res, 500, { message: error.mesage || 'Internal Server Error' })
|
|
48
|
+
: send.fail(res, 405, { message: 'HTTP Method Not Allowed' })
|
|
49
|
+
}
|
|
50
|
+
|
|
23
51
|
const createRouter = () => {
|
|
24
|
-
const router = Router()
|
|
25
|
-
|
|
26
|
-
router
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
52
|
+
const router = Router(finalhandler)
|
|
53
|
+
|
|
54
|
+
router
|
|
55
|
+
.use(require('helmet')())
|
|
56
|
+
.use(require('compression')())
|
|
57
|
+
.use(
|
|
58
|
+
require('cors')({
|
|
59
|
+
methods: ['GET', 'OPTIONS', 'POST'],
|
|
60
|
+
origin: TOM_ALLOWED_ORIGIN
|
|
61
|
+
? TOM_ALLOWED_ORIGIN.replace(/\s/g, '').split(',')
|
|
62
|
+
: '*',
|
|
63
|
+
allowedHeaders: [
|
|
64
|
+
'content-type',
|
|
65
|
+
'x-amz-date',
|
|
66
|
+
'authorization',
|
|
67
|
+
'x-api-key',
|
|
68
|
+
'x-amz-security-token',
|
|
69
|
+
'x-csrf-token'
|
|
70
|
+
]
|
|
71
|
+
})
|
|
72
|
+
)
|
|
73
|
+
.use((req, res, next) =>
|
|
74
|
+
isWebhook(req) ? rawBodyParser(req, res, next) : next()
|
|
75
|
+
)
|
|
76
|
+
.use((req, res, next) =>
|
|
77
|
+
isWebhook(req) ? next() : urlEncodedBodyParser(req, res, next)
|
|
78
|
+
)
|
|
79
|
+
.use((req, res, next) =>
|
|
80
|
+
isWebhook(req) ? next() : jsonBodyParser(req, res, next)
|
|
81
|
+
)
|
|
82
|
+
.use((req, res, next) => {
|
|
83
|
+
req.query = toQuery(req.url)
|
|
84
|
+
req.ipAddress = requestIp.getClientIp(req)
|
|
85
|
+
next()
|
|
43
86
|
})
|
|
44
|
-
)
|
|
45
|
-
|
|
46
|
-
router.use((req, res, next) =>
|
|
47
|
-
isWebhook(req) ? rawBodyParser(req, res, next) : next()
|
|
48
|
-
)
|
|
49
|
-
|
|
50
|
-
router.use((req, res, next) =>
|
|
51
|
-
isWebhook(req) ? next() : urlEncodedBodyParser(req, res, next)
|
|
52
|
-
)
|
|
53
|
-
|
|
54
|
-
router.use((req, res, next) =>
|
|
55
|
-
isWebhook(req) ? next() : jsonBodyParser(req, res, next)
|
|
56
|
-
)
|
|
57
|
-
|
|
58
|
-
router.use((req, res, next) => {
|
|
59
|
-
req.query = toQuery(req.url)
|
|
60
|
-
req.ipAddress = requestIp.getClientIp(req)
|
|
61
|
-
next()
|
|
62
|
-
})
|
|
63
87
|
|
|
64
88
|
if (!isTest) router.use(require('morgan')('tiny'))
|
|
65
89
|
|
|
66
|
-
router
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
90
|
+
router
|
|
91
|
+
.get('/', (req, res) => send(res, 204))
|
|
92
|
+
.get('/robots.txt', (req, res) => send(res, 204))
|
|
93
|
+
.get('/favicon.ico', (req, res) => send(res, 204))
|
|
94
|
+
.get('/ping', (req, res) => send(res, 200, 'pong'))
|
|
70
95
|
|
|
71
96
|
if (TOM_API_KEY) {
|
|
72
97
|
router.use((req, res, next) => {
|
|
@@ -74,8 +99,7 @@ const createRouter = () => {
|
|
|
74
99
|
const apiKey = get(req, 'headers.x-api-key')
|
|
75
100
|
return eq(apiKey, TOM_API_KEY)
|
|
76
101
|
? next()
|
|
77
|
-
:
|
|
78
|
-
statusCode: 401,
|
|
102
|
+
: send.fail(res, 401, {
|
|
79
103
|
message: 'Invalid API token in x-api-key header.'
|
|
80
104
|
})
|
|
81
105
|
})
|
|
@@ -100,12 +124,5 @@ module.exports = tomConfig => {
|
|
|
100
124
|
})
|
|
101
125
|
})
|
|
102
126
|
|
|
103
|
-
router.use((req, res) =>
|
|
104
|
-
res.fail({
|
|
105
|
-
statusCode: 405,
|
|
106
|
-
message: 'HTTP Method Not Allowed'
|
|
107
|
-
})
|
|
108
|
-
)
|
|
109
|
-
|
|
110
127
|
return router
|
|
111
128
|
}
|