Skip to content

Commit

Permalink
refactor: improve internal types (#163)
Browse files Browse the repository at this point in the history
* refactor: remove unreachable code

* refactor: improve internal types

I had a little bit of trouble understanding what options/configs were passed
where and why, so now I've more clearly separated the client config options and
the `studioBasePath` option. I've also adjusted the return types of the Vite
Plugin factory functions to make TypeScript happy.
  • Loading branch information
christianhg authored May 21, 2024
1 parent bc87070 commit 21e6e65
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 26 deletions.
32 changes: 13 additions & 19 deletions packages/sanity-astro/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,35 @@ type IntegrationOptions = ClientConfig & {
studioBasePath?: string;
};

const defaultOptions: IntegrationOptions = {
const defaultClientConfig: ClientConfig = {
apiVersion: "v2023-08-24",
};

export default function sanityIntegration(
options: IntegrationOptions,
): AstroIntegration {
const resolvedOptions = {
...defaultOptions,
...options,
};
export default function sanityIntegration({
studioBasePath,
...clientConfig
}: IntegrationOptions): AstroIntegration {
return {
name: "@sanity/astro",
hooks: {
"astro:config:setup": ({
injectScript,
injectRoute,
updateConfig,
config,
logger,
}) => {
"astro:config:setup": ({ injectScript, injectRoute, updateConfig }) => {
updateConfig({
vite: {
plugins: [
vitePluginSanityClient(resolvedOptions),
vitePluginSanityStudio(resolvedOptions, config),
vitePluginSanityClient({
...defaultClientConfig,
...clientConfig,
}),
vitePluginSanityStudio({ studioBasePath }),
],
},
});
// only load this route if `studioBasePath` is set
if (resolvedOptions.studioBasePath) {
if (studioBasePath) {
injectRoute({
entryPoint: "@sanity/astro/studio/studio-route.astro", // Astro <= 3
entrypoint: "@sanity/astro/studio/studio-route.astro", // Astro > 3
pattern: `/${resolvedOptions.studioBasePath}/[...params]`,
pattern: `/${studioBasePath}/[...params]`,
prerender: false,
});
}
Expand Down
7 changes: 4 additions & 3 deletions packages/sanity-astro/src/vite-plugin-sanity-client.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { ClientConfig } from "@sanity/client";
import type { Plugin } from "vite";
import type { DeepPartial } from "astro/dist/type-utils";
import type { PluginOption } from "vite";

const virtualModuleId = "sanity:client";
const resolvedVirtualModuleId = "\0" + virtualModuleId;

export function vitePluginSanityClient(config: ClientConfig): Plugin {
export function vitePluginSanityClient(config: ClientConfig) {
return {
name: "sanity:client",
resolveId(id: string) {
Expand All @@ -22,5 +23,5 @@ export function vitePluginSanityClient(config: ClientConfig): Plugin {
`;
}
},
};
} satisfies DeepPartial<PluginOption>;
}
10 changes: 6 additions & 4 deletions packages/sanity-astro/src/vite-plugin-sanity-studio.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type { Plugin } from "vite";
import type { DeepPartial } from "astro/dist/type-utils";
import type { PluginOption } from "vite";

export function vitePluginSanityStudio(resolvedOptions, { output }): Plugin {
export function vitePluginSanityStudio(resolvedOptions: {
studioBasePath?: string;
}) {
const virtualModuleId = "sanity:studio";
const resolvedVirtualModuleId = virtualModuleId;

Expand All @@ -19,7 +22,6 @@ export function vitePluginSanityStudio(resolvedOptions, { output }): Plugin {
throw new Error(
"[@sanity/astro]: Sanity Studio requires a `sanity.config.ts|js` file in your project root.",
);
return null;
}
if (!resolvedOptions.studioBasePath) {
throw new Error(
Expand All @@ -45,5 +47,5 @@ export function vitePluginSanityStudio(resolvedOptions, { output }): Plugin {
}
return null;
},
};
} satisfies DeepPartial<PluginOption>;
}

0 comments on commit 21e6e65

Please sign in to comment.