Skip to content

Commit

Permalink
Tie generic types to Zod Schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Fryuni committed Feb 13, 2024
1 parent abf8a1f commit 39ecdb1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
15 changes: 8 additions & 7 deletions package/src/core/define-integration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { AstroIntegration, HookParameters } from "astro";
import type { AnyOptions, AnyPlugin, ExtendedHooks } from "./types.js";
import type { z } from "astro/zod";
import { DEFAULT_HOOK_NAMES } from "../internal/constants.js";
import type { AnyOptions, AnyPlugin, ExtendedHooks } from "./types.js";

/**
* A powerful wrapper around the standard Astro Integrations API. It allows to provide extra hooks, functionality
Expand Down Expand Up @@ -37,13 +38,13 @@ export const defineIntegration = <
plugins?: TPlugins;
setup: (params: {
name: string;
options: TOptions["options"];
options: z.output<TOptions["schema"]>;
}) => ExtendedHooks<TPlugins>;
}): ((options?: TOptions["options"]) => AstroIntegration) => {
return (_options?: TOptions["options"]) => {
const options = optionsDef?.schema.parse(
_options ?? {},
) as TOptions["options"];
}): ((options?: z.input<TOptions["schema"]>) => AstroIntegration) => {
return (_options?: z.input<TOptions["schema"]>) => {
const options = optionsDef?.schema.parse(_options ?? {}) as z.output<
TOptions["schema"]
>;

const resolvedPlugins = Object.values(
(() => {
Expand Down
5 changes: 2 additions & 3 deletions package/src/core/define-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ import type { Options } from "../core/types.js";
* })
* ```
*/
export const defineOptions = <TOptions extends Record<string, unknown>>({
export const defineOptions = <Schema extends import("astro/zod").AnyZodObject>({
schema,
}: { schema: import("astro/zod").AnyZodObject }): Options<TOptions> => ({
options: {} as TOptions,
}: { schema: Schema }): Options<Schema> => ({
schema,
});
5 changes: 2 additions & 3 deletions package/src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,8 @@ export interface ExtendedHooks<TPlugins extends Array<AnyPlugin>> {
>;
}

export type Options<TOptions extends Record<string, unknown>> = {
options: TOptions;
schema: import('astro/zod').AnyZodObject
export type Options<Schema extends import("astro/zod").AnyZodObject> = {
schema: Schema;
};

export type AnyOptions = Options<any>;

0 comments on commit 39ecdb1

Please sign in to comment.