-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add pi hole summary integration (#521)
* feat: add pi hole summary integration * feat: add pi hole summary widget * fix: type issues with integrations and integrationIds * feat: add middleware for integrations and improve cache redis channel * feat: add error boundary for widgets * fix: broken lock file * fix: format format issues * fix: typecheck issue * fix: deepsource issues * fix: widget sandbox without error boundary * chore: address pull request feedback * chore: remove todo comment and created issue * fix: format issues * fix: deepsource issue
- Loading branch information
1 parent
96c71ae
commit d57b771
Showing
45 changed files
with
902 additions
and
124 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
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
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
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
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
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,76 @@ | ||
import { TRPCError } from "@trpc/server"; | ||
|
||
import { and, eq, inArray } from "@homarr/db"; | ||
import { integrations } from "@homarr/db/schema/sqlite"; | ||
import type { IntegrationKind } from "@homarr/definitions"; | ||
import { z } from "@homarr/validation"; | ||
|
||
import { decryptSecret } from "../router/integration"; | ||
import { publicProcedure } from "../trpc"; | ||
|
||
export const createOneIntegrationMiddleware = <TKind extends IntegrationKind>(...kinds: TKind[]) => { | ||
return publicProcedure.input(z.object({ integrationId: z.string() })).use(async ({ input, ctx, next }) => { | ||
const integration = await ctx.db.query.integrations.findFirst({ | ||
where: and(eq(integrations.id, input.integrationId), inArray(integrations.kind, kinds)), | ||
with: { | ||
secrets: true, | ||
}, | ||
}); | ||
|
||
if (!integration) { | ||
throw new TRPCError({ | ||
code: "NOT_FOUND", | ||
message: `Integration with id ${input.integrationId} not found or not of kinds ${kinds.join(",")}`, | ||
}); | ||
} | ||
|
||
const { secrets, kind, ...rest } = integration; | ||
|
||
return next({ | ||
ctx: { | ||
integration: { | ||
...rest, | ||
kind: kind as TKind, | ||
decryptedSecrets: secrets.map((secret) => ({ | ||
...secret, | ||
value: decryptSecret(secret.value), | ||
})), | ||
}, | ||
}, | ||
}); | ||
}); | ||
}; | ||
|
||
export const createManyIntegrationMiddleware = <TKind extends IntegrationKind>(...kinds: TKind[]) => { | ||
return publicProcedure | ||
.input(z.object({ integrationIds: z.array(z.string()).min(1) })) | ||
.use(async ({ ctx, input, next }) => { | ||
const dbIntegrations = await ctx.db.query.integrations.findMany({ | ||
where: and(inArray(integrations.id, input.integrationIds), inArray(integrations.kind, kinds)), | ||
with: { | ||
secrets: true, | ||
}, | ||
}); | ||
|
||
const offset = input.integrationIds.length - dbIntegrations.length; | ||
if (offset !== 0) { | ||
throw new TRPCError({ | ||
code: "NOT_FOUND", | ||
message: `${offset} of the specified integrations not found or not of kinds ${kinds.join(",")}`, | ||
}); | ||
} | ||
|
||
return next({ | ||
ctx: { | ||
integrations: dbIntegrations.map(({ secrets, kind, ...rest }) => ({ | ||
...rest, | ||
kind: kind as TKind, | ||
decryptedSecrets: secrets.map((secret) => ({ | ||
...secret, | ||
value: decryptSecret(secret.value), | ||
})), | ||
})), | ||
}, | ||
}); | ||
}); | ||
}; |
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
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
Oops, something went wrong.