Orchestration SDKs

Customizing requests from the DaVinci module in JavaScript

PingOne JavaScript

The Orchestration SDK for JavaScript supports the RequestMiddleware type for adding custom logic to network requests.

You can use this request middleware to modify requests before they’re sent.

For example, you can add or customize:

  • Query parameters

  • Headers

  • Body data

You can add query parameters when starting a DaVinci flow without creating middleware by using the query argument.

Learn more in Adding custom parameters.

Step 1. Creating request middleware instances

To customize network requests, create one or more RequestMiddleware instance:

Creating a RequestMiddleware instance in JavaScript
const loggingMiddleware = (req, action, next) => {
  switch (action.type) {
    case 'DAVINCI_FLOW':
      console.log(`Starting DaVinci flow: ${req.url}`);
      break;
  }
  next();
}

Request middleware has the following inputs:

Request object

Represents the original request, and has information about the body, method type, parameters, and more.

Action object

Represents the type of operation the request performs. You can check action.type to only apply customizations to certain requests.

Available action types are as follows:

Value Description

DAVINCI_START

Initiating a DaVinci client, by calling davinciClient.start()

DAVINCI_NEXT

Proceeding through a DaVinci flow, by calling davinciClient.next()

DAVINCI_FLOW

Proceeding to a different DaVinci flow, by calling davinciClient.flow()

DAVINCI_SUCCESS

Successfully completed a DaVinci flow

DAVINCI_ERROR

Error occurred during a DaVinci flow

DAVINCI_FAILURE

Unsuccessfully completed a DaVinci flow

DAVINCI_RESUME

Resumed a DaVinci flow that was already in progress

DAVINCI_POLL

Challenge polling calls to the /status endpoint to check the status of a DaVinci flow

Next method

Proceeds to the next middleware, if configured.

Examples

To customize requests, create middleware that alters requests with your own values.

  • Headers

  • Query parameters

  • Body data

Customizing headers using request middleware
const customHeaderMiddleware = (req, action, next) => {
  req.headers.set('X-User-Agent', 'MyApp/1.0');
  next();
}
Customizing query parameters using request middleware
const customParamMiddleware = (req, action, next) => {
  req.url.searchParams.set('lang', 'en-UK');
  req.url.searchParams.set('brand', 'example.co.uk');
  next();
}
Customizing body data using request middleware
const customJSONPost = (req, action, next) => {
  // Add JSON body data to a POST request
  req.body = (JSON.stringify({ name: 'Babs Jensen', "email": "babs@example.com" }));
  req.method = "POST";
  next();
}

const customJSONPut = (req, action, next) => {
  // Add JSON body data to a PUT request
  req.body = (JSON.stringify({ name: 'Updated Name', email: "updated@example.com" }));
  req.method = "PUT";
  next();
}

const customFormPost = (req, action, next) => {
  // Add form-encoded body data to a POST request
  const formData = new FormData();
  formData.append('name', 'Babs Jensen');
  formData.append('email', 'babs@example.com');

  req.body = formData;
  req.method = "POST";
  next();
}

Step 2. Configuring the DaVinci module to use middleware

Pass the names of your custom middleware to the DaVinci module in the requestMiddleware parameter in the configuration:

Configuring the DaVinci module
const davinciClient = await davinci({
  // Custom middleware
  requestMiddleware: [
    customHeaderMiddleware,
    customParamMiddleware,
    customFormPost
  ],
  // Other configuration
  logger: {
    level: 'warn',
    custom: customLogger,
  },
  config: {
    clientId: '6c7eb89a-66e9-ab12-cd34-eeaf795650b2',
    serverConfig: {
      wellknown: 'https://auth.pingone.com/3072206d-c6ce-ch15-m0nd-f87e972c7cc3/as/.well-known/openid-configuration',
      timeout: 3000,
    },
    scope: '"openid", "email", "address", "profile", "phone"',
    responseType: 'code',
  },
});