-
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.
- Loading branch information
Showing
9 changed files
with
157 additions
and
74 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
Empty file.
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,13 @@ | ||
import { type exampleGetByIdResponse } from "../models/example"; | ||
import { LogType, type ILoggerFn } from "../../../src/types"; | ||
|
||
export const exampleGetById = async (id: number, log: ILoggerFn) => { | ||
log(LogType.INFO, { id: id }); | ||
|
||
let res: exampleGetByIdResponse = { | ||
name: "This is an example name", | ||
address: "201 Wannakee Rd", | ||
}; | ||
|
||
return res; | ||
}; |
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,20 @@ | ||
import { Elysia } from "elysia"; | ||
|
||
// plugins | ||
import { elylog } from "../../src/index"; | ||
|
||
// routes | ||
import { ExampleRoutes } from "./routes/example"; | ||
|
||
const app = new Elysia() | ||
// plugins | ||
.use(elylog()) | ||
|
||
// routes | ||
.use(ExampleRoutes) | ||
|
||
.listen(3000); | ||
|
||
console.log( | ||
`🦊 Example API is running at ${app.server?.hostname}:${app.server?.port}` | ||
); |
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,16 @@ | ||
import { t, Static } from "elysia"; | ||
|
||
const exampleGetByIdResponse = t.Object({ | ||
name: t.String({ | ||
description: "Example name", | ||
default: "Test Name", | ||
}), | ||
address: t.String({ | ||
description: "Example street address", | ||
default: "123 Main St", | ||
}), | ||
}); | ||
|
||
export type exampleGetByIdResponse = Static<typeof exampleGetByIdResponse>; | ||
|
||
export { exampleGetByIdResponse }; |
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,18 @@ | ||
import { Elysia, t } from "elysia"; | ||
import { exampleGetById } from "../handlers/example"; | ||
import { exampleGetByIdResponse } from "../models/example"; | ||
import { elylog } from "../../../src/index"; | ||
|
||
export const ExampleRoutes = new Elysia() | ||
.use(elylog()) | ||
.get("/:id", ({ params: { id }, log }) => exampleGetById(id, log), { | ||
params: t.Object({ | ||
id: t.Numeric(), | ||
}), | ||
response: exampleGetByIdResponse, | ||
detail: { | ||
summary: "Get Example by ID", | ||
description: "An example GET request", | ||
tags: ["ExampleTag"], | ||
}, | ||
}); |
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
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
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,58 @@ | ||
export enum LogType { | ||
SYSTEM = "SYS", | ||
METADATA = "META", | ||
INFO = "INFO", | ||
WARNING = "WARN", | ||
ERROR = "ERR", | ||
} | ||
|
||
export type LogBase = { | ||
timestamp: Date; | ||
type: LogType; | ||
uuid: string; | ||
}; | ||
|
||
export type LogRequest = LogBase & { | ||
method: string; | ||
path: string; | ||
}; | ||
|
||
export type LogMessage = LogBase & { | ||
data: object; | ||
}; | ||
|
||
export type LogMetaData = LogBase & { | ||
duration: number; | ||
}; | ||
|
||
export interface ILoggerFn { | ||
(type: LogType, data: object): void; | ||
} | ||
|
||
export interface ILogger { | ||
log: ILoggerFn; | ||
} | ||
|
||
export interface IOptions { | ||
// Elylog generates a request ID (uuidv4) by default on request. | ||
// Set this to a header to use request id's generated by client | ||
// Default: null | ||
headerForRequestId?: string; | ||
|
||
// Log all requests (SYS log type): true (default) | ||
logRequests?: boolean; | ||
|
||
// Log metadata at the end of life cycle (META log type): true (default) | ||
// Metadata: | ||
// - Duration (in ms) | ||
logMetaData?: boolean; | ||
|
||
// Custom print function for request logs (SYS log type): JSON (default) | ||
requestPrintFn?(log: LogRequest): void; | ||
|
||
// Custom print function for log messages (INFO|WARNING|ERROR): JSON (default) | ||
logPrintFn?(log: LogMessage): void; | ||
|
||
// Custom print function for metadata messages (META): JSON (default) | ||
metadataPrintFn?(log: LogMetaData): void; | ||
} |