/* eslint-disable @typescript-eslint/no-explicit-any */ import type { SearchParams } from '../search-params'; import type { Result } from '@internetarchive/result-type'; import type { SearchServiceError } from '../search-service-error'; import { SearchParamURLGenerator } from '../search-param-url-generator'; import { BaseSearchBackend } from './base-search-backend'; import type { SearchBackendOptionsInterface } from './search-backend-options'; /** * The DefaultSearchBackend requests search results from archive.org without specifying a * `service_backend`, causing the PPS to use the appropriate default for the current context. */ export class DefaultSearchBackend extends BaseSearchBackend { private servicePath: string; constructor(options?: SearchBackendOptionsInterface) { super(options); this.servicePath = options?.servicePath ?? '/services/search/beta/page_production'; } /** @inheritdoc */ async performSearch( params: SearchParams ): Promise> { if (this.debuggingEnabled && params.debugging === undefined) { params.debugging = true; } const urlSearchParam = SearchParamURLGenerator.generateURLSearchParams( params ); const queryAsString = urlSearchParam.toString(); const url = `https://${this.baseUrl}${this.servicePath}/?${queryAsString}`; // When performing a document fetch with a list of identifiers, we must pass them // in a POST body, rather than GET params. const { pageType, identifiers } = params; const needsDocumentFetchOptions = pageType === 'client_document_fetch' && !!identifiers?.length; const options = needsDocumentFetchOptions ? { requestOptions: { method: 'POST', body: JSON.stringify({ doc_ids: identifiers }), credentials: 'include', } as RequestInit, } : undefined; return this.fetchUrl(url, options); } }