-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1108 from dpc-sdp/feature/SRM-1302-proxy-files
[SRM-1302] proxy relative file paths
- Loading branch information
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
packages/nuxt-ripple/server/routes/sites/default/files/[...].ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
//@ts-nocheck runtime imports | ||
import { defineEventHandler, H3Event } from 'h3' | ||
import { createHandler, logger } from '@dpc-sdp/ripple-tide-api' | ||
import { createProxyMiddleware } from 'http-proxy-middleware' | ||
|
||
export const createFilesProxyHandler = async (event: H3Event) => { | ||
const { public: config } = useRuntimeConfig() | ||
|
||
const proxyMiddleware = createProxyMiddleware({ | ||
target: config.tide.baseUrl, | ||
logger: logger, | ||
changeOrigin: true, | ||
on: { | ||
proxyReq(proxyReq) { | ||
proxyReq.setHeader('X-Sdp-Request-Location', 'tide') | ||
}, | ||
proxyRes(proxyRes) { | ||
if (proxyRes?.headers) { | ||
proxyRes.headers['X-Sdp-App-Type'] = 'tide' | ||
} | ||
} | ||
} | ||
}) | ||
|
||
return createHandler(event, 'TideFilesProxyHandler', async () => { | ||
await new Promise((resolve, reject) => { | ||
proxyMiddleware(event.node.req, event.node.res, (err) => { | ||
if (err) { | ||
reject(err) | ||
} else { | ||
resolve(true) | ||
} | ||
}) | ||
}) | ||
}) | ||
} | ||
|
||
export default defineEventHandler(async (event: H3Event) => { | ||
return createFilesProxyHandler(event) | ||
}) |