This documentation covers how to update your server-side actions to be compatible with plugin API v4, which runs on Node 18.
Node 18 removes support for the Fibers extension, which allowed the existing version of the server-side actions API to expose functions that returned results directly, rather than as Promises. Consequently, some API functions will change in a non-backwards-compatible way.
If your plugin uses any server-side actions from the versions 3 and below, you will have to manually update your plugin to be compatible with the updated version.
This document explains in detail what the plugin API looks like and how to update plugins on the plugin API versions 3 and below to work with the updated plugin API. In most cases, the necessary changes will be straightforward.
What's the difference between the Fibers extension and Promises?
Fibers and Promises have two different approaches to handling asynchronous operations.
The Fibers extension (old) lets you write asynchronous code in a more synchronous-looking way. By using Fibers, you can pause and resume the execution of your code at certain points, effectively making it appear as if the asynchronous tasks are running synchronously. This can make the code easier to read and understand, as it resembles typical synchronous code flow.
Promises (new), on the other hand, are a native JavaScript feature for managing asynchronous operations. They represent the eventual completion (or failure) of an asynchronous operation and its resulting value. Promises provide a more standardized approach to handling asynchronous tasks, allowing you to chain operations, handle errors, and improve the overall structure of your code.
Changes
Function Name
Old API
New API
context.request
context.request
context.v3.request
context.async
context.async
context.v3.async
.get method on Bubble Things
.get method
.get method (returns promise)
.get method on Bubble Lists
.get method
.get method (returns promise)
.length method on Bubble Lists
.length method
.length method (returns promise)
get_object_from_id
get_object_from_id
context.getThingById
get_objects_from_ids
get_objects_from_ids
context.getThingsById
These functions now return a promise for their original return value (see JavaScript documentation on promises). If you have a server-side action that uses any of these functions in your plugin, the action will break when you update. See below for a guide on how to fix this.
Step-by-step guide
Prepend async to any top-level function that calls context.request, and await just before context.request. Change context.request to context.v3.request.
Also, consider switching over to using node-fetch, which has a more modern, standardized API.
Prepend async to any top-level function that calls context.async, and await just before context.async. Change context.async to context.v3.async.
Also, consider switching over to using util.promisify, which is a more modern syntax.
Prepend async to any top-level function that calls the .get method on Bubble Things and await just before .get.
Prepend async to any top-level function that calls the .get method on Bubble Lists and await just before .get.
Prepend async to any top-level function that calls the .length method on Bubble Lists and await just before .length.
Examples
Here are a few examples of how to update the run_server function of your plugin’s server-side actions.
context.request: Versions 3 and below plugin API
function(properties, context) {// Send a request to the cat API synchronouslyconstresponse=context.request({ url:'https://api.thecatapi.com/v1/images/search', json:true })constcat_image=response.body[0].urlreturn { cat_image }}
context.request: Updated plugin API
asyncfunction(properties, context) {// Send a request to the cat API asynchronously, with promisesconstresponse=awaitcontext.v3.request({ url:'https://api.thecatapi.com/v1/images/search', json:true })constcat_image=response.body[0].urlreturn { cat_image }}.length(): Current plugin APIfunction(properties, context) {let list_length =properties.my_list.length()return { list_length }}.length(): Updated plugin APIasyncfunction(properties, context) {let list_length =awaitproperties.my_list.length()return { list_length }}
Using node-fetch instead of context.request
Here’s an example of how to update your code using context.request to use node-fetch, based on the same starting point as the earlier context.request example.
asyncfunction(properties, context) {// Use the node-fetch module to make an HTTP requestconstresponse=awaitfetch('https://api.thecatapi.com/v1/images/search')constbody=awaitresponse.json()constcat_image= body[0].urlreturn { cat_image }}
Using promises instead of context.async
In the old version of the API, plugin authors frequently had to use context.async to interoperate with modern, Promise-using JS libraries. For instance, to use the weathered npm package to fetch a list of real-time weather warnings, you might write code like this:
But if the code you’re trying to wait for with context.async is callback-based rather than promise-based, you'll still need some sort of wrapper.
Here’s a (somewhat contrived) example of using the callback-based fs.stat API from Node to inspect the filesystem of the lambda your action is running on:
Instead of using context.async, you could also wrap fs.stat with node’s built in promisify utility to make it return a promise, then await that promise:
Here are the changes that we will make to the plugin API:
context.request is being deprecated (this means that we plan to eventually stop supporting it), moved to context.v3.request, and will now return a promise. We recommend that plugin authors use the globally-available node-fetch instead.
context.async is also being deprecated, moved to context.v3.async, and will now return a promise. We recommend that plugin authors use promisify from Node’s built-in util module instead.
The .get method available on Bubble Things now returns a promise.
The .get method available on Bubble Lists now returns a promise.
The .length method available on Bubble Lists now returns a promise.
We are adding two new utility methods on Bubble Things:
id(): returns the Thing’s ID (synchronously – not as a promise).
getAll(): returns a promise for an object with all fields of the Thing.
Lists now also implement the AsyncIterable interface, so you can use for (await ... of ...) loop syntax to loop over the list.
the single_api and list_api fields are still present, but no longer encouraged (we will also remove official documentation for these fields). In their place are two new context methods: isBubbleThing and isBubbleList.
We are adding official support for two previously usable, but undocumented functions: getThingById and getThingsById. We added these functions to the context object to be more consistent with the rest of API. These functions also now return promises.
See below for more detail.
exporttypePrimitive=string|number|boolean|Date|null|undefined/** Any value that can be passed as a property to an action or stored in the DB. */exporttypeBubbleValue=Primitive|Primitive[] |BubbleThing|BubbleList/** Fields present on any Bubble data type. */exportinterfaceThingFields {// These fields are guaranteed to be present on any database Thing.'Slug'?:string'Created Date':Date'Modified Date':Date// Other fields might also be present, depending on the data type. [_:string]:BubbleValue}/** Additional fields present on the User data type. */exportinterfaceUserFieldsextendsThingFields {'email':string'logged_in':boolean}/** * An object representing a single Thing from the Bubble app's database. */exportinterfaceBubbleThing {/** Returns the names of the fields on the Thing. */listProperties():string[]// CHANGED - now returns a promise/** Returns the value stored in a particular field of this Thing. */get(propertyName:string):Promise<BubbleValue>// NEW/** Returns an object with all the thing's fields and their values. */getAll():Promise<Record<string,BubbleValue>>// NEW/** Returns the unique ID of this Thing. */id():string// for historical interest - maybe de-document these single_api:true list_api:false}/** * An object representing a list of Things from the Bubble app's database. * * As lists may be quite large, the data contained in the list isn't all * loaded up front. Therefore, methods for accessing data within the list * are generally asynchronous. */exportinterfaceBubbleList {// CHANGED - now returns a promise/** The number of items in this BubbleList. */length():Promise<number>// CHANGED - now returns a promise/** Fetch a portion of a BubbleList as an array. */get(start:number, length:number):Promise<BubbleThing[]>// NEW/** Allows you to use a BubbleList with the `for (await x of list)` syntax */ [Symbol.asyncIterator]():AsyncIterator<BubbleThing>// for historical interest - maybe de-document these single_api:false list_api:true}/** An object containing utility functions available to server-side actions. */exportinterfaceContext {/** The current user who initiated the workflow that's running this action. */ currentUser:BubbleThing/** The timezone the workflow is running in. */ userTimezone:string/** An object containing any keys set for this plugin in the app. */ keys:Record<string,string>// NEW/** Check if a JS value is a BubbleThing object. */isBubbleThing(x:unknown):boolean// NEW/** Check if a JS value is a BubbleList object. */isBubbleList(x:unknown):boolean// NEW - was secret global before/** Fetch a BubbleThing object for a given unique ID. */getThingById(id:string):Promise<BubbleThing|null>// NEW - was secret global before/** * Fetch the corresponding BubbleThing objects for each ID in an array. * Results will be returned in the same order as requested, with `null` * in place of any IDs with no corresponding Thing. */getThingsById(ids:string[]):Promise<Array<BubbleThing|null>>// NEW/CHANGED - these used to be top-level properties of the context v3:ContextDeprecatedV3}/** Context functions that are no longer recommended, but are provided for backwards compatibility. */exportinterfaceContextDeprecatedV3 {// CHANGED - now returns a promise/** * See documentation for the `request` library. * * @deprecated We recommend you use node-fetch instead. */request(...args:unknown[]):Promise<unknown>// CHANGED - now returns a promise/** * Takes a node-style asynchronous function which expects to be passed a callback, * and turns it into a promise. * * @deprecated We recommend you use node's built-in `util.promisify`. */'async': <T>(fn: (callback: (err:unknown, res?:T) =>void) =>void) =>Promise<T>}declare global {/** Exposes node-fetch to plugins. */functionfetch(...args:unknown[]):Promise<unknown>}