Skip to content

Commit 1b8e8d5

Browse files
fix(cli): improve context types for orpc
1 parent 99d059b commit 1b8e8d5

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

apps/cli/src/helpers/core/api-setup.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from "node:path";
22
import fs from "fs-extra";
33
import type { AvailableDependencies } from "../../constants";
4-
import type { Frontend, ProjectConfig } from "../../types";
4+
import type { API, Backend, Frontend, ProjectConfig } from "../../types";
55
import { addPackageDependency } from "../../utils/add-package-deps";
66

77
async function addBackendWorkspaceDependency(
@@ -45,9 +45,9 @@ function getFrontendType(frontend: Frontend[]): {
4545
}
4646

4747
function getApiDependencies(
48-
api: string,
48+
api: API,
4949
frontendType: ReturnType<typeof getFrontendType>,
50-
backend: string,
50+
backend: Backend,
5151
) {
5252
const deps: Record<
5353
string,
@@ -270,6 +270,13 @@ export async function setupApi(config: ProjectConfig) {
270270
});
271271
}
272272

273+
if (backend === "express") {
274+
await addPackageDependency({
275+
devDependencies: ["@types/express"],
276+
projectDir: apiPackageDir,
277+
});
278+
}
279+
273280
if (webDirExists && apiDeps.web) {
274281
await addPackageDependency({
275282
dependencies: apiDeps.web.dependencies as AvailableDependencies[],

apps/cli/templates/api/orpc/server/src/context.ts.hbs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,17 @@ export async function createContext({ context }: CreateContextOptions) {
8888
}
8989

9090
{{else if (eq backend 'express')}}
91+
import type { Request } from "express";
9192
{{#if (eq auth "better-auth")}}
9293
import { fromNodeHeaders } from "better-auth/node";
9394
import { auth } from "@{{projectName}}/auth";
9495
{{/if}}
9596

96-
export async function createContext(opts: any) {
97+
interface CreateContextOptions {
98+
req: Request;
99+
}
100+
101+
export async function createContext(opts: CreateContextOptions) {
97102
{{#if (eq auth "better-auth")}}
98103
const session = await auth.api.getSession({
99104
headers: fromNodeHeaders(opts.req.headers),

apps/cli/templates/api/orpc/web/react/base/src/utils/orpc.ts.hbs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,19 @@ const getORPCClient = createIsomorphicFn()
3333
.server(() =>
3434
createRouterClient(appRouter, {
3535
context: async ({ req }) => {
36+
{{#if (eq backend "self")}}
3637
return createContext({ req });
38+
{{else if (eq backend "hono")}}
39+
return createContext({ context: req });
40+
{{else if (eq backend "elysia")}}
41+
return createContext({ context: req });
42+
{{else if (eq backend "express")}}
43+
return createContext({ req });
44+
{{else if (eq backend "fastify")}}
45+
return createContext(req.headers);
46+
{{else}}
47+
return createContext();
48+
{{/if}}
3749
},
3850
}),
3951
)

0 commit comments

Comments
 (0)