Created
July 20, 2021 12:16
-
-
Save danieldsf/1f830c60c563fe76a09ae64418ad86c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require("fs"); | |
class Request { | |
constructor(event){ | |
this.setMethod(event.httpMethod); | |
this.setBody(event.body, event.isBase64Encoded); | |
this.setIP(event.requestContext); | |
this.setParams(event.queryStringParameters); | |
} | |
get path(){ | |
return this.params ? this.params.path || '/' : '/'; | |
} | |
setParams(params){ | |
this.params = params || {'path': '/'}; | |
this.query = this.params; | |
return this; | |
} | |
setIP(context){ | |
this.ip = context.identity.sourceIp || '127.0.0.1'; | |
return this; | |
} | |
setBody(body, isBase64){ | |
if(!isBase64){ | |
this.body = body || '{}'; | |
}else{ | |
try { | |
this.body = Buffer.from(body, 'base64').toString('utf8'); | |
} catch (error) { | |
this.body = '{}'; | |
} | |
} | |
// | |
try { | |
this.body = JSON.parse(this.body); | |
} catch (error) { | |
this.body = {}; | |
} | |
// | |
console.log(body); | |
return this; | |
} | |
setMethod(method){ | |
this.method = method; | |
return this; | |
} | |
//'event': this.event, | |
toJson(){ | |
return { | |
'method': this.method, | |
'body': this.body, | |
'ip': this.ip, | |
'params': this.params | |
} | |
} | |
} | |
// TODO enable other status codes... | |
class Response{ | |
constructor(){ | |
this.statusCode = 200; | |
this.contentType = 'text/json'; | |
} | |
status(code){ | |
this.statusCode = code; | |
return this; | |
} | |
send(input){ | |
this.contentType = 'text/html'; | |
return { | |
statusCode: this.statusCode, | |
headers: { | |
'content-type': this.contentType, | |
'Content-Type': this.contentType, | |
'Access-Control-Allow-Origin': '*', | |
'Access-Control-Allow-Methods': "POST, GET, PUT, OPTIONS", | |
'Access-Control-Max-Age': '3600', | |
'Access-Control-Allow-Headers': 'Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With' | |
}, | |
body: input, | |
} | |
} | |
sendFile(input){ | |
let content = fs.readFileSync(input).toString('utf8'); | |
return this.send(content); | |
} | |
json(input){ | |
return { | |
statusCode: this.statusCode, | |
headers: { | |
'content-type': this.contentType, | |
'Content-Type': this.contentType, | |
//response.setHeader("Access-Control-Allow-Origin", "my-authorized-proxy-or-domain"); | |
//response.setHeader("Access-Control-Allow-Methods", "POST, GET"); | |
//response.setHeader("Access-Control-Max-Age", "3600"); | |
//response.setHeader("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With"); | |
'Access-Control-Allow-Origin': '*', | |
'Access-Control-Allow-Methods': "POST, GET, PUT, OPTIONS", | |
'Access-Control-Max-Age': '3600', | |
'Access-Control-Allow-Headers': 'Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With' | |
//'Access-Control-Allow-Headers': '*' | |
}, | |
body: JSON.stringify(input), | |
} | |
} | |
} | |
module.exports = { | |
Request, | |
Response | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment