Updating to Plugin API v4
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.
Changes
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.
Finally, switch your Bubble Plugin API Version to 4 in the “Dependencies” dropdown of the “Shared” tab
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
context.request: Updated plugin API
.length(): Current plugin API
.length(): Updated plugin API
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.
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:
In the new API, which is Promise-based already, you don’t need to jump through this hoop; you can await the result of the call directly.
Using utils.promisify instead of context.async
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:
Change log
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 a utility method on Bubble Things
getAll(): returns a promise for an object with all fields of the Thing
We are adding a new field on Bubble Things:
id: the Thing’s ID
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.
Last updated
Was this helpful?