defineIntegration
defineIntegration
is a powerful wrapper around the standard Astro Integrations API. It allows integration authors to handle user options and global logic easily.
Defining an integration
To define an integration, use the defineIntegration
utility:
It accepts a few arguments, whose usage is explained in the sections below.
Handling options and defaults
defineIntegration
accepts an optionsSchema
argument that is a zod
schema.
This way, you can:
- Add proper documentation using JSDoc annotations
- Provide defaults
- Well, take full advantage of
zod
’s power!
Adding the logic with setup
If you’ve written an integration before by returning an AstroIntegration
from a function, it’s exactly
the same with setup
! This is where all the logic lives:
It accepts an object with data from the integration definition:
In setup, you’ll want to add any logic that is shared between any hook, for example:
- Calling
createResolver
- Save the
config
fromastro:config:done
to be used in later hooks
It needs to return Astro hooks.
Defining extra integration fields
Any extra property present on the return of setup
will be present on the integration object returned by initializing the integration with the options.
You can use this to define fields you might want to access from outside your integration while having access to it’s internal state.