All files / packages/i18n/src/core/adapters/providers TolgeeProvider.ts

100% Statements 5/5
100% Branches 4/4
100% Functions 2/2
100% Lines 5/5

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                            15x       6x         5x 3x     2x          
import { UndefinedLocaleProviderError } from "#core/domain/errors";
import type { Locale, TranslationMap } from "#core/domain/translations";
import TranslationProvider from "#core/ports/TranslationProvider";
 
interface TolgeeProviderOptions {
    token: string;
}
 
interface TolgeeTranslations {
    [locale: Locale]: TranslationMap;
}
 
class TolgeeProvider extends TranslationProvider<TolgeeProviderOptions> {
    protected override configProblems(): string[] {
        return this.options?.token?.trim() ? [] : ["options.token is empty"];
    }
 
    override async getTranslations(locale: Locale): Promise<TranslationMap> {
        const { data } = await this.http.get<TolgeeTranslations>(
            `/v2/projects/${this.project}/translations/${locale}`,
            { headers: { "X-API-Key": this.options.token } },
        );
 
        if (!data[locale]) {
            throw new UndefinedLocaleProviderError(locale, "Tolgee");
        }
 
        return data[locale];
    }
}
 
export default TolgeeProvider;