mirror of
https://github.com/revanced/revanced-website.git
synced 2025-05-30 21:30:13 +02:00
refactor: api client
This commit is contained in:
parent
10812aef27
commit
ad08371ed7
13
.github/workflows/refresh.yaml
vendored
13
.github/workflows/refresh.yaml
vendored
@ -1,13 +0,0 @@
|
|||||||
name: refresh
|
|
||||||
on:
|
|
||||||
schedule:
|
|
||||||
- cron: '0 */2 * * *'
|
|
||||||
workflow_dispatch:
|
|
||||||
jobs:
|
|
||||||
cron:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
environment: production
|
|
||||||
steps:
|
|
||||||
- name: Refresh the site prerender with the latest API data
|
|
||||||
run: |
|
|
||||||
curl -X POST '${{ secrets.DEPLOY_HOOK }}'
|
|
@ -2,73 +2,31 @@ import type { Readable, Subscriber, Unsubscriber, Writable } from "svelte/store"
|
|||||||
import { writable } from "svelte/store";
|
import { writable } from "svelte/store";
|
||||||
import { error } from "@sveltejs/kit";
|
import { error } from "@sveltejs/kit";
|
||||||
|
|
||||||
import { prerendering, browser } from "$app/environment";
|
import { prerendering, browser, dev } from "$app/environment";
|
||||||
|
|
||||||
import * as settings from "./settings";
|
import * as settings from "./settings";
|
||||||
import * as cache from "./cache";
|
import * as cache from "./cache";
|
||||||
|
|
||||||
|
|
||||||
export class API<T> implements Readable<T> {
|
export class API<T> implements Readable<T> {
|
||||||
private store: Writable<T>;
|
private store: Writable<T>;
|
||||||
// Note: transform function will not be called on cache hit.
|
// True if we have or are about to request data from the API.
|
||||||
private transform: (v: any) => T;
|
has_requested: boolean;
|
||||||
// True if we have or are about to request data from the possibly user-specified API.
|
|
||||||
private requested_from_api = false;
|
|
||||||
|
|
||||||
// If `transform_fn_or_key` is unspecified, the data will be returned from the API as is.
|
// `transform` will transform the data received from the API.
|
||||||
// If `transform_fn_or_key` is a function, the JSON data will pass through it.
|
constructor(public readonly endpoint: string, private readonly default_value: T, private readonly transform: ((v: any) => T) = (v) => v as T) {
|
||||||
// If `transform_fn_or_key` is a string, the JSON data will be assigned to a prop on an object.
|
// Initialize with cached data if possible.
|
||||||
// If `load_fn_fallback` is not specified, the load function will instead cause HTTP error 500 if the API request fails (it will always throw if prerendering).
|
const cached_data = cache.get(this.endpoint);
|
||||||
constructor(public readonly endpoint: string, transform_fn_or_key?: ((v: any) => T) | string, private load_fn_fallback?: T) {
|
this.has_requested = cached_data !== null;
|
||||||
if (transform_fn_or_key === undefined) {
|
|
||||||
this.transform = (v) => v as T;
|
this.store = writable(cached_data || this.default_value);
|
||||||
} else if (typeof transform_fn_or_key != "string") {
|
|
||||||
// `transform_fn_or_key` is function.
|
|
||||||
this.transform = transform_fn_or_key;
|
|
||||||
} else {
|
|
||||||
// `transform_fn_or_key` is string.
|
|
||||||
this.transform = (v) => {
|
|
||||||
let data = {};
|
|
||||||
data[transform_fn_or_key] = v;
|
|
||||||
return data as T;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private url(): string {
|
private url() {
|
||||||
let url = `${settings.api_base_url()}/${this.endpoint}`;
|
return `${settings.api_base_url()}/${this.endpoint}`;
|
||||||
|
|
||||||
if (prerendering) {
|
|
||||||
url += '?cacheBypass=';
|
|
||||||
// Just add some random stuff to the string. Doesn't really matter what we add.
|
|
||||||
// This is here to make sure we bypass the cache while prerendering.
|
|
||||||
for (let i = 0; i < 6; i++) {
|
|
||||||
url += Math.floor(Math.random() * 10).toString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return url;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
initialized() {
|
// Please don't call this directly
|
||||||
return this.store !== undefined;
|
private async _update(fetch_fn: typeof fetch) {
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize if needed
|
|
||||||
init(data: T) {
|
|
||||||
if (this.initialized()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.store = writable(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Request data, transform, cache and initialize if necessary.
|
|
||||||
async request(fetch_fn = fetch): Promise<T> {
|
|
||||||
if (browser) {
|
|
||||||
this.requested_from_api = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Try to get data from the cache.
|
// Try to get data from the cache.
|
||||||
let data = cache.get(this.endpoint);
|
let data = cache.get(this.endpoint);
|
||||||
|
|
||||||
@ -81,38 +39,51 @@ export class API<T> implements Readable<T> {
|
|||||||
cache.update(this.endpoint, data);
|
cache.update(this.endpoint, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize with the data. Applicable when page load function runs on client.
|
this.store.set(data);
|
||||||
this.init(data);
|
}
|
||||||
|
|
||||||
// store_in_cache(data)...
|
// Retrieve data and update.
|
||||||
|
private update(fetch_fn = fetch) {
|
||||||
|
// Make sure we set this immediately outside of the async function to avoid JS event loop weirdness.
|
||||||
|
this.has_requested = true;
|
||||||
|
return this._update(fetch_fn);
|
||||||
|
}
|
||||||
|
|
||||||
return data;
|
// Start retrieving data if needed.
|
||||||
|
retrieve_if_needed() {
|
||||||
|
if (!this.has_requested) {
|
||||||
|
return this.update();
|
||||||
|
}
|
||||||
|
return Promise.resolve()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Implements the load function found in `+page/layout.ts` files.
|
// Implements the load function found in `+page/layout.ts` files.
|
||||||
page_load_impl() {
|
page_load_impl() {
|
||||||
return async ({ fetch }) => {
|
return async ({ fetch }) => {
|
||||||
|
if (prerendering) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Might be better to actually return some data from the load function and use that on the client.
|
||||||
|
if (!(dev || browser || prerendering)) {
|
||||||
|
throw new Error("The API client is not optimized for production server-side rendering. Please change that :)");
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return await this.request(fetch);
|
await this.update(fetch);
|
||||||
|
return {};
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
if (this.load_fn_fallback !== undefined && !prerendering) {
|
throw error(504, "API Request Error");
|
||||||
return this.load_fn_fallback;
|
|
||||||
}
|
|
||||||
throw error(500, "API Request Error");
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Implement Svelte store.
|
// Implement Svelte store.
|
||||||
subscribe(run: Subscriber<T>, invalidate?: any): Unsubscriber {
|
subscribe(run: Subscriber<T>, invalidate?: any): Unsubscriber {
|
||||||
if (!this.initialized()) {
|
|
||||||
// Make sure you call <api>.init() with data from the load() function of the page you are working on or a layout above it.
|
|
||||||
throw Error(`API "${this.endpoint}" has not been initialized yet.`);
|
|
||||||
}
|
|
||||||
// Make sure we have up-to-date data from the API.
|
// Make sure we have up-to-date data from the API.
|
||||||
if (!this.requested_from_api && browser) {
|
if (browser) {
|
||||||
this.request().then(this.store.set);
|
this.retrieve_if_needed();
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.store.subscribe(run, invalidate);
|
return this.store.subscribe(run, invalidate);
|
||||||
@ -123,12 +94,28 @@ export class API<T> implements Readable<T> {
|
|||||||
import type { Patch, Repository, Tool } from '../types';
|
import type { Patch, Repository, Tool } from '../types';
|
||||||
import { dev_log } from "$lib/utils";
|
import { dev_log } from "$lib/utils";
|
||||||
|
|
||||||
export type ContribData = { repositories: Repository[] };
|
export type ReposData = Repository[];
|
||||||
export type PatchesData = { patches: Patch[]; packages: string[] };
|
export type PatchesData = { patches: Patch[]; packages: string[] };
|
||||||
export type ToolsData = { tools: { [repo: string]: Tool } };
|
export type ToolsData = { [repo: string]: Tool };
|
||||||
|
|
||||||
export const contributors = new API<ContribData>("contributors", undefined, { repositories: [] });
|
export const repositories = new API<ReposData>("contributors", [], json => json.repositories);
|
||||||
export const tools = new API<ToolsData>("tools", json => {
|
|
||||||
|
// It needs to look this way to not break everything.
|
||||||
|
const tools_placeholder: ToolsData = {
|
||||||
|
"revanced/revanced-manager": {
|
||||||
|
version: "v0.0.0",
|
||||||
|
timestamp: "",
|
||||||
|
repository: "",
|
||||||
|
assets: [{
|
||||||
|
url: "",
|
||||||
|
name: "",
|
||||||
|
content_type: "",
|
||||||
|
size: null,
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const tools = new API<ToolsData>("tools", tools_placeholder, json => {
|
||||||
// The API returns data in a weird shape. Make it easier to work with.
|
// The API returns data in a weird shape. Make it easier to work with.
|
||||||
let map: Map<string, Tool> = new Map();
|
let map: Map<string, Tool> = new Map();
|
||||||
for (const tool of json["tools"]) {
|
for (const tool of json["tools"]) {
|
||||||
@ -155,10 +142,10 @@ export const tools = new API<ToolsData>("tools", json => {
|
|||||||
map.set(repo, value);
|
map.set(repo, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
return { tools: Object.fromEntries(map) };
|
return Object.fromEntries(map);
|
||||||
});
|
});
|
||||||
|
|
||||||
export const patches = new API<PatchesData>("patches", patches => {
|
export const patches = new API<PatchesData>("patches", { patches: [], packages: [] }, patches => {
|
||||||
let packages: string[] = [];
|
let packages: string[] = [];
|
||||||
|
|
||||||
// gets packages
|
// gets packages
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import type { Repository } from 'src/data/types';
|
import { repositories } from "../../../data/api";
|
||||||
|
|
||||||
export let repositories: Repository[];
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<hr />
|
<hr />
|
||||||
@ -26,25 +24,23 @@
|
|||||||
<a href="/patches"><h6>Patches</h6></a>
|
<a href="/patches"><h6>Patches</h6></a>
|
||||||
<a href="/contributors"><h6>Contributors</h6></a>
|
<a href="/contributors"><h6>Contributors</h6></a>
|
||||||
</div>
|
</div>
|
||||||
{#if repositories.length}
|
<div class="link-column">
|
||||||
<div class="link-column">
|
<h5>Repos</h5>
|
||||||
<h5>Repos</h5>
|
{#each $repositories as { name }}
|
||||||
{#each repositories as { name }}
|
<a href="https://github.com/{name}" target="_blank" rel="noreferrer">
|
||||||
<a href="https://github.com/{name}" target="_blank" rel="noreferrer">
|
<div>
|
||||||
<div>
|
<h6>
|
||||||
<h6>
|
{name
|
||||||
{name
|
.replace(/-/g, ' ')
|
||||||
.replace(/-/g, ' ')
|
.replace(/revanced\/revanced/g, '')
|
||||||
.replace(/revanced\/revanced/g, '')
|
.replace(/cli/g, 'CLI')
|
||||||
.replace(/cli/g, 'CLI')
|
.replace(/api/g, 'API')
|
||||||
.replace(/api/g, 'API')
|
.replace(/(?:^|\s)\S/g, (x) => x.toUpperCase())}
|
||||||
.replace(/(?:^|\s)\S/g, (x) => x.toUpperCase())}
|
</h6>
|
||||||
</h6>
|
</div>
|
||||||
</div>
|
</a>
|
||||||
</a>
|
{/each}
|
||||||
{/each}
|
</div>
|
||||||
</div>
|
|
||||||
{/if}
|
|
||||||
<div class="link-column">
|
<div class="link-column">
|
||||||
<!-- to replace -->
|
<!-- to replace -->
|
||||||
<h5>Socials</h5>
|
<h5>Socials</h5>
|
||||||
|
@ -7,14 +7,14 @@
|
|||||||
|
|
||||||
<section class="error">
|
<section class="error">
|
||||||
<h1>{status}</h1>
|
<h1>{status}</h1>
|
||||||
{#if status == 500}
|
{#if status == 404}
|
||||||
<p>
|
|
||||||
{$page.error.message}
|
|
||||||
</p>
|
|
||||||
{:else if status == 404}
|
|
||||||
<p>That page received a cease and desist letter from a multi-billion dollar tech company.</p>
|
<p>That page received a cease and desist letter from a multi-billion dollar tech company.</p>
|
||||||
<br />
|
<br />
|
||||||
<Navigation href="/" is_selected={() => true}>Home</Navigation>
|
<Navigation href="/" is_selected={() => true}>Home</Navigation>
|
||||||
|
{:else}
|
||||||
|
<p>
|
||||||
|
{$page.error.message}
|
||||||
|
</p>
|
||||||
{/if}
|
{/if}
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
|
@ -6,13 +6,6 @@
|
|||||||
import RouterEvents from '../data/RouterEvents';
|
import RouterEvents from '../data/RouterEvents';
|
||||||
import '../app.css';
|
import '../app.css';
|
||||||
|
|
||||||
import type { PageData } from './$types';
|
|
||||||
|
|
||||||
import { contributors } from "../data/api";
|
|
||||||
|
|
||||||
export let data: PageData;
|
|
||||||
contributors.init(data);
|
|
||||||
|
|
||||||
// Just like the set/clearInterval example found here: https://svelte.dev/docs#run-time-svelte-store-derived
|
// Just like the set/clearInterval example found here: https://svelte.dev/docs#run-time-svelte-store-derived
|
||||||
const show_loading_animation = derived(RouterEvents, ($event, set) => {
|
const show_loading_animation = derived(RouterEvents, ($event, set) => {
|
||||||
if ($event.navigating) {
|
if ($event.navigating) {
|
||||||
@ -44,3 +37,6 @@
|
|||||||
{:else}
|
{:else}
|
||||||
<slot />
|
<slot />
|
||||||
{/if}
|
{/if}
|
||||||
|
<!--
|
||||||
|
afn if you are moving the footer here, please make it not use the repositories store directly and instead use component props :) -->
|
||||||
|
<!-- <Footer repos={$repositories}> -->
|
||||||
|
@ -1,7 +1,14 @@
|
|||||||
import type { PageLoad } from './$types';
|
import type { PageLoad } from './$types';
|
||||||
|
|
||||||
import { contributors } from '../data/api';
|
import { repositories } from '../data/api';
|
||||||
|
|
||||||
export const prerender = true;
|
export const prerender = true;
|
||||||
|
|
||||||
export const load: PageLoad = contributors.page_load_impl();
|
const base = repositories.page_load_impl();
|
||||||
|
|
||||||
|
export const load: PageLoad = async ({ fetch }) => {
|
||||||
|
// The entire site may softlock if the user sets a bad API url if we don't do this.
|
||||||
|
try {
|
||||||
|
return await base({ fetch });
|
||||||
|
} catch(_) { }
|
||||||
|
}
|
||||||
|
@ -5,11 +5,7 @@
|
|||||||
import ContributorHost from '$lib/components/molecules/ContributorHost.svelte';
|
import ContributorHost from '$lib/components/molecules/ContributorHost.svelte';
|
||||||
import Footer from '$lib/components/molecules/Footer.svelte';
|
import Footer from '$lib/components/molecules/Footer.svelte';
|
||||||
|
|
||||||
// Handled by `+layout.ts`.
|
import { repositories } from '../../data/api';
|
||||||
import { contributors } from '../../data/api';
|
|
||||||
|
|
||||||
import type { PageData } from './$types';
|
|
||||||
export let data: PageData;
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<svelte:head>
|
<svelte:head>
|
||||||
@ -25,7 +21,7 @@
|
|||||||
<h2>Want to show up here? <span><a href="https://github.com/revanced" target="_blank" rel="noreferrer">Become a contributor</a></span></h2>
|
<h2>Want to show up here? <span><a href="https://github.com/revanced" target="_blank" rel="noreferrer">Become a contributor</a></span></h2>
|
||||||
</div>
|
</div>
|
||||||
<div class="contrib-grid">
|
<div class="contrib-grid">
|
||||||
{#each $contributors.repositories as { contributors: contribs, name }}
|
{#each $repositories as { contributors: contribs, name }}
|
||||||
<div in:fly={{ y: 10, easing: quintOut, duration: 750 }}>
|
<div in:fly={{ y: 10, easing: quintOut, duration: 750 }}>
|
||||||
<ContributorHost {contribs} repo={name} />
|
<ContributorHost {contribs} repo={name} />
|
||||||
</div>
|
</div>
|
||||||
@ -34,7 +30,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
<Footer {...data} />
|
<Footer />
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
.contrib-grid {
|
.contrib-grid {
|
||||||
|
5
src/routes/contributors/+page.ts
Normal file
5
src/routes/contributors/+page.ts
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
import type { PageLoad } from './$types';
|
||||||
|
|
||||||
|
import { repositories } from '../../data/api';
|
||||||
|
|
||||||
|
export const load: PageLoad = repositories.page_load_impl();
|
@ -1,13 +1,9 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import type { PageData } from './$types';
|
import { tools } from '../../data/api';
|
||||||
import { tools as api_tools } from '../../data/api';
|
|
||||||
import Button from '$lib/components/atoms/Button.svelte';
|
import Button from '$lib/components/atoms/Button.svelte';
|
||||||
import Footer from '$lib/components/molecules/Footer.svelte';
|
import Footer from '$lib/components/molecules/Footer.svelte';
|
||||||
|
|
||||||
export let data: PageData;
|
$: manager = $tools["revanced/revanced-manager"];
|
||||||
api_tools.init(data);
|
|
||||||
|
|
||||||
$: manager = $api_tools.tools["revanced/revanced-manager"];
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="wrapper">
|
<div class="wrapper">
|
||||||
@ -17,7 +13,7 @@
|
|||||||
<img src="../manager_two.png" alt="Manager Screenshot"/>
|
<img src="../manager_two.png" alt="Manager Screenshot"/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<Footer {...data}/>
|
<Footer />
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
|
|
||||||
|
@ -1,6 +1,4 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import type { PageData } from './$types';
|
|
||||||
|
|
||||||
import { fly } from 'svelte/transition';
|
import { fly } from 'svelte/transition';
|
||||||
import { quintOut } from 'svelte/easing';
|
import { quintOut } from 'svelte/easing';
|
||||||
|
|
||||||
@ -12,10 +10,6 @@
|
|||||||
import PatchCell from '$lib/components/molecules/PatchCell.svelte';
|
import PatchCell from '$lib/components/molecules/PatchCell.svelte';
|
||||||
import Footer from '$lib/components/molecules/Footer.svelte';
|
import Footer from '$lib/components/molecules/Footer.svelte';
|
||||||
|
|
||||||
export let data: PageData;
|
|
||||||
// Needed when someone navigates directly to the page.
|
|
||||||
api_patches.init(data);
|
|
||||||
|
|
||||||
$: ({ patches, packages } = $api_patches);
|
$: ({ patches, packages } = $api_patches);
|
||||||
|
|
||||||
let current: boolean = false;
|
let current: boolean = false;
|
||||||
@ -55,7 +49,7 @@
|
|||||||
{/each}
|
{/each}
|
||||||
</div>
|
</div>
|
||||||
</main>
|
</main>
|
||||||
<Footer {...data} />
|
<Footer />
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
main {
|
main {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user