Files
test1/app/api/oapi-client.ts
2026-03-20 07:33:46 +00:00

85 lines
1.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { NextResponse } from 'next/server';
import { HTTPClient } from '@/http';
import { logger } from '@/utils/logger'
export const oapiClient = new HTTPClient({
baseURL: process.env.NOVA_BASE_URL,
headers: {
'Content-Type': 'application/json',
'Tenant-Id': process.env.NOVA_TENANT_ID,
'Authorization': process.env.NOVA_ACCESS_KEY,
}
}, {
onRequest: async (config) => {
logger.info('oapi request start', {
method: config.method,
url: config.url,
body: config.body,
query: config.query,
headers: config.headers,
})
return config
},
onResponse: async (response, config) => {
logger.info('oapi response received', {
method: config.method,
url: config.url,
})
},
onError: (error, config) => {
logger.error('oapi request error', {
method: config.method,
url: config.url,
error
})
}
})
export const sendResponse = (res: any) => {
// 兼容 HTTP 层 defaultGetResult 的解包结果:
// 若上游返回 { success: true, data: ... },此处可能拿到 data 本身(含 null
if (res == null || typeof res !== 'object' || !('success' in res)) {
return NextResponse.json(
{
success: true,
data: res,
},
{ status: 200 }
)
}
if (res.success === false) {
logger.error('oapi request failed', {
code: res.code,
message: res.message,
request_id: res.request_id,
})
return NextResponse.json(
{
code: res.code,
message: res.message,
request_id: res.request_id,
success: false,
},
{ status: 500 }
)
}
logger.info('oapi request success', {
code: res.code,
request_id: res.request_id,
})
return NextResponse.json(
{
code: res.code,
request_id: res.request_id,
success: true,
data: res,
},
{ status: 200 }
)
}