diff --git a/package/src/index.ts b/package/src/index.ts index 54ec14f..7def9a0 100644 --- a/package/src/index.ts +++ b/package/src/index.ts @@ -1,10 +1,10 @@ -import { defineIntegration } from 'astro-integration-kit'; +import { AstroError } from 'astro/errors'; import { name as packageName } from '~/package.json'; -import { CreateExports } from '~/types.ts'; +import { CreateExports } from '~/types'; import { OptionsSchema } from '~/validators'; -import type { AstroAdapter } from 'astro'; +import type { AstroAdapter, AstroIntegration } from 'astro'; import type { Options } from '~/types.ts'; @@ -30,17 +30,24 @@ export function getAdapter(args: Options = {}): AstroAdapter { }; } -export default defineIntegration({ - name: packageName, - optionsSchema: OptionsSchema.optional(), - // biome-ignore lint/nursery/useExplicitType: Parent inferred type. - setup: (integration) => ({ +export default function integration(options?: Options): AstroIntegration { + // Note: Running the parser manually instead of using `defineIntegration` because + // of this issue comment: https://github.com/NuroDev/astro-bun/pull/11#discussion_r1896212455 + const parsedOptions = OptionsSchema.optional().safeParse(options); + if (!parsedOptions.success) + throw new AstroError( + `Invalid options passed to "${packageName}" integration\n`, + parsedOptions.error.issues.map((i) => i.message).join('\n'), + ); + + return { + name: packageName, hooks: { // biome-ignore lint/nursery/useExplicitType: Parent inferred type. 'astro:config:done': (params) => { params.setAdapter( getAdapter({ - ...integration.options, + ...parsedOptions.data, assets: params.config.build.assets, client: params.config.build.client?.toString(), host: params.config.server.host, @@ -50,5 +57,5 @@ export default defineIntegration({ ); }, }, - }), -}); + }; +} diff --git a/package/src/types.ts b/package/src/types.ts index c53fce0..50f0e19 100644 --- a/package/src/types.ts +++ b/package/src/types.ts @@ -17,4 +17,29 @@ export type CreateExports = { [CreateExports.STOP]: () => void; }; -export type Options = z.infer; +// export type Options = z.infer; + +export interface Options { + /** TODO(@nurodev): Undocumented */ + assets?: z.infer['assets']; + /** TODO(@nurodev): Undocumented */ + client?: z.infer['client']; + /** + * Enable clustering for the server. (Only linux!) + * + * @default false + */ + cluster?: z.infer['cluster']; + /** + * The hostname to serve the application on. + */ + host?: z.infer['host']; + /** + * The port to serve the application on. + * + * @default 4321 + */ + port?: z.infer['port']; + /** TODO(@nurodev): Undocumented */ + server?: z.infer['server']; +} diff --git a/package/src/validators.ts b/package/src/validators.ts index 915750e..e9f4513 100644 --- a/package/src/validators.ts +++ b/package/src/validators.ts @@ -2,27 +2,11 @@ import { z } from 'zod'; export const OptionsSchema = z .object({ - /** TODO(@nurodev): Undocumented */ assets: z.string(), - /** TODO(@nurodev): Undocumented */ client: z.string(), - /** - * Enable clustering for the server. (Only linux!) - * - * @default false - */ cluster: z.boolean().optional().default(false), - /** - * The hostname to serve the application on. - */ host: z.union([z.string(), z.boolean()]), - /** - * The port to serve the application on. - * - * @default 4321 - */ port: z.coerce.number().default(4321), - /** TODO(@nurodev): Undocumented */ server: z.string(), }) .partial();