Skip to content

feat: add namespace parameter support to defineResource (Task 3 Sprint 2.5)#53

Merged
pipewrk merged 2 commits intomainfrom
sprint_1.5/46-resource-definition-updates
Oct 3, 2025
Merged

feat: add namespace parameter support to defineResource (Task 3 Sprint 2.5)#53
pipewrk merged 2 commits intomainfrom
sprint_1.5/46-resource-definition-updates

Conversation

@pipewrk
Copy link
Contributor

@pipewrk pipewrk commented Oct 3, 2025

Summary

Implements Task 3 of Sprint 2.5: Resource Definition Updates - adding namespace parameter support to defineResource function with auto-detection and explicit override capabilities.

Changes Made

✅ Resource Config Interface Updated

  • Added optional namespace?: string parameter to ResourceConfig<T, TQuery> interface
  • Comprehensive JSDoc documentation with examples for auto-detection, explicit override, and shorthand syntax
  • Full backward compatibility maintained - existing resource definitions continue to work unchanged

✅ Event Generation Updated

  • Updated createEventsGetter to use resolved namespace from parent controller
  • Event names now follow pattern: {namespace}.{resourceName}.{action}
  • Framework defaults to 'wpk' namespace, user applications auto-detect from plugin context

✅ Store Key Generation Updated

  • Store keys now use pattern: {namespace}/{resourceName} (e.g., my-plugin/job)
  • Updated lazy store registration to use resolved namespace
  • Cache invalidation properly respects namespaced store keys

✅ Integration & Namespace Resolution

  • Added resolveNamespaceAndName() helper function in define.ts that handles:
    • Explicit namespace override: { namespace: 'my-plugin', name: 'job' }
    • Shorthand syntax: { name: 'my-plugin:job' }
    • Auto-detection fallback: { name: 'job' } → uses getNamespace() detection
  • Proper separation of concerns: parent controller resolves, child functions receive processed data

Architecture Decisions

✅ Minimal API Surface Changes

  • Only createEventsGetter signature changed to accept resolved values from parent
  • All other grouped API functions unchanged - continue to receive config parameter
  • Parent-controller pattern: define.ts handles resolution, passes clean data down

✅ Avoiding Dangerous Patterns

  • No config object mutation - pass resolved values explicitly rather than modifying original config
  • Leverages existing ResourceConfig structure - namespace parameter was the right place for this data
  • Parent does heavy lifting - child functions receive exactly what they need, no duplication of logic

Test Coverage

  • All 600 existing tests pass
  • New comprehensive namespace tests added to define.test.ts:
    • Auto-detection scenarios
    • Explicit namespace override
    • Shorthand namespace:name syntax
    • Edge cases and validation
  • Updated grouped-api-factories tests to match new createEventsGetter signature

Acceptance Criteria Met

Resource config interface updated: Optional namespace parameter added with full TypeScript support
Event generation updated: Uses resolved namespace, follows {namespace}.{resource}.{action} pattern
Store key generation updated: Uses {namespace}/{resource} pattern with proper registration
Integration tests: All existing tests pass + new namespace-specific test coverage

Breaking Changes

None - This is fully backward compatible. Existing resource definitions continue to work exactly as before.

Next Steps

This completes Task 3 of Sprint 2.5. The namespace detection system (getNamespace()) from Task 2 will be integrated separately to provide the auto-detection capabilities.

Closes #46

- Add optional  parameter to ResourceConfig interface
- Integrate namespace auto-detection in defineResource function
- Update createEventsGetter to use resolved namespace/resourceName
- Update store key generation to use {namespace}/{resourceName} pattern
- Maintain backward compatibility with existing resource definitions
- Add comprehensive tests for namespace functionality

Closes #46

Task 3 of Sprint 2.5: Resource Definition Updates
- ✅ Resource config interface updated with namespace parameter
- ✅ Event generation uses resolved namespace pattern {namespace}.{resource}.{action}
- ✅ Store key generation uses pattern {namespace}/{resource}
- ✅ All 600 tests passing with namespace integration
Copilot AI review requested due to automatic review settings October 3, 2025 08:56
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 namespace parameter support for the defineResource function as part of Task 3 Sprint 2.5. It adds optional namespace configuration with auto-detection capabilities and explicit override support while maintaining full backward compatibility.

  • Adds optional namespace parameter to ResourceConfig interface with auto-detection fallback
  • Updates event generation to use resolved namespace in {namespace}.{resourceName}.{action} pattern
  • Updates store key generation to use {namespace}/{resourceName} pattern
  • Implements namespace resolution logic supporting explicit override, shorthand syntax, and auto-detection

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
packages/kernel/src/resource/types.ts Adds optional namespace parameter to ResourceConfig interface with comprehensive JSDoc documentation
packages/kernel/src/resource/grouped-api.ts Updates createEventsGetter to use resolved namespace and resource name from config
packages/kernel/src/resource/define.ts Implements namespace resolution logic and integrates it throughout the resource definition process
packages/kernel/src/resource/tests/grouped-api-factories.test.ts Updates tests to match new createEventsGetter signature with namespace parameter
packages/kernel/src/resource/tests/define.test.ts Adds comprehensive test coverage for namespace scenarios including auto-detection, explicit override, and shorthand syntax

Comment on lines +38 to +45
// If name also contains colon syntax, parse the resource name part
// This handles the edge case where both are provided
if (config.name.includes(':')) {
const [, resourceName] = config.name.split(':', 2);
if (resourceName) {
return { namespace: config.namespace, resourceName };
}
}
Copy link

Copilot AI Oct 3, 2025

Choose a reason for hiding this comment

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

The destructuring assignment const [, resourceName] skips the first element but doesn't validate it exists. Consider using const parts = config.name.split(':', 2); const resourceName = parts[1]; for clearer intent and easier debugging.

Copilot uses AI. Check for mistakes.
Comment on lines +51 to +54
const [namespace, resourceName] = config.name.split(':', 2);
if (namespace && resourceName) {
return { namespace, resourceName };
}
Copy link

Copilot AI Oct 3, 2025

Choose a reason for hiding this comment

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

Consider extracting the namespace parsing logic into a separate helper function since it's used in two places (lines 40-44 and 50-53). This would reduce duplication and make the parsing logic more testable.

Copilot uses AI. Check for mistakes.
Addresses review feedback from PR #53:

- Extract duplicate namespace parsing logic into parseNamespaceFromString() helper
- Improve destructuring clarity by using explicit parts array
- Reduce code duplication and improve testability
- Better separation of concerns between parsing and resolution logic

All 723 tests continue to pass, maintaining full backward compatibility.
@pipewrk pipewrk merged commit d215a5a into main Oct 3, 2025
7 checks passed
@pipewrk pipewrk deleted the sprint_1.5/46-resource-definition-updates branch October 5, 2025 01:48
pipewrk added a commit that referenced this pull request Nov 8, 2025
Addresses review feedback from PR #53:

- Extract duplicate namespace parsing logic into parseNamespaceFromString() helper
- Improve destructuring clarity by using explicit parts array
- Reduce code duplication and improve testability
- Better separation of concerns between parsing and resolution logic

All 723 tests continue to pass, maintaining full backward compatibility.
pipewrk added a commit that referenced this pull request Nov 8, 2025
…ion-updates

feat: add namespace parameter support to defineResource (Task 3 Sprint 2.5)
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.

Task 3: Resource Definition Updates

2 participants