Skip to content

feat(kernel): Implement transport layer + field filtering (A3 + A5)#20

Merged
pipewrk merged 1 commit intomainfrom
sprint-1/a4-cache-invalidation
Oct 1, 2025
Merged

feat(kernel): Implement transport layer + field filtering (A3 + A5)#20
pipewrk merged 1 commit intomainfrom
sprint-1/a4-cache-invalidation

Conversation

@pipewrk
Copy link
Contributor

@pipewrk pipewrk commented Oct 1, 2025

Overview

This PR completes Sprint 1 Task A3 (Transport Layer) and Sprint 1 Task A5 (Field Filtering), building on the cache invalidation work from PR #19.

What's New

🚀 Transport Layer (A3)

Added a production-ready transport wrapper around @wordpress/api-fetch:

  • Request correlation: Auto-generated request IDs (req_{timestamp}_{random})
  • Event emission: Emits wpk.resource.request, wpk.resource.response, wpk.resource.error
  • Error normalization: Converts WordPress REST errors to typed KernelErrors
  • Query parameter handling: Clean URL construction with proper encoding
  • Type safety: Explicit TypeScript interfaces, no any types

📋 Field Filtering (A5)

Implemented _fields parameter support for REST API optimization:

// Only fetch specific fields
const things = await thing.list({ fields: ['id', 'title'] });
// REST call: GET /wpk/v1/things?_fields=id,title

🔌 Resource Client Methods

Wired up the resource client methods to use the transport layer:

  • list(): Fetches collections with query params and field filtering
  • get(): Fetches single items with path interpolation
  • Both methods now make real REST calls instead of throwing NotImplementedError

🧪 Comprehensive Testing

Added 21 transport tests covering:

  • Successful request/response flow with request IDs
  • Event emission for all lifecycle stages
  • Query parameter and _fields handling
  • POST/PUT with request bodies
  • Error normalization (WordPress REST, network, existing KernelError)
  • Environment handling (missing window, missing hooks)

📦 Import Standardization

Updated all test files to use @kernel/ aliases instead of relative imports for consistency.

Test Results

All 254 tests passing
94.94% code coverage (target: >60%)
TypeScript compilation clean
ESLint passes with no errors

Files Changed

New Files

  • packages/kernel/src/transport/types.ts - Transport interfaces and types
  • packages/kernel/src/transport/fetch.ts - Transport implementation
  • packages/kernel/src/transport/__tests__/fetch.test.ts - Transport tests

Modified Files

  • packages/kernel/package.json - Added @wordpress/api-fetch peer dependency
  • packages/kernel/src/index.ts - Exported transport types and functions
  • packages/kernel/src/resource/defineResource.ts - Wired up list() and get()
  • 10 test files - Standardized imports to @kernel/ aliases

Dependencies

Added peer dependency:

  • @wordpress/api-fetch (>=7.0.0)

Breaking Changes

None. This is purely additive functionality.

Next Steps

With A3 and A5 complete, the next priorities are:

  • A6: Implement remaining resource methods (create, update, remove)
  • A7: Add optimistic updates
  • A8: Implement retry logic with exponential backoff

Related

… + A5)

- Add transport layer wrapping @wordpress/api-fetch
  - Request correlation with auto-generated IDs (req_{timestamp}_{random})
  - Event emission for wpk.resource.request/response/error
  - Error normalization to KernelError types
  - Query parameter handling with URL builder
  - Field filtering support via _fields parameter (A5)

- Wire up resource client methods
  - Implement list() method with query params and _fields support
  - Implement get() method with path interpolation
  - Both methods now call transport instead of throwing NotImplementedError

- Add comprehensive transport tests
  - 21 tests covering request/response flow
  - Event emission verification
  - Query parameter and _fields handling
  - Error normalization scenarios
  - Environment handling (missing window/hooks)

- Standardize import patterns across test files
  - Update all test files to use @kernel/ aliases
  - Remove relative imports (../) for consistency

- Add @wordpress/api-fetch as peer dependency (>=7.0.0)

This completes Sprint 1 tasks A3 (Transport Layer) and A5 (Field Filtering).
All 254 tests passing, 94.94% coverage.
Copilot AI review requested due to automatic review settings October 1, 2025 01:22
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

This PR implements the transport layer and field filtering functionality for the WP Kernel framework, providing a production-ready REST client wrapper around @wordpress/api-fetch. The implementation includes request correlation, event emission, error normalization, and optimized field selection via the _fields parameter.

  • Adds comprehensive transport layer with request/response lifecycle management
  • Implements field filtering support for REST API optimization
  • Wires up resource client methods (list() and get()) to use the transport layer

Reviewed Changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
packages/kernel/src/transport/types.ts Defines TypeScript interfaces for transport requests, responses, and event payloads
packages/kernel/src/transport/fetch.ts Core transport implementation with request correlation, event emission, and error handling
packages/kernel/src/transport/__tests__/fetch.test.ts Comprehensive test suite covering all transport functionality scenarios
packages/kernel/src/resource/defineResource.ts Updates resource client methods to use transport layer instead of throwing NotImplementedError
packages/kernel/src/index.ts Exports transport types and functions, removes duplicate HttpMethod export
packages/kernel/package.json Adds @wordpress/api-fetch peer dependency
Multiple test files Standardizes imports to use @kernel/ aliases

Comment on lines +195 to +201
type ApiFetchOptions = {
path?: string;
url?: string;
method?: string;
data?: unknown;
parse?: boolean;
};
Copy link

Copilot AI Oct 1, 2025

Choose a reason for hiding this comment

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

The ApiFetchOptions type is defined inline within the function. Consider moving this to the types.ts file for reusability and better organization, or import it from @wordpress/api-fetch if available.

Copilot uses AI. Check for mistakes.
Comment on lines +38 to +39
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const globalWp = (window as any).wp;
Copy link

Copilot AI Oct 1, 2025

Choose a reason for hiding this comment

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

The same window.wp type casting pattern is used multiple times in this file. Consider extracting this into a reusable type definition or helper function to reduce duplication and improve maintainability.

Copilot uses AI. Check for mistakes.
Comment on lines +239 to +240
status: 200, // @wordpress/api-fetch doesn't expose status, assume 200 on success
headers: {}, // @wordpress/api-fetch doesn't expose headers
Copy link

Copilot AI Oct 1, 2025

Choose a reason for hiding this comment

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

These hardcoded values and comments indicate a limitation of the underlying @wordpress/api-fetch library. Consider documenting this limitation in the function's JSDoc or the transport module documentation to make it clear to API consumers.

Copilot uses AI. Check for mistakes.
Comment on lines +215 to +220
const response = await transportFetch<{
items?: T[];
total?: number;
hasMore?: boolean;
nextCursor?: string;
}>({
Copy link

Copilot AI Oct 1, 2025

Choose a reason for hiding this comment

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

The response type is defined inline. Consider extracting this to a named interface (e.g., ListResponseData<T>) in the types file for better reusability and maintainability.

Suggested change
const response = await transportFetch<{
items?: T[];
total?: number;
hasMore?: boolean;
nextCursor?: string;
}>({
const response = await transportFetch<ListResponse<T>>({

Copilot uses AI. Check for mistakes.
@pipewrk pipewrk merged commit c48a120 into main Oct 1, 2025
7 checks passed
@pipewrk pipewrk deleted the sprint-1/a4-cache-invalidation branch October 5, 2025 01:48
pipewrk added a commit that referenced this pull request Nov 8, 2025
feat(kernel): Implement transport layer + field filtering (A3 + A5)
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