All files / packages/i18n/src/core/ports TranslationProvider.ts

100% Statements 9/9
100% Branches 7/7
100% Functions 3/3
100% Lines 8/8

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                        32x   32x 32x 32x   32x           17x       32x           32x          
import { MisconfiguredVendorError } from "#core/domain/errors";
import type { Locale, TranslationMap } from "#core/domain/translations";
import type { Vendor } from "#core/domain/vendor";
import type { HttpClient } from "./HttpClient";
 
abstract class TranslationProvider<T extends object = object> implements Vendor<T> {
    readonly baseURL: string;
    readonly project: string | number;
    readonly options: T;
 
    constructor(
        { baseURL, project, options = {} as T }: Omit<Vendor<T>, "options"> & { options?: T },
        protected readonly http: HttpClient,
    ) {
        this.baseURL = baseURL;
        this.project = project;
        this.options = options;
 
        this.assertConfigured();
    }
 
    public abstract getTranslations(locale: Locale): Promise<TranslationMap>;
 
    protected configProblems(): string[] {
        return [];
    }
 
    private assertConfigured(): void {
        const problems = [
            !this.project ? "project is empty" : "",
            !URL.canParse(this.baseURL) ? "baseURL is not an absolute URL" : "",
            ...this.configProblems(),
        ].filter(Boolean);
 
        if (problems.length) throw new MisconfiguredVendorError(this.constructor.name, problems);
    }
}
 
export default TranslationProvider;