Skip to content

Commit

Permalink
move search processing to backend
Browse files Browse the repository at this point in the history
  • Loading branch information
juandjara committed Nov 20, 2023
1 parent dad82f0 commit 10d9674
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
3 changes: 2 additions & 1 deletion api/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ if (!invidiousURL) {

export default {
autocompleteUrl: 'https://suggestqueries.google.com/complete/search?client=youtube&ds=yt&q=',
detailUrlTemplate: `${invidiousURL}/api/v1/videos/{id}`
detailUrlTemplate: `${invidiousURL}/api/v1/videos/{id}`,
searchUrl: `${invidiousURL}/api/v1/search`
}
22 changes: 22 additions & 0 deletions api/routes/search.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import axios from 'axios'
import { Request, Response } from 'express'
import config from '../lib/config'

export default async function search(req: Request, res: Response) {
try {
const url = `${config.searchUrl}?q=${req.query.q}`
const { data } = await axios.get(url)
const json = data.map((d: any) => {
return {
id: d.videoId,
title: d.title,
lengthSeconds: d.lengthSeconds,
image: `https://i.ytimg.com/vi/${d.videoId}/mqdefault.jpg`
}
})
return res.json(json)
} catch (err) {
console.error(err)
return res.status(500).json(err)
}
}
15 changes: 3 additions & 12 deletions src/routes/Search.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { API_URL, INVIDIOUS_URL } from '@/config'
import { API_URL } from '@/config'
import { DotsThree, DownloadSimple, ListPlus, Play, X } from 'phosphor-react'
import { json, LoaderFunction, useLoaderData, useOutletContext } from 'react-router-dom'
import { buttonCN } from '@/styles'
Expand All @@ -19,18 +19,9 @@ export const loader: LoaderFunction = async ({ request }) => {
return { results: [] }
}

const res = await fetch(`${INVIDIOUS_URL}/api/v1/search?q=${query}`)
const res = await fetch(`${API_URL}/search?q=${query}`)
const data = await res.json()
const parsed = data.map((d: any) => {
return {
id: d.videoId,
title: d.title,
lengthSeconds: d.lengthSeconds,
image: `https://i.ytimg.com/vi/${d.videoId}/mqdefault.jpg`
}
})

return json<SearchResult[]>(parsed)
return json<SearchResult[]>(data)
}

export default function Search() {
Expand Down

0 comments on commit 10d9674

Please sign in to comment.