Skip to content

Commit e67935d

Browse files
committed
chore: add eslint
1 parent 6e5268e commit e67935d

5 files changed

Lines changed: 88 additions & 14 deletions

File tree

packages/core/eslint.config.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import globals from "globals";
2+
import pluginJs from "@eslint/js";
3+
import tseslint from "typescript-eslint";
4+
import prettier from "eslint-plugin-prettier/recommended"
5+
6+
export default [
7+
{languageOptions: { globals: globals.browser }},
8+
pluginJs.configs.recommended,
9+
...tseslint.configs.recommended,
10+
prettier,
11+
];

packages/core/package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,15 @@
3939
"zod": "^3.22.4"
4040
},
4141
"devDependencies": {
42+
"@eslint/js": "^9.0.0",
4243
"@types/node": "^18.16.0",
4344
"@typescript-eslint/eslint-plugin": "^7.0.2",
44-
"eslint": "^8.56.0",
45-
"eslint-config-prettier": "^8.8.0",
45+
"eslint": "^8.57.0",
46+
"eslint-config-prettier": "^8.10.0",
4647
"eslint-plugin-prettier": "^5.1.3",
48+
"globals": "^15.0.0",
4749
"typescript": "^5.3.3",
48-
"typescript-eslint": "^7.0.2",
50+
"typescript-eslint": "^7.6.0",
4951
"unbuild": "^2.0.0",
5052
"vite": "^5.1.4",
5153
"vitest": "^1.3.1"

packages/core/src/index.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { z } from "zod"
22
import { baseLogSchema } from "./schemas"
3-
import { catchUncaughtAction, catchUncaughtRoute } from "./request-utils"
3+
import {
4+
catchUncaughtAction,
5+
catchUncaughtRoute,
6+
json,
7+
redirect,
8+
response,
9+
} from "./request-utils"
410
import { buildJsonLog, buildTextLog } from "./utils"
511

612
export type FlytrapLogsOptions = {
@@ -71,7 +77,7 @@ export function createFlytrapLogger<T>({
7177
method: "GET",
7278
} satisfies z.infer<typeof baseLogSchema>
7379

74-
let logs: Array<Partial<Log>> = [defaultLog as Log]
80+
const logs: Array<Partial<Log>> = [defaultLog as Log]
7581

7682
// Function defintions
7783
function getContext() {
@@ -125,12 +131,27 @@ export function createFlytrapLogger<T>({
125131
fn: T,
126132
options?: Partial<z.infer<typeof baseLogSchema>>
127133
) {
134+
// @ts-expect-error
128135
return catchUncaughtAction(fn, addContext, flush, options)
129136
},
130137
catchUncaughtRoute<T extends { params: Record<string, unknown> }>(
131-
fn: (request: Request, context: T) => Promise<Response> | Response
138+
fn: (request: Request, context: T) => Promise<Response> | Response,
139+
options?: Partial<z.infer<typeof baseLogSchema>>
132140
) {
133-
return catchUncaughtRoute(fn, addContext, flush)
141+
// @ts-expect-error
142+
return catchUncaughtRoute(fn, addContext, flush, options)
143+
},
144+
response(body: BodyInit, opts: ResponseInit = {}) {
145+
// @ts-expect-error
146+
return response(body, opts, addContext)
147+
},
148+
json(data: any, opts: ResponseInit = {}) {
149+
// @ts-expect-error
150+
return json(data, opts, addContext)
151+
},
152+
redirect(url: string | URL, status?: number) {
153+
// @ts-expect-error
154+
return redirect(url, status, addContext)
134155
},
135156
}
136157
}

packages/core/src/request-utils.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { createFlytrapLogger } from "./index"
21
import { serializeError } from "serialize-error"
32
import { baseLogSchema } from "./schemas"
43
import { z } from "zod"
@@ -8,7 +7,7 @@ import { AddContextFn, FlushFn } from "./types"
87
export function response(
98
body: BodyInit,
109
opts: ResponseInit = {},
11-
addContext: ReturnType<typeof createFlytrapLogger>["addContext"]
10+
addContext: AddContextFn<z.infer<typeof baseLogSchema>>
1211
) {
1312
addContext({
1413
res: parseJsonOrPassthrough(body),
@@ -18,15 +17,40 @@ export function response(
1817
return new Response(body, opts)
1918
}
2019

20+
export function json(
21+
data: any,
22+
opts: ResponseInit = {},
23+
addContext: AddContextFn<z.infer<typeof baseLogSchema>>
24+
) {
25+
addContext({
26+
res: data,
27+
http_status: opts.status ?? 200,
28+
})
29+
return Response.json(data, opts)
30+
}
31+
32+
export function redirect(
33+
url: string | URL,
34+
status: number = 302,
35+
addContext: AddContextFn<z.infer<typeof baseLogSchema>>
36+
) {
37+
addContext({
38+
http_status: status,
39+
})
40+
return Response.redirect(url, status)
41+
}
42+
2143
export function catchUncaughtRoute<
22-
T extends { params: Record<string, unknown> }
44+
T extends { params: Record<string, unknown> },
2345
>(
2446
fn: (request: Request, context: T) => Promise<Response> | Response,
2547
addContext: AddContextFn<z.infer<typeof baseLogSchema>>,
26-
flush: FlushFn
48+
flush: FlushFn,
49+
options?: Partial<z.infer<typeof baseLogSchema>>
2750
): (request: Request, context: T) => Promise<Response> {
2851
return async (request: Request, context: T) => {
2952
const t0 = Date.now()
53+
if (options) addContext(options)
3054
try {
3155
addContext({
3256
req_headers: headersToRecord(request.headers),

pnpm-lock.yaml

Lines changed: 19 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)