This proxy is useful as soon as a man in the middle is required for injecting additional and sensitive information into the request to your backend.
pnpm i @deckweiss/api-proxy
// src/hooks.server.ts
import { createApiProxy } from '@deckweiss/api-proxy';
export const handle = createApiProxy({
apiUrl: 'https://example.com',
basePath: '/api/v1' // if you requested path starts with this string, then it gets forwarded to {apiUrl}
})
createApiProxy({
async middleware(event) {
const accessToken = (await event.locals.auth())?.accessToken;
if (accessToken) {
event.request.headers.set('authorization', `Bearer ${accessToken}`);
}
}
})
createApiProxy({
async middleware(event) {
event.request.headers.delete('cookie')
}
})