Skip to content

Conversation

@ricardo-devis-agullo
Copy link
Collaborator

@ricardo-devis-agullo ricardo-devis-agullo commented Aug 30, 2025

closes #1437

🍪 Add setCookie functionality to component context

This PR adds the ability for components to set cookies via context.setCookie(), mirroring the existing context.setHeader() functionality.

✨ Features

  • New API: context.setCookie(name, value, options?) for components
  • Cookie accumulation: Cookies are collected during component execution
  • HTTP response integration: Cookies are applied to the response using res.cookie()
  • Batch support: Works with both single component and batch component requests
  • Type safety: Full TypeScript support with proper interfaces

🔧 Implementation Details

Core Changes

  • src/resources/index.ts: Added error message for invalid cookie parameters
  • src/registry/routes/helpers/get-component.ts:
    • Extended GetComponentResult interface with cookies array
    • Added cookie accumulation logic
    • Implemented contextObj.setCookie() function
  • src/registry/routes/component.ts: Added cookie application to HTTP response
  • src/registry/routes/components.ts: Added batch cookie support with setCookies() helper

Testing

  • New fixture component: test/fixtures/components/hello-world-custom-cookies/
  • Acceptance tests: Added comprehensive cookie tests in test/acceptance/registry.js
  • Unit tests: Updated component list expectations
  • All tests passing: 807/807 ✅

📝 Usage Example

Components can now set cookies in their server.js:

module.exports.data = function (context, callback) {
  context.setCookie('Test-Cookie', 'Cookie-Value');
  context.setCookie('Secure-Cookie', 'Secure-Value', { 
    httpOnly: true, 
    secure: true 
  });
  callback(null, {});
};

This will result in the appropriate Set-Cookie headers being sent with the HTTP response.

🔄 Compatibility

  • Backward compatible: No breaking changes
  • Consistent API: Follows the same pattern as context.setHeader()
  • Express integration: Uses standard Express res.cookie() method
  • Cookie options: Supports all standard Express cookie options

🧪 Testing

The implementation includes comprehensive tests covering:

  • Single component cookie setting
  • Batch component cookie handling
  • Error handling for invalid parameters
  • Integration with existing header functionality

All existing functionality remains unchanged and all tests pass.

📋 Checklist

  • Feature implementation complete
  • TypeScript types added
  • Error handling implemented
  • Tests written and passing
  • Documentation updated
  • No breaking changes
  • Follows existing patterns

- Add context.setCookie(name, value, options) API for components
- Extend GetComponentResult interface with cookies array
- Implement cookie accumulation in get-component helper
- Add cookie application in component and batch routes
- Add comprehensive tests with new fixture component
- All tests passing (807/807)

This mirrors the existing setHeader functionality and allows components
to set cookies that will be applied to the HTTP response.
@ricardo-devis-agullo ricardo-devis-agullo merged commit 7a8c8f0 into master Aug 30, 2025
3 checks passed
@ricardo-devis-agullo ricardo-devis-agullo deleted the set-cookie branch August 30, 2025 12:25
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.

Unable To Set Multiple Set-Cookie Headers on Single Response

2 participants