🏗️ Migrate overseerr search to tRPC

This commit is contained in:
Meier Lukas
2023-06-10 15:01:56 +02:00
parent ed5e548257
commit f57d91123e
3 changed files with 73 additions and 26 deletions

View File

@@ -9,6 +9,7 @@ import { dnsHoleRouter } from './routers/dns-hole';
import { downloadRouter } from './routers/download';
import { mediaRequestsRouter } from './routers/media-request';
import { mediaServerRouter } from './routers/media-server';
import { overseerrRouter } from './routers/overseerr';
/**
* This is the primary router for your server.
@@ -26,6 +27,7 @@ export const rootRouter = createTRPCRouter({
download: downloadRouter,
mediaRequest: mediaRequestsRouter,
mediaServer: mediaServerRouter,
overseerr: overseerrRouter,
});
// export type definition of API

View File

@@ -0,0 +1,44 @@
import { TRPCError } from '@trpc/server';
import axios from 'axios';
import { z } from 'zod';
import { getConfig } from '~/tools/config/getConfig';
import { createTRPCRouter, publicProcedure } from '../trpc';
export const overseerrRouter = createTRPCRouter({
all: publicProcedure
.input(
z.object({
configName: z.string(),
query: z.string().or(z.undefined()),
})
)
.query(async ({ input }) => {
const config = getConfig(input.configName);
const app = config.apps.find(
(app) => app.integration?.type === 'overseerr' || app.integration?.type === 'jellyseerr'
);
if (input.query === '' || input.query === undefined) {
return [];
}
const apiKey = app?.integration?.properties.find((x) => x.field === 'apiKey')?.value;
if (!app || !apiKey) {
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'Wrong request',
});
}
const appUrl = new URL(app.url);
const data = await axios
.get(`${appUrl.origin}/api/v1/search?query=${input.query}`, {
headers: {
// Set X-Api-Key to the value of the API key
'X-Api-Key': apiKey,
},
})
.then((res) => res.data);
return data;
}),
});