Response

This is the official documentation of Forest Admin Cloud.

After executing an action, the UI will display a notification message based on the HTTP status code received.


To make the code easier to read, all the code snippets below should be wrapped in the following code. Ensure you update the collection and action names as needed.

import type { Agent } from '@forestadmin/forest-cloud';
import { Schema } from '../typings';

export default function customizeAgent(agent: Agent<Schema>) {
  agent.customizeCollection('contacts', (collection) => {
    collection.addAction('Ban user', {
      scope: 'Single',
      execute: async (context, resultBuilder) => {
        // Insert the code snippet here.
      },
    });
  });
}

Simple response

The resultBuilder argument passed to the execution function enables you to customize the response of an action.

return resultBuilder.success('Company is now live!');

return resultBuilder.error('Company was already live!');

HTML response

Additionally, you can return an HTML response to present richer content.

return resultBuilder.success('Success', {
  html: `
    <h2 style="color: #dc3545; text-align: center;">Contact Banned Successfully!</h2>
    <p style="font-size: 16px; color: #333; text-align: center;">
      An automated email has been sent to the user detailing the reason for the ban.
      The user has a 30-day window to dispute the decision in order to prevent a
      permanent ban on the platform.
    </p>
  `,
});

File response

You can also provide a downloadable file as a response to a triggered action.

To use this feature, you have to include generateFile: true within the action's definition.

collection.addAction('Ban user', {
  scope: 'Single',
  generateFile: true,
  execute: async (context, resultBuilder) => {
  // ...

Inline content

return resultBuilder.file('The content of the file', 'filename.txt', 'text/plain');

Buffer content

const buffer = Buffer.from('The content of the file');
return resultBuilder.file(buffer, 'filename.txt', 'text/plain');

Stream content

const stream = fs.createReadStream('path/to/file');
return resultBuilder.file(stream, 'filename.txt', 'text/plain');

Making a redirection

You can redirect to another location after triggering an action.

return resultBuilder.redirectTo(
  '/MyProject/Production/Operations/data/contacts/index',
);

Triggering a Webhook

Webhooks enable triggering actions in third-party applications or starting background tasks for users, using HTTP/HTTPS callbacks. Triggers occur in the user's browser, thus are subject to CORS restrictions.

return resultBuilder.webhook(
  'https://mywebhookurl.someapp.com', // Webhook URL.
  'POST', // Webhook method (typically a POST).
  {}, // Webhook headers.
  {}, // Webhook body (only JSON is supported).
);

Modifying HTTP Headers

It is possible to change the HTTP response headers by using the setHeader() function.

return resultBuilder.setHeader('myHeaderName', 'myHeaderValue')

After calling setHeader(), you can call any response method previously described, such as:

return resultBuilder
    .setHeader('myHeaderName', 'myHeaderValue')
    .success('Contact is now banned!').

It's important to understand that the webhook() function and the setHeader() function serve different purposes. The former sends headers with the webhook call, while the latter modifies the headers of the HTTP response. These operations do not interfere with each other.

Last updated