universal.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. const { ERROR } = require('./error')
  2. function getHttpClientInfo () {
  3. const requestId = this.getUniCloudRequestId()
  4. const { clientIP, userAgent, source, secretType = 'none' } = this.getClientInfo()
  5. const { clientInfo = {} } = JSON.parse(this.getHttpInfo().body)
  6. return {
  7. ...clientInfo,
  8. clientIP,
  9. userAgent,
  10. source,
  11. secretType,
  12. requestId
  13. }
  14. }
  15. function getHttpUniIdToken () {
  16. const { uniIdToken = '' } = JSON.parse(this.getHttpInfo().body)
  17. return uniIdToken
  18. }
  19. function verifyHttpMethod () {
  20. const { headers, httpMethod } = this.getHttpInfo()
  21. if (!/^application\/json/.test(headers['content-type']) || httpMethod.toUpperCase() !== 'POST') {
  22. throw {
  23. errCode: ERROR.UNSUPPORTED_REQUEST,
  24. errMsg: 'unsupported request'
  25. }
  26. }
  27. }
  28. function universal () {
  29. if (this.getClientInfo().source === 'http') {
  30. verifyHttpMethod.call(this)
  31. this.getParams()[0] = JSON.parse(this.getHttpInfo().body).params
  32. this.getUniversalClientInfo = getHttpClientInfo.bind(this)
  33. this.getUniversalUniIdToken = getHttpUniIdToken.bind(this)
  34. } else {
  35. this.getUniversalClientInfo = this.getClientInfo
  36. this.getUniversalUniIdToken = this.getUniIdToken
  37. }
  38. }
  39. module.exports = universal