Access storage API in webtrigger

Hi,

I would like to access the storage API from a webtrigger and retrieve a value which was inserted by a jira:projectpage custom UI handler. My source (shortened):

Webtrigger:

import API, { fetch, storage } from '@forge/api';

exports.runAsync = async (request) => {

  const someValue = await storage.get("someValue");

  debugger;

  if(!someValue) {
    return {
      statusCode: 303,
      statusText: 'See Other',
      headers: {
        'Location': ['http://www.somesite.de'],
      }
    }
  }

  return buildOutput(someValue)
};

Custom UI Handler:

import Resolver from '@forge/resolver';
import api, { route, properties, webTrigger, storage } from '@forge/api';

const resolver = new Resolver();

resolver.define('setValue', async (req) => {

    await storage.set("someValue", req.payload);

    return getUrlForWebtrigger() 

});

export const handler = resolver.getDefinitions();

The issue I have is, that the custom UI handler can store “someValue” but the webtrigger can’t load it (someValue is undefined):

const someValue = await storage.get("someValue");

Based on the documentation, storage Api is shared per site and per product. Since I can’t specify in the manifest that the “webtrigger” is for JIRA, does that mean, I can’t access the same storage in the webtrigger as in the jira:projectPage custom UI handler?

Thank you
Sven

Hi @SvenLauterbach

So for webtriggers, the context of which it is related to (ie, Jira site, Confluence site…) is related to the installation context in which the webtrigger is being created.

For instance, via the CLI, you can do forge webtrigger <installation-id>.
That installation-id is given by running forge install:list and each installation is linked to a product (ie, Jira).

I have tried to reproduce your issue but was not able to. I have used a Jira issue-panel along with a Webtrigger:

modules:
  jira:issuePanel:
    - key: hello-world-panel
      resource: main
      resolver:
        function: resolver
  webtrigger:
    - key: my-webtrigger
      function: my-webtrigger-fn
  function:
    - key: my-webtrigger-fn
      handler: index-webtrigger.handler
    - key: resolver
      handler: index.handler
resources:
  - key: main
    path: static/hello-world/build
app:
  id: ari:cloud:ecosystem::app/<id>
  name: hello-world-custom-ui
permissions:
  scopes:
    - "storage:app"

And my code looks like this:

Resolver:

import Resolver from '@forge/resolver';
import { storage, webTrigger } from '@forge/api';

const resolver = new Resolver();

resolver.define('getText', async (req) => {
  await storage.set("someValue", 'blah');
  return await webTrigger.getUrl('my-webtrigger');
});

export const handler = resolver.getDefinitions();

And my Webtrigger side:

import { storage } from '@forge/api';

exports.handler = async () => {
  const dataFromQuery = await storage.query().where('key', { condition: 'STARTS_WITH', value: "someValue" }).getOne();
  const dataFromGet = await storage.get("someValue");

  return {
    body: JSON.stringify({
        dataFromQuery,
        dataFromGet
  }),
  headers: {
    'Content-Type': ['application/json'],
    'X-Request-Id': [`rnd-${Math.random()}`]
  },
  statusCode: 200,
  statusText: 'OK'
 };
}

When accessed, the Webtrigger gives me the following output:

{
  "dataFromQuery":{
    "value":"blah",
    "key":"someValue"
  },
  "dataFromGet":"blah"
}

Hope this helps :slight_smile:

2 Likes

Thanks for your answer @XavierCaron . I updated your example for my use case and now it works, I don’t know what went wrong…

2 Likes