headintheclouddev / typings-suitescript-2.0

TypeScript typings for SuiteScript version 2.0
MIT License
148 stars 93 forks source link

request type in beforeLoad context #314

Closed lohart13 closed 3 hours ago

lohart13 commented 2 days ago

Looking at the beforeLoad docs, the request parameter in the context can be undefined if the event was triggered by the server.

I can submit a PR to fix this if you would like.

MrRob commented 2 days ago

Sure, also please clarify how to reproduce the issue

lohart13 commented 1 day ago

Sure, in the below example the request object will be undefined if the user event script is trigged server side (such as via Suitelet or CSV import creating/editing a record) hence the need to check that request object is defined. This currently needs the below eslint disable rule as the linter can see that the beforeLoadContext interface shows request is always defined.

https://github.com/headintheclouddev/typings-suitescript-2.0/blob/338bfbf41891efea644499ce03ae26a90b529d1a/N/types.d.ts#L185-L191

export const beforeLoad: EntryPoints.UserEvent.beforeLoad = ({ form, request }) => {
    // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
    if (request?.parameters) {
        if ('success' in request.parameters) {
            form.addPageInitMessage({
                type: message.Type.CONFIRMATION,
                title: 'Success',
                duration: 5000,
            });
        }
    }
}