fix: brave search operators in headers

This commit is contained in:
yanlong.wang 2024-07-23 15:50:28 +08:00
parent d5a3ce10af
commit 786b1828b7
No known key found for this signature in database
GPG Key ID: C0A623C0BADF9F37

View File

@ -1,4 +1,4 @@
import { AsyncService, AutoCastable, DownstreamServiceFailureError, Prop, marshalErrorLike } from 'civkit';
import { AsyncService, AutoCastable, DownstreamServiceFailureError, Prop, RPC_CALL_ENVIRONMENT, marshalErrorLike } from 'civkit';
import { singleton } from 'tsyringe';
import { Logger } from '../shared/services/logger';
import { SecretExposer } from '../shared/services/secrets';
@ -6,6 +6,7 @@ import { BraveSearchHTTP, WebSearchQueryParams } from '../shared/3rd-party/brave
import { GEOIP_SUPPORTED_LANGUAGES, GeoIPService } from './geoip';
import { AsyncContext } from '../shared';
import { WebSearchOptionalHeaderOptions } from '../shared/3rd-party/brave-types';
import type { Request, Response } from 'express';
@singleton()
export class BraveSearchService extends AsyncService {
@ -146,4 +147,28 @@ export class BraveSearchExplicitOperatorsDto extends AutoCastable {
return searchTerm
}
static override from(input: any) {
const instance = super.from(input) as BraveSearchExplicitOperatorsDto;
const ctx = Reflect.get(input, RPC_CALL_ENVIRONMENT) as {
req: Request,
res: Response,
} | undefined;
const params = ['ext', 'filetype', 'inbody', 'intitle', 'inpage', 'lang', 'loc', 'site'];
for (const p of params) {
const customValue = ctx?.req.get(`x-${p}`) || ctx?.req.get(`${p}`);
if (!customValue) {
continue;
}
const filtered = customValue.split(', ').filter(Boolean)
if (filtered.length) {
Reflect.set(instance, p, filtered);
}
}
return instance;
}
}