Skip to content

refactor: code simplifier pass 1 - consolidate patterns and remove duplication#21

Merged
whoabuddy merged 6 commits intomainfrom
refactor/code-simplifier-pass-1
Jan 20, 2026
Merged

refactor: code simplifier pass 1 - consolidate patterns and remove duplication#21
whoabuddy merged 6 commits intomainfrom
refactor/code-simplifier-pass-1

Conversation

@whoabuddy
Copy link
Copy Markdown
Contributor

Summary

First pass of code simplification across the codebase, reducing duplication and consolidating common patterns.

  • Phase 1: Created shared encoding utilities, consolidated X402PaymentRequired type, simplified StorageDO ID generation
  • Phase 2: Created hashing endpoint factory pattern (6 endpoints reduced from ~100 lines each to ~15 lines)
  • Phase 3: Consolidated test validation helpers to _shared_utils.ts
  • Phase 4: Created shared OpenAPI schema definitions for common parameters and responses
  • Phase 5: Removed 3 unused type definitions

Total: 33 files changed, -580 net lines

Test plan

  • Run npm run check - type check passes
  • Run npm test - existing tests should pass
  • Verify /docs endpoint still renders OpenAPI spec correctly

🤖 Generated with Claude Code

whoabuddy and others added 5 commits January 20, 2026 11:26
- Extract encoding utilities to src/utils/encoding.ts
- Consolidate X402PaymentRequired type to src/types.ts
- Consolidate StorageDO ID generation into single helper
- Extract JSON parsing helper in StorageDO
- Update CLAUDE.md to reflect actual 3-tier pricing model
- Clarify base endpoint aliases are semantic, not pricing tiers

Net reduction: 176 lines (138 added, 314 removed)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Create createHashingEndpoint() factory in base.ts
- Extract keccak256 implementation to keccak256-impl.ts
- Refactor all 6 hashing endpoints to use factory pattern
- Each endpoint now ~15 lines of config vs ~100+ lines of duplicated code

Net reduction: 411 lines (from 742 to 331 across hashing endpoints)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Centralize duplicate validation helpers to _shared_utils.ts:
- hasTokenType, hasField, hasFields, isOk functions
- validators object with additional helpers (resultEquals, resultIsString, etc.)
- Remove duplicates from endpoint-registry.ts and _test_generator.ts

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Create shared schema helpers to reduce duplication:
- tokenTypeParam, response constants (402, 400, 404, 500)
- Property type helpers (okProp, stringProp, boolProp, etc.)
- Parameter builders (pathParam, queryParamString, queryParamInt)

Update KV and Paste storage endpoints to use shared schemas.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Remove types that were defined but never used:
- PricingConfig
- HealthResponse
- ErrorResponse

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings January 20, 2026 19:08
@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages bot commented Jan 20, 2026

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
x402-api-production 82337f7 Jan 20 2026, 07:43 PM

@cloudflare-workers-and-pages
Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
x402-api-production 0dba0e6 Jan 20 2026, 07:08 PM

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR consolidates duplicated code patterns across the codebase, reducing net lines by 580 while improving maintainability. The refactoring focuses on creating shared utilities, consolidating type definitions, and implementing factory patterns to eliminate repetitive code.

Changes:

  • Consolidated X402PaymentRequired and TokenContract types to src/types.ts and removed 7 duplicate definitions across test files
  • Created shared encoding utilities (src/utils/encoding.ts) for hex/base64 conversions used by hashing endpoints
  • Created hashing endpoint factory pattern that reduced 6 endpoints from ~120 lines each to ~15 lines
  • Consolidated test validation helpers to _shared_utils.ts and created OpenAPI schema helpers in src/endpoints/schema.ts
  • Simplified StorageDO ID generation with shared helper functions
  • Removed 3 unused type definitions (PricingConfig, HealthResponse, ErrorResponse)

Reviewed changes

Copilot reviewed 33 out of 33 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/types.ts Added consolidated X402PaymentRequired and TokenContract types, removed unused type definitions
src/utils/encoding.ts New shared encoding utilities for hex/bytes/base64 conversions
src/endpoints/schema.ts New shared OpenAPI schema definitions for common parameters and responses
src/endpoints/hashing/base.ts New factory function for creating hashing endpoints with shared logic
src/endpoints/hashing/*.ts Refactored 6 hashing endpoints to use factory pattern (SHA256, SHA512, SHA512-256, Keccak256, RIPEMD160, Hash160)
src/endpoints/hashing/keccak256-impl.ts Extracted Keccak-256 implementation to separate file
src/endpoints/storage//.ts Updated storage endpoints to use shared schema definitions
src/middleware/x402.ts Updated to import X402PaymentRequired and TokenContract from types.ts
src/durable-objects/StorageDO.ts Consolidated ID generation with generateRandomString helper and parseJsonField helper
tests/_shared_utils.ts Added consolidated validation helpers (hasTokenType, hasField, hasFields, isOk, validators)
tests/_test_generator.ts Removed duplicate validators and X402PaymentRequired, now imports from shared locations
tests/endpoint-registry.ts Updated to import validation helpers from _shared_utils.ts
tests/*-lifecycle.test.ts Updated 7 test files to import X402PaymentRequired from _test_generator
tests/_run_all_tests.ts Updated imports for X402PaymentRequired
src/endpoints/base.ts Added clarifying documentation for endpoint class aliases
CLAUDE.md Updated pricing documentation for clarity

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Address PR feedback: use tokenTypeParam, response400, response402
from schema.ts instead of inline definitions.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@whoabuddy
Copy link
Copy Markdown
Contributor Author

Addressed Copilot's feedback in 82337f7 - now using tokenTypeParam, response400, and response402 from schema.ts in the hashing endpoint factory.

@whoabuddy whoabuddy merged commit 0baa6b4 into main Jan 20, 2026
2 checks passed
@whoabuddy whoabuddy deleted the refactor/code-simplifier-pass-1 branch January 20, 2026 20:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants