Added configuration for search for frontend.
This commit is contained in:
@@ -1,5 +1,21 @@
|
|||||||
{
|
{
|
||||||
"features": {
|
"features": {
|
||||||
"registration": false
|
"registration": false
|
||||||
}
|
},
|
||||||
|
"providers": {
|
||||||
|
"default": "google",
|
||||||
|
"google": {
|
||||||
|
"name": "Google",
|
||||||
|
"filters": {},
|
||||||
|
"languages": {
|
||||||
|
"zh": "Chinese",
|
||||||
|
"nl": "Dutch",
|
||||||
|
"en": "English",
|
||||||
|
"fr": "Francais",
|
||||||
|
"ko": "Korean",
|
||||||
|
"ja": "Japanese",
|
||||||
|
"es": "Spanish"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@@ -7,7 +7,7 @@ export class GoogleSearchContext extends SearchContext {
|
|||||||
|
|
||||||
|
|
||||||
generateQueryParams(): string {
|
generateQueryParams(): string {
|
||||||
const filterParams = ['maxResults', 'startIndex', 'orderBy'];
|
const filterParams = ['maxResults', 'startIndex', 'orderBy', 'langRestrict'];
|
||||||
const searchParams = ['intitle', 'inauthor', 'inpublisher', 'subject', 'isbn'];
|
const searchParams = ['intitle', 'inauthor', 'inpublisher', 'subject', 'isbn'];
|
||||||
|
|
||||||
const queryParams = filterParams
|
const queryParams = filterParams
|
||||||
|
@@ -1,14 +1,16 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { BadRequestException, HttpException, Injectable } from '@nestjs/common';
|
||||||
import { BookSearchResultDto } from '../dto/book-search-result.dto';
|
import { BookSearchResultDto } from '../dto/book-search-result.dto';
|
||||||
import { HttpService } from '@nestjs/axios';
|
import { HttpService } from '@nestjs/axios';
|
||||||
import { firstValueFrom, map, timeout } from 'rxjs';
|
import { catchError, EMPTY, firstValueFrom, map, timeout } from 'rxjs';
|
||||||
import { AxiosResponse } from 'axios';
|
import { AxiosError, AxiosResponse } from 'axios';
|
||||||
import { GoogleSearchContext } from '../contexts/google.search.context';
|
import { GoogleSearchContext } from '../contexts/google.search.context';
|
||||||
|
import { PinoLogger } from 'nestjs-pino';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class GoogleService {
|
export class GoogleService {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly http: HttpService,
|
private readonly http: HttpService,
|
||||||
|
private readonly logger: PinoLogger,
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
async searchRaw(searchQuery: string): Promise<BookSearchResultDto[]> {
|
async searchRaw(searchQuery: string): Promise<BookSearchResultDto[]> {
|
||||||
@@ -28,7 +30,7 @@ export class GoogleService {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const defaultQueryParams = 'langRestrict=en&printType=books&fields=items(kind,id,volumeInfo(title,description,authors,publisher,publishedDate,industryIdentifiers,language,categories,maturityRating,imageLinks,canonicalVolumeLink,seriesInfo))';
|
const defaultQueryParams = 'printType=books&fields=items(kind,id,volumeInfo(title,description,authors,publisher,publishedDate,industryIdentifiers,language,categories,maturityRating,imageLinks,canonicalVolumeLink,seriesInfo))';
|
||||||
const customQueryParams = context.generateQueryParams();
|
const customQueryParams = context.generateQueryParams();
|
||||||
|
|
||||||
return await firstValueFrom(
|
return await firstValueFrom(
|
||||||
@@ -36,6 +38,23 @@ export class GoogleService {
|
|||||||
.pipe(
|
.pipe(
|
||||||
timeout({ first: 5000 }),
|
timeout({ first: 5000 }),
|
||||||
map(value => this.transform(value)),
|
map(value => this.transform(value)),
|
||||||
|
catchError((err: any) => {
|
||||||
|
if (err instanceof AxiosError) {
|
||||||
|
if (err.status == 400) {
|
||||||
|
throw new BadRequestException(err.response.data);
|
||||||
|
} else if (err.status == 429) {
|
||||||
|
throw new HttpException(err.response?.data, 429);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.logger.error({
|
||||||
|
class: GoogleService.name,
|
||||||
|
method: this.search.name,
|
||||||
|
msg: 'Unknown Google search error.',
|
||||||
|
error: err,
|
||||||
|
});
|
||||||
|
return EMPTY;
|
||||||
|
})
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user