generated from florian-lefebvre/astro-integration-template
-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
define-integration.ts
86 lines (80 loc) · 2.24 KB
/
define-integration.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import type { AstroIntegration, HookParameters } from "astro";
import { defu } from "defu";
import type { AnyOptions, AnyPlugin, ExtendedHooks } from "./types.js";
import { DEFAULT_HOOK_NAMES } from "../internal/constants.js";
/**
* A powerful wrapper around the standard Astro Integrations API. It allows to provide extra hooks, functionality
* and best-practices when creating Astro Integrations.
*
* @param {object} params
* @param {string} params.name - The name of your integration
* @param {object} params.object - Any default config options you want to set
* @param {function} params.setup - This will be called from your `astro:config:setup` call with the user options
*
* @see https://astro-integration-kit.netlify.app/utilities/define-integration/
*
* @example
* ```ts
* export default defineIntegration({
* name: "my-integration",
* setup({ options }) {
* console.log(options.foo); // "bar"
* }
* })
* ```
*/
export const defineIntegration = <
TOptions extends AnyOptions = never,
TPlugins extends Array<AnyPlugin> = [],
>({
name,
options: optionsDef,
setup,
plugins: _plugins,
}: {
name: string;
options?: TOptions;
plugins?: TPlugins;
setup: (params: {
name: string;
options: TOptions;
}) => ExtendedHooks<TPlugins>;
}): ((options?: TOptions["options"]) => AstroIntegration) => {
return (_options?: TOptions["options"]) => {
const options = defu(
_options ?? {},
optionsDef?.defaults ?? {},
) as TOptions["options"];
const resolvedPlugins = Object.values(
(() => {
const plugins: Record<string, AnyPlugin> = {};
for (const plugin of _plugins ?? []) {
plugins[plugin.name] = plugin;
}
return plugins;
})(),
);
const providedHooks = setup({ name, options });
const hooks: AstroIntegration["hooks"] = Object.fromEntries(
DEFAULT_HOOK_NAMES.map((hookName) => [
hookName,
(params: HookParameters<typeof hookName>) => {
const plugins = resolvedPlugins.filter((p) => p.hook === hookName);
return providedHooks[hookName]?.({
...params,
...Object.fromEntries(
plugins.map((plugin) => [
plugin.name,
plugin.implementation(params, { name }),
]),
),
} as any);
},
]),
);
return {
name,
hooks,
};
};
};