commit
Fix the system api
diff --git a/src/lib/index.ts b/src/lib/index.ts
index e5685b7..8d0c28d 100644
--- a/src/lib/index.ts
+++ b/src/lib/index.ts
@@ -22,6 +22,10 @@ export const apiBase = 'https://doughmination.uk';
export const wsUrl = 'wss://doughmination.uk/v2/ws';
+// Plural System
+
+export const pluralBase = 'https://doughmination.co.uk/api';
+
// Discord
export const discordId = '1464890289922641993';
diff --git a/src/lib/scripts/api.ts b/src/lib/scripts/api.ts
index d25db92..2257635 100644
--- a/src/lib/scripts/api.ts
+++ b/src/lib/scripts/api.ts
@@ -7,21 +7,18 @@
// { detail } / { error }, or wrap it in a { success, data, error } envelope.
import { apiBase } from '#lib';
+import { cached } from '#lib/scripts/cache.ts';
+import { ApiError, type ErrorBody, errorMessage } from '#lib/scripts/errors';
-export class ApiError extends Error {
- constructor(
- message: string,
- readonly status: number
- ) {
- super(message);
- }
-}
-
-type ErrorBody = { detail?: string; error?: string | { message?: string } };
-
-function errorMessage(body: ErrorBody | null, status: number): string {
- const error = typeof body?.error === 'string' ? body.error : body?.error?.message;
- return body?.detail ?? error ?? `Request failed (${status})`;
+export interface Device {
+ device: string;
+ level?: number;
+ charging?: boolean;
+ lowPowerMode?: boolean;
+ wifi?: string;
+ watch?: boolean;
+ airpods?: boolean;
+ updated_at: string;
}
/** A bare-payload route. Throws ApiError on any non-2xx response. */
@@ -41,3 +38,13 @@ export async function apiData<T>(path: string, init?: RequestInit): Promise<T> {
if (!body.success || body.data === undefined) throw new ApiError(errorMessage(body, 200), 200);
return body.data;
}
+
+export function fetchDevice(device: string): Promise<Device> {
+ return cached(
+ `device:${device}`,
+ () => api<Device>(`/v2/devices/${encodeURIComponent(device)}`),
+ {
+ events: ['device_update', 'reconnect']
+ }
+ );
+}
\ No newline at end of file
diff --git a/src/lib/scripts/errors.ts b/src/lib/scripts/errors.ts
new file mode 100644
index 0000000..7406b97
--- /dev/null
+++ b/src/lib/scripts/errors.ts
@@ -0,0 +1,15 @@
+export class ApiError extends Error {
+ constructor(
+ message: string,
+ readonly status: number
+ ) {
+ super(message);
+ }
+}
+
+export type ErrorBody = { detail?: string; error?: string | { message?: string } };
+
+export function errorMessage(body: ErrorBody | null, status: number): string {
+ const error = typeof body?.error === 'string' ? body.error : body?.error?.message;
+ return body?.detail ?? error ?? `Request failed (${status})`;
+}
\ No newline at end of file
diff --git a/src/lib/scripts/plural.ts b/src/lib/scripts/plural.ts
index 336f10c..305c783 100644
--- a/src/lib/scripts/plural.ts
+++ b/src/lib/scripts/plural.ts
@@ -4,9 +4,19 @@
// Fronting and device state from the Doughmination API
// (https://doughmination.uk, see /docs there)
-
-import { api } from '#lib/scripts/api.ts';
+import { ApiError, type ErrorBody, errorMessage } from '#lib/scripts/errors';
import { cached } from '#lib/scripts/cache.ts';
+import { pluralBase } from '#lib';
+
+async function pluralApi<T>(path: string, init?: RequestInit): Promise<T> {
+ const res = await fetch(pluralBase + path, {
+ ...init,
+ headers: { ...(init?.body ? { 'Content-Type': 'application/json' } : {}), ...init?.headers }
+ });
+ const body = (await res.json().catch(() => null)) as (T & ErrorBody) | null;
+ if (!res.ok) throw new ApiError(errorMessage(body, res.status), res.status);
+ return body as T;
+}
export interface Member {
id: string;
@@ -19,36 +29,15 @@ export interface Member {
status: { text: string; emoji: string | null; updated_at: string } | null;
}
-export interface Device {
- device: string;
- level?: number;
- charging?: boolean;
- lowPowerMode?: boolean;
- wifi?: string;
- watch?: boolean;
- airpods?: boolean;
- updated_at: string;
-}
-
// Both are kept until the socket says they've changed
export function fetchFronters(): Promise<Member[]> {
return cached(
'fronters',
- async () => (await api<{ members: Member[] }>('/v2/plural/fronters')).members ?? [],
+ async () => (await pluralApi<{ members: Member[] }>('/plural/fronters')).members ?? [],
{ events: ['fronters_update', 'reconnect'] }
);
}
-export function fetchDevice(device: string): Promise<Device> {
- return cached(
- `device:${device}`,
- () => api<Device>(`/v2/devices/${encodeURIComponent(device)}`),
- {
- events: ['device_update', 'reconnect']
- }
- );
-}
-
export function memberName(m: Member): string {
return m.display_name || m.name;
}
diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte
index c47d629..6760eda 100644
--- a/src/routes/+page.svelte
+++ b/src/routes/+page.svelte
@@ -18,13 +18,16 @@
import { STATUS_LABEL, subscribePresence, type Presence } from '#lib/scripts/discord.ts';
import { timeAgo } from '#lib/scripts/music.ts';
import {
- fetchDevice,
fetchFronters,
memberName,
- type Device,
type Member
} from '#lib/scripts/plural.ts';
+ import {
+ type Device,
+ fetchDevice
+ } from "#lib/scripts/api";
+
import { discordId } from '#lib';
const PHONE = 'S26';
@@ -118,12 +121,12 @@
<SelfieDust
{revealed}
from={{
- src: '/avatar.avif',
- alt: 'Selfie of Clove',
+ src: '/pixel-art.png',
+ alt: '',
width: 400,
height: 400
}}
- to={{ src: '/avatar.avif', alt: '', width: 400, height: 400 }}
+ to={{ src: '/avatar.avif', alt: 'Selfie of Clove', width: 400, height: 400 }}
/>
{#if status}
diff --git a/static/pixel-art.png b/static/pixel-art.png
new file mode 100644
index 0000000..daacb16
Binary files /dev/null and b/static/pixel-art.png differ