Skip to content

defineIntegration

defineIntegration is a powerful wrapper around the standard Astro Integrations API. It allows integration authors to handle user options and global logic easily.

my-integration/index.ts
1
import { defineIntegration, addDts } from "astro-integration-kit"
2
import { z } from "astro/zod"
3
4
export default defineIntegration({
5
name: "my-integration",
6
optionsSchema: z.object({
7
virtualModuleId: z.string()
8
}),
9
setup({ options, name }) {
10
return {
11
"astro:config:setup": (params) => {
12
addDts(params, {
13
name,
14
content: `declare module ${JSON.stringify(options.virtualModuleId)} {}`
15
})
16
}
17
}
18
}
19
})

Defining an integration

To define an integration, use the defineIntegration utility:

my-integration/index.ts
1
import { defineIntegration } from "astro-integration-kit";
2
3
export default defineIntegration({
4
// ...
5
name: "my-integration"
6
})

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.

my-integration/index.ts
1
import { defineIntegration } from "astro-integration-kit";
2
import { z } from "astro/zod";
3
4
export default defineIntegration({
5
// ...
6
optionsSchema: z.object({
7
/**
8
* A comment
9
*
10
* @default `"bar"`
11
*/
12
foo: z.string().optional().default("bar"),
13
}),
14
})

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:

my-integration/index.ts
1
import { defineIntegration } from "astro-integration-kit";
2
3
export default defineIntegration({
4
// ...
5
setup() {
6
return {}
7
}
8
})

It accepts an object with data from the integration definition:

my-integration/index.ts
1
import { defineIntegration } from "astro-integration-kit";
2
3
export default defineIntegration({
4
// ...
5
setup({ options, name }) {
6
return {}
7
}
8
})

In setup, you’ll want to add any logic that is shared between any hook, for example:

  • Calling createResolver
  • Save the config from astro:config:done to be used in later hooks

It needs to return Astro hooks.

Using non-built-in Astro hooks

Some official Astro integrations, like @astrojs/db, define new integration hooks that are not built into Astro core. Astro Integration Kit does not integrate with those hooks by default as they are optional.

If you want to use those hooks on your integration or plugin, you can import that extra behavior.

Astro DB (@astrojs/db)

my-integration/index.ts
1
import { defineIntegration } from "astro-integration-kit";
2
import "astro-integration-kit/extras/db";
3
4
export default defineIntegration({
5
// ...
6
setup() {
7
return {
8
"astro:db:setup": ({ extendDb }) => {
9
// ...
10
},
11
};
12
},
13
});