Skip to content

Commit

Permalink
Merge pull request #1108 from dpc-sdp/feature/SRM-1302-proxy-files
Browse files Browse the repository at this point in the history
[SRM-1302] proxy relative file paths
  • Loading branch information
dylankelly authored Apr 14, 2024
2 parents 73bec93 + b67a3e5 commit 838e1b2
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions packages/nuxt-ripple/server/routes/sites/default/files/[...].ts
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)
})

0 comments on commit 838e1b2

Please sign in to comment.