Move to class structure
This commit is contained in:
@@ -1,78 +1,81 @@
|
||||
export const name = "gitea-searchengine"
|
||||
export const type = "web" // web | images | videos | news | custom
|
||||
export const bangShortcut = "gitea"
|
||||
const HOSTED_URL = "https://gitea.com";
|
||||
const API_PATH = "/api/v1/repos/search";
|
||||
|
||||
const hostedGiteaUrl = "https://gitea.com"
|
||||
const apiPath = "/api/v1/repos/search"
|
||||
export default class GiteaEngine {
|
||||
name = "Gitea";
|
||||
type = "web"; // web | images | videos | news | custom
|
||||
bangShortcut = "gitea";
|
||||
|
||||
var giteaSearchUrl : string = ""
|
||||
var giteaBase : string = ""
|
||||
var giteaToken : string = ""
|
||||
giteaSearchUrl : string = "";
|
||||
giteaBase : string = "";
|
||||
giteaToken : string = "";
|
||||
|
||||
export const settingsSchema = [
|
||||
{ key: "giteaUrl", label: "Gitea Instance URL", description: "The base URL of your custom gitea instance, if empty uses the public hosted version.", type: "url", required: false },
|
||||
{ key: "accessToken", label: "Access Token", description: "Access Token to the gitea instance, allows searching for private repositories. Requires repository read permission.", type: "password", required: false },
|
||||
]
|
||||
settingsSchema = [
|
||||
{ key: "giteaUrl", label: "Gitea Instance URL", description: "The base URL of your custom gitea instance, if empty uses the public hosted version.", type: "url", required: false },
|
||||
{ key: "accessToken", label: "Access Token", description: "Access Token to the gitea instance, allows searching for private repositories. Requires repository read permission.", type: "password", required: false },
|
||||
]
|
||||
|
||||
export const configure = (settings:any) => {
|
||||
// called after settings save and on server restart
|
||||
giteaBase = (settings.giteaUrl || hostedGiteaUrl )
|
||||
giteaSearchUrl = giteaBase + apiPath
|
||||
giteaToken = settings.accessToken || ""
|
||||
}
|
||||
|
||||
export const executeSearch = async (
|
||||
query: string,
|
||||
page = 1,
|
||||
timeFilter: string,
|
||||
context: {
|
||||
lang: string
|
||||
fetch: typeof fetch
|
||||
signProxyUrl: (url: string) => string
|
||||
buildAcceptLanguage: () => string
|
||||
dateFrom?: string
|
||||
dateTo?: string
|
||||
sentinel?: (
|
||||
response: { ok: boolean; status: number },
|
||||
engineName?: string
|
||||
) => void
|
||||
engineError?: (
|
||||
status: string,
|
||||
message: string,
|
||||
opts?: { httpStatus?: number; engine?: string }
|
||||
) => Error
|
||||
configure(settings:any) {
|
||||
// called after settings save and on server restart
|
||||
this.giteaBase = (settings.giteaUrl || HOSTED_URL );
|
||||
this.giteaSearchUrl = this.giteaBase + API_PATH;
|
||||
this.giteaToken = settings.accessToken || "";
|
||||
}
|
||||
) => {
|
||||
const results: Array<{
|
||||
title: string
|
||||
url: string
|
||||
snippet: string
|
||||
source: string
|
||||
thumbnail?: string
|
||||
}> = []
|
||||
|
||||
try {
|
||||
const doFetch = context?.fetch ?? fetch
|
||||
|
||||
var headers = {}
|
||||
|
||||
if(giteaToken.length > 0){
|
||||
headers = { "Authorization": `token ${giteaToken}` }
|
||||
async executeSearch (
|
||||
query: string,
|
||||
page = 1,
|
||||
timeFilter: string,
|
||||
context: {
|
||||
lang: string
|
||||
fetch: typeof fetch
|
||||
signProxyUrl: (url: string) => string
|
||||
buildAcceptLanguage: () => string
|
||||
dateFrom?: string
|
||||
dateTo?: string
|
||||
sentinel?: (
|
||||
response: { ok: boolean; status: number },
|
||||
engineName?: string
|
||||
) => void
|
||||
engineError?: (
|
||||
status: string,
|
||||
message: string,
|
||||
opts?: { httpStatus?: number; engine?: string }
|
||||
) => Error
|
||||
}
|
||||
) {
|
||||
const results: Array<{
|
||||
title: string
|
||||
url: string
|
||||
snippet: string
|
||||
source: string
|
||||
thumbnail?: string
|
||||
}> = []
|
||||
|
||||
const response = await doFetch(`${giteaSearchUrl}?q=${encodeURIComponent(query)}&page=${encodeURIComponent(page)}`, {headers: headers})
|
||||
context?.sentinel?.(response, name)
|
||||
const data = await response.json()
|
||||
return data.map((r:any) => ({
|
||||
title: r.full_name,
|
||||
url: r.html_url,
|
||||
snippet: r.description ?? "",
|
||||
source: "Gitea",
|
||||
...(r.avatar_url ? { thumbnail: r.avatar_url } : {}),
|
||||
...(r.duration ? { duration: r.duration } : {}),
|
||||
}));
|
||||
} catch (e: any) {
|
||||
if (e?.name === "SentinelBreach") throw e
|
||||
return []
|
||||
try {
|
||||
const doFetch = context?.fetch ?? fetch
|
||||
|
||||
var headers = {}
|
||||
|
||||
if(this.giteaToken.length > 0){
|
||||
headers = { "Authorization": `token ${this.giteaToken}` }
|
||||
}
|
||||
|
||||
const response = await doFetch(`${this.giteaSearchUrl}?q=${encodeURIComponent(query)}&page=${encodeURIComponent(page)}`, {headers: headers})
|
||||
context?.sentinel?.(response, name)
|
||||
const data = await response.json()
|
||||
return data.map((r:any) => ({
|
||||
title: r.full_name,
|
||||
url: r.html_url,
|
||||
snippet: r.description ?? "",
|
||||
source: "Gitea",
|
||||
...(r.avatar_url ? { thumbnail: r.avatar_url } : {}),
|
||||
...(r.duration ? { duration: r.duration } : {}),
|
||||
}));
|
||||
} catch (e: any) {
|
||||
if (e?.name === "SentinelBreach") throw e
|
||||
return []
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user