[ownership] collectionRouter: optional injected duelCard hook (#57)

Attaches duel: DuelCard to collection cards when the server has a frozen
profile (requested by ui-duel); omitted while pending, never fetches.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 22:02:50 -05:00
co-authored by Claude Opus 5.5
parent fcdd05f2b1
commit 9943de3b53
2 changed files with 38 additions and 3 deletions
+22 -3
View File
@@ -24,7 +24,8 @@ export type CollectionResponse = {
needed: number;
/** playable >= deckSize. */
eligible: boolean;
cards: ChainCard[];
/** `duel` is present when the server has a frozen profile for the card (see collectionRouter's `duelCard`). */
cards: (ChainCard & { duel?: unknown })[];
};
export function collectionResponse(deps: Pick<OwnershipDeps, 'ledger' | 'accounts'>, sub: string,
@@ -37,7 +38,16 @@ export function collectionResponse(deps: Pick<OwnershipDeps, 'ledger' | 'account
eligible: playable >= r.deckSize, cards };
}
export function collectionRouter(deps: OwnershipDeps & { rules?: Partial<OwnershipRules> }): Router {
export type CollectionRouterDeps = OwnershipDeps & {
rules?: Partial<OwnershipRules>;
/**
* Optional: the same chain card → duel card function the PvP host uses, so the browser shows and
* plays exactly what PvP would. Return undefined while a card's profile is pending; it must not fetch.
*/
duelCard?: (card: ChainCard) => unknown;
};
export function collectionRouter(deps: CollectionRouterDeps): Router {
const router = Router();
router.get('/', async (req: Request, res: Response) => {
res.setHeader('Cache-Control', 'no-store');
@@ -46,7 +56,16 @@ export function collectionRouter(deps: OwnershipDeps & { rules?: Partial<Ownersh
const m = /^Bearer ([A-Za-z0-9_.-]{1,8192})$/.exec(req.get('authorization') ?? '');
try {
const account = await verifyAccount(deps, m?.[1]);
res.json(collectionResponse(deps, account.sub, { ...deps.rules, ...(q ? { minConfirmations: Number(q) } : {}) }));
const body = collectionResponse(deps, account.sub, { ...deps.rules, ...(q ? { minConfirmations: Number(q) } : {}) });
if (deps.duelCard) {
const toDuel = deps.duelCard;
body.cards = body.cards.map(c => {
let duel: unknown;
try { duel = toDuel(c); } catch { duel = undefined; }
return duel === undefined ? c : { ...c, duel };
});
}
res.json(body);
} catch (e) {
const err = e as Error & { status?: number };
const status = e instanceof DeckError ? err.status! : err.status === 503 ? 503 : 500;
+16
View File
@@ -273,3 +273,19 @@ test('GET /api/game/collection requires a verified eom.dev token (local loopback
assert.equal((await res.json()).owned, 0);
} finally { await close(); }
});
test('GET /api/game/collection attaches the injected duel card when one is ready, and omits it otherwise', async () => {
const { ledger, deps } = fixture();
mintMany(ledger, ALICE, 3);
const duelCard = (c: { pageId: number }) => {
if (c.pageId === 2) return undefined; // profile pending
if (c.pageId === 3) throw new Error('boom'); // never breaks the collection
return { kind: 'Figure', pageId: c.pageId };
};
const { base, close } = await serve(collectionRouter({ ...deps, duelCard }));
try {
const body = await (await fetch(base, { headers: { authorization: 'Bearer good.token.alice' } })).json();
assert.deepEqual(body.cards.map((c: { duel?: unknown }) => c.duel), [{ kind: 'Figure', pageId: 1 }, undefined, undefined]);
assert.ok(!('duel' in body.cards[1]));
} finally { await close(); }
});