feat: Add types and IDE comments to session

This commit is contained in:
Wanjohi
2025-11-03 23:04:29 +03:00
parent c99962ee1d
commit 6947230a1d
6 changed files with 584 additions and 185 deletions

View File

@@ -1,9 +1,11 @@
import { Hono } from "hono";
import { describeRoute, resolver } from "hono-openapi";
import { notPublic } from "../utils/auth.js";
import { ErrorResponses } from "../utils/error.js";
import { Session } from "@nestri/core/session/index";
import { describeRoute, resolver } from "hono-openapi";
export namespace SessionRoute {
export const route = new Hono().get(
export const route = new Hono().use(notPublic).get(
"/",
describeRoute({
tags: ["Session"],
@@ -19,7 +21,14 @@ export namespace SessionRoute {
},
},
},
401: ErrorResponses[401],
403: ErrorResponses[403],
429: ErrorResponses[429],
},
}),
async (c) => {
const sessions = await Session.list();
return c.json(sessions);
},
);
}

View File

@@ -12,6 +12,17 @@ const client = createClient({
subjects,
});
export const notPublic: MiddlewareHandler = async (c, next) => {
const actor = Actor.use();
if (actor.type === "public")
throw new VisibleError(
"authentication",
ErrorCodes.Authentication.UNAUTHORIZED,
"Missing authorization header",
);
return next();
};
export const auth: MiddlewareHandler = async (c, next) => {
const authHeader =
c.req.query("authorization") ?? c.req.header("authorization");

View File

@@ -0,0 +1,138 @@
import { resolver } from "hono-openapi";
import { ErrorResponse } from "@nestri/core/error";
export const ErrorResponses = {
/**Validation error*/
400: {
content: {
"application/json": {
schema: resolver(
ErrorResponse.meta({
description: "Validation error",
example: {
type: "validation",
code: "invalid_parameter",
message: "The request was invalid",
param: "email",
},
}),
),
},
},
description:
"Bad Request - The request could not be understood or was missing required parameters.",
},
/**Authentication Error*/
401: {
content: {
"application/json": {
schema: resolver(
ErrorResponse.meta({
description: "Authentication error",
example: {
type: "authentication",
code: "unauthorized",
message: "Authentication required",
},
}),
),
},
},
description:
"Unauthorized - Authentication is required and has failed or has not been provided.",
},
/**Permission Error*/
403: {
content: {
"application/json": {
schema: resolver(
ErrorResponse.meta({
description: "Permission error",
example: {
type: "forbidden",
code: "permission_denied",
message: "You do not have permission to access this resource",
},
}),
),
},
},
description:
"Forbidden - You do not have permission to access this resource.",
},
/**Not Found Error*/
404: {
content: {
"application/json": {
schema: resolver(
ErrorResponse.meta({
description: "Not found error",
example: {
type: "not_found",
code: "resource_not_found",
message: "The requested resource could not be found",
},
}),
),
},
},
description: "Not Found - The requested resource does not exist.",
},
/**Conflict Error*/
409: {
content: {
"application/json": {
schema: resolver(
ErrorResponse.meta({
description: "Conflict Error",
example: {
type: "already_exists",
code: "resource_already_exists",
message:
"The resource could not be created because it already exists",
},
}),
),
},
},
description:
"Conflict - The resource could not be created because it already exists.",
},
/**Rate Limit Error*/
429: {
content: {
"application/json": {
schema: resolver(
ErrorResponse.meta({
description: "Rate limit error",
example: {
type: "rate_limit",
code: "too_many_requests",
message: "Rate limit exceeded",
},
}),
),
},
},
description:
"Too Many Requests - You have made too many requests in a short period of time.",
},
/**Internal Server Error*/
500: {
content: {
"application/json": {
schema: resolver(
ErrorResponse.meta({
description: "Server error",
example: {
type: "internal",
code: "internal_error",
message: "Internal server error",
},
}),
),
},
},
description: "Internal Server Error - Something went wrong on our end.",
},
};