Skip to content

Commit

Permalink
🚀 whoami endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaultleouay committed Oct 25, 2024
1 parent f3812b5 commit 549b03b
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
45 changes: 45 additions & 0 deletions apps/server/src/v1/whoami/get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { createRoute, z } from "@hono/zod-openapi";
import { eq } from "@openstatus/db";
import { db } from "@openstatus/db/src/db";
import { workspace } from "@openstatus/db/src/schema/workspaces";
import { HTTPException } from "hono/http-exception";
import type { whoamiApi } from ".";
import { openApiErrorResponses } from "../../libs/errors/openapi-error-responses";
import { schema } from "./schema";

const getRoute = createRoute({
method: "get",
tags: ["whoami"],
path: "/",
description: "Get the current workspace information",
responses: {
200: {
content: {
"application/json": {
schema: schema,
},
},
description: "The current workspace information with the limits",
},
...openApiErrorResponses,
},
}); // Error: createRoute is not defined

export function registerGetWhoami(api: typeof whoamiApi) {
return api.openapi(getRoute, async (c) => {
const workspaceId = c.get("workspaceId");

const workspaceData = await db
.select()
.from(workspace)
.where(eq(workspace.id, Number(workspaceId)))
.get();

if (!workspaceData) {
throw new HTTPException(404, { message: "Not Found" });
}

const data = schema.parse(workspaceData);
return c.json(data, 200);
});
}
7 changes: 7 additions & 0 deletions apps/server/src/v1/whoami/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { OpenAPIHono } from "@hono/zod-openapi";
import type { Variables } from "..";
import { handleZodError } from "../../libs/errors";

export const whoamiApi = new OpenAPIHono<{ Variables: Variables }>({
defaultHook: handleZodError,
}); // Compare this snippet from apps/server/src/v1/whoami/index.ts:
18 changes: 18 additions & 0 deletions apps/server/src/v1/whoami/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { z } from "@hono/zod-openapi";
import { allPlans } from "@openstatus/db/src/schema/plan/config";
import { limitSchema } from "@openstatus/db/src/schema/plan/schema";
import { workspacePlans } from "@openstatus/db/src/schema/workspaces/constants";

export const schema = z.object({
workspaceId: z.number().openapi({
description: "The current workspace id",
}),
plan: z
.enum(workspacePlans)
.nullable()
.default("free")
.transform((val) => val ?? "free")
.openapi({
description: "The current workspace plan",
}),
});

0 comments on commit 549b03b

Please sign in to comment.