refactor(server): moved the server files outside the src to improve static files
This commit is contained in:
26
server/globalUtils/apiHits.d.ts
vendored
Normal file
26
server/globalUtils/apiHits.d.ts
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
import type { Context } from "hono";
|
||||
import { z } from "zod";
|
||||
declare const requestSchema: z.ZodObject<{
|
||||
ip: z.ZodOptional<z.ZodString>;
|
||||
endpoint: z.ZodString;
|
||||
action: z.ZodOptional<z.ZodString>;
|
||||
stats: z.ZodOptional<z.ZodString>;
|
||||
}, "strip", z.ZodTypeAny, {
|
||||
ip?: string;
|
||||
endpoint?: string;
|
||||
action?: string;
|
||||
stats?: string;
|
||||
}, {
|
||||
ip?: string;
|
||||
endpoint?: string;
|
||||
action?: string;
|
||||
stats?: string;
|
||||
}>;
|
||||
type ApiHitData = z.infer<typeof requestSchema>;
|
||||
export declare const apiHit: (c: Context, data: unknown) => Promise<{
|
||||
success: boolean;
|
||||
data?: ApiHitData;
|
||||
errors?: any[];
|
||||
}>;
|
||||
export {};
|
||||
//# sourceMappingURL=apiHits.d.ts.map
|
||||
1
server/globalUtils/apiHits.d.ts.map
Normal file
1
server/globalUtils/apiHits.d.ts.map
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"apiHits.d.ts","sourceRoot":"","sources":["apiHits.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,OAAO,EAAC,MAAM,MAAM,CAAC;AAClC,OAAO,EAAC,CAAC,EAAW,MAAM,KAAK,CAAC;AAGhC,QAAA,MAAM,aAAa;;;;;;;;;;;;;;;EAKjB,CAAC;AAEH,KAAK,UAAU,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,aAAa,CAAC,CAAC;AAEhD,eAAO,MAAM,MAAM,MACZ,OAAO,QACJ,OAAO,KACd,OAAO,CAAC;IAAC,OAAO,EAAE,OAAO,CAAC;IAAC,IAAI,CAAC,EAAE,UAAU,CAAC;IAAC,MAAM,CAAC,EAAE,GAAG,EAAE,CAAA;CAAC,CAuB/D,CAAC"}
|
||||
20
server/globalUtils/apiHits.js
Normal file
20
server/globalUtils/apiHits.js
Normal file
@@ -0,0 +1,20 @@
|
||||
import { z, ZodError } from "zod";
|
||||
const requestSchema = z.object({
|
||||
ip: z.string().optional(),
|
||||
endpoint: z.string(),
|
||||
action: z.string().optional(),
|
||||
stats: z.string().optional(),
|
||||
});
|
||||
export const apiHit = async (c, data) => {
|
||||
try {
|
||||
const forwarded = c.req.header("host");
|
||||
const validatedData = requestSchema.parse(data);
|
||||
return { success: true, data: validatedData };
|
||||
}
|
||||
catch (error) {
|
||||
if (error instanceof ZodError) {
|
||||
return { success: false, errors: error.errors };
|
||||
}
|
||||
return { success: false, errors: [{ message: "An unknown error occurred" }] };
|
||||
}
|
||||
};
|
||||
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