nomagick commited on
Commit
786b182
·
unverified ·
1 Parent(s): d5a3ce1

fix: brave search operators in headers

Browse files
backend/functions/src/services/brave-search.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { AsyncService, AutoCastable, DownstreamServiceFailureError, Prop, marshalErrorLike } from 'civkit';
2
  import { singleton } from 'tsyringe';
3
  import { Logger } from '../shared/services/logger';
4
  import { SecretExposer } from '../shared/services/secrets';
@@ -6,6 +6,7 @@ import { BraveSearchHTTP, WebSearchQueryParams } from '../shared/3rd-party/brave
6
  import { GEOIP_SUPPORTED_LANGUAGES, GeoIPService } from './geoip';
7
  import { AsyncContext } from '../shared';
8
  import { WebSearchOptionalHeaderOptions } from '../shared/3rd-party/brave-types';
 
9
 
10
  @singleton()
11
  export class BraveSearchService extends AsyncService {
@@ -146,4 +147,28 @@ export class BraveSearchExplicitOperatorsDto extends AutoCastable {
146
 
147
  return searchTerm
148
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
  }
 
1
+ import { AsyncService, AutoCastable, DownstreamServiceFailureError, Prop, RPC_CALL_ENVIRONMENT, marshalErrorLike } from 'civkit';
2
  import { singleton } from 'tsyringe';
3
  import { Logger } from '../shared/services/logger';
4
  import { SecretExposer } from '../shared/services/secrets';
 
6
  import { GEOIP_SUPPORTED_LANGUAGES, GeoIPService } from './geoip';
7
  import { AsyncContext } from '../shared';
8
  import { WebSearchOptionalHeaderOptions } from '../shared/3rd-party/brave-types';
9
+ import type { Request, Response } from 'express';
10
 
11
  @singleton()
12
  export class BraveSearchService extends AsyncService {
 
147
 
148
  return searchTerm
149
  }
150
+
151
+ static override from(input: any) {
152
+ const instance = super.from(input) as BraveSearchExplicitOperatorsDto;
153
+ const ctx = Reflect.get(input, RPC_CALL_ENVIRONMENT) as {
154
+ req: Request,
155
+ res: Response,
156
+ } | undefined;
157
+
158
+ const params = ['ext', 'filetype', 'inbody', 'intitle', 'inpage', 'lang', 'loc', 'site'];
159
+
160
+ for (const p of params) {
161
+ const customValue = ctx?.req.get(`x-${p}`) || ctx?.req.get(`${p}`);
162
+ if (!customValue) {
163
+ continue;
164
+ }
165
+
166
+ const filtered = customValue.split(', ').filter(Boolean)
167
+ if (filtered.length) {
168
+ Reflect.set(instance, p, filtered);
169
+ }
170
+ }
171
+
172
+ return instance;
173
+ }
174
  }