refactor(server): moved the server files outside the src to improve static files
This commit is contained in:
40
server/globalUtils/apiHits.ts
Normal file
40
server/globalUtils/apiHits.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import type {Context} from "hono";
|
||||
import {z, ZodError} from "zod";
|
||||
|
||||
// Define the request body schema
|
||||
const requestSchema = z.object({
|
||||
ip: z.string().optional(),
|
||||
endpoint: z.string(),
|
||||
action: z.string().optional(),
|
||||
stats: z.string().optional(),
|
||||
});
|
||||
|
||||
type ApiHitData = z.infer<typeof requestSchema>;
|
||||
|
||||
export const apiHit = async (
|
||||
c: Context,
|
||||
data: unknown
|
||||
): 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);
|
||||
// Validate the data
|
||||
const validatedData = requestSchema.parse(data);
|
||||
|
||||
// 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
|
||||
if (error instanceof ZodError) {
|
||||
// console.log({success: false, errors: error.errors});
|
||||
return {success: false, errors: error.errors};
|
||||
}
|
||||
|
||||
// Catch other unexpected errors
|
||||
return {success: false, errors: [{message: "An unknown error occurred"}]};
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user