Skip to content

Commit

Permalink
Fixed options type doc inferance bug
Browse files Browse the repository at this point in the history
  • Loading branch information
NuroDev committed Dec 24, 2024
1 parent 46aa327 commit ac2b22b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 28 deletions.
29 changes: 18 additions & 11 deletions package/src/index.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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,
Expand All @@ -50,5 +57,5 @@ export default defineIntegration({
);
},
},
}),
});
};
}
27 changes: 26 additions & 1 deletion package/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,29 @@ export type CreateExports = {
[CreateExports.STOP]: () => void;
};

export type Options = z.infer<typeof OptionsSchema>;
// export type Options = z.infer<typeof OptionsSchema>;

export interface Options {
/** TODO(@nurodev): Undocumented */
assets?: z.infer<typeof OptionsSchema>['assets'];
/** TODO(@nurodev): Undocumented */
client?: z.infer<typeof OptionsSchema>['client'];
/**
* Enable clustering for the server. (Only linux!)
*
* @default false
*/
cluster?: z.infer<typeof OptionsSchema>['cluster'];
/**
* The hostname to serve the application on.
*/
host?: z.infer<typeof OptionsSchema>['host'];
/**
* The port to serve the application on.
*
* @default 4321
*/
port?: z.infer<typeof OptionsSchema>['port'];
/** TODO(@nurodev): Undocumented */
server?: z.infer<typeof OptionsSchema>['server'];
}
16 changes: 0 additions & 16 deletions package/src/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

0 comments on commit ac2b22b

Please sign in to comment.