tom-microservice 3.3.6 → 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/bin/listen.js CHANGED
@@ -2,20 +2,16 @@
2
2
 
3
3
  'use strict'
4
4
 
5
- const express = require('express')
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
- createServer(routes).listen(port, () => {
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.3.6",
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
- "got": "~11.8.3",
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",
@@ -3,25 +3,27 @@
3
3
  const { isArray, pick } = require('lodash')
4
4
  const isBuffer = require('is-buffer')
5
5
 
6
- module.exports = ({ fn, tom }) => async (req, res) => {
7
- let status
8
- const payload = {}
6
+ module.exports =
7
+ ({ fn, tom }) =>
8
+ async (req, res) => {
9
+ let status
10
+ const payload = {}
9
11
 
10
- try {
11
- const body = isBuffer(req.body) ? { body: req.body } : req.body
12
- const props = pick(req, ['ipAddress', 'headers'])
12
+ try {
13
+ const body = isBuffer(req.body) ? { body: req.body } : req.body
14
+ const props = pick(req, ['ipAddress', 'headers'])
13
15
 
14
- const opts = isArray(body)
15
- ? { commands: body, ...props }
16
- : { ...body, ...props }
16
+ const opts = isArray(body)
17
+ ? { commands: body, ...props }
18
+ : { ...body, ...props }
17
19
 
18
- const res = await fn(opts)
19
- payload.data = res
20
- status = 200
21
- } catch (err) {
22
- payload.message = err.message || err
23
- status = 400
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
- return res.status(status).send({ status, ...payload })
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 express = require('express')
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.use(require('helmet')())
27
- router.use(require('jsendp')())
28
- router.use(require('compression')())
29
- router.use(
30
- require('cors')({
31
- methods: ['GET', 'OPTIONS', 'POST'],
32
- origin: TOM_ALLOWED_ORIGIN
33
- ? TOM_ALLOWED_ORIGIN.replace(/\s/g, '').split(',')
34
- : '*',
35
- allowedHeaders: [
36
- 'content-type',
37
- 'x-amz-date',
38
- 'authorization',
39
- 'x-api-key',
40
- 'x-amz-security-token',
41
- 'x-csrf-token'
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.get('/', (req, res) => res.status(204).send())
67
- router.get('/robots.txt', (req, res) => res.status(204).send())
68
- router.get('/favicon.ico', (req, res) => res.status(204).send())
69
- router.get('/ping', (req, res) => res.send('pong'))
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
- : res.fail({
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
  }