mirror of
https://github.com/withastro/astro.git
synced 2025-02-03 22:29:08 -05:00
feat: import defineIntegration from AIK
This commit is contained in:
parent
6280c3e290
commit
615e0b717b
2 changed files with 69 additions and 0 deletions
|
@ -2,6 +2,7 @@
|
|||
// Additionally, this code, much like `@types/astro.ts`, is used to generate documentation, so make sure to pass
|
||||
// your changes by our wonderful docs team before merging!
|
||||
|
||||
import type { satisfies } from 'semver';
|
||||
import type { ZodError } from 'zod';
|
||||
|
||||
export interface ErrorData {
|
||||
|
@ -1156,6 +1157,17 @@ export const i18nNotEnabled = {
|
|||
hint: 'See https://docs.astro.build/en/guides/internationalization for a guide on setting up i18n.',
|
||||
} satisfies ErrorData;
|
||||
|
||||
/**
|
||||
* @docs
|
||||
* @description
|
||||
*/
|
||||
export const AstroIntegrationInvalidOptions = {
|
||||
name: 'AstroIntegrationInvalidOptions',
|
||||
title: 'Astro Integration Invalid Options',
|
||||
message: (name: string, error: string) =>
|
||||
`Invalid options passed to "${name}" integration\n${error}`,
|
||||
} satisfies ErrorData;
|
||||
|
||||
/**
|
||||
* @docs
|
||||
* @kind heading
|
||||
|
|
57
packages/astro/src/integrations/define-integration.ts
Normal file
57
packages/astro/src/integrations/define-integration.ts
Normal file
|
@ -0,0 +1,57 @@
|
|||
import type { AstroIntegration } from "../@types/astro.js";
|
||||
import { AstroError, AstroErrorData, errorMap } from "../core/errors/index.js";
|
||||
import { z } from "../../zod.mjs";
|
||||
|
||||
type AstroIntegrationSetupFn<Options extends z.ZodTypeAny> = (params: {
|
||||
name: string;
|
||||
options: z.output<Options>;
|
||||
}) => Omit<AstroIntegration, "name">;
|
||||
|
||||
/** TODO: */
|
||||
export const defineIntegration = <
|
||||
TOptionsSchema extends z.ZodTypeAny = z.ZodNever,
|
||||
TSetup extends
|
||||
AstroIntegrationSetupFn<TOptionsSchema> = AstroIntegrationSetupFn<TOptionsSchema>,
|
||||
>({
|
||||
name,
|
||||
optionsSchema,
|
||||
setup,
|
||||
}: {
|
||||
name: string;
|
||||
optionsSchema?: TOptionsSchema;
|
||||
setup: TSetup;
|
||||
}): ((
|
||||
...args: [z.input<TOptionsSchema>] extends [never]
|
||||
? []
|
||||
: undefined extends z.input<TOptionsSchema>
|
||||
? [options?: z.input<TOptionsSchema>]
|
||||
: [options: z.input<TOptionsSchema>]
|
||||
) => AstroIntegration & ReturnType<TSetup>) => {
|
||||
return (...args): AstroIntegration & ReturnType<TSetup> => {
|
||||
const parsedOptions = (optionsSchema ?? z.never().optional()).safeParse(
|
||||
args[0],
|
||||
{
|
||||
errorMap,
|
||||
},
|
||||
);
|
||||
|
||||
if (!parsedOptions.success) {
|
||||
throw new AstroError({
|
||||
...AstroErrorData.AstroIntegrationInvalidOptions,
|
||||
message: AstroErrorData.AstroIntegrationInvalidOptions.message(
|
||||
name,
|
||||
parsedOptions.error.issues.map((i) => i.message).join('\n')
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
const options = parsedOptions.data as z.output<TOptionsSchema>;
|
||||
|
||||
const integration = setup({ name, options }) as ReturnType<TSetup>;
|
||||
|
||||
return {
|
||||
name,
|
||||
...integration,
|
||||
};
|
||||
};
|
||||
};
|
Loading…
Add table
Reference in a new issue