Reactive-Resume/server/src/fonts/fonts.service.ts

30 lines
862 B
TypeScript
Raw Normal View History

2022-03-02 16:44:11 +00:00
import { HttpService } from '@nestjs/axios';
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Font } from '@reactive-resume/schema';
import { get } from 'lodash';
import { firstValueFrom } from 'rxjs';
2022-04-30 10:58:17 +00:00
import cachedResponse from './assets/cachedResponse.json';
2022-03-02 16:44:11 +00:00
@Injectable()
export class FontsService {
constructor(private configService: ConfigService, private httpService: HttpService) {}
async getAll(): Promise<Font[]> {
const apiKey = this.configService.get<string>('google.apiKey');
const url = 'https://www.googleapis.com/webfonts/v1/webfonts?key=' + apiKey;
2022-04-30 10:58:17 +00:00
let data = [];
if (apiKey) {
const response = await firstValueFrom(this.httpService.get(url));
data = get(response.data, 'items', []);
} else {
data = cachedResponse;
}
2022-03-02 16:44:11 +00:00
return data;
}
}