feat: add country location to response metadata (#1892)

This commit is contained in:
Shubham Palriwala
2024-01-12 22:13:53 +05:30
committed by GitHub
parent 1635297226
commit ad63be3005
4 changed files with 9 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
import { responses } from "@/app/lib/api/response";
import { transformErrorToDetails } from "@/app/lib/api/validator";
import { sendToPipeline } from "@/app/lib/pipelines";
import { headers } from "next/headers";
import { NextResponse } from "next/server";
import { UAParser } from "ua-parser-js";
@@ -22,6 +23,7 @@ export async function POST(request: Request): Promise<NextResponse> {
responseInput.personId = null;
}
const agent = UAParser(request.headers.get("user-agent"));
const country = headers().get("CF-IPCountry") || headers().get("X-Vercel-IP-Country") || undefined;
const inputValidation = ZResponseLegacyInput.safeParse(responseInput);
if (!inputValidation.success) {
@@ -60,6 +62,7 @@ export async function POST(request: Request): Promise<NextResponse> {
device: agent?.device.type,
os: agent?.os.name,
},
country: country,
};
// check if personId is anonymous

View File

@@ -1,6 +1,7 @@
import { responses } from "@/app/lib/api/response";
import { transformErrorToDetails } from "@/app/lib/api/validator";
import { sendToPipeline } from "@/app/lib/pipelines";
import { headers } from "next/headers";
import { NextResponse } from "next/server";
import { UAParser } from "ua-parser-js";
@@ -45,6 +46,7 @@ export async function POST(request: Request, context: Context): Promise<NextResp
}
const agent = UAParser(request.headers.get("user-agent"));
const country = headers().get("CF-IPCountry") || headers().get("X-Vercel-IP-Country") || undefined;
const inputValidation = ZResponseInput.safeParse({ ...responseInput, environmentId });
if (!inputValidation.success) {
@@ -83,6 +85,7 @@ export async function POST(request: Request, context: Context): Promise<NextResp
device: agent?.device.type,
os: agent?.os.name,
},
country: country,
};
response = await createResponse({

View File

@@ -45,6 +45,7 @@ export const ZResponseMeta = z.object({
device: z.string().optional(),
})
.optional(),
country: z.string().optional(),
});
export type TResponseMeta = z.infer<typeof ZResponseMeta>;
@@ -86,6 +87,7 @@ export const ZResponseInput = z.object({
os: z.string().optional(),
})
.optional(),
country: z.string().optional(),
})
.optional(),
});

View File

@@ -209,6 +209,7 @@ export default function SingleResponseCard({
</p>
)}
{response.meta?.source && <p>Source: {response.meta.source}</p>}
{response.meta?.country && <p>Country: {response.meta.country}</p>}
</div>
)}
</>