8000 chore: use same schema for prefix and bytes by ogzhanolguncu · Pull Request #3284 · unkeyed/unkey · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

chore: use same schema for prefix and bytes #3284

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

Merged
merged 3 commits into from
May 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations 8000
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@ import { Button, FormInput, SettingCard } from "@unkey/ui";
import { useRouter } from "next/navigation";
import { Controller, useForm } from "react-hook-form";
import { z } from "zod";
import { keyBytesSchema } from "../../_components/create-key/create-key.schema";

const formSchema = z.object({
keyAuthId: z.string(),
defaultBytes: z
.number()
.min(8, "Key must be between 8 and 255 bytes long")
.max(255, "Key must be between 8 and 255 bytes long")
.optional(),
defaultBytes: keyBytesSchema,
});

type Props = {
Expand Down Expand Up @@ -45,12 +42,37 @@ export const DefaultBytes: React.FC<Props> = ({ keyAuth, apiId }) => {

const setDefaultBytes = trpc.api.setDefaultBytes.useMutation({
onSuccess() {
toast.success("Default Byte length for this API is updated!");
toast.success("Default Byte Length Updated", {
description: "Default byte length for this API has been successfully updated.",
});
router.refresh();
},
onError(err) {
console.error(err);
toast.error(err.message);

if (err.data?.code === "NOT_FOUND") {
toast.error("API Configuration Not Found", {
description:
"Unable to find the correct API configuration. Please refresh and try again.",
});
} else if (err.data?.code === "INTERNAL_SERVER_ERROR") {
toast.error("Server Error", {
description:
"We encountered an issue while updating the default bytes. Please try again later or contact support at support@unkey.dev",
});
} else if (err.data?.code === "BAD_REQUEST") {
toast.error("Invalid Configuration", {
description: `Please check your byte length settings. ${err.message || ""}`,
});
} else {
toast.error("Failed to Update Default Bytes", {
description: err.message || "An unexpected error occurred. Please try again later.",
action: {
label: "Contact Support",
onClick: () => window.open("https://support.unkey.dev", "_blank"),
},
});
}
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@ import { Button, FormInput, SettingCard } from "@unkey/ui";
import { useRouter } from "next/navigation";
import { Controller, useForm } from "react-hook-form";
import { z } from "zod";
import { keyPrefixSchema } from "../../_components/create-key/create-key.schema";

const formSchema = z.object({
keyAuthId: z.string(),
defaultPrefix: z
.string()
.max(8, { message: "Prefixes cannot be longer than 8 characters" })
.refine((prefix) => !prefix.includes(" "), {
message: "Prefixes cannot contain spaces.",
}),
defaultPrefix: keyPrefixSchema.pipe(z.string()),
});

type Props = {
Expand Down Expand Up @@ -46,12 +42,33 @@ export const DefaultPrefix: React.FC<Props> = ({ keyAuth, apiId }) => {

const setDefaultPrefix = trpc.api.setDefaultPrefix.useMutation({
onSuccess() {
toast.success("Default prefix for this API is updated!");
toast.success("Default Prefix Updated", {
description: "Default prefix for this API has been successfully updated.",
});
router.refresh();
},
onError(err) {
console.error(err);
toast.error(err.message);

if (err.data?.code === "NOT_FOUND") {
toast.error("API Configuration Not Found", {
description:
"Unable to find the correct API configuration. Please refresh and try again.",
});
} else if (err.data?.code === "INTERNAL_SERVER_ERROR") {
toast.error("Server Error", {
description:
"We encountered an issue while updating the default prefix. Please try again later or contact support at support@unkey.dev",
});
} else {
toast.error("Failed to Update Default Prefix", {
description: err.message || "An unexpected error occurred. Please try again later.",
action: {
label: "Contact Support",
onClick: () => window.open("https://support.unkey.dev", "_blank"),
},
});
}
},
});

Expand Down
43 changes: 18 additions & 25 deletions apps/dashboard/lib/trpc/routers/api/setDefaultBytes.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import { TRPCError } from "@trpc/server";
import { z } from "zod";

import { keyBytesSchema } from "@/app/(app)/apis/[apiId]/_components/create-key/create-key.schema";
import { insertAuditLogs } from "@/lib/audit";
import { db, eq, schema } from "@/lib/db";
import { TRPCError } from "@trpc/server";
import { z } from "zod";
import { requireUser, requireWorkspace, t } from "../../trpc";

export const setDefaultApiBytes = t.procedure
.use(requireUser)
.use(requireWorkspace)
.input(
z.object({
defaultBytes: z
.number()
.min(16, "Byte size needs to be at least 16")
.max(255, "Byte size cannot exceed 255")
.optional(),
defaultBytes: keyBytesSchema,
keyAuthId: z.string(),
}),
)
Expand All @@ -35,28 +31,24 @@ export const setDefaultApiBytes = t.procedure
"We were unable to update the key auth. Please try again or contact support@unkey.dev",
});
});

if (!keyAuth) {
throw new TRPCError({
code: "NOT_FOUND",
message:
"We are unable to find the correct key auth. Please try again or contact support@unkey.dev.",
});
}
await db
.transaction(async (tx) => {

try {
await db.transaction(async (tx) => {
await tx
.update(schema.keyAuth)
.set({
defaultBytes: input.defaultBytes,
})
.where(eq(schema.keyAuth.id, keyAuth.id))
.catch((_err) => {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message:
"We were unable to update the API default bytes. Please try again or contact support@unkey.dev.",
});
});
.where(eq(schema.keyAuth.id, keyAuth.id));

await insertAuditLogs(tx, {
workspaceId: ctx.workspace.id,
actor: {
Expand All @@ -76,12 +68,13 @@ export const setDefaultApiBytes = t.procedure
userAgent: ctx.audit.userAgent,
},
});
})
.catch((_err) => {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message:
"We were unable to update the default bytes. Please try again or contact support@unkey.dev.",
});
});
} catch (err) {
console.error(err);
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message:
"We were unable to update the default bytes. Please try again or contact support@unkey.dev.",
});
}
});
8 changes: 2 additions & 6 deletions apps/dashboard/lib/trpc/routers/api/setDefaultPrefix.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TRPCError } from "@trpc/server";
import { z } from "zod";

import { keyPrefixSchema } from "@/app/(app)/apis/[apiId]/_components/create-key/create-key.schema";
import { insertAuditLogs } from "@/lib/audit";
import { db, eq, schema } from "@/lib/db";
import { requireUser, requireWorkspace, t } from "../../trpc";
Expand All @@ -10,12 +11,7 @@ export const setDefaultApiPrefix = t.procedure
.use(requireWorkspace)
.input(
z.object({
defaultPrefix: z
.string()
.max(8, { message: "Prefixes cannot be longer than 8 characters" })
.refine((prefix) => !prefix.includes(" "), {
message: "Prefixes cannot contain spaces.",
}),
defaultPrefix: keyPrefixSchema.pipe(z.string()),
keyAuthId: z.string(),
}),
)
Expand Down
0