0

I'm working on a POC for Piral using Blazor WASM and having trouble calling a custom API function defined in the Piral shell from inside the Blazor WASM application. I am looking for the ability to call this custom function on demand, but the property representing this function in my Razor file is always null.

Details

I created a Piral shell application using the Piral CLI and defined a custom API function, like this (index.tsx):

interface IPiletAuthApi extends Partial<PiletApi> {
    getToken: () => Promise<string>;
}

function createCustomApi(): PiralPlugin<IPiletAuthApi> {
    // return a constructor using the global context
    return (context) => {
        // return a constructor for each local API using the pilet's metadata
        return (api, meta) => ({
            getToken: async () => "blah"
        });
    };
}

Then I created and provided this custom API when creating/rendering a Piral shell instance (index.tsx):

renderInstance({
    plugins: [
        createBlazorApi(),
        createCustomApi(),
    ],
    layout,
    errors,
    requestPilets() {
        return fetch(feedUrl)
            .then(res => res.json())
            .then(res => res.items);
    },
});

Then I created a Blazor Wasm micro-frontend using Piral's .NET template. There is an automatically generated Counter.razor file, in which I tried accessing the custom API like this:

@page "/counter"
...
@code {
    // This property is always null
    [Parameter]
    [PiralParameter("getToken")]
    public object getToken { get; set; }

    // When uncommented, this property is also always null
    // [PiralParameter("getToken")]
    // public Func<Task<string>> GetToken { get; }

    ...
}

What's the right way to call the custom API function from inside a Blazor WASM micro-frontend?

Daniel Gabriel
  • 3,939
  • 2
  • 26
  • 37

0 Answers0