api/routes/op.post.ts

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

36 lines · 959 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  } catch (error) {
29    console.error(error)
30
31    throw createError({
32      statusCode: 502,
33      message: 'Failed to proxy request'
34    })
35  }
36})