This commit is contained in:
sdarbinyan
2026-03-06 18:40:58 +04:00
parent c3e4e695eb
commit 0b3b2ee463
25 changed files with 253 additions and 165 deletions

View File

@@ -1,7 +1,7 @@
import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { Observable, timer } from 'rxjs';
import { map, retry } from 'rxjs/operators';
import { Category, Item } from '../models';
import { environment } from '../../environments/environment';
@@ -11,6 +11,11 @@ import { environment } from '../../environments/environment';
export class ApiService {
private readonly baseUrl = environment.apiUrl;
private readonly retryConfig = {
count: 2,
delay: (error: unknown, retryCount: number) => timer(Math.pow(2, retryCount) * 500)
};
constructor(private http: HttpClient) {}
private normalizeItem(item: Item): Item {
@@ -32,7 +37,7 @@ export class ApiService {
}
getCategories(): Observable<Category[]> {
return this.http.get<Category[]>(`${this.baseUrl}/category`);
return this.http.get<Category[]>(`${this.baseUrl}/category`).pipe(retry(this.retryConfig));
}
getCategoryItems(categoryID: number, count: number = 50, skip: number = 0): Observable<Item[]> {
@@ -40,12 +45,12 @@ export class ApiService {
.set('count', count.toString())
.set('skip', skip.toString());
return this.http.get<Item[]>(`${this.baseUrl}/category/${categoryID}`, { params })
.pipe(map(items => this.normalizeItems(items)));
.pipe(retry(this.retryConfig), map(items => this.normalizeItems(items)));
}
getItem(itemID: number): Observable<Item> {
return this.http.get<Item>(`${this.baseUrl}/item/${itemID}`)
.pipe(map(item => this.normalizeItem(item)));
.pipe(retry(this.retryConfig), map(item => this.normalizeItem(item)));
}
searchItems(search: string, count: number = 50, skip: number = 0): Observable<{ items: Item[], total: number }> {
@@ -55,6 +60,7 @@ export class ApiService {
.set('skip', skip.toString());
return this.http.get<{ items: Item[], total: number, count: number, skip: number }>(`${this.baseUrl}/searchitems`, { params })
.pipe(
retry(this.retryConfig),
map(response => ({
items: this.normalizeItems(response?.items || []),
total: response?.total || 0
@@ -162,6 +168,6 @@ export class ApiService {
params = params.set('category', categoryID.toString());
}
return this.http.get<Item[]>(`${this.baseUrl}/randomitems`, { params })
.pipe(map(items => this.normalizeItems(items)));
.pipe(retry(this.retryConfig), map(items => this.normalizeItems(items)));
}
}

View File

@@ -0,0 +1,15 @@
import { Injectable } from '@angular/core';
import { ApiService } from './api.service';
@Injectable({ providedIn: 'root' })
export class PrefetchService {
private prefetched = new Set<number>();
constructor(private api: ApiService) {}
prefetchItem(itemID: number): void {
if (this.prefetched.has(itemID)) return;
this.prefetched.add(itemID);
this.api.getItem(itemID).subscribe();
}
}