From 8cbe3b051a1efe3b4bbb3920fc15e9d48338892e Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Fri, 30 Jul 2021 12:57:36 +0700 Subject: [PATCH] [chore] deduplicate config types (#2030) --- .changeset/olive-mayflies-retire.md | 5 ++ packages/kit/types/config.d.ts | 93 ++++++----------------------- packages/kit/types/helper.d.ts | 11 ++++ packages/kit/types/index.d.ts | 2 +- packages/kit/types/internal.d.ts | 2 +- 5 files changed, 37 insertions(+), 76 deletions(-) create mode 100644 .changeset/olive-mayflies-retire.md diff --git a/.changeset/olive-mayflies-retire.md b/.changeset/olive-mayflies-retire.md new file mode 100644 index 000000000000..6771ffb7f760 --- /dev/null +++ b/.changeset/olive-mayflies-retire.md @@ -0,0 +1,5 @@ +--- +"@sveltejs/kit": patch +--- + +Change `force` to `onError` in prerender config options diff --git a/packages/kit/types/config.d.ts b/packages/kit/types/config.d.ts index b4107317d2f9..5e4e081b7f30 100644 --- a/packages/kit/types/config.d.ts +++ b/packages/kit/types/config.d.ts @@ -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; @@ -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; + prerender: (options: { all?: boolean; dest: string; fallback?: string }) => Promise; } export interface Adapter { name: string; - adapt: ({ utils, config }: { utils: AdapterUtils; config: ValidatedConfig }) => Promise; + adapt: (context: { utils: AdapterUtils; config: ValidatedConfig }) => Promise; } export interface PageOpts { @@ -41,6 +34,17 @@ export interface PageOptsContext { export type ScriptablePageOpt = T | (({ request, page }: PageOptsContext) => Promise); +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[]; @@ -79,7 +83,7 @@ export interface Config { prerender?: { crawl?: boolean; enabled?: ScriptablePageOpt; - force?: boolean; + onError?: PrerenderOnErrorValue; pages?: string[]; }; router?: ScriptablePageOpt; @@ -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; - 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; - onError: PrerenderOnErrorValue; - pages: string[]; - }; - router: ScriptablePageOpt; - serviceWorker: { - exclude: string[]; - }; - ssr: ScriptablePageOpt; - target: string; - trailingSlash: TrailingSlash; - vite: () => ViteConfig; - }; - preprocess: any; -} +export type ValidatedConfig = RecursiveRequired & { + kit: { files: { setup: string } }; // only for validated +}; diff --git a/packages/kit/types/helper.d.ts b/packages/kit/types/helper.d.ts index 1134b843435c..ffc2d3dfd4cf 100644 --- a/packages/kit/types/helper.d.ts +++ b/packages/kit/types/helper.d.ts @@ -31,3 +31,14 @@ export type InferValue = T extends Record = T | Promise; export type Rec = Record; +export type RecursiveRequired = { + // 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 extends never + ? RecursiveRequired + : K extends 'vite' + ? Extract + : T[K]; +}; diff --git a/packages/kit/types/index.d.ts b/packages/kit/types/index.d.ts index 683031a2dc78..b68a3a1c9ac7 100644 --- a/packages/kit/types/index.d.ts +++ b/packages/kit/types/index.d.ts @@ -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 { diff --git a/packages/kit/types/internal.d.ts b/packages/kit/types/internal.d.ts index 8f532ee10d51..e0bf948df10a 100644 --- a/packages/kit/types/internal.d.ts +++ b/packages/kit/types/internal.d.ts @@ -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';