test(apihits): framework for apiHits to be implemented but no db insert yet
This commit is contained in:
26
database/schema/apiHits.ts
Normal file
26
database/schema/apiHits.ts
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
import {pgTable, text, timestamp} from "drizzle-orm/pg-core";
|
||||||
|
import {createSelectSchema} from "drizzle-zod";
|
||||||
|
|
||||||
|
export const apiHits = pgTable(
|
||||||
|
"apiHits",
|
||||||
|
{
|
||||||
|
ip: text("ip"),
|
||||||
|
endpoint: text("endpoint"),
|
||||||
|
action: text("action"),
|
||||||
|
lastBody: text("lastBody"),
|
||||||
|
stats: text("stats"),
|
||||||
|
add_date: timestamp().defaultNow(),
|
||||||
|
upd_date: timestamp().defaultNow(),
|
||||||
|
}
|
||||||
|
// (table) => [
|
||||||
|
// // uniqueIndex('emailUniqueIndex').on(sql`lower(${table.email})`),
|
||||||
|
// uniqueIndex("role_name").on(table.name),
|
||||||
|
// ]
|
||||||
|
);
|
||||||
|
|
||||||
|
// Schema for inserting a user - can be used to validate API requests
|
||||||
|
// export const insertRolesSchema = createInsertSchema(roles, {
|
||||||
|
// name: z.string().min(3, {message: "Role name must be more than 3 letters"}),
|
||||||
|
// });
|
||||||
|
// Schema for selecting a Expenses - can be used to validate API responses
|
||||||
|
export const selectRolesSchema = createSelectSchema(apiHits);
|
||||||
@@ -6,6 +6,7 @@ const requestSchema = z.object({
|
|||||||
ip: z.string().optional(),
|
ip: z.string().optional(),
|
||||||
endpoint: z.string(),
|
endpoint: z.string(),
|
||||||
action: z.string().optional(),
|
action: z.string().optional(),
|
||||||
|
lastBody: z.string().optional(),
|
||||||
stats: z.string().optional(),
|
stats: z.string().optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -13,19 +14,25 @@ type ApiHitData = z.infer<typeof requestSchema>;
|
|||||||
|
|
||||||
export const apiHit = async (
|
export const apiHit = async (
|
||||||
c: Context,
|
c: Context,
|
||||||
data: unknown
|
data: ApiHitData
|
||||||
): Promise<{success: boolean; data?: ApiHitData; errors?: any[]}> => {
|
): Promise<{success: boolean; data?: ApiHitData; errors?: any[]}> => {
|
||||||
// console.log(data);
|
// console.log(data);
|
||||||
try {
|
try {
|
||||||
// Extract IP from request headers or connection info
|
// Extract IP from request headers or connection info
|
||||||
const forwarded = c.req.header("host");
|
const forwarded = c.req.header("host");
|
||||||
|
|
||||||
//console.log(forwarded);
|
console.log(forwarded);
|
||||||
// Validate the data
|
// 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
|
// Proceed with the validated data
|
||||||
// console.log("Validated Data:", validatedData);
|
|
||||||
return {success: true, data: validatedData};
|
return {success: true, data: validatedData};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// Explicitly check if the error is an instance of ZodError
|
// Explicitly check if the error is an instance of ZodError
|
||||||
|
|||||||
Reference in New Issue
Block a user