api/routes/op.post.ts

b2c3e4e6874256bc959c763c2427cd2f3e4e4fcd
wotaku/api/routes/op.post.ts history · blame · raw

37 lines · 958 bytes

 1/**
 2*  All Rights Reserved
 3*
 4*  Copyright (c) 2025 taskylizard
 5*
 6*  All rights reserved. This code and its associated files may not be copied, modified, distributed, sublicensed, or used in any form, in whole or in part, without prior written permission from the copyright holder.
 7*/
 8import { joinURL } from 'ufo'
 9
10export default defineEventHandler(async (event) => {
11  const config = useRuntimeConfig(event).op
12
13  try {
14    const target = joinURL(config.url, 'track')
15
16    const headers = {
17      'X-Forwarded-For': getRequestIP(event, { xForwardedFor: true }),
18      'Content-Type': 'application/json',
19      'openpanel-client-id': config.id,
20    }
21
22    headers['openpanel-sdk-name'] = 'nitro'
23    headers['openpanel-sdk-version'] = '1.0.0'
24
25    return proxyRequest(event, target, {
26      headers,
27    })
28  }
29  catch (error) {
30    console.error(error)
31
32    throw createError({
33      statusCode: 502,
34      message: 'Failed to proxy request',
35    })
36  }
37})