JavaScript

JavaScript Implementation

The JavaScript implementation type allows you to write custom JavaScript or TypeScript code to implement method logic.

Use the following file naming structure for the JavaScript implementation:

<method-name>.js

Here's an example of a JavaScript implementation for a list method for a data collection:

// list.js
export default async function list({ apiClient, parameters }) {
  // Make API request
  const response = await apiClient.get('/items', {
    page: parameters.page,
    limit: parameters.limit
  })

  // Transform response
  return {
    items: response.items.map(item => ({
      id: item.id,
      name: item.name,
      // Transform other fields...
    })),
    cursor: response.nextPage
  }
}

Input Parameters

JavaScript implementations always receive the following input parameters, in addition to method-specific ones:

  • apiClient – API client configured with authentication that can be used to make requests to the external API.
  • credentials – authentication credentials for the current connection.

Methods used before a connection is established (such as methods used to authenticate) will not have credentials and apiClient parameters available.

Logging

You can log messages to the console using the console.log function:

console.log('Fetching items...')

To log a JSON object, you need to stringify it:

console.log(JSON.stringify(data))