-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: getting all logs without authentication
- Loading branch information
1 parent
7370449
commit dab3188
Showing
3 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* Takes a URL and returns an object with the query string parameters, multiple of the same key will be an array | ||
*/ | ||
export function getQueryParams(url: string): Record<string, string | string[]> { | ||
const search = new URL(url).searchParams; | ||
const params: Record<string, string | string[]> = {}; | ||
search.forEach((value, key) => { | ||
if (params[key]) { | ||
if (Array.isArray(params[key])) { | ||
params[key] = [...params[key], value]; | ||
} else { | ||
params[key] = [params[key] as string, value]; | ||
} | ||
} else { | ||
params[key] = value; | ||
} | ||
}); | ||
return params; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,40 @@ | ||
import { Hono } from "hono"; | ||
|
||
import type { ServerContext } from "@/types/hono"; | ||
import { getQueryParams } from "@/utils/query-string"; | ||
import { getLogsFiltersSchema } from "./schemas"; | ||
import { db } from "@/config/db"; | ||
|
||
const app = new Hono<ServerContext>(); | ||
|
||
/** | ||
* Get all log entries | ||
*/ | ||
app.get("/", async (c) => { | ||
const searchQuery = getQueryParams(c.req.url); | ||
const search = getLogsFiltersSchema.parse(searchQuery); | ||
|
||
const logLevels = search.level.filter((val) => val !== "all"); | ||
|
||
const logs = await db.query.logs.findMany({ | ||
limit: search.page_size, | ||
offset: search.page_size * (search.page - 1), | ||
orderBy: (fields, { asc, desc }) => (search.sort === "ASC" ? asc(fields.createdAt) : desc(fields.createdAt)), | ||
where: (fields, { and, eq, inArray }) => | ||
and( | ||
// ...[eq(fields.serviceId, data.serviceId)], | ||
...(search.environment ? [eq(fields.environment, search.environment)] : []), | ||
...(search.lookup ? [eq(fields.lookupFilterValue, search.lookup)] : []), | ||
...(logLevels.length > 0 ? [inArray(fields.level, logLevels)] : []), | ||
), | ||
}); | ||
|
||
return c.json(logs); | ||
}); | ||
|
||
/** | ||
* Create a log entry | ||
*/ | ||
app.post("/", async (c) => {}); | ||
|
||
export default app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { z } from "zod"; | ||
|
||
const f = { | ||
action: z.string(), | ||
environment: z.string(), | ||
ip: z.string().nullable().optional(), | ||
lookupFilterValue: z.string().nullable().optional(), | ||
data: z.record(z.string(), z.any()).nullable(), | ||
level: z | ||
.preprocess( | ||
(val) => { | ||
if (val) return val; | ||
return val; | ||
}, | ||
z.enum(["info", "warn", "error", "fatal"]), | ||
) | ||
.default("info"), | ||
levelWithAll: z | ||
.preprocess( | ||
(val) => { | ||
if (val) return val; | ||
return ["all"]; | ||
}, | ||
z.array(z.enum(["all", "info", "warn", "error", "fatal"])), | ||
) | ||
.default(["all"]), | ||
}; | ||
|
||
export const createLogSchema = z.object({ | ||
action: f.action, | ||
environment: f.environment.default("production"), | ||
ip: f.ip, | ||
lookupFilterValue: f.lookupFilterValue, | ||
data: f.data, | ||
level: f.level, | ||
}); | ||
|
||
export const getLogsFiltersSchema = z.object({ | ||
lookup: f.lookupFilterValue.optional(), | ||
environment: f.environment.optional(), | ||
sort: z.enum(["ASC", "DESC"]).default("DESC"), | ||
page: z.number().min(1).default(1), | ||
page_size: z.number().min(1).default(50), | ||
level: f.levelWithAll, | ||
}); |