Phase 1 of `docs/console.md` (builds on #6, whose docs commit it includes; merge #6 first). **API** - `permissions.ts`: `resource:action` permissions, built-in roles as bundles at a scope (hub, catalog, product), `can`/`require`, "may give" checks. A missing permission answers 403 with `missing: {permission, scope}`. - Catalog management: settings (incl. rating ceiling, upload policy, mirroring), key rotation, operators, membership (open / approval / invite links, bans), delisting, suspension by moderators, and `GET /api/v1/catalog/changes` for catalogs. - Membership is enforced: getting a product (403 `members_only` with the catalogs to join), the library list, and outfits relayed to others. Seeing products still needs no membership. - Console for everyone who controls a catalog, product or hosted room; `/console/audit` (tagged entries, scoped by permission, with filters), `/console/permissions`; `/me` says whether the console is for them. - Migration 0010: `role_assignment`, `catalog_member`, `catalog_invite`, `catalog_change`, catalog settings, audit `tags` (backfilled from old entries). **Website** - Console overview by what you manage; audit log page with filters; catalog list scoped to your teams; a catalog management page (settings, key, team, members, invitations, products, moderation, recent changes). - Public catalog pages with join / ask / accept invitation / leave; product pages say "members only" and link the catalogs. **Checked**: 43 API tests pass (8 new in `catalogs.test.ts`); API, web and db type checks; every new page rendered against a local hub for owner, operator, member, outsider and admin; the Eden DELETE-with-query calls tested against the API. Server-action forms weren't clicked through in a browser. Not in this PR (later phases): catalog deletion requests (with cases), drafts and moderator product edits, notifications, API tokens, two-factor, imports, charts. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Phase 1 of `docs/console.md` (builds on #6, whose docs commit it includes; merge #6 first). **API** - `permissions.ts`: `resource:action` permissions, built-in roles as bundles at a scope (hub, catalog, product), `can`/`require`, "may give" checks. A missing permission answers 403 with `missing: {permission, scope}`. - Catalog management: settings (incl. rating ceiling, upload policy, mirroring), key rotation, operators, membership (open / approval / invite links, bans), delisting, suspension by moderators, and `GET /api/v1/catalog/changes` for catalogs. - Membership is enforced: getting a product (403 `members_only` with the catalogs to join), the library list, and outfits relayed to others. Seeing products still needs no membership. - Console for everyone who controls a catalog, product or hosted room; `/console/audit` (tagged entries, scoped by permission, with filters), `/console/permissions`; `/me` says whether the console is for them. - Migration 0010: `role_assignment`, `catalog_member`, `catalog_invite`, `catalog_change`, catalog settings, audit `tags` (backfilled from old entries). **Website** - Console overview by what you manage; audit log page with filters; catalog list scoped to your teams; a catalog management page (settings, key, team, members, invitations, products, moderation, recent changes). - Public catalog pages with join / ask / accept invitation / leave; product pages say "members only" and link the catalogs. **Checked**: 43 API tests pass (8 new in `catalogs.test.ts`); API, web and db type checks; every new page rendered against a local hub for owner, operator, member, outsider and admin; the Eden DELETE-with-query calls tested against the API. Server-action forms weren't clicked through in a browser. Not in this PR (later phases): catalog deletion requests (with cases), drafts and moderator product edits, notifications, API tokens, two-factor, imports, charts. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: dhkatz <[email protected]>
91 lines
3.9 KiB
TypeScript
91 lines
3.9 KiB
TypeScript
// The hub's API, called from server components and server actions. Calls go to the API's internal address
|
|
// (API_URL) with the visitor's cookie, so the API sees the same session the browser has.
|
|
import "server-only";
|
|
import { treaty } from "@elysiajs/eden";
|
|
import type { App } from "@t5de/api";
|
|
import { headers } from "next/headers";
|
|
import { cache } from "react";
|
|
|
|
const apiURL = process.env.API_URL ?? "http://localhost:4101";
|
|
|
|
/** The visitor's cookie and address, to pass on to the API. */
|
|
async function forwarded(): Promise<Record<string, string>> {
|
|
const incoming = await headers();
|
|
const forward: Record<string, string> = {};
|
|
for (const name of ["cookie", "x-forwarded-for", "user-agent"]) {
|
|
const value = incoming.get(name);
|
|
if (value) forward[name] = value;
|
|
}
|
|
return forward;
|
|
}
|
|
|
|
/**
|
|
* The API's /api/v1 routes, as the person making this request. (Never await the client itself: it's a proxy, and
|
|
* awaiting it would call a route named "then".)
|
|
*/
|
|
export const api = treaty<App>(apiURL, { headers: forwarded }).api.v1;
|
|
|
|
type V1 = typeof api;
|
|
type Problem = { error: string; message: string };
|
|
/** What a route answers when it succeeds (the API's error shape is part of its declared answers). */
|
|
type Ok<T> = Exclude<T, Problem>;
|
|
type Data<Route extends (...args: any[]) => Promise<{ data: unknown }>> = Ok<
|
|
NonNullable<Awaited<ReturnType<Route>>["data"]>
|
|
>;
|
|
|
|
export type Person = Data<V1["me"]["get"]>["person"];
|
|
export type Account = Data<V1["account"]["get"]>;
|
|
export type Link = Account["links"][number];
|
|
export type Challenge = Account["challenges"][number];
|
|
export type AgeInfo = Data<V1["age"]["get"]>;
|
|
export type OnlineEntry = Data<V1["online"]["get"]>["online"][number];
|
|
export type ConsoleData = Data<V1["console"]["get"]>;
|
|
export type AuditEntry = ConsoleData["audit"][number];
|
|
export type CatalogPage = Data<ReturnType<V1["catalogs"]>["get"]>;
|
|
export type CatalogMember = Data<ReturnType<V1["catalogs"]>["members"]["get"]>["members"][number];
|
|
export type CatalogInvite = Data<ReturnType<V1["catalogs"]>["invites"]["get"]>["invites"][number];
|
|
export type HostedProduct = Data<ReturnType<V1["catalogs"]>["products"]["get"]>["products"][number];
|
|
export type CatalogTeam = Data<ReturnType<V1["catalogs"]>["team"]["get"]>;
|
|
export type ProductSummary = Data<V1["products"]["get"]>["products"][number];
|
|
export type ProductPage = Data<ReturnType<V1["products"]>["get"]>;
|
|
export type Revision = ProductPage["revisions"][number];
|
|
export type Catalog = Data<V1["catalogs"]["get"]>["catalogs"][number];
|
|
export type LibraryEntry = Data<V1["library"]["get"]>["library"][number];
|
|
export type HostedRoom = Data<V1["hosted-rooms"]["get"]>["rooms"][number];
|
|
|
|
/** An error the API answered: message is written to be shown as is. */
|
|
export class ApiError extends Error {
|
|
constructor(
|
|
readonly status: number,
|
|
readonly slug: string,
|
|
message: string,
|
|
) {
|
|
super(message);
|
|
}
|
|
}
|
|
|
|
type Answer<T> = { data: T; error: null } | { data: null; error: { status: unknown; value: unknown } };
|
|
|
|
/** The answer's data, or throws ApiError with the API's message. */
|
|
export function unwrap<T>(answer: Answer<T>): Ok<NonNullable<T>> {
|
|
if (answer.error) {
|
|
const status = typeof answer.error.status === "number" ? answer.error.status : 500;
|
|
const value = answer.error.value as { error?: unknown; message?: unknown } | null;
|
|
const slug = typeof value?.error === "string" ? value.error : "error";
|
|
const message =
|
|
typeof value?.message === "string" && value.message
|
|
? value.message
|
|
: "Something went wrong. Try again in a moment.";
|
|
throw new ApiError(status, slug, message);
|
|
}
|
|
return answer.data as Ok<NonNullable<T>>;
|
|
}
|
|
|
|
/** Who's signed in, or undefined. Once per request. */
|
|
export const getMe = cache(async (): Promise<Person | undefined> => {
|
|
const { data } = await api.me.get();
|
|
return data && "person" in data ? data.person : undefined;
|
|
});
|
|
|
|
export const isModerator = (p: Pick<Person, "role"> | undefined) => p?.role === "moderator" || p?.role === "admin";
|