Plugins don't support asynchronous functions

Open
#552 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
javascript, react
Domain
api, frontend

Research direction

Start by tracing the Fetch API plugin's preRequest hook and the requestOptions function used before the FormEdit component, then follow how Formio.registerPlugin invokes them. Check how the request pipeline handles returned values and Promises. Done means asynchronous plugin callbacks can await token retrieval while the request still proceeds with the modified headers or parameters.

Written by the indexing model from the issue text.

Description

Hi all,

I was looking at the documentation and some other suggestions in the issues list about how to inject new headers to Formio requests (more specifically when you want to use the Resources in a select Data Source Type, for example).

I know that I can set the formioToken and then use x-jwt-token in my backend, which would work fine for me if I didn't need to also have some other params passed along with the request (like location id, warehouse id, etc) that are selected in the frontend.

I tried a few different ways, but none of them worked. After implementing them, when I go to the form builder and select the Resources option in Data Source Type, it just doesn't trigger the http request anymore, if I remove those implementations then it comes back sending the request but, of course, without the headers I want to add.

Here's how I'm doing using Fetch API plugin:

`import { Formio } from 'formiojs';

const FetchPlugin = {
priority: 0,
preRequest: async (url, method, request, formio) => {
// Assuming you can get the JWT token asynchronously from your state
const jwtToken = await asyncFunctionToGetToken();
request.headers.set('Authorization', Bearer ${jwtToken});
return Promise.resolve(request);
},
};

Formio.registerPlugin(FetchPlugin);`

and here's another try using it right before I call the FormEdit component:

Formio.plugins = [{ priority: 0, requestOptions: function (value) { // Assuming you already have the token const jwtToken = state.userReducer.authJWT; value.headers.append("Authorization", Bearer ${jwtToken}); return Promise.resolve(value); } }];

I also tried creating a XMLHttpRequest interceptor, but am having the same symptom.

The versions I'm running are @formio/react 5.3.0 and formiojs 4.17.1

Any ideas I can do that (so I don't need to use the x-jwt-token header) or how can I add new parameters (such location id and some other ids that are referenced in the frontend)?

Dominant language
TypeScript
Stars
371
Forks
309
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from formio/react

All issues in formio/react

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.