feat: add client info in backend fetch header

This commit is contained in:
2026-01-20 08:25:10 +07:00
parent cb436fe40c
commit e27b18b22e
5 changed files with 37 additions and 3 deletions

View File

@ -1,4 +1,7 @@
"use server";
import { headers } from "next/headers";
import { UAParser } from "ua-parser-js";
export interface BackendResponse<T = unknown> {
success: boolean;
message: string;
@ -7,10 +10,23 @@ export interface BackendResponse<T = unknown> {
}
export const backendFetch = async (path: string, options: RequestInit = {}) => {
const userAgent = (await headers()).get("user-agent") || "";
const userIp = (await headers()).get("x-forwarded-for") || "unknown";
const ua = new UAParser(userAgent).getResult();
const clientInfo = {
os: ua.os.name ?? "unknown",
osVersion: ua.os.version ?? "unknown",
browser: ua.browser.name ?? "unknown",
browserVersion: ua.browser.version ?? "unknown",
deviceType: ua.device.type ?? "desktop",
ip: userIp,
};
const res = await fetch(`${process.env.BACKEND_ENDPOINT}/${path}`, {
...options,
headers: {
"Content-Type": "application/json",
"x-client-info": JSON.stringify(clientInfo),
Authorization: `Bearer ${process.env.BACKEND_API_KEY}`,
...options.headers,
},