Skip to content

Commit

Permalink
chore: clean up emulator types (#11737)
Browse files Browse the repository at this point in the history
* chore: clean up emulator types

- the Emulator type is not a class export
- make validated config types more correct by leaving adapter types as optional

* fix

* ok that doesnt work for some reason

---------

Co-authored-by: Rich Harris <rich.harris@vercel.com>
  • Loading branch information
dummdidumm and Rich-Harris committed Jan 25, 2024
1 parent 0dfb0cc commit 440a546
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 11 deletions.
3 changes: 2 additions & 1 deletion packages/kit/src/core/adapt/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export async function adapt(
log,
vite_config
) {
const { name, adapt } = config.kit.adapter;
// This is only called when adapter is truthy, so the cast is safe
const { name, adapt } = /** @type {import('@sveltejs/kit').Adapter} */ (config.kit.adapter);

console.log(colors.bold().cyan(`\n> Using ${name}`));

Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/exports/public.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ export interface Cookies {
/**
* A collection of functions that influence the environment during dev, build and prerendering
*/
export class Emulator {
export interface Emulator {
/**
* A function that is called with the current route `config` and `prerender` option
* and returns an `App.Platform` object
Expand Down
2 changes: 0 additions & 2 deletions packages/kit/src/exports/vite/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,6 @@ export async function dev(vite, vite_config, svelte_config) {
});

const env = loadEnv(vite_config.mode, svelte_config.kit.env.dir, '');

// TODO because of `RecursiveRequired`, TypeScript thinks this is guaranteed to exist, but it isn't
const emulator = await svelte_config.kit.adapter?.emulate?.();

return () => {
Expand Down
1 change: 0 additions & 1 deletion packages/kit/src/exports/vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ export async function sveltekit() {
preprocess,
onwarn: svelte_config.onwarn,
compilerOptions: {
// @ts-expect-error SvelteKit requires hydratable true by default
hydratable: isSvelte5Plus() ? undefined : true,
...svelte_config.compilerOptions
},
Expand Down
1 change: 0 additions & 1 deletion packages/kit/src/exports/vite/preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ export async function preview(vite, vite_config, svelte_config) {
read: (file) => createReadableStream(`${dir}/${file}`)
});

// TODO because of `RecursiveRequired`, TypeScript thinks this is guaranteed to exist, but it isn't
const emulator = await svelte_config.kit.adapter?.emulate?.();

return () => {
Expand Down
12 changes: 9 additions & 3 deletions packages/kit/src/types/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
Reroute,
RequestEvent,
SSRManifest,
Emulator
Emulator,
Adapter
} from '@sveltejs/kit';
import {
HttpMethod,
Expand Down Expand Up @@ -434,9 +435,14 @@ export interface Uses {
search_params: Set<string>;
}

export type ValidatedConfig = RecursiveRequired<Config>;
export type ValidatedConfig = Config & {
kit: ValidatedKitConfig;
extensions: string[];
};

export type ValidatedKitConfig = RecursiveRequired<KitConfig>;
export type ValidatedKitConfig = Omit<RecursiveRequired<KitConfig>, 'adapter'> & {
adapter?: Adapter;
};

export * from '../exports/index.js';
export * from './private.js';
11 changes: 9 additions & 2 deletions packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ declare module '@sveltejs/kit' {
/**
* A collection of functions that influence the environment during dev, build and prerendering
*/
export class Emulator {
export interface Emulator {
/**
* A function that is called with the current route `config` and `prerender` option
* and returns an `App.Platform` object
Expand Down Expand Up @@ -1748,7 +1748,14 @@ declare module '@sveltejs/kit' {
endpoint_id?: string;
}

type ValidatedConfig = RecursiveRequired<Config>;
type ValidatedConfig = Config & {
kit: ValidatedKitConfig;
extensions: string[];
};

type ValidatedKitConfig = Omit<RecursiveRequired<KitConfig>, 'adapter'> & {
adapter?: Adapter;
};
/**
* Throws an error with a HTTP status code and an optional message.
* When called during request handling, this will cause SvelteKit to
Expand Down

0 comments on commit 440a546

Please sign in to comment.