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 20, 2024
1 parent 278107a commit cad9f69
Show file tree
Hide file tree
Showing 13 changed files with 362 additions and 279 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
43 changes: 27 additions & 16 deletions packages/api/src/router/invite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,41 @@ import { z } from "@homarr/validation";

import { createTRPCRouter, protectedProcedure } from "../trpc";
import { throwIfCredentialsDisabled } from "./invite/checks";
import { selectInviteSchema } from "@homarr/db/validationSchemas/invite";

export const inviteRouter = createTRPCRouter({
getAll: protectedProcedure.query(async ({ ctx }) => {
throwIfCredentialsDisabled();
const dbInvites = await ctx.db.query.invites.findMany({
orderBy: asc(invites.expirationDate),
columns: {
token: false,
},
with: {
creator: {
columns: {
id: true,
name: true,
getAll: protectedProcedure
.output(z.array(selectInviteSchema.pick({
id: true,
expirationDate: true
}).extend({ creator: z.object({ name: z.string().nullable(), id: z.string() }) })))
.input(z.undefined())
.meta({ openapi: { method: "GET", path: "/api/invites", tags: ["invites"], protect: true } })
.query(async ({ ctx }) => {
throwIfCredentialsDisabled();
return await ctx.db.query.invites.findMany({
orderBy: asc(invites.expirationDate),
columns: {
token: false,
},
with: {
creator: {
columns: {
id: true,
name: true,
},
},
},
},
});
return dbInvites;
}),
});
}),
createInvite: protectedProcedure
.input(
z.object({
expirationDate: z.date(),
}),
)
.output(z.object({ id: z.string(), token: z.string() }))
.meta({ openapi: { method: "GET", path: "/api/invites", tags: ["invites"], protect: true } })
.mutation(async ({ ctx, input }) => {
throwIfCredentialsDisabled();
const id = createId();
Expand All @@ -56,6 +65,8 @@ export const inviteRouter = createTRPCRouter({
id: z.string(),
}),
)
.output(z.undefined())
.meta({ openapi: { method: "DELETE", path: "/api/invites/{id}", tags: ["invites"], protect: true } })
.mutation(async ({ ctx, input }) => {
throwIfCredentialsDisabled();
const dbInvite = await ctx.db.query.invites.findFirst({
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 cad9f69

Please sign in to comment.