Skip to content

Commit

Permalink
feat: add loggers during dev (#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gejsi authored Aug 27, 2022
1 parent c039ddb commit 7a20de9
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/wise-dolphins-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-t3-app": patch
---

Added loggers during development
9 changes: 6 additions & 3 deletions cli/src/installers/trpc.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Installer } from "~/installers/index.js";
import path from "path";
import fs from "fs-extra";
import path from "path";
import { PKG_ROOT } from "~/consts.js";
import type { Installer } from "~/installers/index.js";

export const trpcInstaller: Installer = async ({
projectDir,
Expand All @@ -23,7 +23,10 @@ export const trpcInstaller: Installer = async ({

const trpcAssetDir = path.join(PKG_ROOT, "template/addons/trpc");

const apiHandlerSrc = path.join(trpcAssetDir, "api-handler.ts");
const apiHandlerSrc = path.join(
trpcAssetDir,
usingPrisma ? "api-handler-prisma.ts" : "api-handler.ts",
);
const apiHandlerDest = path.join(projectDir, "src/pages/api/trpc/[trpc].ts");

const utilsSrc = path.join(trpcAssetDir, "utils.ts");
Expand Down
3 changes: 2 additions & 1 deletion cli/template/addons/prisma/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ declare global {
export const prisma =
global.prisma ||
new PrismaClient({
log: ["query"],
log:
env.NODE_ENV === "development" ? ["query", "error", "warn"] : ["error"],
});

if (env.NODE_ENV !== "production") {
Expand Down
12 changes: 12 additions & 0 deletions cli/template/addons/trpc/api-handler-prisma.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// src/pages/api/trpc/[trpc].ts
import { createNextApiHandler } from "@trpc/server/adapters/next";
import { prisma } from "../../../server/db/client";
import { appRouter } from "../../../server/router";
import { createContext } from "../../../server/router/context";

// export API handler
export default createNextApiHandler({
router: appRouter,
createContext,
teardown: () => prisma.$disconnect(),
});
2 changes: 1 addition & 1 deletion cli/template/addons/trpc/api-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import { createContext } from "../../../server/router/context";
// export API handler
export default createNextApiHandler({
router: appRouter,
createContext: createContext,
createContext,
});
14 changes: 12 additions & 2 deletions cli/template/page-studs/_app/with-auth-trpc.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// src/pages/_app.tsx
import { httpBatchLink } from "@trpc/client/links/httpBatchLink";
import { loggerLink } from "@trpc/client/links/loggerLink";
import { withTRPC } from "@trpc/next";
import type { AppRouter } from "../server/router";
import { SessionProvider } from "next-auth/react";
import type { AppType } from "next/dist/shared/lib/utils";
import superjson from "superjson";
import { SessionProvider } from "next-auth/react";
import type { AppRouter } from "../server/router";
import "../styles/globals.css";

const MyApp: AppType = ({
Expand Down Expand Up @@ -32,6 +34,14 @@ export default withTRPC<AppRouter>({
const url = `${getBaseUrl()}/api/trpc`;

return {
links: [
loggerLink({
enabled: (opts) =>
process.env.NODE_ENV === "development" ||
(opts.direction === "down" && opts.result instanceof Error),
}),
httpBatchLink({ url }),
],
url,
transformer: superjson,
/**
Expand Down
12 changes: 11 additions & 1 deletion cli/template/page-studs/_app/with-trpc.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// src/pages/_app.tsx
import { httpBatchLink } from "@trpc/client/links/httpBatchLink";
import { loggerLink } from "@trpc/client/links/loggerLink";
import { withTRPC } from "@trpc/next";
import type { AppRouter } from "../server/router";
import type { AppType } from "next/dist/shared/lib/utils";
import superjson from "superjson";
import type { AppRouter } from "../server/router";
import "../styles/globals.css";

const MyApp: AppType = ({ Component, pageProps }) => {
Expand All @@ -24,6 +26,14 @@ export default withTRPC<AppRouter>({
const url = `${getBaseUrl()}/api/trpc`;

return {
links: [
loggerLink({
enabled: (opts) =>
process.env.NODE_ENV === "development" ||
(opts.direction === "down" && opts.result instanceof Error),
}),
httpBatchLink({ url }),
],
url,
transformer: superjson,
/**
Expand Down

1 comment on commit 7a20de9

@vercel
Copy link

@vercel vercel bot commented on 7a20de9 Aug 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

create-t3-app – ./

create-t3-app-t3-oss.vercel.app
create-t3-app-git-main-t3-oss.vercel.app
create-t3-app-nu.vercel.app

Please sign in to comment.