-
-
Notifications
You must be signed in to change notification settings - Fork 26
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
21 changed files
with
631 additions
and
21 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"create-jd-app": patch | ||
--- | ||
|
||
Feat: tRPC installer |
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
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 |
---|---|---|
@@ -1,28 +1,45 @@ | ||
import { type IUtil } from "~types"; | ||
import type { ICtx, IUtil } from "~types"; | ||
|
||
const getIndexLocation: IUtil = (ctx) => { | ||
const usingPRPC = ctx.installers.includes("pRPC"); | ||
const usingTRPC = ctx.installers.includes("tRPC"); | ||
const usingTw = | ||
ctx.installers.includes("TailwindCSS") || ctx.installers.includes("UnoCSS"); | ||
const usingAuth = ctx.installers.includes("AuthJS"); | ||
return createFileHelper( | ||
usingTRPC || usingPRPC, | ||
usingTw, | ||
usingAuth, | ||
usingTRPC ? "tRPC" : "pRPC", | ||
ctx | ||
); | ||
}; | ||
|
||
export default getIndexLocation; | ||
|
||
function createFileHelper( | ||
usingRPC: boolean, | ||
usingTw: boolean, | ||
usingAuth: boolean, | ||
rpc: "pRPC" | "tRPC", | ||
ctx: ICtx | ||
) { | ||
const fileName = `${rpc.toLowerCase()}`; | ||
let indexFile = ""; | ||
if (usingPRPC && usingTw && usingAuth) { | ||
indexFile = "with-auth-prpc-tw.tsx"; | ||
} else if (usingPRPC && !usingTw && usingAuth) { | ||
indexFile = "with-auth-prpc.tsx"; | ||
} else if (usingPRPC && usingTw) { | ||
indexFile = "with-prpc-tw.tsx"; | ||
} else if (usingPRPC && !usingTw) { | ||
indexFile = "with-prpc.tsx"; | ||
if (usingRPC && usingTw && usingAuth) { | ||
indexFile = `with-auth-${fileName}-tw.tsx`; | ||
} else if (usingRPC && !usingTw && usingAuth) { | ||
indexFile = `with-auth-${fileName}.tsx`; | ||
} else if (usingRPC && usingTw) { | ||
indexFile = `with-${fileName}-tw.tsx`; | ||
} else if (usingRPC && !usingTw) { | ||
indexFile = `with-${fileName}.tsx`; | ||
} else if (usingAuth && usingTw) { | ||
indexFile = "with-auth-tw.tsx"; | ||
} else if (!usingPRPC && usingTw) { | ||
} else if (!usingRPC && usingTw) { | ||
indexFile = "with-tw.tsx"; | ||
} else if (usingAuth) { | ||
indexFile = "with-auth.tsx"; | ||
} | ||
indexFile = indexFile ? `${ctx.templateDir}/index/${indexFile}` : ``; | ||
return indexFile; | ||
}; | ||
|
||
export default getIndexLocation; | ||
return indexFile ? `${ctx.templateDir}/index/${indexFile}` : ``; | ||
} |
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,26 @@ | ||
import { QueryClient } from "@tanstack/solid-query"; | ||
import type { IAppRouter } from "~/server/trpc/router/_app"; | ||
import { createTRPCSolidStart } from "solid-trpc"; | ||
import { httpBatchLink } from "@trpc/client"; | ||
|
||
const getBaseUrl = () => { | ||
if (typeof window !== "undefined") return ""; | ||
if (process.env.VERCEL_URL) return `https://${process.env.VERCEL_URL}`; | ||
// replace example.com with your actual production url | ||
if (process.env.NODE_ENV === "production") return "https://example.com"; | ||
return `http://localhost:${process.env.PORT ?? 3000}`; | ||
}; | ||
|
||
export const trpc = createTRPCSolidStart<IAppRouter>({ | ||
config() { | ||
return { | ||
links: [ | ||
httpBatchLink({ | ||
url: `${getBaseUrl()}/api/trpc`, | ||
}), | ||
], | ||
}; | ||
}, | ||
}); | ||
|
||
export const queryClient = new QueryClient(); |
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,21 @@ | ||
import { QueryClient } from "@tanstack/solid-query"; | ||
import type { IAppRouter } from "~/server/trpc/router/_app"; | ||
import { createTRPCSolid } from "solid-trpc"; | ||
import { httpBatchLink } from "@trpc/client"; | ||
|
||
const getBaseUrl = () => { | ||
if (typeof window !== "undefined") return ""; | ||
return `http://localhost:${process.env.PORT ?? 3000}`; | ||
}; | ||
|
||
export const trpc = createTRPCSolid<IAppRouter>(); | ||
|
||
export const client = trpc.createClient({ | ||
links: [ | ||
httpBatchLink({ | ||
url: `${getBaseUrl()}/api/trpc`, | ||
}), | ||
], | ||
}); | ||
|
||
export const queryClient = new QueryClient(); |
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,51 @@ | ||
import { withPackages } from "~helpers/packages"; | ||
import type { IInstaller } from "~types"; | ||
|
||
const config: IInstaller = (ctx) => ({ | ||
files: [ | ||
{ | ||
path: `${ctx.templateDir}/trpc/server`, | ||
to: `${ctx.userDir}/src/server/trpc`, | ||
sep: true, | ||
}, | ||
{ | ||
path: `${__dirname}/files/${ctx.ssr ? "ssr-utils" : "utils"}.txt`, | ||
to: `${ctx.userDir}/src/utils/trpc.ts`, | ||
}, | ||
{ | ||
path: `${__dirname}/utils/getRoot`, | ||
type: "exec", | ||
to: `${ctx.userDir}/src/root.tsx`, | ||
}, | ||
{ | ||
path: `${ctx.templateDir}/trpc/api`, | ||
to: `${ctx.userDir}/src/routes/api`, | ||
}, | ||
{ | ||
path: `${__dirname}/utils/getTrpcUtils`, | ||
to: `${ctx.userDir}/src/server/trpc/utils.ts`, | ||
type: "exec", | ||
}, | ||
{ | ||
path: `${__dirname}/utils/getTrpcContext`, | ||
to: `${ctx.userDir}/src/server/trpc/context.ts`, | ||
type: "exec", | ||
}, | ||
{ | ||
path: `${__dirname}/utils/getMainRouter`, | ||
to: `${ctx.userDir}/src/server/trpc/router/example.ts`, | ||
type: "exec", | ||
}, | ||
], | ||
pkgs: withPackages({ | ||
normal: [ | ||
ctx.ssr ? "solid-trpc->ssr" : "solid-trpc", | ||
"@tanstack/solid-query", | ||
"solid-start-trpc", | ||
"@trpc/client", | ||
"@trpc/server", | ||
], | ||
}), | ||
}); | ||
|
||
export default config; |
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,28 @@ | ||
import type { IUtil } from "~types"; | ||
|
||
const getMainRouter: IUtil = (ctx) => { | ||
const useNextAuth = ctx.installers.includes("AuthJS"); | ||
return `import { z } from "zod"; | ||
import { procedure, router${ | ||
useNextAuth ? ", protectedProcedure" : "" | ||
} } from "../utils"; | ||
export default router({ | ||
hello: procedure.input(z.object({ name: z.string() })).query(({ input }) => { | ||
return \`Hello \${input.name}\`; | ||
}), | ||
random: procedure | ||
.input(z.object({ num: z.number() })) | ||
.mutation(({ input }) => { | ||
return Math.floor(Math.random() * 100) / input.num; | ||
}),${ | ||
useNextAuth | ||
? `\n secret: protectedProcedure.query(({ ctx }) => { | ||
return \`This is top secret - \${ctx.session.user.name}\`; | ||
}),` | ||
: "" | ||
} | ||
}); | ||
`; | ||
}; | ||
|
||
export default getMainRouter; |
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 { type IUtil } from "~types"; | ||
|
||
const getRoot: IUtil = (ctx) => { | ||
return `// @refresh reload | ||
import "./root.css"; | ||
import { Suspense } from "solid-js"; | ||
import { | ||
Body, | ||
ErrorBoundary, | ||
FileRoutes, | ||
Head, | ||
Html, | ||
Meta, | ||
Routes, | ||
Scripts, | ||
Title, | ||
Link, | ||
} from "solid-start"; | ||
import { trpc, queryClient${ctx.ssr ? "" : ", client"} } from "~/utils/trpc"; | ||
export default function Root() { | ||
return ( | ||
<Html lang="en"> | ||
<Head> | ||
<Title>Create JD App</Title> | ||
<Meta charset="utf-8" /> | ||
<Meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<Meta name="theme-color" content="#026d56" /> | ||
<Meta name="description" content="Generated by create-jd-app" /> | ||
<Link rel="icon" href="/favicon.ico" /> | ||
</Head> | ||
<Body> | ||
<trpc.Provider${ | ||
ctx.ssr ? "" : " client={client}" | ||
} queryClient={queryClient}> | ||
<Suspense> | ||
<ErrorBoundary> | ||
<Routes> | ||
<FileRoutes /> | ||
</Routes> | ||
</ErrorBoundary> | ||
</Suspense> | ||
</trpc.Provider> | ||
<Scripts /> | ||
</Body> | ||
</Html> | ||
); | ||
} | ||
`; | ||
}; | ||
|
||
export default getRoot; |
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,34 @@ | ||
import type { IUtil } from "~types"; | ||
|
||
const getTrpcContext: IUtil = (ctx) => { | ||
const usePrisma = ctx.installers.includes("Prisma"); | ||
const useNextAuth = ctx.installers.includes("AuthJS"); | ||
return `import type { inferAsyncReturnType } from "@trpc/server"; | ||
import type { createSolidAPIHandlerContext } from "solid-start-trpc";${ | ||
usePrisma ? `\nimport { prisma } from "~/server/db/client";` : "" | ||
}${ | ||
useNextAuth | ||
? `\nimport { getSession } from "@auth/solid-start";\nimport { authOpts } from "~/routes/api/auth/[...solidauth]";` | ||
: "" | ||
} | ||
export const createContextInner = async ( | ||
opts: createSolidAPIHandlerContext | ||
) => {${ | ||
useNextAuth | ||
? `\n const session = await getSession(opts.req, authOpts);` | ||
: "" | ||
} | ||
return { | ||
...opts,${usePrisma ? `\n prisma,` : ""}${ | ||
useNextAuth ? `\n session,` : "" | ||
} | ||
}; | ||
}; | ||
export const createContext = async (opts: createSolidAPIHandlerContext) => { | ||
return await createContextInner(opts); | ||
}; | ||
export type IContext = inferAsyncReturnType<typeof createContext>; | ||
`; | ||
}; | ||
|
||
export default getTrpcContext; |
Oops, something went wrong.