-
-
Notifications
You must be signed in to change notification settings - Fork 415
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f3812b5
commit 549b03b
Showing
3 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}), | ||
}); |