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(
input: RequestInfo | URL,
init?: RequestInit
): Promise<Response> {
public async fetch(input: RequestInfo | URL, init?: RequestInit) {
const method = (
init?.method ??
(input instanceof Request ? input.method : undefined) ??
@ -45,13 +42,13 @@ export class FetchService implements OnModuleInit {
Logger.debug(`${method} ${urlRedacted}`, 'FetchService');
if (method === 'GET') {
const matchedWebFetchRoute = this.getMatchingWebFetchRoute(url);
const webFetchRoute = this.getMatchingWebFetchRoute(url);
if (matchedWebFetchRoute) {
const response = await this.fetchViaWebFetchTool(
if (webFetchRoute) {
const response = await this.fetchViaWebFetchTool({
url,
matchedWebFetchRoute
);
webFetchRoute
});
if (response) {
return response;
@ -78,10 +75,13 @@ export class FetchService implements OnModuleInit {
}
}
private async fetchViaWebFetchTool(
url: string,
webFetchRoute: WebFetchRoute
): Promise<Response | undefined> {
private async fetchViaWebFetchTool({
url,
webFetchRoute
}: {
url: string;
webFetchRoute: WebFetchRoute;
}) {
const [openRouterApiKey, openRouterModel] = await Promise.all([
this.propertyService.getByKey<string>(PROPERTY_API_KEY_OPENROUTER),
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.
*
* [
* { "domain": "example.com", "responseContentType": "application/json" }
* {
* "domain": "example.com",
* "responseContentType": "application/json"
* }
* ]
*
* Matches the domain itself and its subdomains (e.g. `api.example.com`).

Loading…
Cancel
Save