Skip to content

Commit

Permalink
fixing type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
webdevcody committed Jul 24, 2024
1 parent 42fc641 commit 3b16a79
Show file tree
Hide file tree
Showing 16 changed files with 82 additions and 70 deletions.
1 change: 0 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/** @type {import("eslint").Linter.Config} */
module.exports = {
root: true,
extends: ["@repo/eslint-config/next.js"],
parser: "@typescript-eslint/parser",
parserOptions: {
project: true,
Expand Down
2 changes: 1 addition & 1 deletion src/app/(coming-soon)/actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ export const subscribeEmailAction = unauthenticatedAction
})
)
.handler(async ({ input: { email } }) => {
await rateLimitByIp("newsletter", 1);
await rateLimitByIp({ key: "newsletter" });
await subscribeEmailUseCase(email);
});
2 changes: 1 addition & 1 deletion src/app/(coming-soon)/newsletter-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function NewsletterForm() {
<Alert variant="destructive">
<Terminal className="h-4 w-4" />
<AlertTitle>Something went wrong</AlertTitle>
<AlertDescription>{error}</AlertDescription>
<AlertDescription>{error.message}</AlertDescription>
</Alert>
)}
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/app/api/groups/[groupId]/images/[imageId]/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { streamImageFromUrl } from "@/app/api/users/[userId]/images/[imageId]/route";
import { streamImageFromUrl } from "@/app/api/streams";
import { env } from "@/env";
import { getCurrentUser } from "@/lib/session";
import { getGroupImageUrlUseCase } from "@/use-cases/files";
Expand Down
44 changes: 44 additions & 0 deletions src/app/api/streams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { NextResponse } from "next/server";

export async function streamImageFromUrl(url: string) {
const fetchResponse = await fetch(url);

if (!fetchResponse.ok) {
return NextResponse.json({ error: "File not found" }, { status: 404 });
}

const file = fetchResponse.body;

if (!file) {
return NextResponse.json({ error: "File not found" }, { status: 404 });
}

const contentType = fetchResponse.headers.get("content-type") || "image/*";
const contentLength =
Number(fetchResponse.headers.get("content-length")) || 0;

const stream = new ReadableStream({
start(controller) {
const reader = file.getReader();
const pump = () => {
reader.read().then(({ done, value }) => {
if (done) {
controller.close();
return;
}
controller.enqueue(value);
pump();
});
};
pump();
},
});

return new Response(stream, {
headers: {
"Content-Type": contentType,
"Content-Length": String(contentLength),
"Cache-Control": "public, max-age=31536000, immutable",
},
});
}
44 changes: 1 addition & 43 deletions src/app/api/users/[userId]/images/[imageId]/route.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,8 @@
import { streamImageFromUrl } from "@/app/api/streams";
import { env } from "@/env";
import { getProfileImageUrlUseCase } from "@/use-cases/users";
import { NextResponse } from "next/server";

export async function streamImageFromUrl(url: string) {
const fetchResponse = await fetch(url);

if (!fetchResponse.ok) {
return NextResponse.json({ error: "File not found" }, { status: 404 });
}

const file = fetchResponse.body;

if (!file) {
return NextResponse.json({ error: "File not found" }, { status: 404 });
}

const contentType = fetchResponse.headers.get("content-type") || "image/*";
const contentLength =
Number(fetchResponse.headers.get("content-length")) || 0;

const stream = new ReadableStream({
start(controller) {
const reader = file.getReader();
const pump = () => {
reader.read().then(({ done, value }) => {
if (done) {
controller.close();
return;
}
controller.enqueue(value);
pump();
});
};
pump();
},
});

return new Response(stream, {
headers: {
"Content-Type": contentType,
"Content-Length": String(contentLength),
"Cache-Control": "public, max-age=31536000, immutable",
},
});
}

export const GET = async (
req: Request,
{ params }: { params: { userId: string; imageId: string } }
Expand Down
4 changes: 3 additions & 1 deletion src/app/dashboard/actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ export const createGroupAction = authenticatedAction
.createServerAction()
.input(schema)
.handler(async ({ input: { name, description }, ctx: { user } }) => {
await rateLimitByKey(`${user.id}-create-group`, 1, 10000).catch(toZSAError);
await rateLimitByKey({
key: `${user.id}-create-group`,
}).catch(toZSAError);
await createGroupUseCase(user, {
name,
description,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export const MenuBar = () => {

export const extensions = [
Color.configure({ types: [TextStyle.name, ListItem.name] }),
TextStyle.configure({ types: [ListItem.name] }),
TextStyle.configure({ types: [ListItem.name] } as any),
StarterKit.configure({
bulletList: {
keepMarks: true,
Expand Down
2 changes: 1 addition & 1 deletion src/app/dashboard/groups/[groupId]/posts/[postId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ async function RepliesList({
);
}

export async function ReplyCard({ reply }: { reply: Reply }) {
async function ReplyCard({ reply }: { reply: Reply }) {
const user = await getCurrentUser();
const hasMutateAccess = await hasAccessToMutateReplyUseCase(user, reply.id);

Expand Down
6 changes: 5 additions & 1 deletion src/app/dashboard/settings/profile/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ export const updateProfileImageAction = authenticatedAction
})
)
.handler(async ({ input, ctx }) => {
await rateLimitByKey(`update-profile-image-${ctx.user.id}`, 3, 60000);
await rateLimitByKey({
key: `update-profile-image-${ctx.user.id}`,
limit: 3,
window: 60000,
});
const file = input.fileWrapper.get("file") as File;
await updateProfileImageUseCase(file, ctx.user.id);
revalidatePath(`/dashboard/settings/profile`);
Expand Down
3 changes: 0 additions & 3 deletions src/app/dashboard/settings/security/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ import { ConfigurationPanel } from "@/components/configuration-panel";
import { LogoutButton } from "./logout-button";

export default async function SecurityPage() {
return <SessionsPanel />;
}
export async function SessionsPanel() {
return (
<ConfigurationPanel title="Sessions">
<div className="flex flex-col gap-4">
Expand Down
2 changes: 1 addition & 1 deletion src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Session } from "lucia";
import { env } from "@/env";
import { UserId as CustomUserId } from "@/use-cases/types";

const adapter = new DrizzlePostgreSQLAdapter(database, sessions, users);
const adapter = new DrizzlePostgreSQLAdapter(database, sessions as any, users);

export const lucia = new Lucia(adapter, {
sessionCookie: {
Expand Down
2 changes: 1 addition & 1 deletion src/components/delete-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function DeleteModal({
setIsOpen: (open: boolean) => void;
title: string;
confirmText?: string;
isPending?: boolean;
isPending: boolean;
}) {
return (
<AlertDialog open={isOpen} onOpenChange={setIsOpen}>
Expand Down
18 changes: 15 additions & 3 deletions src/components/stripe/upgrade-button/checkout-button.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"use client";

import { generateStripeSessionAction } from "./actions";
import { ReactNode } from "react";
import { SubmitButton } from "@/components/submit-button";
import { useServerAction } from "zsa-react";
import { LoaderButton } from "@/components/loader-button";

export function CheckoutButton({
className,
Expand All @@ -11,9 +14,18 @@ export function CheckoutButton({
children: ReactNode;
priceId: string;
}) {
const { execute, isPending } = useServerAction(generateStripeSessionAction);

return (
<form action={generateStripeSessionAction.bind(null, { priceId })}>
<SubmitButton className={className}>{children}</SubmitButton>
<form
onSubmit={(e) => {
e.preventDefault();
execute({ priceId });
}}
>
<LoaderButton isLoading={isPending} className={className}>
{children}
</LoaderButton>
</form>
);
}
12 changes: 6 additions & 6 deletions src/lib/limiter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ export async function rateLimitByIp({
limit = 1,
window = 10000,
}: {
key: string;
limit: number;
window: number;
key?: string;
limit?: number;
window?: number;
}) {
const ip = getIp();

Expand All @@ -62,9 +62,9 @@ export async function rateLimitByKey({
limit = 1,
window = 10000,
}: {
key: string;
limit: number;
window: number;
key?: string;
limit?: number;
window?: number;
}) {
const tracker = trackers[key] || { count: 0, expiresAt: 0 };

Expand Down
6 changes: 1 addition & 5 deletions src/use-cases/authorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ import { getMembership } from "@/data-access/membership";
import { Group, GroupId } from "@/db/schema";
import { UserSession } from "@/use-cases/types";

export function isPremiumUser(user: UserSession) {
return user.plan !== "free";
}

export function isGroupOwner(user: UserSession, group: Group) {
function isGroupOwner(user: UserSession, group: Group) {
return user.id === group.userId;
}

Expand Down

0 comments on commit 3b16a79

Please sign in to comment.