-
Notifications
You must be signed in to change notification settings - Fork 161
fix: types from new changes in node toolkit[CNX-215] #10142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
||
export const handler: EventHandler = (event, context) => { | ||
if (event.type === 'resources.search') { | ||
if (event.type === 'resources.search' && 'cma' in context) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
}
There was a problem hiding this comment.
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.
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> }) => { |
There was a problem hiding this comment.
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
export const getMockShopUrl = (context: { appInstallationParameters?: Record<string, any> }) => { | |
export const getMockShopUrl = (context: FunctionEventDeliveryContext<InstallationParameters>) => { |
or refactor the function to
export const getMockShopUrl = (context: { appInstallationParameters?: Record<string, any> }) => { | |
export const getMockShopUrl = (parameters: InstallationParameters) => { |
Closing it currently since this can be a breaking change for customers. |
Purpose
Approach
Testing steps
Breaking Changes
Dependencies and/or References
Deployment