-
Notifications
You must be signed in to change notification settings - Fork 27
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
6 changed files
with
112 additions
and
29 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 |
---|---|---|
@@ -1,32 +1,31 @@ | ||
import { z } from "zod"; | ||
import { z } from 'zod'; | ||
|
||
import { createTRPCRouter, publicProcedure } from "~/server/api/trpc"; | ||
import { createTRPCRouter, publicProcedure } from '~/server/api/trpc'; | ||
|
||
let post = { | ||
id: 1, | ||
name: "Hello World", | ||
name: 'Hello World', | ||
}; | ||
|
||
export const postRouter = createTRPCRouter({ | ||
hello: publicProcedure | ||
.input(z.object({ text: z.string() })) | ||
.query(({ input }) => { | ||
return { | ||
greeting: `Hello ${input.text}`, | ||
}; | ||
}), | ||
hello: publicProcedure.input(z.object({ text: z.string() })).query(({ ctx, input }) => { | ||
ctx.log.info('Hello from the `hello` procedure', { input }); | ||
return { | ||
greeting: `Hello ${input.text}`, | ||
}; | ||
}), | ||
|
||
create: publicProcedure | ||
.input(z.object({ name: z.string().min(1) })) | ||
.mutation(async ({ input }) => { | ||
// simulate a slow db call | ||
await new Promise((resolve) => setTimeout(resolve, 1000)); | ||
create: publicProcedure.input(z.object({ name: z.string().min(1) })).mutation(async ({ ctx, input }) => { | ||
// simulate a slow db call | ||
await new Promise((resolve) => setTimeout(resolve, 1000)); | ||
|
||
post = { id: post.id + 1, name: input.name }; | ||
return post; | ||
}), | ||
post = { id: post.id + 1, name: input.name }; | ||
ctx.log.info('Hello from `create` procedure.', { post }); | ||
return post; | ||
}), | ||
|
||
getLatest: publicProcedure.query(() => { | ||
getLatest: publicProcedure.query((opts) => { | ||
opts.ctx.log.info('Hello from `getLatest` procedure.', { latestPost: post }); | ||
return post; | ||
}), | ||
}); |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -52,6 +52,7 @@ | |
"typescript": "^5.1.6" | ||
}, | ||
"dependencies": { | ||
"@trpc/server": "^10.43.3", | ||
"whatwg-fetch": "^3.6.2" | ||
} | ||
} |
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,52 @@ | ||
import { experimental_standaloneMiddleware } from '@trpc/server'; | ||
import { Logger, type RequestReport } from './logger'; | ||
import { type NextRequest } from 'next/server'; | ||
|
||
export type axiomTRPCMiddlewareCtx = { | ||
/** | ||
* TODO: | ||
* I think it's probably better to pass req at the root instead of axiomTRPCMeta | ||
* so that it can also be used in other places instead of possibly needing to be passed twice | ||
*/ | ||
req: Request | NextRequest; | ||
/** | ||
* TODO: | ||
* figure out the best name for this - it's anything you want to stick on all logs | ||
* that are sent throughout the duration of this procedure. | ||
*/ | ||
axiomTRPCMeta: Record<string, unknown>; | ||
}; | ||
|
||
export const axiomTRPCMiddleware = experimental_standaloneMiddleware<{ | ||
ctx: axiomTRPCMiddlewareCtx; | ||
}>().create((opts) => { | ||
const { req } = opts.ctx; | ||
|
||
let region = ''; | ||
if ('geo' in req) { | ||
region = req.geo?.region ?? ''; | ||
} | ||
|
||
const report: RequestReport = { | ||
startTime: new Date().getTime(), | ||
path: req.url, | ||
method: req.method, | ||
host: req.headers.get('host'), | ||
userAgent: req.headers.get('user-agent'), | ||
scheme: 'https', | ||
ip: req.headers.get('x-forwarded-for'), | ||
region, | ||
}; | ||
|
||
const log = new Logger({ | ||
args: { | ||
input: opts.rawInput, // TODO: put something if nullish? | ||
axiomTRPCMeta: opts.ctx.axiomTRPCMeta, | ||
}, | ||
req: report, | ||
}); | ||
|
||
return opts.next({ | ||
ctx: { log }, | ||
}); | ||
}); |