Skip to content

Commit

Permalink
feat: bump packages, throw error if exchangeToken failed
Browse files Browse the repository at this point in the history
  • Loading branch information
kane50613 committed Jun 23, 2024
1 parent cc2bee5 commit 18c191a
Show file tree
Hide file tree
Showing 11 changed files with 1,547 additions and 1,425 deletions.
56 changes: 28 additions & 28 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,30 @@
},
"dependencies": {
"@icons-pack/react-simple-icons": "^9.5.0",
"@octokit/rest": "^20.1.1",
"@radix-ui/react-accordion": "^1.1.2",
"@radix-ui/react-dropdown-menu": "^2.0.6",
"@radix-ui/react-popover": "^1.0.7",
"@radix-ui/react-select": "^2.0.0",
"@radix-ui/react-separator": "^1.0.3",
"@radix-ui/react-slot": "^1.0.2",
"@octokit/rest": "^21.0.0",
"@radix-ui/react-accordion": "^1.2.0",
"@radix-ui/react-dropdown-menu": "^2.1.1",
"@radix-ui/react-popover": "^1.1.1",
"@radix-ui/react-select": "^2.1.1",
"@radix-ui/react-separator": "^1.1.0",
"@radix-ui/react-slot": "^1.1.0",
"@t3-oss/env-nextjs": "^0.10.1",
"@tiptap/html": "^2.4.0",
"@vercel/analytics": "^1.2.2",
"@vercel/analytics": "^1.3.1",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
"iron-session": "^8.0.1",
"lucide-react": "^0.378.0",
"iron-session": "^8.0.2",
"lucide-react": "^0.396.0",
"markdown-to-jsx": "^7.4.7",
"marked": "^12.0.2",
"marked": "^13.0.0",
"next": "15.0.0-rc.0",
"next-safe-action": "^6.2.0",
"novel": "^0.4.2",
"react": "19.0.0-rc-4c2e457c7c-20240522",
"react-dom": "19.0.0-rc-4c2e457c7c-20240522",
"react-intersection-observer": "^9.10.2",
"next-safe-action": "^7.1.1",
"novel": "^0.4.3",
"react": "19.0.0-rc-3563387fe3-20240621",
"react-dom": "19.0.0-rc-3563387fe3-20240621",
"react-intersection-observer": "^9.10.3",
"remove-markdown": "^0.5.0",
"sonner": "^1.4.41",
"sonner": "^1.5.0",
"swr": "^2.2.5",
"tailwind-merge": "^2.3.0",
"tailwindcss-animate": "^1.0.7",
Expand All @@ -53,29 +53,29 @@
"@faker-js/faker": "^8.4.1",
"@octokit/openapi-types": "^22.2.0",
"@octokit/types": "^13.5.0",
"@playwright/test": "^1.44.0",
"@playwright/test": "^1.44.1",
"@prettier/plugin-xml": "3.4.1",
"@tailwindcss/typography": "^0.5.13",
"@types/eslint": "^8.56.10",
"@types/node": "^20.12.12",
"@types/react": "^18.3.2",
"@types/node": "^20.14.8",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@types/remove-markdown": "^0.3.4",
"@typescript-eslint/eslint-plugin": "^7.9.0",
"@typescript-eslint/parser": "^7.9.0",
"babel-plugin-react-compiler": "0.0.0-experimental-592953e-20240517",
"@typescript-eslint/eslint-plugin": "^7.13.1",
"@typescript-eslint/parser": "^7.13.1",
"babel-plugin-react-compiler": "0.0.0-experimental-938cd9a-20240601",
"eslint": "^8.57.0",
"eslint-config-next": "^14.2.3",
"eslint-config-next": "^14.2.4",
"eslint-plugin-react-compiler": "0.0.0-experimental-c8b3f72-20240517",
"fastify": "^4.27.0",
"fastify": "^4.28.0",
"postcss": "^8.4.38",
"prettier": "^3.2.5",
"prettier-plugin-embed": "^0.4.15",
"prettier-plugin-organize-imports": "^3.2.4",
"prettier-plugin-tailwindcss": "^0.5.14",
"prettier-plugin-tailwindcss": "^0.6.5",
"simple-git-hooks": "^2.11.1",
"tailwindcss": "^3.4.3",
"tsx": "^4.10.5",
"tailwindcss": "^3.4.4",
"tsx": "^4.15.7",
"typescript": "^5.4.5"
},
"ct3aMetadata": {
Expand Down
2,782 changes: 1,443 additions & 1,339 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

9 changes: 4 additions & 5 deletions src/actions/create-comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ const schema = zfd.formData({
body: zfd.text(z.string().min(1)),
});

export const createComment = authAction(
schema,
async ({ issue_number, body }) => {
export const createComment = authAction
.schema(schema)
.action(async ({ parsedInput: { issue_number, body } }) => {
const session = await getSession();

return client(session).issues.createComment({
Expand All @@ -23,5 +23,4 @@ export const createComment = authAction(
issue_number,
body,
});
},
);
});
27 changes: 15 additions & 12 deletions src/actions/delete-post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@ import { authAction } from "@/common/action";
import { z } from "zod";
import { revalidatePost } from "@/actions/helpers/revalidate-post";

export const deletePost = authAction(z.number(), async (id, { session }) => {
const issue = await getIssue(id).catch(notFound);
export const deletePost = authAction
.schema(z.number())
.action(async ({ parsedInput, ctx: { session } }) => {
const issue = await getIssue(parsedInput).catch(notFound);

const result = await client(session).issues.update({
issue_number: id,
owner: env.NEXT_PUBLIC_GITHUB_REPO_OWNER,
repo: env.NEXT_PUBLIC_GITHUB_REPO,
state: "closed",
});
const result = await client(session).issues.update({
issue_number: parsedInput,
owner: env.NEXT_PUBLIC_GITHUB_REPO_OWNER,
repo: env.NEXT_PUBLIC_GITHUB_REPO,
state: "closed",
});

if (result.data.state !== "closed") throw new Error("Failed to delete post");
if (result.data.state !== "closed")
throw new Error("Failed to delete post");

revalidatePost(issue);
revalidatePost(issue);

redirect("/posts");
});
redirect("/posts");
});
6 changes: 3 additions & 3 deletions src/actions/list-all-posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import { listPosts as listPostsInternal } from "@/common/github";
import { action } from "@/common/action";
import { z } from "zod";

export const listAllPosts = action(z.number(), async (page: number) =>
listPostsInternal(undefined, page),
);
export const listAllPosts = action
.schema(z.number())
.action(async ({ parsedInput }) => listPostsInternal(undefined, parsedInput));
11 changes: 5 additions & 6 deletions src/actions/list-issue-comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ const schema = z.object({
page: z.number(),
});

export const listIssueComments = action(
schema,
async ({ issue_number, page }) => {
export const listIssueComments = action
.schema(schema)
.action(async ({ parsedInput: { issue_number, page } }) => {
const session = await getUnsafeSession();

return client(session)
Expand All @@ -25,9 +25,8 @@ export const listIssueComments = action(
page,
})
.then((r) => r.data);
},
);
});

export type ListIssueComments = NonNullable<
Awaited<ReturnType<typeof listIssueComments>>["data"]
NonNullable<Awaited<ReturnType<typeof listIssueComments>>>["data"]
>;
9 changes: 5 additions & 4 deletions src/actions/list-my-posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { authAction } from "@/common/action";
import { z } from "zod";

// needs the re-export here with the "use server" directive, otherwise it will not be available in the client
export const listMyPosts = authAction(
z.number(),
async (page: number, { user }) => listPostsInternal(user.login, page),
);
export const listMyPosts = authAction
.schema(z.number())
.action(async ({ parsedInput, ctx }) =>
listPostsInternal(ctx.user.login, parsedInput),
);
12 changes: 7 additions & 5 deletions src/actions/upsert-post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ const schema = zfd.formData({

type Schema = z.infer<typeof schema>;

export const upsertPost = authAction(schema, async ({ id, ...data }) => {
const issue = id ? await updatePost(id, data) : await createPost(data);
export const upsertPost = authAction
.schema(schema)
.action(async ({ parsedInput: { id, ...data } }) => {
const issue = id ? await updatePost(id, data) : await createPost(data);

revalidatePost(issue);
revalidatePost(issue);

redirect(`/posts/${issue.number}`);
});
redirect(`/posts/${issue.number}`);
});

async function updatePost(id: number, data: Schema) {
const session = await getSession();
Expand Down
14 changes: 10 additions & 4 deletions src/app/api/auth/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ async function grabToken(req: Request, code: string) {

const token = await exchangeToken(req, code);

if ("error" in token) throw new Error(JSON.stringify(token));

const givenScopes = token.scope.match(/[a-z:_]+/g) ?? [];

if (
Expand Down Expand Up @@ -90,8 +92,12 @@ async function exchangeToken(req: Request, code: string) {
},
);

return (await tokenRequest.json()) as {
access_token: string;
scope: string;
};
return (await tokenRequest.json()) as
| {
access_token: string;
scope: string;
}
| {
error: string;
};
}
18 changes: 9 additions & 9 deletions src/common/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import { getUser } from "@/common/github";

export const action = createSafeActionClient();

export const authAction = createSafeActionClient({
async middleware() {
const session = await getSession();
export const authAction = action.use(async ({ next }) => {
const session = await getSession();

const user = await getUser(session);
const user = await getUser(session);

if (!whitelistCheckWithFallback(user.login))
throw new Error("You are not allowed to perform this action");
if (!whitelistCheckWithFallback(user.login))
throw new Error("You are not allowed to perform this action");

return {
return next({
ctx: {
user,
session,
};
},
},
});
});
28 changes: 18 additions & 10 deletions src/lib/action-hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ import { toast } from "sonner";

export const useActionWithHandler: typeof useAction = (func, utils) => {
return useAction(func, {
onError(error) {
onError({ error }) {
console.error(error);

if (error.serverError) toast.error(error.serverError);
if (error.serverError)
toast.error(
typeof error.serverError === "string"
? error.serverError
: JSON.stringify(error.serverError),
);

if (error.validationErrors)
toast.error(JSON.stringify(error.validationErrors));
Expand All @@ -18,26 +23,29 @@ export const useActionWithHandler: typeof useAction = (func, utils) => {
};

export function wrapInfiniteSafeAction<Data>(
func: (input: number) => Promise<{
data?: Data;
serverError?: string;
validationErrors?: Partial<Record<string, string[]>>;
}>,
func: (input: number) => Promise<
| {
data?: Data;
serverError?: string;
validationErrors?: unknown;
}
| undefined
>,
) {
return async function (page: number) {
const data = await func(page);

if (data.serverError) {
if (data?.serverError) {
toast.error(JSON.stringify(data.serverError));
throw new Error(data.serverError);
}

if (data.validationErrors) {
if (data?.validationErrors) {
toast.error(JSON.stringify(data.validationErrors));
throw new Error("Validation error");
}

if (!data.data) throw new Error("No data returned");
if (!data?.data) throw new Error("No data returned");

return data.data;
};
Expand Down

0 comments on commit 18c191a

Please sign in to comment.