Skip to content

Commit 9616cef

Browse files
committed
feat: use the builder$ api from prpc
1 parent 31026ef commit 9616cef

File tree

13 files changed

+96
-67
lines changed

13 files changed

+96
-67
lines changed

.changeset/thick-tables-dress.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"create-jd-app": minor
3+
---
4+
5+
feat: use the builder$ api from prpc

src/helpers/config.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default defineConfig({
1919
external: ["@prisma/client"],
2020
},`
2121
: ""
22-
}${usePRPC ? `\n plugins: [prpcVite()],` : ""}
22+
}${usePRPC ? `${usePrisma ? "\n" : ""} plugins: [prpcVite()],` : ""}
2323
},`
2424
: ""
2525
}${
@@ -34,7 +34,11 @@ export default defineConfig({
3434
};
3535

3636
export const modifyConfigIfNeeded = async (ctx: ICtx) => {
37-
if (ctx.vercel || ctx.installers.includes("Prisma")) {
37+
if (
38+
ctx.vercel ||
39+
ctx.installers.includes("pRPC") ||
40+
ctx.installers.includes("Prisma")
41+
) {
3842
await fs.writeFile(
3943
path.join(ctx.userDir, "app.config.ts"),
4044
getAppConfig(ctx)

src/helpers/packages.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ const packages = {
1212
"@prisma/client": "^5.10.2",
1313
// prpc
1414
"@tanstack/solid-query": "^5.28.6",
15-
"@solid-mediakit/prpc": "^1.0.4",
16-
"@solid-mediakit/prpc-plugin": "^1.0.2",
15+
"@solid-mediakit/prpc": "^1.2.1",
16+
"@solid-mediakit/prpc-plugin": "^1.2.1",
1717
// next auth
1818
"@solid-mediakit/auth": "^2.0.7",
1919
"@auth/core": "^0.28.0",

src/installers/pRPC/files/authMw.txt

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/installers/pRPC/files/hello.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { z } from "zod";
2+
import { helloBuilder } from "../prpc";
3+
4+
export const helloQuery = helloBuilder
5+
.input(
6+
z.object({
7+
hello: z.string(),
8+
})
9+
)
10+
.query$(({ payload, ctx$ }) => {
11+
if (payload.hello === "hello") {
12+
return ctx$.hello;
13+
}
14+
return ctx$.world;
15+
}, "myNewQuery");

src/installers/pRPC/files/user.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { z } from "zod";
2+
import { userBuilder } from "../prpc";
3+
4+
export const protectedQuery = userBuilder
5+
.input(
6+
z.object({
7+
hello: z.string(),
8+
})
9+
)
10+
.query$(({ payload, ctx$ }) => {
11+
return `this is top secret: ${payload.hello} ${ctx$.user?.name}`;
12+
}, "myProtectedQuery");

src/installers/pRPC/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@ const config: IInstaller = (ctx) => {
1313
}),
1414
files: [
1515
{
16-
path: `${__dirname}/utils/getQueries`,
17-
to: `${ctx.userDir}/src/server/queries.ts`,
16+
path: `${__dirname}/files/hello.txt`,
17+
to: `${ctx.userDir}/src/server/hello/hello.queries.ts`,
18+
},
19+
{
20+
path: `${__dirname}/utils/getBuilder`,
21+
to: `${ctx.userDir}/src/server/prpc.ts`,
1822
type: "exec",
1923
},
2024
useAuth
2125
? {
22-
path: `${__dirname}/files/authMw.txt`,
23-
to: `${ctx.userDir}/src/server/middleware.ts`,
26+
path: `${__dirname}/files/user.txt`,
27+
to: `${ctx.userDir}/src/server/user/user.queries.ts`,
2428
}
2529
: undefined,
2630
],
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { IUtil } from "~types";
2+
3+
const getBuilder: IUtil = (ctx) => {
4+
const useAuth = ctx.installers.includes("AuthJS");
5+
return `import { builder$${
6+
useAuth ? ", error$" : ""
7+
} } from "@solid-mediakit/prpc";${
8+
useAuth
9+
? `\nimport { authOptions } from "./auth";\nimport { getSession } from "@solid-mediakit/auth";`
10+
: ""
11+
}
12+
13+
export const helloBuilder = builder$()
14+
.middleware$(() => {
15+
return {
16+
hello: 1,
17+
};
18+
})
19+
.middleware$((ctx) => {
20+
return {
21+
...ctx,
22+
world: 2,
23+
};
24+
});${
25+
useAuth
26+
? `\n\nexport const userBuilder = builder$().middleware$(async ({ event$ }) => {
27+
const session = await getSession(event$.request, authOptions);
28+
if (!session) {
29+
return error$("Unauthorized", {
30+
status: 401,
31+
});
32+
}
33+
return session;
34+
});`
35+
: ""
36+
}
37+
`;
38+
};
39+
40+
export default getBuilder;

src/installers/pRPC/utils/getQueries.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.

template/index/with-auth-prpc-tw.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { type VoidComponent, Suspense, Show } from "solid-js";
22
import { A } from "@solidjs/router";
33
import { createSession, signOut, signIn } from "@solid-mediakit/auth/client";
4-
import { testQuery } from "~/server/queries";
4+
import { helloQuery } from "~/server/hello/hello.queries";
55

66
const Home: VoidComponent = () => {
7-
const hello = testQuery(() => ({ hello: "from pRPC" }));
7+
const hello = helloQuery(() => ({ hello: "from pRPC" }));
88
return (
99
<main class="flex min-h-screen flex-col items-center justify-center bg-gradient-to-b from-[#026d56] to-[#152a2c]">
1010
<div class="container flex flex-col items-center justify-center gap-12 px-4 py-16 ">

template/index/with-auth-prpc.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import styles from "./index.module.css";
22
import { type VoidComponent, Suspense, Show } from "solid-js";
33
import { A } from "@solidjs/router";
44
import { createSession, signOut, signIn } from "@solid-mediakit/auth/client";
5-
import { testQuery } from "~/server/queries";
5+
import { helloQuery } from "~/server/hello/hello.queries";
66

77
const Home: VoidComponent = () => {
8-
const hello = testQuery(() => ({ hello: "from pRPC" }));
8+
const hello = helloQuery(() => ({ hello: "from pRPC" }));
99
return (
1010
<main>
1111
<div class={styles.container}>

template/index/with-prpc-tw.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import { type VoidComponent } from "solid-js";
22
import { A } from "@solidjs/router";
3-
import { testQuery } from "~/server/queries";
3+
import { helloQuery } from "~/server/hello/hello.queries";
44

55
const Home: VoidComponent = () => {
6-
const hello = testQuery(() => ({
7-
hello: "from pRPC",
8-
}));
6+
const hello = helloQuery(() => ({ hello: "from pRPC" }));
97
return (
108
<main class="flex min-h-screen flex-col items-center justify-center bg-gradient-to-b from-[#026d56] to-[#152a2c]">
119
<div class="container flex flex-col items-center justify-center gap-12 px-4 py-16 ">

template/index/with-prpc.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import { type VoidComponent } from "solid-js";
22
import { A } from "@solidjs/router";
33
import styles from "./index.module.css";
4-
import { testQuery } from "~/server/queries";
4+
import { helloQuery } from "~/server/hello/hello.queries";
55

66
const Home: VoidComponent = () => {
7-
const hello = testQuery(() => ({
8-
hello: "from pRPC",
9-
}));
7+
const hello = helloQuery(() => ({ hello: "from pRPC" }));
108
return (
119
<main>
1210
<div class={styles.container}>

0 commit comments

Comments
 (0)