(options: CreateDevProxyAppOptions)
| 237 | } |
| 238 | |
| 239 | export const createDevProxyApp = (options: CreateDevProxyAppOptions) => { |
| 240 | const app = new HonoApp() |
| 241 | const fetchImpl = options.fetchImpl || globalThis.fetch |
| 242 | const logger = options.logger || console |
| 243 | const allowedOrigins = options.cors?.allowedOrigins || 'local' |
| 244 | |
| 245 | app.onError((error, context) => { |
| 246 | logger.error('[dev-proxy]', error) |
| 247 | |
| 248 | const headers = new Headers() |
| 249 | applyCorsHeaders(headers, context.req.header('origin'), allowedOrigins) |
| 250 | |
| 251 | return new Response('Upstream proxy request failed.', { |
| 252 | status: 502, |
| 253 | headers, |
| 254 | }) |
| 255 | }) |
| 256 | |
| 257 | app.use('*', async (context, next) => { |
| 258 | if (context.req.method === 'OPTIONS') { |
| 259 | const headers = new Headers() |
| 260 | applyCorsHeaders(headers, context.req.header('origin'), allowedOrigins) |
| 261 | headers.set('Access-Control-Allow-Methods', ALLOW_METHODS) |
| 262 | headers.set( |
| 263 | 'Access-Control-Allow-Headers', |
| 264 | context.req.header('Access-Control-Request-Headers') || DEFAULT_ALLOW_HEADERS, |
| 265 | ) |
| 266 | if (context.req.header('Access-Control-Request-Private-Network') === 'true') |
| 267 | headers.set('Access-Control-Allow-Private-Network', 'true') |
| 268 | |
| 269 | return new Response(null, { |
| 270 | status: 204, |
| 271 | headers, |
| 272 | }) |
| 273 | } |
| 274 | |
| 275 | await next() |
| 276 | applyCorsHeaders(context.res.headers, context.req.header('origin'), allowedOrigins) |
| 277 | }) |
| 278 | |
| 279 | registerProxyRoutes(app, options.routes, fetchImpl, allowedOrigins) |
| 280 | |
| 281 | return app |
| 282 | } |
no test coverage detected