test(apihits): framework for apiHits to be implemented but no db insert yet

This commit is contained in:
2025-03-10 16:16:46 -05:00
parent 008717b988
commit 22e050ebfa
2 changed files with 37 additions and 4 deletions

View File

@@ -6,6 +6,7 @@ const requestSchema = z.object({
ip: z.string().optional(),
endpoint: z.string(),
action: z.string().optional(),
lastBody: z.string().optional(),
stats: z.string().optional(),
});
@@ -13,19 +14,25 @@ type ApiHitData = z.infer<typeof requestSchema>;
export const apiHit = async (
c: Context,
data: unknown
data: ApiHitData
): Promise<{success: boolean; data?: ApiHitData; errors?: any[]}> => {
// console.log(data);
try {
// Extract IP from request headers or connection info
const forwarded = c.req.header("host");
//console.log(forwarded);
console.log(forwarded);
// Validate the data
const validatedData = requestSchema.parse(data);
const checkData = {
ip: forwarded!,
endpoint: data?.endpoint,
lastBody: data?.lastBody,
action: data?.action,
stats: data?.stats,
};
const validatedData = requestSchema.parse(checkData);
// Proceed with the validated data
// console.log("Validated Data:", validatedData);
return {success: true, data: validatedData};
} catch (error) {
// Explicitly check if the error is an instance of ZodError