Skip to content

Commit 21b81a6

Browse files
committed
[IMP] change ERP Proxy to support Multipart queries
1 parent d6aaa22 commit 21b81a6

File tree

1 file changed

+7
-27
lines changed

1 file changed

+7
-27
lines changed

modules/shopinvader/runtime/server/erpProxy.ts

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { createError, readBody, getMethod, setResponseHeader, setResponseStatus } from 'h3';
2-
import https from 'https';
1+
import { createError, proxyRequest } from 'h3';
2+
33
export default defineEventHandler(async (event) => {
4+
45
const runtimeConfig = useRuntimeConfig()?.shopinvader || {}
56
const config = runtimeConfig?.erp?.proxy || null
67
if(!config) {
@@ -9,42 +10,21 @@ export default defineEventHandler(async (event) => {
910
statusMessage: 'No proxy config found',
1011
});
1112
}
13+
1214
try {
13-
const method = getMethod(event)
1415
const url:string = event.node.req.url?.replace('/shopinvader', '') || '';
1516
const reqHeaders = getHeaders(event)
1617

17-
const body = method !== 'GET' && method !== 'HEAD' ? await readBody(event) : null
18-
const agent = new https.Agent({
19-
rejectUnauthorized: false,
20-
})
21-
22-
const headers:any = {
18+
let headers:HeadersInit = {
2319
'Content-Type': reqHeaders?.['content-type'] || 'application/json',
2420
}
2521

26-
if(reqHeaders?.cookie) {
27-
headers['cookie'] = reqHeaders.cookie
28-
}
2922
if(config?.auth) {
3023
headers['Authorization'] = config.auth
3124
}
32-
33-
const response = await $fetch.raw(`${config?.url}${url}`, {
34-
method,
35-
agent,
36-
body,
37-
headers
25+
return await proxyRequest(event, `${config?.url}${url}`, {
26+
headers,
3827
})
39-
const cookies = response.headers.get('set-cookie')
40-
const contentType = response.headers.get('content-type') || 'application/json'
41-
if(cookies) {
42-
setResponseHeader(event, 'set-cookie', cookies)
43-
}
44-
setResponseHeader(event, 'content-type', contentType)
45-
setResponseStatus(event, response.status)
46-
47-
return response._data
4828
} catch (error:any) {
4929
return createError({
5030
statusCode: error?.response?.status,

0 commit comments

Comments
 (0)