-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
139 lines (118 loc) · 3.44 KB
/
server.js
File metadata and controls
139 lines (118 loc) · 3.44 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const data = require('./animals.json')
const express = require('express')
const scribbles = require('scribbles')
const parseXmlString = require('xml2json');
global.console = scribbles
const app = express()
const port = 3001
app.use(
express.raw({
inflate: true,
limit: '50mb',
type: () => true, // this matches all content types
})
);
//=====================================================
//================================= Operation Functions
//=====================================================
function listPets(req, res){
if(req.query.limit){
res.json(data.slice(0, req.query.limit))
}else {
res.json(data)
}
} // END listPets
//=====================================================
//=================================== Firetail settings
//=====================================================
const firetailSetup = require("@public.firetail.io/firetail-api");
const firetailOpts = {
addApi: "./petstore.yaml",
overRideError:(err)=>{
return err
},
authCallbacks:{
jwt:({authorization})=>{
const token = authorization.split(" ").pop().replace(/['"]+/g, '')
const tokenDecodablePart = token.split('.').pop();
const decoded = Buffer.from(tokenDecodablePart, 'base64').toString();
return JSON.parse(decoded)
},
key:({authorization})=>{
if("key" !== authorization){
throw new Error("Invalid token")
}
return true
},
basic:({user, pass})=>{
if (user == 'admin' && pass == 'password') {
return true // authorized
} else {
throw new Error('You are not authenticated!');
}
},
oauth2:({authorization,scopes},headers)=>{
if("RsT5OjbzRn430zqMLgV3Ia" !== authorization){
throw new Error("Invalid token")
}
const result = {
scopes:Object.keys(scopes)
}
result.scopes.forEach(scope=>{
result[scope] = scope
})
return result
},
},
// override Express'es controller based on "operationId"
operations:{
listPets,
}, // END operations
customBodyDecoders:{
'application/xml': body => parseXmlString.toJson(body,{object:true}).Pet
}
} // END firetailOpts
//=====================================================
//======================================== Add Firetail
//=====================================================
app.use(firetailSetup(firetailOpts))
//=====================================================
//========================================= Your server
//=====================================================
app.get('/', (req, res) => {
res.send("FireTail sample")
})
app.delete('/pets/:petId', (req, res) => {
if(! data.map(({id})=>`${id}`)
.includes(req.params.petId)){
res.status(400)
.json({
type:req.originalUrl,
title:`No pet with an ID of "${req.params.petId}" was found`,
status:400
})
return;
}
const copy = [...data]
let removedItem = null
//empty
copy.forEach(()=>{
data.pop()
})
//rebuild
copy.forEach(item=>{
if(item.id !== +req.params.petId){
data.push(item)
} else {
removedItem = item
}
})
res.status(202)
res.json(removedItem)
}) // END app.delete '/pets/:petId'
//=====================================================
//======================================== Server start
//=====================================================
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})