Skip to content

Commit

Permalink
feat: use the builder$ api from prpc
Browse files Browse the repository at this point in the history
  • Loading branch information
OrJDev committed Apr 4, 2024
1 parent 31026ef commit 9616cef
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 67 deletions.
5 changes: 5 additions & 0 deletions .changeset/thick-tables-dress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-jd-app": minor
---

feat: use the builder$ api from prpc
8 changes: 6 additions & 2 deletions src/helpers/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default defineConfig({
external: ["@prisma/client"],
},`
: ""
}${usePRPC ? `\n plugins: [prpcVite()],` : ""}
}${usePRPC ? `${usePrisma ? "\n" : ""} plugins: [prpcVite()],` : ""}
},`
: ""
}${
Expand All @@ -34,7 +34,11 @@ export default defineConfig({
};

export const modifyConfigIfNeeded = async (ctx: ICtx) => {
if (ctx.vercel || ctx.installers.includes("Prisma")) {
if (
ctx.vercel ||
ctx.installers.includes("pRPC") ||
ctx.installers.includes("Prisma")
) {
await fs.writeFile(
path.join(ctx.userDir, "app.config.ts"),
getAppConfig(ctx)
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ const packages = {
"@prisma/client": "^5.10.2",
// prpc
"@tanstack/solid-query": "^5.28.6",
"@solid-mediakit/prpc": "^1.0.4",
"@solid-mediakit/prpc-plugin": "^1.0.2",
"@solid-mediakit/prpc": "^1.2.1",
"@solid-mediakit/prpc-plugin": "^1.2.1",
// next auth
"@solid-mediakit/auth": "^2.0.7",
"@auth/core": "^0.28.0",
Expand Down
13 changes: 0 additions & 13 deletions src/installers/pRPC/files/authMw.txt

This file was deleted.

15 changes: 15 additions & 0 deletions src/installers/pRPC/files/hello.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { z } from "zod";
import { helloBuilder } from "../prpc";

export const helloQuery = helloBuilder
.input(
z.object({
hello: z.string(),
})
)
.query$(({ payload, ctx$ }) => {
if (payload.hello === "hello") {
return ctx$.hello;
}
return ctx$.world;
}, "myNewQuery");
12 changes: 12 additions & 0 deletions src/installers/pRPC/files/user.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { z } from "zod";
import { userBuilder } from "../prpc";

export const protectedQuery = userBuilder
.input(
z.object({
hello: z.string(),
})
)
.query$(({ payload, ctx$ }) => {
return `this is top secret: ${payload.hello} ${ctx$.user?.name}`;
}, "myProtectedQuery");
12 changes: 8 additions & 4 deletions src/installers/pRPC/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ const config: IInstaller = (ctx) => {
}),
files: [
{
path: `${__dirname}/utils/getQueries`,
to: `${ctx.userDir}/src/server/queries.ts`,
path: `${__dirname}/files/hello.txt`,
to: `${ctx.userDir}/src/server/hello/hello.queries.ts`,
},
{
path: `${__dirname}/utils/getBuilder`,
to: `${ctx.userDir}/src/server/prpc.ts`,
type: "exec",
},
useAuth
? {
path: `${__dirname}/files/authMw.txt`,
to: `${ctx.userDir}/src/server/middleware.ts`,
path: `${__dirname}/files/user.txt`,
to: `${ctx.userDir}/src/server/user/user.queries.ts`,
}
: undefined,
],
Expand Down
40 changes: 40 additions & 0 deletions src/installers/pRPC/utils/getBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { IUtil } from "~types";

const getBuilder: IUtil = (ctx) => {
const useAuth = ctx.installers.includes("AuthJS");
return `import { builder$${
useAuth ? ", error$" : ""
} } from "@solid-mediakit/prpc";${
useAuth
? `\nimport { authOptions } from "./auth";\nimport { getSession } from "@solid-mediakit/auth";`
: ""
}
export const helloBuilder = builder$()
.middleware$(() => {
return {
hello: 1,
};
})
.middleware$((ctx) => {
return {
...ctx,
world: 2,
};
});${
useAuth
? `\n\nexport const userBuilder = builder$().middleware$(async ({ event$ }) => {
const session = await getSession(event$.request, authOptions);
if (!session) {
return error$("Unauthorized", {
status: 401,
});
}
return session;
});`
: ""
}
`;
};

export default getBuilder;
34 changes: 0 additions & 34 deletions src/installers/pRPC/utils/getQueries.ts

This file was deleted.

4 changes: 2 additions & 2 deletions template/index/with-auth-prpc-tw.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { type VoidComponent, Suspense, Show } from "solid-js";
import { A } from "@solidjs/router";
import { createSession, signOut, signIn } from "@solid-mediakit/auth/client";
import { testQuery } from "~/server/queries";
import { helloQuery } from "~/server/hello/hello.queries";

const Home: VoidComponent = () => {
const hello = testQuery(() => ({ hello: "from pRPC" }));
const hello = helloQuery(() => ({ hello: "from pRPC" }));
return (
<main class="flex min-h-screen flex-col items-center justify-center bg-gradient-to-b from-[#026d56] to-[#152a2c]">
<div class="container flex flex-col items-center justify-center gap-12 px-4 py-16 ">
Expand Down
4 changes: 2 additions & 2 deletions template/index/with-auth-prpc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import styles from "./index.module.css";
import { type VoidComponent, Suspense, Show } from "solid-js";
import { A } from "@solidjs/router";
import { createSession, signOut, signIn } from "@solid-mediakit/auth/client";
import { testQuery } from "~/server/queries";
import { helloQuery } from "~/server/hello/hello.queries";

const Home: VoidComponent = () => {
const hello = testQuery(() => ({ hello: "from pRPC" }));
const hello = helloQuery(() => ({ hello: "from pRPC" }));
return (
<main>
<div class={styles.container}>
Expand Down
6 changes: 2 additions & 4 deletions template/index/with-prpc-tw.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { type VoidComponent } from "solid-js";
import { A } from "@solidjs/router";
import { testQuery } from "~/server/queries";
import { helloQuery } from "~/server/hello/hello.queries";

const Home: VoidComponent = () => {
const hello = testQuery(() => ({
hello: "from pRPC",
}));
const hello = helloQuery(() => ({ hello: "from pRPC" }));
return (
<main class="flex min-h-screen flex-col items-center justify-center bg-gradient-to-b from-[#026d56] to-[#152a2c]">
<div class="container flex flex-col items-center justify-center gap-12 px-4 py-16 ">
Expand Down
6 changes: 2 additions & 4 deletions template/index/with-prpc.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { type VoidComponent } from "solid-js";
import { A } from "@solidjs/router";
import styles from "./index.module.css";
import { testQuery } from "~/server/queries";
import { helloQuery } from "~/server/hello/hello.queries";

const Home: VoidComponent = () => {
const hello = testQuery(() => ({
hello: "from pRPC",
}));
const hello = helloQuery(() => ({ hello: "from pRPC" }));
return (
<main>
<div class={styles.container}>
Expand Down

0 comments on commit 9616cef

Please sign in to comment.