diff --git a/bin/scratch.ts b/bin/scratch.ts index e77ed2d..98da7cb 100644 --- a/bin/scratch.ts +++ b/bin/scratch.ts @@ -4,9 +4,11 @@ import 'dotenv/config' import { gracefulExit } from 'exit-hook' import restoreCursor from 'restore-cursor' +import { SearxngClient } from '../src/services/searxng-client.js' + // import { ClearbitClient } from '../src/index.js' // import { ProxycurlClient } from '../src/services/proxycurl-client.js' -import { WikipediaClient } from '../src/services/wikipedia-client.js' +// import { WikipediaClient } from '../src/services/wikipedia-client.js' /** * Scratch pad for testing. @@ -27,9 +29,16 @@ async function main() { // }) // console.log(JSON.stringify(res, null, 2)) - const wikipedia = new WikipediaClient() - const res = await wikipedia.getPageSummary({ - title: 'Naruto_(TV_series)' + // const wikipedia = new WikipediaClient() + // const res = await wikipedia.getPageSummary({ + // title: 'Naruto_(TV_series)' + // }) + // console.log(JSON.stringify(res, null, 2)) + + const searxng = new SearxngClient() + const res = await searxng.search({ + query: '4 + 2 * 10' + // engines: ['google'] }) console.log(JSON.stringify(res, null, 2)) diff --git a/src/services/searxng-client.ts b/src/services/searxng-client.ts index 4b5d3b6..42a8896 100644 --- a/src/services/searxng-client.ts +++ b/src/services/searxng-client.ts @@ -227,11 +227,15 @@ export namespace searxng { content?: string author?: string iframe_src?: string + category?: SearchCategory + engine?: SearchEngine + publishedDate?: string } export interface SearchResponse { results: SearchResult[] suggestions: string[] + query: string } } @@ -240,11 +244,9 @@ export namespace searxng { */ export class SearxngClient { readonly ky: KyInstance - readonly apiKey: string readonly apiBaseUrl: string constructor({ - apiKey = getEnv('SEARXNG_API_KEY'), apiBaseUrl = getEnv('SEARXNG_API_BASE_URL'), ky = defaultKy }: { @@ -252,33 +254,43 @@ export class SearxngClient { apiBaseUrl?: string ky?: KyInstance } = {}) { - assert( - apiKey, - 'SearxngClient missing required "apiKey" (defaults to "SEARXNG_API_KEY")' - ) assert( apiBaseUrl, 'SearxngClient missing required "apiBaseUrl" (defaults to "SEARXNG_API_BASE_URL")' ) - this.apiKey = apiKey this.apiBaseUrl = apiBaseUrl this.ky = ky.extend({ prefixUrl: apiBaseUrl }) } - async search(opts: searxng.SearchOptions): Promise { + async search({ + query, + ...opts + }: searxng.SearchOptions): Promise { const res = await this.ky .get('search', { searchParams: pruneUndefined({ - ...omit(opts, 'categories', 'engines'), + q: query, + ...opts, categories: opts.categories?.join(','), - engines: opts.categories?.join(','), + engines: opts.engines?.join(','), format: 'json' }) }) .json() - return pick(res, 'results', 'suggestions') + res.results = res.results?.map( + (result: any) => + omit( + result, + 'parsed_url', + 'engines', + 'positions', + 'template' + ) as searxng.SearchResult + ) + + return pick(res, 'results', 'suggestions', 'query') } }