feat: use new method

This commit is contained in:
nafees nazik
2024-07-10 02:00:53 +05:30
parent c6302233ed
commit a0e70c8372
3 changed files with 39 additions and 46 deletions
+18 -22
View File
@@ -1,30 +1,27 @@
import { withMemberAuth } from "@/server/api/auth";
import { ErrorResponses } from "@/server/api/error";
import type { PublicAPI } from "@/server/api/hono";
import { ApiCompanySchema } from "@/server/api/schema/company";
import { createRoute, z } from "@hono/zod-openapi";
import { z } from "@hono/zod-openapi";
import type { Company } from "@prisma/client";
import { v1Api } from "../../utils/endpoint-creator";
const route = createRoute({
method: "get",
path: "/v1/companies",
responses: {
200: {
content: {
"application/json": {
schema: z.array(ApiCompanySchema).openapi({
description: "List of companies",
}),
const route = v1Api
.createRoute({
method: "get",
path: "/v1/companies",
responses: {
200: {
content: {
"application/json": {
schema: z.array(ApiCompanySchema).openapi({
description: "List of companies",
}),
},
},
description: "List companies",
},
description: "List companies",
},
...ErrorResponses,
},
});
const getMany = (app: PublicAPI) => {
app.openapi(route, async (c) => {
})
.handler(async (c) => {
const db = c.get("db");
const { membership } = await withMemberAuth(c);
const companyIds = membership.map((m) => m.companyId);
@@ -45,6 +42,5 @@ const getMany = (app: PublicAPI) => {
return c.json(response, 200);
});
};
export default getMany;
export default route;
+19 -22
View File
@@ -1,8 +1,8 @@
import { withCompanyAuth } from "@/server/api/auth";
import { ApiError, ErrorResponses } from "@/server/api/error";
import type { PublicAPI } from "@/server/api/hono";
import { ApiError } from "@/server/api/error";
import { ApiCompanySchema } from "@/server/api/schema/company";
import { createRoute, z } from "@hono/zod-openapi";
import { z } from "@hono/zod-openapi";
import { v1Api } from "../../utils/endpoint-creator";
export const RequestSchema = z.object({
id: z
@@ -19,28 +19,26 @@ export const RequestSchema = z.object({
}),
});
const route = createRoute({
method: "get",
path: "/v1/companies/:id",
request: { params: RequestSchema },
responses: {
200: {
content: {
"application/json": {
schema: ApiCompanySchema,
const route = v1Api
.createRoute({
method: "get",
path: "/v1/companies/:id",
request: { params: RequestSchema },
responses: {
200: {
content: {
"application/json": {
schema: ApiCompanySchema,
},
},
description: "Get a company by ID",
},
description: "Get a company by ID",
},
...ErrorResponses,
},
});
const getOne = (app: PublicAPI) => {
app.openapi(route, async (c) => {
})
.handler(async (c) => {
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
const { company } = (await withCompanyAuth(c)) as { company: any };
c.req.param("id");
if (!company) {
throw new ApiError({
code: "NOT_FOUND",
@@ -50,6 +48,5 @@ const getOne = (app: PublicAPI) => {
return c.json(company, 200);
});
};
export default getOne;
export default route;
+2 -2
View File
@@ -3,6 +3,6 @@ import getMany from "./getMany";
import getOne from "./getOne";
export const registerCompanyRoutes = (api: PublicAPI) => {
getOne(api);
getMany(api);
api.openapi(getOne.route, getOne.handler);
api.openapi(getMany.route, getMany.handler);
};