mirror of
https://github.com/FranP-code/create-better-t-stack.git
synced 2025-10-12 23:52:15 +00:00
fix oRPC context, route for express
This commit is contained in:
@@ -70,13 +70,12 @@ export async function createContext({ context }: CreateContextOptions) {
|
||||
}
|
||||
|
||||
{{else if (eq backend 'express')}}
|
||||
import type { CreateExpressContextOptions } from "@trpc/server/adapters/express";
|
||||
{{#if auth}}
|
||||
import { fromNodeHeaders } from "better-auth/node";
|
||||
import { auth } from "./auth";
|
||||
{{/if}}
|
||||
|
||||
export async function createContext(opts: CreateExpressContextOptions) {
|
||||
export async function createContext(opts: any) {
|
||||
{{#if auth}}
|
||||
const session = await auth.api.getSession({
|
||||
headers: fromNodeHeaders(opts.req.headers),
|
||||
|
||||
@@ -7,6 +7,9 @@ import { appRouter } from "./routers/index";
|
||||
{{#if (eq api "orpc")}}
|
||||
import { RPCHandler } from "@orpc/server/node";
|
||||
import { appRouter } from "./routers";
|
||||
{{#if auth}}
|
||||
import { createContext } from "./lib/context";
|
||||
{{/if}}
|
||||
{{/if}}
|
||||
import cors from "cors";
|
||||
import express from "express";
|
||||
@@ -16,6 +19,7 @@ import { google } from "@ai-sdk/google";
|
||||
{{/if}}
|
||||
{{#if auth}}
|
||||
import { auth } from "./lib/auth";
|
||||
import { toNodeHandler } from "better-auth/node";
|
||||
{{/if}}
|
||||
|
||||
const app = express();
|
||||
@@ -50,7 +54,11 @@ const handler = new RPCHandler(appRouter);
|
||||
app.use('/rpc{*path}', async (req, res, next) => {
|
||||
const { matched } = await handler.handle(req, res, {
|
||||
prefix: '/rpc',
|
||||
{{#if auth}}
|
||||
context: await createContext({ req }),
|
||||
{{else}}
|
||||
context: {},
|
||||
{{/if}}
|
||||
});
|
||||
if (matched) return;
|
||||
next();
|
||||
@@ -58,9 +66,8 @@ app.use('/rpc{*path}', async (req, res, next) => {
|
||||
{{/if}}
|
||||
|
||||
{{#if (includes examples "ai")}}
|
||||
// AI chat endpoint
|
||||
app.post("/ai", async (req, res) => {
|
||||
const { messages = [] } = req.body;
|
||||
const { messages = [] } = req.body || {};
|
||||
const result = streamText({
|
||||
model: google("gemini-1.5-flash"),
|
||||
messages,
|
||||
@@ -73,6 +80,7 @@ app.get("/", (_req, res) => {
|
||||
res.status(200).send("OK");
|
||||
});
|
||||
|
||||
app.listen(3000, () => {
|
||||
console.log("Server is running on port 3000");
|
||||
const port = process.env.PORT || 3000;
|
||||
app.listen(port, () => {
|
||||
console.log(`Server is running on port ${port}`);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user