web/src/utils/fetch-data.ts

30 lines · 888 bytes

 1import yaml from 'js-yaml';
 2import { error } from './logger';
 3
 4import type { AwesomePrivacy } from '../types/Service';
 5
 6const awesomePrivacyData =
 7  'https://raw.githubusercontent.com/Lissy93/awesome-privacy/main/awesome-privacy.yml';
 8
 9export const fetchData = async (): Promise<AwesomePrivacy> => {
10  try {
11    const res = await fetch(awesomePrivacyData);
12    if (!res.ok) {
13      error('Data', `HTTP ${res.status} fetching awesome-privacy.yml (${awesomePrivacyData})`);
14      return {} as AwesomePrivacy;
15    }
16    const text = await res.text();
17    return yaml.load(text) as AwesomePrivacy;
18  } catch (err) {
19    error('Data', `Failed to fetch awesome-privacy.yml: ${err}`);
20    return {} as AwesomePrivacy;
21  }
22};
23
24export const slugify = (title: string) => {
25  return (title || '')
26    .toLowerCase()
27    .replace(/\s/g, '-')
28    .replace(/\+|&/g, 'and')
29    .replaceAll('?', '');
30};