import { Inject, Injectable } from '../decorators' import ApiSuccessHandler from '../response-handlers/api-success-handlers' @Injectable('googleWebfontsApiService') export default class GoogleWebfontsApiService { constructor( @Inject('$http') private $http: ng.IHttpService, @Inject('$q') private $q: ng.IQService, @Inject('apiSuccessHandler') private apiSuccessHandler: ApiSuccessHandler, ) { } getFontList(sort?: string): ng.IPromise { const call = 'https://www.googleapis.com/webfonts/v1/webfonts' if (!sort) { sort = 'alpha' } const params = { key: 'AIzaSyB-3EKXiPv1rjJ0Ab3u4s9iUaTqHXMLVOI', sort, } return this.$http.get(call, { params }) .then((response) => { return this.apiSuccessHandler.response(response).items }) } // Get the available font variants for a given font family getFontVariants(family: string, list?: google.fonts.WebfontFamily[]): ng.IPromise { return this.$q.resolve(list || this.getFontList()) .then(families => (families.find(opt => opt.family === family)?.variants || [])) } // Filter down the variants to just font weights and order them by ascending weight sortFontWeightVariants(options: string[] | undefined): string[] { return (options || []) .filter(opt => !opt.includes('italic')) .map(opt => opt.replace('regular', '400').replace('bold', '700')) .sort((a, b) => { const aNum = parseInt(a, 10) const bNum = parseInt(b, 10) return aNum - bNum }) } }