Autocompletion & Typings
This is the official documentation of the @forestadmin/agent Node.js agent.
Since the Forest Admin agent is fully written in TypeScript, every function has autocompletion capabilities. The agent can generate typing files based on your models.
This allows:
- Autocompletion of collection names and fields 
- Typing validation on all your handlers (when using TypeScript) 
Generate a typing file
The typing file generated by Forest Admin will allow you to autocomplete any given fields of a collection. This is an auto-generated file, so we highly suggest not editing it manually.
In the following sections, you can see that two options are given to the createAgent function:
- typingsPath: The location you want your typing file to be created.
- typingsMaxDepth: The maximum depth of introspection for relationships.
Using the typing file in a TypeScript context
In a TypeScript context, the usage of the typing file is pretty straightforward. You can simply import the typing file and template your createAgent with the schema to use it.
import { createAgent } from '@forestadmin/agent';
import { Schema } from './typings';
import transactions from './customization/transactions';
await createAgent<Schema>({
  // ...
  typingsPath: './typings.ts',
  typingsMaxDepth: 5,
})
  .customizeCollection('transactions', transactions)
  .mountOnStandaloneServer(3000)
  .start();import { CollectionCustomizer } from '@forestadmin/agent';
import { Schema } from '../typings';
export default (transactions: CollectionCustomizer<Schema, 'transactions'>) =>
  transactions.removeField('amountInEur');You should be able to see that the customizeCollection method is strongly typed, as well as the transactions handler.
Using the typing file in a JavaScript context
You can still benefit from it in a JavaScript environment by using the JSDoc syntax.
If you choose to split your customizeCollection handler in a dedicated file (That's the project scaffolding we usually use at Forest Admin), you can use the following JSDoc example to keep the autocomplete experience intact.
const { createAgent } = require('@forestadmin/agent');
const transactions = require('./customization/transactions');
/**
 * Importing the typing file to keep the autocompletion
 * @typedef {import('@forestadmin/agent').Agent} Agent
 * @typedef {import('../typings').Schema} Schema
 */
/**
 * @type {Agent<Schema>}
 */
const agent = createAgent({
  //...
  typingsPath: './typings.ts',
  typingsMaxDepth: 5,
});
await agent
  .customizeCollection('transactions', transactions)
  .mountOnStandaloneServer(3000)
  .start();/**
 * Importing the typing file to keep the autocompletion
 * @typedef {import('@forestadmin/agent').CollectionCustomizer} CollectionCustomizer
 * @typedef {import('../typings').Schema} Schema
 */
/**
 * @param {CollectionCustomizer<Schema, 'transactions'>} transactions
 */
module.exports = transactions => {
  // Autocompletion should be available here
  transactions.removeField('amountInEur');
};Last updated
Was this helpful?
