Skip to content

Conversation

vikaskumr
Copy link
Contributor

Purpose

Approach

Testing steps

Breaking Changes

Dependencies and/or References

Deployment

@vikaskumr vikaskumr requested a review from a team as a code owner October 1, 2025 14:39

export const handler: EventHandler = (event, context) => {
if (event.type === 'resources.search') {
if (event.type === 'resources.search' && 'cma' in context) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can have this approach or make a separate handler type for each handler.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm pretty sure this should not be necessary as we're not making use of any of the "CMA" specific context values in the handler.

Copy link
Contributor Author

@vikaskumr vikaskumr Oct 1, 2025

Choose a reason for hiding this comment

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

@BasKiers We are Getting TypeScript compilation errors when trying to use the function handlers:

Type 'FunctionEventDeliveryContext<InstallationParameters>' is not assignable to parameter of type 'FunctionEventDeliveryContext<Record<string, any>> & { cmaHost: string; uploadHost: string; appToken: string; cmaClientOptions?: any; cma?: PlainClientAPI | undefined; }'.

This is happening because LookUpHandler is intersection of Delivery and Management Context and typeScript couldn't guarantee that when routing to individual handlers, the context would have all required properties.

Basically this line in our public example app.

export type EventHandler = FunctionEventHandler<FunctionTypeEnum, InstallationParameters>;

Tldr is lookupHandler and SearchHandler have more restrictive requirements that the union type can't guarantee.

Copy link
Contributor Author

@vikaskumr vikaskumr Oct 1, 2025

Choose a reason for hiding this comment

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

We can solve this issue in lookupHandler by changing the & to | in node app toolkits but issue still remains in searchHandler

 [FunctionTypeEnum.ResourcesLookup]: {
    event: ResourcesLookupRequest
    context: FunctionEventManagementContext<P> | FunctionEventDeliveryContext<P>
    response: ResourcesLookupResponse
  }

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, I also tried quite some things but the main problem is that event and context are two separate function arguments and therefore Typescript has no ability to refine the type of context based on the refined type of event.

For now, I think this is the "best" solution we have other than making all properties in the ManagementContext optional (which would only make the DX worse for customers as they don't know what to expect from the context).

However, I would change this line to check for a property that is "required" in the ManagementContext type, e.g.

Suggested change
if (event.type === 'resources.search' && 'cma' in context) {
if (event.type === 'resources.search' && 'cmaHost' in context) {

import { Product } from './types';

export const getMockShopUrl = (context: FunctionEventContext<Record<string, any>>) => {
export const getMockShopUrl = (context: { appInstallationParameters?: Record<string, any> }) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I would rather use the context type

Suggested change
export const getMockShopUrl = (context: { appInstallationParameters?: Record<string, any> }) => {
export const getMockShopUrl = (context: FunctionEventDeliveryContext<InstallationParameters>) => {

or refactor the function to

Suggested change
export const getMockShopUrl = (context: { appInstallationParameters?: Record<string, any> }) => {
export const getMockShopUrl = (parameters: InstallationParameters) => {

@vikaskumr
Copy link
Contributor Author

Closing it currently since this can be a breaking change for customers.

@vikaskumr vikaskumr closed this Oct 2, 2025
@vikaskumr vikaskumr reopened this Oct 2, 2025
@vikaskumr vikaskumr added the do not merge Don't merge this PR until this label is removed label Oct 2, 2025
@vikaskumr vikaskumr closed this Oct 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
do not merge Don't merge this PR until this label is removed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants