Skip to content

Commit

Permalink
perf: Optimize version detection logic
Browse files Browse the repository at this point in the history
  • Loading branch information
TyxTang committed Nov 11, 2024
1 parent c3aa724 commit c22fff9
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions src/server/api/routers/media-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,20 @@ export const mediaRequestsRouter = createTRPCRouter({
const apiKey =
app.integration?.properties.find((prop) => prop.field === 'apiKey')?.value ?? '';
const headers: HeadersInit = { 'X-Api-Key': apiKey };

// Get Version
const versionResponse = await fetch(`${app.url}/api/v1/status`, { headers });
const versionBody = await versionResponse.json();
const version = versionBody.version || '0.0.0';
const shouldUseV2ApiCall = compareVersions(version, '2.0.1') >= 0;
//isUseV2ApiCall Check
let shouldUseV2ApiCall = false;
try {
const versionResponse = await fetch(`${app.url}/api/v1/status`, { headers });
if (!versionResponse.ok) {
throw new Error(`HTTP error! status: ${versionResponse.status}`);
}
const versionBody = await versionResponse.json();
const version = versionBody.version;
shouldUseV2ApiCall = compareVersions(version, '2.0.1') >= 0;
} catch (error) {
Consola.warn(`Failed to fetch version from ${app.url}: ${error}`);
shouldUseV2ApiCall = false;
}

return fetch(`${app.url}/api/v1/request?take=25&skip=0&sort=added`, {
headers,
Expand Down Expand Up @@ -110,10 +118,20 @@ export const mediaRequestsRouter = createTRPCRouter({
const apiKey =
app.integration?.properties.find((prop) => prop.field === 'apiKey')?.value ?? '';
const headers: HeadersInit = { 'X-Api-Key': apiKey };
const versionResponse = await fetch(`${app.url}/api/v1/status`, { headers });
const versionBody = await versionResponse.json();
const version = versionBody.version;
const shouldUseV2ApiCall = compareVersions(version, '2.0.1') >= 0;
////isUseV2ApiCall Check
let shouldUseV2ApiCall = false;
try {
const versionResponse = await fetch(`${app.url}/api/v1/status`, { headers });
if (!versionResponse.ok) {
throw new Error(`HTTP error! status: ${versionResponse.status}`);
}
const versionBody = await versionResponse.json();
const version = versionBody.version;
shouldUseV2ApiCall = compareVersions(version, '2.0.1') >= 0;
} catch (error) {
Consola.warn(`Failed to fetch version from ${app.url}: ${error}`);
shouldUseV2ApiCall = false;
}
return fetch(`${app.url}/api/v1/user?take=25&skip=0&sort=requests`, {
headers,
})
Expand Down

0 comments on commit c22fff9

Please sign in to comment.