microsoft / Msdyn365.Commerce.Online

Dynamics 365 Commerce online project
Other
51 stars 41 forks source link

Unable to access config values from within a module #71

Closed nick4501 closed 9 months ago

nick4501 commented 10 months ago

I've created a new module with a custom data action. I'm trying to read config values from within the data action's "action" function:

const apiSettings = Msdyn365.msdyn365Commerce.apiSettings;
const contextValueFromConfig = ctx.requestContext.apiSettings

...but both of these objects are completely empty. I'm running this locally, via https://localhost:4000/modules?type=xxxxx and also tried this as part of a page mock.

I assumed that these would be populated from the corresponding values in settings\app.settings.json, but this doesn't appear to be the case. What am I doing wrong?

anastasiiaandreichuk commented 10 months ago

Hi @nick4501, App settings and api settings are different objects.

app.settings.json file holds app settings. Those settings can be accessed by using ctx.requestContext.app.config in data action's action function. More information: https://learn.microsoft.com/en-us/dynamics365/commerce/e-commerce-extensibility/app-settings

apiSettings can be mocked 3 different ways: 1) Data action mock:

{
    "CacheObjectType": "MyCacheObjectType",
    "CacheKey": "MyCacheKey",
    "Mock": {
      "foo": "bar"      
    },
    "apiSettings": {
       "channelId": 0
     }
}

2) Module mock:

{
    "id": "R1Module1",
    "config": {
      "imageAlignment": "left",
    },
    "data": {
      "actionResponse": {
        "text": "Sample Action Response"
      }
    },
    "typeName": "product-feature",
    "context": {
        "actionContext": {
        "id": "T1",
        "requestContext": {
        "locale": "en-us",
        "apiSettings": {
                "channelId": 0
        }
        }
        }
    }
}

3) Page mock - as a part of renderingContext:

"renderingContext": {
   "apiSettings": {
      "channelId": 0
    },
    "locale": "en-us"
}

Api settings can be accessed by using ctx.requestContext.apiSettings in data action's action function.

Please let us know if this worked for you.