Skip to content

Commit

Permalink
feat: #1243 add api routes
Browse files Browse the repository at this point in the history
  • Loading branch information
manuel-rw committed Oct 19, 2024
1 parent 9b5b8e6 commit 864bedb
Show file tree
Hide file tree
Showing 12 changed files with 337 additions and 263 deletions.
75 changes: 29 additions & 46 deletions packages/api/src/router/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,14 @@ import { apps } from "@homarr/db/schema/sqlite";
import { validation, z } from "@homarr/validation";

import { createTRPCRouter, protectedProcedure, publicProcedure } from "../trpc";
import { selectAppSchema } from "@homarr/db/validationSchemas/app";

export const appRouter = createTRPCRouter({
all: publicProcedure
.input(z.void())
.output(
z.array(
z.object({
name: z.string(),
id: z.string(),
description: z.string().nullable(),
iconUrl: z.string(),
href: z.string().nullable(),
}),
selectAppSchema
),
)
.meta({ openapi: { method: "GET", path: "/api/apps", tags: ["apps"], protect: true } })
Expand All @@ -30,13 +25,7 @@ export const appRouter = createTRPCRouter({
.input(z.object({ query: z.string(), limit: z.number().min(1).max(100).default(10) }))
.output(
z.array(
z.object({
name: z.string(),
id: z.string(),
description: z.string().nullable(),
iconUrl: z.string(),
href: z.string().nullable(),
}),
selectAppSchema,
),
)
.meta({ openapi: { method: "GET", path: "/api/apps/search", tags: ["apps"], protect: true } })
Expand All @@ -51,11 +40,7 @@ export const appRouter = createTRPCRouter({
.input(z.void())
.output(
z.array(
z.object({
name: z.string(),
id: z.string(),
iconUrl: z.string(),
}),
selectAppSchema.pick({ id: true, name: true, iconUrl: true }),
),
)
.meta({
Expand All @@ -79,13 +64,7 @@ export const appRouter = createTRPCRouter({
byId: publicProcedure
.input(validation.common.byId)
.output(
z.object({
name: z.string(),
id: z.string(),
description: z.string().nullable(),
iconUrl: z.string(),
href: z.string().nullable(),
}),
selectAppSchema,
)
.meta({ openapi: { method: "GET", path: "/api/apps/{id}", tags: ["apps"], protect: true } })
.query(async ({ ctx, input }) => {
Expand Down Expand Up @@ -115,28 +94,32 @@ export const appRouter = createTRPCRouter({
href: input.href,
});
}),
update: protectedProcedure.input(validation.app.edit).mutation(async ({ ctx, input }) => {
const app = await ctx.db.query.apps.findFirst({
where: eq(apps.id, input.id),
});

if (!app) {
throw new TRPCError({
code: "NOT_FOUND",
message: "App not found",
update: protectedProcedure
.input(validation.app.edit)
.output(z.void())
.meta({ openapi: { method: "PATCH", path: "/api/apps/{id}", tags: ["apps"], protect: true } })
.mutation(async ({ ctx, input }) => {
const app = await ctx.db.query.apps.findFirst({
where: eq(apps.id, input.id),
});
}

await ctx.db
.update(apps)
.set({
name: input.name,
description: input.description,
iconUrl: input.iconUrl,
href: input.href,
})
.where(eq(apps.id, input.id));
}),
if (!app) {
throw new TRPCError({
code: "NOT_FOUND",
message: "App not found",
});
}

await ctx.db
.update(apps)
.set({
name: input.name,
description: input.description,
iconUrl: input.iconUrl,
href: input.href,
})
.where(eq(apps.id, input.id));
}),
delete: protectedProcedure
.output(z.void())
.meta({ openapi: { method: "DELETE", path: "/api/apps/{id}", tags: ["apps"], protect: true } })
Expand Down
7 changes: 5 additions & 2 deletions packages/api/src/router/serverSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import { createTRPCRouter, protectedProcedure, publicProcedure } from "../trpc";

export const serverSettingsRouter = createTRPCRouter({
// this must be public so anonymous users also get analytics
getAnalytics: publicProcedure.query(async ({ ctx }) => {
getAnalytics: publicProcedure
.input(z.undefined())
.query(async ({ ctx }) => {
const setting = await ctx.db.query.serverSettings.findFirst({
where: eq(serverSettings.settingKey, "analytics"),
});
Expand All @@ -30,7 +32,8 @@ export const serverSettingsRouter = createTRPCRouter({

return SuperJSON.parse<(typeof defaultServerSettings)["analytics"]>(setting.value);
}),
getAll: protectedProcedure.query(async ({ ctx }) => {
getAll: protectedProcedure
.query(async ({ ctx }) => {
const settings = await ctx.db.query.serverSettings.findMany();

const data = {} as ServerSettings;
Expand Down
Loading

0 comments on commit 864bedb

Please sign in to comment.