Browse Source

Refactoring

pull/6960/head
Thomas Kaul 3 months ago
parent
commit
7060a94bec
  1. 26
      apps/api/src/services/fetch/fetch.service.ts
  2. 5
      apps/api/src/services/fetch/interfaces/web-fetch-route.interface.ts

26
apps/api/src/services/fetch/fetch.service.ts

@ -29,10 +29,7 @@ export class FetchService implements OnModuleInit {
)) ?? []; )) ?? [];
} }
public async fetch( public async fetch(input: RequestInfo | URL, init?: RequestInit) {
input: RequestInfo | URL,
init?: RequestInit
): Promise<Response> {
const method = ( const method = (
init?.method ?? init?.method ??
(input instanceof Request ? input.method : undefined) ?? (input instanceof Request ? input.method : undefined) ??
@ -45,13 +42,13 @@ export class FetchService implements OnModuleInit {
Logger.debug(`${method} ${urlRedacted}`, 'FetchService'); Logger.debug(`${method} ${urlRedacted}`, 'FetchService');
if (method === 'GET') { if (method === 'GET') {
const matchedWebFetchRoute = this.getMatchingWebFetchRoute(url); const webFetchRoute = this.getMatchingWebFetchRoute(url);
if (matchedWebFetchRoute) { if (webFetchRoute) {
const response = await this.fetchViaWebFetchTool( const response = await this.fetchViaWebFetchTool({
url, url,
matchedWebFetchRoute webFetchRoute
); });
if (response) { if (response) {
return response; return response;
@ -78,10 +75,13 @@ export class FetchService implements OnModuleInit {
} }
} }
private async fetchViaWebFetchTool( private async fetchViaWebFetchTool({
url: string, url,
webFetchRoute: WebFetchRoute webFetchRoute
): Promise<Response | undefined> { }: {
url: string;
webFetchRoute: WebFetchRoute;
}) {
const [openRouterApiKey, openRouterModel] = await Promise.all([ const [openRouterApiKey, openRouterModel] = await Promise.all([
this.propertyService.getByKey<string>(PROPERTY_API_KEY_OPENROUTER), this.propertyService.getByKey<string>(PROPERTY_API_KEY_OPENROUTER),
this.propertyService.getByKey<string>(PROPERTY_OPENROUTER_MODEL) this.propertyService.getByKey<string>(PROPERTY_OPENROUTER_MODEL)

5
apps/api/src/services/fetch/interfaces/web-fetch-route.interface.ts

@ -5,7 +5,10 @@
* Configured via the `WEB_FETCH_ROUTES` property as a JSON array, e.g. * Configured via the `WEB_FETCH_ROUTES` property as a JSON array, e.g.
* *
* [ * [
* { "domain": "example.com", "responseContentType": "application/json" } * {
* "domain": "example.com",
* "responseContentType": "application/json"
* }
* ] * ]
* *
* Matches the domain itself and its subdomains (e.g. `api.example.com`). * Matches the domain itself and its subdomains (e.g. `api.example.com`).

Loading…
Cancel
Save