Introduction
The main purpose for creating and using Adapters is to provide a universal interface for extending DocLayer. The plug-and-play system that will allow you seamlessly integrate your own external platforms (of your choice) to handle dynamic events in your application. For example, to easily add features like newsletter subscriptions, analytics reporting, or any type of external data handling on the Client. And if needed, to also use dedicated server actions resolving critical or confidential backend operations.
To ensure a consistent approach, we provide a dedicated method for building type-safe adapters tailored to specific parts of the application. This helps create a clear and comprehensive workflow for handling various types of interactivity. Below, you'll find a guide on how to create a new adapters, integrate it into your application, and extend entry points such as the app.config.ts file, for easy and fast configuration.
Available adapters
Currently, the number of available adapters is limited to:
- newsletter: Resend, Substack
- analytics: Google Analytics, PostHog
We will be successfully adding new adapters going forward to extend the application's versatility. Although the list of built-in adapters is currently limited, you can help us by building and adding new ones to the codebase. Please don't hesitate to at least share your ideas for additional adapters, or if you are eager to build one, feel free to contribute to the project. Thank you in advance!
Building new Adapter
To build a new adapter, use the defineDoclayerAdapter macro. This function helps you connect to external data sources and ensures a universal response format, similar to Nuxt's asyncData. Place your adapter in the adapters/ folder to keep it consistent with the location of other adapters.
To demonstrate how it can be implemented, we'll use the Newsletter adapter as an example. Here’s how you can define it:
OK, let's split this example to more digestible pieces.
The defineDoclayerAdapter macro takes a callback as its argument. This callback receives two parameters: adapter and context. The adapter parameter is a function that triggers an asyncData call, following the same interface as Nuxt’s useAsyncData composable. The context parameter contains both the global configuration defined in your app.config.ts file and the user's input. In this example, the user input would typically be an email address provided through the DlNewsletter component.
For full type inference, you can provide two generics to defineDoclayerAdapter: the first for your Context, and the second for your API Response. This allows the TypeScript compiler to provide autocomplete suggestions within your adapter.
Now that we've created the adapter, we can initialize it. You can do this within your app.config.ts file, like this:
It's important to specify which adapter (as the resolved identifier may vary) you want to attach the global config to, it might be needed for your further actions.
Never store sensitive data in app.config.ts
Because the contents of app.config.ts are exposed to the client, you should never use the adapter's global config in this file to pass sensitive data such as credentials or API keys. For any server-side logic that requires sensitive information, create a dedicated server route and have your adapter interact with it instead. You can learn how to do this below.
The DlNewsletter component will automatically detect your adapter and perform the API call when the form is submitted. However, if you want to use the adapter in your own custom components, you can run the API call like this:
Both the global config and the user's input are available inside your adapter definition. You can use them to handle any logic you need within the adapter.
Pass useAsyncData composable
As the adapter partly operates outside of the component instance, you need to pass useAsyncData to it if calling from either script setup or a regular Vue context. If not, Nuxt will detect that the composable is called outside of the component's context and report the issue.
Handling sensitive data
Since DocLayer builds statically and does not handle server data by default, you need to engage server functions to perform this kind of actions. On how to use server functions with a specific deployment platform you can read here.
The above implementation works well for client-only scenarios. Secrets, such as API keys, have to stay on the server. app.config.ts is shipped to the browser, so it cannot hold credentials.
To securely handle sensitive operations, create a dedicated server endpoint and have your adapter call it instead of calling the external API directly from the client. This server route should access secrets from the environment and make the upstream API request on behalf of the client.
This is how it can be implemented:
Create adapter
First, create your adapter and point it at the server endpoint you are about to create. Build the upstream payload here, then post that string together with the user input.
A body callback in the global config is also app code. It runs inside the adapter. Serverless functions only receive JSON, so useAdapterData on the server returns serializable fields such as endpoint. Call config.body in the adapter, where that function is already in scope, and send the resulting string as payload.
Initialize adapter
Next, initialize your adapter in the app.config.ts file. endpoint is public configuration. body shapes the upstream request for the end user if needed any additional customization. Do not put secrets in either of them.
You can see an example of a request body on the Resend adapter page.
Create server endpoint
Then, define the server endpoint and place it inside the server/api folder. Read the serialized config with useAdapterData(event), attach the secret from the environment, and forward payload.
Check the payload before you forward it
The browser can post any string. Parse payload and confirm it matches the input you expect before calling the external API. You can do it also within the adapter itself if delivered remotely.
Keep the request body on the server (optional)
If the upstream body should not be editable from app.config.ts, skip body and build the JSON inside the server route from the input the adapter posts. That is the right place for fields that must not ship to the client.
That's it! Now you can use your adapter with the server function, securely handling your sensitive integration data.
Performance tip
Since ofetch is a peer dependency of DocLayer, you can use it as a faster and more versatile alternative to the native fetch API. Just add it to your package.dependencies object.
Adapter as an effect
Although adapters are primarily designed for asynchronous data fetching from third-party services, you can also use them to perform side effects during the Nuxt server-side rendering process. To do this, pass true as the third argument to the adapter function. In this mode, the macro bypasses useAsyncData and executes your handler immediately with access to the current SSR context.
The nuxt argument is Nuxt's SSR context for the request in progress. The macro reads it from the Vue instance that is currently setting up. That object exists only on the server, for the duration of that render. Use it for anything that has to happen as part of the HTML response.
Call the adapter from script setup of a component that participates in the server render. Where you call it decides how widely it applies. app.vue runs once for the whole application. A layout or a page runs only for the routes that use it.
Call it during setup
The SSR context is available only while a component instance is setting up. Keep the call synchronous inside script setup. You don't need to pass useAsyncData, since the handler does not returns it.
After hydration, Vue runs that setup again on the client. There is no SSR context in the browser, so nuxt is missing and the handler returns. What's written into the server HTML stays on the page.
Defining one-type multiple adapters
Some adapters - like analytics - can be defined as multiple ones, because, for example, you want to use two different services to measure your app at the same time. In that case you can run them from an array.
Currently only analytics adapters can be run from an array.
Request Adapter
On the following pages, you can explore a set of available adapters. The list isn't extensive yet, but we will continually add more. If you have an idea for a tool we could integrate with, let us know - create an issue. We will be happy to consider your suggestions! Thank you!
