Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore] deduplicate config types #2030

Merged
merged 11 commits into from
Jul 30, 2021
5 changes: 5 additions & 0 deletions .changeset/olive-mayflies-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sveltejs/kit": patch
---

Change `force` to `onError` in prerender config options
93 changes: 19 additions & 74 deletions packages/kit/types/config.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { UserConfig as ViteConfig } from 'vite';
import { RecursiveRequired } from './helper';
import { ServerRequest } from './hooks';
import { Logger, TrailingSlash } from './internal';
import { UserConfig as ViteConfig } from 'vite';

export interface AdapterUtils {
log: Logger;
Expand All @@ -11,20 +12,12 @@ export interface AdapterUtils {
copy_static_files: (dest: string) => void;
copy: (from: string, to: string, filter?: (basename: string) => boolean) => void;
update_ignores: ({ patterns, log }: { patterns: string[]; log?: boolean }) => void;
prerender: ({
all,
dest,
fallback
}: {
all?: boolean;
dest: string;
fallback?: string;
}) => Promise<void>;
prerender: (options: { all?: boolean; dest: string; fallback?: string }) => Promise<void>;
}

export interface Adapter {
name: string;
adapt: ({ utils, config }: { utils: AdapterUtils; config: ValidatedConfig }) => Promise<void>;
adapt: (context: { utils: AdapterUtils; config: ValidatedConfig }) => Promise<void>;
}

export interface PageOpts {
Expand All @@ -41,6 +34,17 @@ export interface PageOptsContext {

export type ScriptablePageOpt<T> = T | (({ request, page }: PageOptsContext) => Promise<T>);

export interface PrerenderErrorHandler {
(details: {
status: number;
path: string;
referrer: string | null;
referenceType: 'linked' | 'fetched';
}): void;
}

export type PrerenderOnErrorValue = 'fail' | 'continue' | PrerenderErrorHandler;

export interface Config {
compilerOptions?: any;
extensions?: string[];
Expand Down Expand Up @@ -79,7 +83,7 @@ export interface Config {
prerender?: {
crawl?: boolean;
enabled?: ScriptablePageOpt<boolean>;
force?: boolean;
onError?: PrerenderOnErrorValue;
pages?: string[];
};
router?: ScriptablePageOpt<boolean>;
Expand All @@ -94,65 +98,6 @@ export interface Config {
preprocess?: any;
}

export type PrerenderErrorHandler = (errorDetails: {
status: number;
path: string;
referrer: string | null;
referenceType: 'linked' | 'fetched';
}) => void | never;

export type PrerenderOnErrorValue = 'fail' | 'continue' | PrerenderErrorHandler;

export interface ValidatedConfig {
compilerOptions: any;
extensions: string[];
kit: {
adapter: Adapter;
amp: boolean;
appDir: string;
files: {
assets: string;
hooks: string;
lib: string;
routes: string;
serviceWorker: string;
setup: string;
template: string;
};
floc: boolean;
host: string;
hostHeader: string;
hydrate: ScriptablePageOpt<boolean>;
package: {
dir: string;
emitTypes: boolean;
exports: {
include: string[];
exclude: string[];
};
files: {
include: string[];
exclude: string[];
};
};
paths: {
assets: string;
base: string;
};
prerender: {
crawl: boolean;
enabled: ScriptablePageOpt<boolean>;
onError: PrerenderOnErrorValue;
pages: string[];
};
router: ScriptablePageOpt<boolean>;
serviceWorker: {
exclude: string[];
};
ssr: ScriptablePageOpt<boolean>;
target: string;
trailingSlash: TrailingSlash;
vite: () => ViteConfig;
};
preprocess: any;
}
export type ValidatedConfig = RecursiveRequired<Config> & {
kit: { files: { setup: string } }; // only for validated
};
11 changes: 11 additions & 0 deletions packages/kit/types/helper.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,14 @@ export type InferValue<T, Key extends keyof T, Default> = T extends Record<Key,
: Default;
export type MaybePromise<T> = T | Promise<T>;
export type Rec<T = any> = Record<string, T>;
export type RecursiveRequired<T> = {
benmccann marked this conversation as resolved.
Show resolved Hide resolved
// Recursive implementation of TypeScript's Required utility type.
// will continue until it reaches a primitive or union
// with a Function in it, except for the 'vite' key
// which we want the end result to be just a function
[K in keyof T]-?: Extract<T[K], Function> extends never
? RecursiveRequired<T[K]>
: K extends 'vite'
? Extract<T[K], Function>
: T[K];
};
2 changes: 1 addition & 1 deletion packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import './ambient-modules';

export { Adapter, AdapterUtils, Config, ValidatedConfig, PrerenderErrorHandler } from './config';
export { Adapter, AdapterUtils, Config, PrerenderErrorHandler, ValidatedConfig } from './config';
export { EndpointOutput, RequestHandler } from './endpoint';
export { ErrorLoad, ErrorLoadInput, Load, LoadInput, LoadOutput, Page } from './page';
export {
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/types/internal.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PageOpts, ScriptablePageOpt, ValidatedConfig } from './config';
import { PageOpts, ScriptablePageOpt } from './config';
import { RequestHandler } from './endpoint';
import { Headers, Location, ParameterizedBody } from './helper';
import { GetSession, Handle, ServerResponse, ServerFetch, StrictBody } from './hooks';
Expand Down