Skip to content

feat(resource): implement defineResource API (A2)#16

Merged
pipewrk merged 2 commits intomainfrom
sprint-1/a2-define-resource
Sep 30, 2025
Merged

feat(resource): implement defineResource API (A2)#16
pipewrk merged 2 commits intomainfrom
sprint-1/a2-define-resource

Conversation

@pipewrk
Copy link
Contributor

@pipewrk pipewrk commented Sep 30, 2025

Summary

Implements A2: defineResource Public API with comprehensive resource definition, config validation, and typed client method generation.

Features

  • ✅ function for declaring typed REST resources
  • ✅ Complete config validation with DeveloperError for dev-time safety
  • ✅ Automatic client method generation (list, get, create, update, remove)
  • ✅ Path interpolation for :id/:slug patterns in REST routes
  • ✅ Store key generation (gk/{resourceName})
  • ✅ Default and custom cache key generators
  • ✅ Full TypeScript type safety with generics
  • ✅ Test file type checking in pre-commit hook

Files Added

  • packages/kernel/src/resource/types.ts - Type definitions for resource system
  • packages/kernel/src/resource/interpolate.ts - Path parameter interpolation
  • packages/kernel/src/resource/defineResource.ts - Core defineResource function
  • packages/kernel/src/resource/index.ts - Module exports
  • packages/kernel/src/resource/__tests__/interpolate.test.ts - 29 tests
  • packages/kernel/src/resource/__tests__/defineResource.test.ts - 41 tests
  • packages/kernel/tsconfig.tests.json - TypeScript config for test files
  • .husky/pre-commit - Updated with test typecheck

Tests

  • 70 new tests (29 interpolation + 41 defineResource)
  • 153 total tests passing
  • All tests type-checked with strict TypeScript

Verification

  • ✅ Tests: 153 passing
  • ✅ Build: TypeScript compilation successful
  • ✅ Lint: ESLint passing
  • ✅ TypeCheck: Source and test files
  • ✅ Pre-commit hook: Includes test typecheck

Dependencies

Next Steps

After merge, continue to A3: Store Factory (issue #3)

Note

Client methods currently throw NotImplementedError - transport integration will be added in A3.

Closes #2

…nt generation

- Add defineResource<T, TQuery>(config) function
- Implement comprehensive config validation (name, routes, methods)
- Generate typed client methods (list, get, create, update, remove)
- Add path interpolation for :id/:slug patterns
- Generate store keys and cache key functions
- Add 70 unit tests (29 interpolation + 41 defineResource)
- Export resource system from kernel package
- Add tsconfig.tests.json for test file type checking
- Add typecheck:tests script and pre-commit hook

Tests: 153 passing
Build: ✓ TypeScript compilation successful
Lint: ✓ ESLint passing
TypeCheck: ✓ Source and test files

Implements issue #2 (A2: defineResource Public API)
Depends on #1 (A1: Result Types & Errors)
Copilot AI review requested due to automatic review settings September 30, 2025 21:27
Copy link
Contributor

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

Implements the A2: defineResource Public API as a core building block for the WP Kernel resource system. This adds comprehensive resource definition capabilities with type safety, validation, and automatic client generation for REST endpoints.

Key changes include:

  • Core defineResource function with TypeScript generics for type-safe resource definitions
  • Complete configuration validation using the KernelError system from A1
  • Automatic generation of typed client methods (list, get, create, update, remove) based on configured routes

Reviewed Changes

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

Show a summary per file
File Description
packages/kernel/src/resource/types.ts Complete TypeScript type definitions for resource system including config interfaces, client methods, and response types
packages/kernel/src/resource/interpolate.ts REST path interpolation utilities for handling :id/:slug patterns with validation
packages/kernel/src/resource/defineResource.ts Core defineResource function with config validation and client method generation
packages/kernel/src/resource/index.ts Module exports for the resource system
packages/kernel/src/resource/tests/interpolate.test.ts Comprehensive tests for path interpolation (29 tests)
packages/kernel/src/resource/tests/defineResource.test.ts Extensive tests for resource definition and validation (41 tests)
packages/kernel/src/index.ts Adds resource system exports to main kernel module
packages/kernel/tsconfig.tests.json TypeScript configuration for test files
packages/kernel/package.json Adds typecheck:tests script
package.json Adds root-level typecheck:tests script
.husky/pre-commit Updates pre-commit hook to include test file type checking
.changeset/define-resource-api.md Changeset documenting the new feature

* @param config - Resource configuration to validate
* @throws DeveloperError if configuration is invalid
*/
function validateConfig<T, TQuery>(config: ResourceConfig<T, TQuery>): void {
Copy link

Copilot AI Sep 30, 2025

Choose a reason for hiding this comment

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

[nitpick] The validateConfig function performs extensive validation on every defineResource call. Consider memoizing validation results for identical configurations or moving some checks to build time to improve runtime performance.

Copilot uses AI. Check for mistakes.
- Add detailed configuration reference (name, routes, cacheKeys)
- Document generated client methods with examples
- Explain TypeScript generics (T, TQuery)
- Add validation & error handling section
- Document path interpolation patterns
- Add advanced patterns (nested resources, custom cache keys)
- Include best practices & current limitations
- Add cross-references to related docs
@pipewrk pipewrk merged commit e9e91cd into main Sep 30, 2025
7 checks passed
@pipewrk pipewrk deleted the sprint-1/a2-define-resource branch October 5, 2025 01:48
pipewrk added a commit that referenced this pull request Nov 8, 2025
feat(resource): implement defineResource API (A2)
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.

A2: defineResource Public API

2 participants