Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add loggers during dev #353

Merged
merged 4 commits into from
Aug 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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