Sana Assistant (online)
Table of Contents

Implementing the webstore endpoint extension

From this article you will learn how to create the webstore endpoint extension. Custom webstore endpoint extension is used as an example in this article.

Please use the following reference articles to find more details on extensions infrastructure:

Start with a new project

Create a new add-on project named "Sana.Extensions.CustomWebstoreEndpointExtension" as described in the add-on development tutorial.

Implement the extension class

Create a new class CustomWebstoreEndpointExtension inherited from WebstoreEndpointExtension. More information about WebstoreEndpointExtension can be found in WebstoreEndpointExtension reference article.

public class CustomWebstoreEndpointExtension : WebstoreEndpointExtension
{
}

Implement InvokeAsync method

This method handles an HTTP requests that come from addon specific url.

See InvokeAsync method of the webstore endpoint extension for more details.

The example below can handle the HTTP request with the following url: /api/addon/CustomWebstoreEndpoint/loadData

public override Task InvokeAsync(WebstoreEndpointContext context, Func<Task> next, CancellationToken cancellationToken)
{
    if (context.Request.Path.StartsWith("loadData"))
    {
        if (!context.Webstore.IsAuthenticated)
        {
            context.Response.StatusCode = 401;
            return Task.CompletedTask;
        }

        return context.Response.WriteAsJsonAsync(new {
            data = Guid.NewGuid().ToString(),
        }, cancellationToken);
    }

    return next();
}

Implement epic.js

The example below demonstrates how to request data from webstore endpoint:

import { ofType } from 'redux-observable';
import {
  switchMap,
  tap,
} from 'rxjs/operators';

const DATA_REQUESTED = 'DATA_REQUESTED';

const addonId = 'CustomWebstoreEndpoint';

const epic = (action$, _state$, { api }) => {

  const testAddon$ = action$.pipe(
    ofType(DATA_REQUESTED),
    switchMap(() =>
      api.fetch(`/api/addon/${addonId}/loadData`, {
        method: 'POST',
      }).pipe(
        tap(data => console.log(data))
      )),
  );

  return testAddon$;
};

More information about epic can be found in Epic article.

See also