All files / app/services/links links-api.service.ts

100% Statements 16/16
100% Branches 0/0
100% Functions 4/4
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 474x 4x 4x 4x   4x                           4x 8x 8x                 6x           6x 5x 5x 6x   5x          
import { environment } from '../../../environments/environment';
import { HttpClient } from '@angular/common/http';
import { Injectable, inject } from '@angular/core';
import { ApiResponse, assertIsDefined } from '@drevo-web/shared';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
 
interface LinksCheckResponse {
    [key: string]: { isExist: boolean };
}
 
/**
 * Low-level API service for links-related HTTP requests.
 *
 * @internal Use LinksService instead
 */
@Injectable({
    providedIn: 'root',
})
export class LinksApiService {
    private readonly apiUrl = environment.apiUrl;
    private readonly http = inject(HttpClient);
 
    /**
     * Check if links exist
     *
     * @param links - Array of link keys to check
     * @returns Observable with record of link existence statuses
     */
    checkLinks(links: string[]): Observable<Record<string, boolean>> {
        return this.http
            .post<
                ApiResponse<LinksCheckResponse>
            >(`${this.apiUrl}/api/wiki-links/check`, { links }, { withCredentials: true })
            .pipe(
                map(response => {
                    assertIsDefined(response.data, 'Response data is undefined');
                    const result: Record<string, boolean> = {};
                    Object.entries(response.data).forEach(([key, value]) => {
                        result[key] = value.isExist;
                    });
                    return result;
                })
            );
    }
}