Commit 043dbc4f27
Unsigned
Layout: unified · split
web/src/pages/api/awesome-privacy.json.ts added +21
| @@ -0,0 +1,21 @@ | ||
| 1 | import type { APIRoute } from 'astro'; | |
| 2 | import yaml from 'js-yaml'; | |
| 3 | ||
| 4 | import type { AwesomePrivacy } from '../../types/Service'; | |
| 5 | ||
| 6 | const awesomePrivacyYamlPath = 'https://raw.githubusercontent.com/Lissy93/awesome-privacy/main/awesome-privacy.yml'; | |
| 7 | ||
| 8 | export const GET: APIRoute = async () => { | |
| 9 | ||
| 10 | const yamlContent = await fetch(awesomePrivacyYamlPath) | |
| 11 | .then(response => response.text()) | |
| 12 | .catch(error => { | |
| 13 | return JSON.stringify({ error: "Failed to fetch YAML file", details: error }); | |
| 14 | }); | |
| 15 | ||
| 16 | const yamlObject = yaml.load(yamlContent) as AwesomePrivacy; | |
| 17 | ||
| 18 | return new Response( | |
| 19 | JSON.stringify(yamlObject), { headers: { 'content-type': 'application/json' } } | |
| 20 | ) | |
| 21 | } | |
web/src/pages/api/line-numbers.json.ts added +94
| @@ -0,0 +1,94 @@ | ||
| 1 | import type { APIRoute } from 'astro'; | |
| 2 | import yaml from 'js-yaml'; | |
| 3 | ||
| 4 | import type { AwesomePrivacy, Service } from '../../types/Service'; | |
| 5 | ||
| 6 | interface LineNumberRange { | |
| 7 | start: number; | |
| 8 | end: number; | |
| 9 | } | |
| 10 | ||
| 11 | interface LineNumberData { | |
| 12 | [category: string]: { | |
| 13 | [section: string]: { | |
| 14 | [service: string]: { | |
| 15 | lineNumbers: LineNumberRange | null; | |
| 16 | yaml: string; | |
| 17 | } | |
| 18 | }; | |
| 19 | }; | |
| 20 | } | |
| 21 | ||
| 22 | const awesomePrivacyYamlPath = 'https://raw.githubusercontent.com/Lissy93/awesome-privacy/main/awesome-privacy.yml'; | |
| 23 | ||
| 24 | /** | |
| 25 | * Given a service object and an array of string lines from the raw YAML | |
| 26 | * Find the starting and ending line number for that service | |
| 27 | */ | |
| 28 | const calculateServiceRange = (service: Service, yamlLines: string[]): LineNumberRange | null => { | |
| 29 | const lookFor = `- name: ${service.name}`; | |
| 30 | const start = yamlLines.findIndex(line => line.includes(lookFor)) + 1; | |
| 31 | if (start === -1) return null; | |
| 32 | const detectEnd = (line: string) => { | |
| 33 | return line.trim().length === 0 | |
| 34 | || line.startsWith(' - ') | |
| 35 | || line.includes('notableMentions:') | |
| 36 | || line.includes('furtherInfo:') | |
| 37 | || line.includes('wordOfWarning:') | |
| 38 | } | |
| 39 | const remainingLines = yamlLines.slice(start); | |
| 40 | const end = start + remainingLines.findIndex(detectEnd); | |
| 41 | ||
| 42 | return { start, end }; | |
| 43 | } | |
| 44 | ||
| 45 | /** | |
| 46 | * Given a service object, convert it into a correctly formatted YAML string | |
| 47 | */ | |
| 48 | const convertJsonIntoYaml = (service: Service): string => { | |
| 49 | return yaml.dump(service); | |
| 50 | }; | |
| 51 | ||
| 52 | /** | |
| 53 | * Given the object representation of the YAML and the array of lines from the raw YAML | |
| 54 | * Organize the data into a format that can be returned as JSON | |
| 55 | */ | |
| 56 | const makeResults = (yamlObject: AwesomePrivacy, yamlLines: string[]): LineNumberData => { | |
| 57 | const organizedData: LineNumberData = {}; | |
| 58 | yamlObject.categories.forEach((category) => { | |
| 59 | organizedData[category.name] = {}; | |
| 60 | category.sections.forEach((section) => { | |
| 61 | organizedData[category.name][section.name] = {}; | |
| 62 | section.services.forEach((service) => { | |
| 63 | organizedData[category.name][section.name][service.name] = { | |
| 64 | lineNumbers: calculateServiceRange(service, yamlLines), | |
| 65 | yaml: convertJsonIntoYaml(service), | |
| 66 | }; | |
| 67 | }); | |
| 68 | }); | |
| 69 | }); | |
| 70 | return organizedData; | |
| 71 | } | |
| 72 | ||
| 73 | export const GET: APIRoute = async () => { | |
| 74 | ||
| 75 | // Fetch the raw YAML from the awesome-privacy repository | |
| 76 | const yamlContent = await fetch(awesomePrivacyYamlPath) | |
| 77 | .then(response => response.text()) | |
| 78 | .catch(error => { | |
| 79 | return JSON.stringify({ error: "Failed to fetch YAML file", details: error }); | |
| 80 | }); | |
| 81 | ||
| 82 | // Array of lines from the raw YAML | |
| 83 | const yamlLines: string[] = yamlContent.split('\n'); | |
| 84 | ||
| 85 | // Object representation of the YAML | |
| 86 | const yamlObject = yaml.load(yamlContent) as AwesomePrivacy; | |
| 87 | ||
| 88 | // Make results | |
| 89 | const results = makeResults(yamlObject, yamlLines); | |
| 90 | ||
| 91 | return new Response( | |
| 92 | JSON.stringify(results), { headers: { 'content-type': 'application/json' } } | |
| 93 | ) | |
| 94 | } | |