4.9 KiB
4.9 KiB
Tasks — add-api-plugin-auth
1. Scaffold
- 1.1 Create
packages/api/api-plugin-auth/withsrc/{plugin,middleware,principal,services,schemas,routes,schema,error-codes,otel,types}/+tests/{setup,integration}/ - 1.2
package.jsonv0.1.0. Deps:@suaveplan/api-plugin@suaveplan/api-openapi@suaveplan/api-migrations@suaveplan/better-auth-surrealdb@suaveplan/surrealdb@suaveplan/error@suaveplan/typestslib,hono@hono/zod-openapi(catalog:server),zod(catalog:forms),@opentelemetry/api,better-auth(catalog:auth) - 1.3 15 subpath exports per the proposal
- 1.4 tsconfigs + vite + vitest (100/100/100/100, testTimeout 60000 for integration)
- 1.5
tests/setup/polyfills.ts
2. Implementation
- 2.1
src/error-codes.{ts,test.ts,md,index.ts}— 10 AUTH.* codes +AuthError - 2.2
src/principal/principal.{ts,test.ts,md,index.ts}—Principalinterface +mapBetterAuthUserToPrincipaldefault - 2.3
src/schemas/principal.{ts,test.ts,md,index.ts}—PrincipalSchema,SessionSchema,AccountSchema(Zod) - 2.4
src/schemas/options.{ts,test.ts,md,index.ts}—AuthPluginOptionsSchema - 2.5
src/schemas/routes/<name>.{ts,test.ts,md}— per-route request/response Zod schemas (sign-up, sign-in, sign-out, session, forgot-password, reset-password, verify-email, oauth-start, oauth-callback) +schemas/routes/index.ts - 2.6
src/otel/instruments.{ts,test.ts,md,index.ts}— counters + histogram - 2.7
src/services/auth-service.{ts,test.ts,md,index.ts}—AuthServiceinterface + impl;ServiceRegistrymodule augmentation - 2.8
src/middleware/require-auth.{ts,test.ts,md,index.ts}—requireAuth()factory - 2.9
src/middleware/require-role.{ts,test.ts,md}—requireRole(...)factory - 2.10
src/middleware/require-email-verified.{ts,test.ts,md}—requireEmailVerified()factory - 2.11
src/middleware/session.{ts,test.ts,md}— global session-resolver middleware (setsc.set("user")+c.set("session"); HonoContextVariableMapaugmentation) - 2.12
src/routes/build-routes.{ts,test.ts,md,index.ts}—buildAuthRoutes(auth)returningPluginRouteEntry[]viaRouteBuilder - 2.13
src/schema/schema.{ts,md,index.ts}— re-exportBETTER_AUTH_SURREAL_SCHEMA+applyBetterAuthSchemafrom@suaveplan/better-auth-surrealdb - 2.14
src/plugin/plugin.{ts,test.ts,md,index.ts}—createAuthPlugin(options)factory; 6-steponInitlifecycle (resolve services → apply schema → construct Better-Auth → register service → register routes → install session middleware);onClosereleases DB - 2.15
src/plugin/on-init.test.ts— covers each of the 6 onInit steps + error paths - 2.16
src/index.ts— named re-exports only
3. Tests
- 3.1 Co-located
*.test.tsper spec scenario - 3.2
src/api-plugin-auth.spec-conformance.test.ts— every AUTH.* code thrown;expectTypeOfonPrincipal,AuthService,ContextVariableMapaugmentation - 3.3
tests/integration/full-boot.test.ts— realPluginRegistry+MigrationRunner+OpenAPIRegistry+ SurrealDB testcontainer. Full sign-up → sign-in → /session → sign-out roundtrip - 3.4
tests/integration/openapi-doc.test.ts— served/openapi.jsonincludes all 9 auth routes with Zod-typed schemas - 3.5
tests/integration/schema-idempotency.test.ts— restart plugin twice; schema re-apply succeeds without breaking existing rows - 3.6
tests/property.test.ts— fast-check 200 runs: arbitrary valid Principal payloads round-trip throughPrincipalSchema - 3.7
tests/dep-graph.test.ts— deps + 15 subpaths + sibling .md per schemas/routes/* - 3.8 All tests via
@suaveplan/testing/runner,{ timeout: 5000 }(60000 for integration)
4. Documentation
- 4.1 Sibling
.mdper impl (≥200 words) - 4.2
README.md— quick-start usingdefineApp().use(createAuthPlugin({ betterAuth: { … } })); the 3-way plug-and-play contract explained with the auth plugin as the canonical example;requireAuth/requireRolerecipes; OAuth provider config; schema apply / migrations integration notes
5. Gates
- 5.1
bunx turbo lint typecheck test --filter=@suaveplan/api-plugin-auth --force - 5.2 Coverage 100/100/100/100
- 5.3
bun scripts/openspec-conformance.ts @suaveplan/api-plugin-authexit 0 - 5.4 Build green
- 5.5
bunx turbo test --filter=...@suaveplan/api-plugin-auth --force— dependent sweep (relevant onceapi-plugin-user+api-plugin-vaultexist; harmless until then) - 5.6 Banned-pattern grep + no
export *+ no direct vitest/bun:test imports
6. Archive
- 6.1 Fold spec →
openspec/specs/api-plugin-auth/spec.md - 6.2 mv change →
openspec/archive/<date>-add-api-plugin-auth/ - 6.3 DO NOT refresh MANIFEST or STATUS — orchestrator consolidates after the 4-package batch