tom-microservice 3.4.4 → 3.4.5

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.4.4",
5
+ "version": "3.4.5",
6
6
  "main": "src",
7
7
  "bin": {
8
8
  "tom": "bin/index.js"
@@ -3,10 +3,12 @@
3
3
  const { isArray, pick } = require('lodash')
4
4
  const isBuffer = require('is-buffer')
5
5
 
6
+ const send = require('../send')
7
+
6
8
  module.exports =
7
- ({ fn, tom }) =>
9
+ ({ fn }) =>
8
10
  async (req, res) => {
9
- let status
11
+ let status = 'success'
10
12
  const payload = {}
11
13
 
12
14
  try {
@@ -19,11 +21,10 @@ module.exports =
19
21
 
20
22
  const res = await fn(opts)
21
23
  payload.data = res
22
- status = 200
23
- } catch (err) {
24
- payload.message = err.message || err
25
- status = 400
24
+ } catch (error) {
25
+ payload.message = error.message || error
26
+ status = 'fail'
26
27
  }
27
28
 
28
- return res.status(status).send({ status, ...payload })
29
+ return send[status](res, payload)
29
30
  }
package/src/routes.js CHANGED
@@ -5,28 +5,7 @@ const bodyParser = require('body-parser')
5
5
  const requestIp = require('request-ip')
6
6
  const toQuery = require('to-query')()
7
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
+ const send = require('./send')
30
9
 
31
10
  const withRoute = require('./interface/route')
32
11
  const createTom = require('.')
@@ -43,8 +22,12 @@ const isWebhook = req => req.path.endsWith('webhook')
43
22
  const finalhandler = (error, req, res) => {
44
23
  const hasError = error !== undefined
45
24
  return hasError
46
- ? send.error(res, 500, { message: error.mesage || 'Internal Server Error' })
47
- : send.fail(res, 405, { message: 'HTTP Method Not Allowed' })
25
+ ? send.error(
26
+ res,
27
+ { message: error.mesage || 'Internal Server Error' },
28
+ error.statusCode
29
+ )
30
+ : send.fail(res, { message: 'HTTP Method Not Allowed' }, 405)
48
31
  }
49
32
 
50
33
  const createRouter = () => {
package/src/send.js ADDED
@@ -0,0 +1,26 @@
1
+ 'use strict'
2
+
3
+ const send = require('send-http')
4
+
5
+ send.fail = (res, data, statusCode = 400) => {
6
+ send(res, statusCode, {
7
+ status: 'fail',
8
+ ...data
9
+ })
10
+ }
11
+
12
+ send.error = (res, data, statusCode = 500) => {
13
+ send(res, statusCode, {
14
+ status: 'error',
15
+ ...data
16
+ })
17
+ }
18
+
19
+ send.success = (res, data, statusCode = 200) => {
20
+ send(res, statusCode, {
21
+ status: 'success',
22
+ ...data
23
+ })
24
+ }
25
+
26
+ module.exports = send