forked from i18next/i18next-http-middleware
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpFunctions.js
More file actions
159 lines (158 loc) · 5.54 KB
/
httpFunctions.js
File metadata and controls
159 lines (158 loc) · 5.54 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
export const getPath = (req) => {
if (req.path) return req.path
if (req.raw && req.raw.path) return req.raw.path
if (req.url) return req.url
console.log('no possibility found to get path')
}
export const getUrl = (req) => {
if (req.url && req.url.href) return req.url.href
if (req.url) return req.url
if (req.raw && req.raw.url) return req.raw.url
console.log('no possibility found to get url')
}
export const setUrl = (req, url) => {
if (req.url) {
req.url = url
return
}
console.log('no possibility found to get url')
}
export const getOriginalUrl = (req) => {
if (req.originalUrl) return req.originalUrl
if (req.raw && req.raw.originalUrl) return req.raw.originalUrl
return getUrl(req)
}
export const getQuery = (req) => {
if (
req.query &&
typeof req.query.entries === 'function' &&
typeof Object.fromEntries === 'function' &&
typeof req.query[Symbol.iterator] === 'function'
) {
return Object.fromEntries(req.query)
}
if (req.query) return req.query
if (req.searchParams) return req.searchParams
if (req.raw && req.raw.query) return req.raw.query
if (req.ctx && req.ctx.queryParams) return req.ctx.queryParams
if (req.url && req.url.searchParams) return req.url.searchParams
const url = req.url || (req.raw && req.raw.url)
if (url && url.indexOf('?') < 0) return {}
if (typeof URL !== 'undefined') {
try {
return Object.fromEntries(new URL(url).searchParams.entries())
} catch { }
}
console.log('no possibility found to get query')
return {}
}
export const getParams = (req) => {
if (req.params) return req.params
if (req.raw && req.raw.params) return req.raw.params
if (req.ctx && req.ctx.params) return req.ctx.params
console.log('no possibility found to get params')
return {}
}
export const getHeaders = (req) => {
if (!req.headers) {
console.log('no possibility found to get headers')
return
}
if (typeof Headers !== 'undefined' && req.headers.constructor === Headers) {
return Object.fromEntries(req.headers.entries())
}
return req.headers
}
export const getCookies = (req) => {
if (req.cookies) return req.cookies
if (getHeaders(req)) {
const list = {}
const rc = getHeaders(req).cookie
rc &&
rc.split(';').forEach((cookie) => {
const parts = cookie.split('=')
list[parts.shift().trim()] = decodeURI(encodeURI(parts.join('=')))
})
return list
}
console.log('no possibility found to get cookies')
}
export const getBody = (req) => {
if (req.ctx && typeof req.ctx.body === 'function') {
return req.ctx.body.bind(req.ctx)
}
if (req.ctx && req.ctx.body) return req.ctx.body
if (req.json) return req.json
if (req.body) return req.body
if (req.payload) return req.payload
if (req.request && req.request.body) return req.request.body
console.log('no possibility found to get body')
return {}
}
export const getHeader = (res, name) => {
if (res.getHeader) return res.getHeader(name)
if (res.headers) return res.headers[name]
if (getHeaders(res) && getHeaders(res)[name]) return getHeaders(res)[name]
console.log('no possibility found to get header')
return undefined
}
export const setHeader = (res, name, value) => {
if (res._headerSent || res.headersSent) return
if (typeof res.setHeader === 'function') return res.setHeader(name, value)
if (typeof res.header === 'function') return res.header(name, value)
if (res.responseHeaders && typeof res.responseHeaders.set === 'function') {
return res.responseHeaders.set(name, value)
}
if (res.headers && typeof res.headers.set === 'function') {
return res.headers.set(name, value)
}
if (typeof res.set === 'function') {
return res.set(name, value)
}
console.log('no possibility found to set header')
}
export const setContentType = (res, type) => {
if (typeof res.contentType === 'function') return res.contentType(type)
if (typeof res.type === 'function') return res.type(type)
setHeader(res, 'Content-Type', type)
}
export const setStatus = (res, code) => {
if (typeof res.status === 'function') return res.status(code)
// eslint-disable-next-line no-return-assign
if (res.status) return (res.status = code)
console.log('no possibility found to set status')
}
export const send = (res, body) => {
if (typeof res.send === 'function') return res.send(body)
if (res.request && res.response && res.app) res.body = body
if (typeof Response !== 'undefined' && res.constructor === Response) {
return new Response(body, {
status: res.status,
headers: res.headers
})
}
return body
}
export const getSession = (req) => {
if (req.session) return req.session
if (req.raw && req.raw.session) return req.raw.session
console.log('no possibility found to get session')
}
export const extendOptionsWithDefaults = (options = {}) => {
options.getPath = options.getPath || getPath
options.getOriginalUrl = options.getOriginalUrl || getOriginalUrl
options.getUrl = options.getUrl || getUrl
options.setUrl = options.setUrl || setUrl
options.getParams = options.getParams || getParams
options.getSession = options.getSession || getSession
options.getQuery = options.getQuery || getQuery
options.getCookies = options.getCookies || getCookies
options.getBody = options.getBody || getBody
options.getHeaders = options.getHeaders || getHeaders
options.getHeader = options.getHeader || getHeader
options.setHeader = options.setHeader || setHeader
options.setContentType = options.setContentType || setContentType
options.setStatus = options.setStatus || setStatus
options.send = options.send || send
return options
}