Routes (additiv unter /api/v1/marketplace/*): - POST/GET /authors/me — eigenes Author-Profil anlegen/updaten/lesen - GET /authors/:slug — public Profile-Lookup (banned-reason gestrippt) - POST /decks — Deck-Init (Slug-Validation + Pflicht-Author-Profil + CHECK auf paid + Pro-License) - POST /decks/:slug/publish — Versions-Snapshot mit per-Karte cardContentHash aus @cards/domain, per-Version-Hash, AI-Mod-Stub-Log, atomarer latest_version_id-Bump in Drizzle-Transaction - PATCH /decks/:slug — Metadaten-Update (Owner-Only) - GET /decks/:slug — Public-Detail mit optional-auth-Middleware Geport aus cards-decommission-base:services/cards-server/, mit Greenfield-Anpassungen: - Hashing über @cards/domain.cardContentHash (gemeinsame SoT zwischen privatem cards.cards und marketplace.deck_cards), per- Version-Hash als SHA-256 über sortierte Karten-Hashes mit Ord-Prefix - AI-Moderation als R2-Stub (pass+rationale+model='stub'), echte mana-llm-Anbindung in späterer Welle - Auth-Middleware-Shape an Greenfield (userId/tier/authMode in c.get(...) statt user-Object), optional-auth als Schwester für anonymen Public-Read - Hono-typing: outer Marketplace-Decks-Router ist Partial<AuthVars> weil Public-GET kein JWT braucht; Auth-Subroute ist strict Lese-Referenz: - 3331 LOC altes cards-server-Code (routes, services, middleware, lib) unter docs/marketplace/archive/code/ archiviert. Read-only, nicht im Build-Path. Verifikation: - 16 neue Vitest-Tests (Slug + Version-Hash), 72 gesamt grün - type-check 0 errors - E2E-Smoke gegen lokale cards-api: Cardecky-Author + Deck r2-stoische-ethik mit 3 Karten v1.0.0 (basic + basic + cloze), per-Karten-Hashes geschrieben, ai_moderation_log-Row da, semver-409 + paid-422-Errors verifiziert. Smoke-Daten danach aufgeräumt. Verbleibend für R3+: Discovery (explore + search), Engagement (stars/ subscribe/fork), Smart-Merge mit FSRS-State-Erhalt; danach R4 PRs + Card-Discussions, R5 Frontend-Routes. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
87 lines
2.7 KiB
TypeScript
87 lines
2.7 KiB
TypeScript
import { Hono } from 'hono';
|
|
import { z } from 'zod';
|
|
import type { AuthUser } from '../middleware/jwt-auth';
|
|
import type { AuthorService } from '../services/authors';
|
|
import type { DeckService } from '../services/decks';
|
|
import type { PurchaseService } from '../services/purchases';
|
|
import { BadRequestError, UnauthorizedError } from '../lib/errors';
|
|
|
|
const cardTypes = [
|
|
'basic',
|
|
'basic-reverse',
|
|
'cloze',
|
|
'type-in',
|
|
'image-occlusion',
|
|
'audio',
|
|
'multiple-choice',
|
|
] as const;
|
|
|
|
const initSchema = z.object({
|
|
slug: z.string(),
|
|
title: z.string().min(1).max(140),
|
|
description: z.string().max(2000).optional(),
|
|
language: z.string().min(2).max(8).optional(),
|
|
license: z.string().max(64).optional(),
|
|
priceCredits: z.number().int().min(0).max(10_000).optional(),
|
|
});
|
|
|
|
const publishSchema = z.object({
|
|
semver: z.string(),
|
|
changelog: z.string().max(2000).optional(),
|
|
cards: z
|
|
.array(
|
|
z.object({
|
|
type: z.enum(cardTypes),
|
|
fields: z.record(z.string(), z.string()),
|
|
})
|
|
)
|
|
.min(1)
|
|
.max(5_000),
|
|
});
|
|
|
|
function requireUser(user: AuthUser | undefined): AuthUser {
|
|
if (!user || !user.userId) throw new UnauthorizedError();
|
|
return user;
|
|
}
|
|
|
|
export function createDeckRoutes(
|
|
authorService: AuthorService,
|
|
deckService: DeckService,
|
|
purchaseService?: PurchaseService
|
|
) {
|
|
const router = new Hono<{ Variables: { user?: AuthUser } }>();
|
|
|
|
// Init = write, auth required.
|
|
router.post('/', async (c) => {
|
|
const user = requireUser(c.get('user'));
|
|
await authorService.assertNotBanned(user.userId);
|
|
const parsed = initSchema.safeParse(await c.req.json().catch(() => ({})));
|
|
if (!parsed.success) throw new BadRequestError('Invalid body', parsed.error.format());
|
|
const deck = await deckService.init(user.userId, parsed.data);
|
|
return c.json(deck, 201);
|
|
});
|
|
|
|
// GET deck-by-slug is public — anyone can preview a deck. If a
|
|
// JWT is present we also annotate `hasPurchased` so the buy
|
|
// button can be hidden for owners.
|
|
router.get('/:slug', async (c) => {
|
|
const result = await deckService.getBySlug(c.req.param('slug'));
|
|
const user = c.get('user');
|
|
const hasPurchased =
|
|
user?.userId && purchaseService
|
|
? await purchaseService.hasPurchased(user.userId, result.deck.id)
|
|
: null;
|
|
return c.json({ ...result, hasPurchased });
|
|
});
|
|
|
|
router.post('/:slug/publish', async (c) => {
|
|
const user = requireUser(c.get('user'));
|
|
await authorService.assertNotBanned(user.userId);
|
|
const parsed = publishSchema.safeParse(await c.req.json().catch(() => ({})));
|
|
if (!parsed.success) throw new BadRequestError('Invalid body', parsed.error.format());
|
|
const result = await deckService.publish(user.userId, c.req.param('slug'), parsed.data);
|
|
return c.json(result, 201);
|
|
});
|
|
|
|
return router;
|
|
}
|