-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (59 loc) · 1.99 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
'use strict'
const { send, buffer, text ,json, createError } = require('micro')
const { router, get, post, options } = require('microrouter')
const rethinkDBObj = require('rethinkdb')
const cors = require('micro-cors')()
const defaultPack = require('./subpackage.js')
// let config = require('config')
console.log(defaultPack)
let Promise = require('promise')
const rp = require('request-promise')
const subscription = cors(async(req, res) => {
if(req.params !== undefined && req.params['_']!== undefined ) {
let subscriptionPath = req.params['_']
if (subscriptionPath.substring(subscriptionPath.lastIndexOf('/') + 1, subscriptionPath.length).length <= 0) {
subscriptionPath = subscriptionPath.substring(0, subscriptionPath.lastIndexOf('/'))
}
// console.log(subscriptionPath,'====', subscriptionPath.split('/'))
subscriptionPath = subscriptionPath.split('/')
await findSubscription(subscriptionPath)
.then((result) => {
// console.log('==Final Obj==', result)
// console.log(result.validate())
if (result.validate()) {
send(res, 200, 'Subscription page')
} else {
send(res, 403, 'Subscription not available')
}
})
.catch((err) => {
if (err) { }
send(res, 403, 'Subscription not available')
return false
})
} else {
send(res, 403, 'Subscription Path not available')
}
})
async function findSubscription (arrPath) {
return new Promise((resolve, reject) => {
let obj = defaultPack
arrPath.forEach(function (val, idx) {
// console.log("\n\n==1==Path=", val, idx, '====Obj Val=', obj[val])
if (obj[val] !== undefined) {
obj = obj[val]
console.log('===obj=', obj)
} else {
reject(null)
}
})
resolve(obj)
})
}
const notfound = cors((req, res) => send(res, 200, 'this is base page'))
module.exports = router(
get('/subscription/*', subscription),
get('/subscription', subscription),
post('/subscription', subscription),
options('/*', notfound)
)