From 84f77fea107c2dc4637e045832d61396562c0ac8 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Sat, 19 Feb 2022 20:37:40 -0800 Subject: [PATCH] Ensure public types don't reference internal types --- packages/kit/types/index.d.ts | 106 +++++++++++++++++++++--------- packages/kit/types/internal.d.ts | 108 ++++++++----------------------- 2 files changed, 104 insertions(+), 110 deletions(-) diff --git a/packages/kit/types/index.d.ts b/packages/kit/types/index.d.ts index 5f1b7ac72028..b8d873250358 100644 --- a/packages/kit/types/index.d.ts +++ b/packages/kit/types/index.d.ts @@ -2,22 +2,6 @@ /// import { CompileOptions } from 'svelte/types/compiler/interfaces'; -import { - Logger, - PrerenderOnErrorValue, - SSRNodeLoader, - SSRRoute, - TrailingSlash, - Either, - MaybePromise, - RecursiveRequired, - RouteDefinition, - AdapterEntry, - ResponseHeaders, - Fallthrough, - RequiredResolveOptions, - Body -} from './internal'; import './ambient'; export class App { @@ -34,6 +18,32 @@ export interface Adapter { adapt(builder: Builder): Promise; } +export interface AdapterEntry { + /** + * A string that uniquely identifies an HTTP service (e.g. serverless function) and is used for deduplication. + * For example, `/foo/a-[b]` and `/foo/[c]` are different routes, but would both + * be represented in a Netlify _redirects file as `/foo/:param`, so they share an ID + */ + id: string; + + /** + * A function that compares the candidate route with the current route to determine + * if it should be treated as a fallback for the current route. For example, `/foo/[c]` + * is a fallback for `/foo/a-[b]`, and `/[...catchall]` is a fallback for all routes + */ + filter: (route: RouteDefinition) => boolean; + + /** + * A function that is invoked once the entry has been created. This is where you + * should write the function to the filesystem and generate redirect manifests. + */ + complete: (entry: { + generateManifest: (opts: { relativePath: string; format?: 'esm' | 'cjs' }) => string; + }) => void; +} + +export type Body = JSONValue | Uint8Array | ReadableStream | import('stream').Readable; + export interface Builder { log: Logger; rimraf(dir: string): void; @@ -269,6 +279,8 @@ export type CspDirectives = { >; }; +export type Either = Only | Only; + export interface EndpointOutput { status?: number; headers?: Headers | Partial; @@ -288,6 +300,10 @@ export interface ExternalFetch { (req: Request): Promise; } +export interface Fallthrough { + fallthrough: true; +} + export interface GetSession { (event: RequestEvent): MaybePromise; } @@ -303,6 +319,12 @@ export interface HandleError { (input: { error: Error & { frame?: string }; event: RequestEvent }): void; } +export type HttpMethod = 'get' | 'head' | 'post' | 'put' | 'delete' | 'patch'; + +export type JSONObject = { [key: string]: JSONValue }; + +export type JSONValue = string | number | boolean | null | ToJSON | JSONValue[] | JSONObject; + export interface Load, Props = Record> { (input: LoadInput): MaybePromise>>; } @@ -325,6 +347,19 @@ export interface LoadOutput> { maxage?: number; } +export interface Logger { + (msg: string): void; + success(msg: string): void; + error(msg: string): void; + warn(msg: string): void; + minor(msg: string): void; + info(msg: string): void; +} + +export type MaybePromise = T | Promise; + +type Only = { [P in keyof T]: T[P] } & { [P in Exclude]?: never }; + export interface Prerendered { pages: Map< string, @@ -360,6 +395,11 @@ export interface PrerenderErrorHandler { }): void; } +export type PrerenderOnErrorValue = 'fail' | 'continue' | PrerenderErrorHandler; + +/** `string[]` is only for set-cookie, everything else must be type of `string` */ +export type ResponseHeaders = Record; + export interface RequestEvent { request: Request; url: URL; @@ -378,23 +418,31 @@ export interface RequestOptions { platform?: App.Platform; } +export interface RequiredResolveOptions { + ssr: boolean; + transformPage: ({ html }: { html: string }) => string; +} + export type ResolveOptions = Partial; +export interface RouteDefinition { + type: 'page' | 'endpoint'; + pattern: RegExp; + segments: RouteSegment[]; + methods: HttpMethod[]; +} + +export interface RouteSegment { + content: string; + dynamic: boolean; + rest: boolean; +} + export interface SSRManifest { appDir: string; assets: Set; - /** private fields */ - _: { - mime: Record; - entry: { - file: string; - js: string[]; - css: string[]; - }; - nodes: SSRNodeLoader[]; - routes: SSRRoute[]; - }; } -// TODO should this be public? -export type ValidatedConfig = RecursiveRequired; +type ToJSON = { toJSON(...args: any[]): Exclude }; + +export type TrailingSlash = 'never' | 'always' | 'ignore'; diff --git a/packages/kit/types/internal.d.ts b/packages/kit/types/internal.d.ts index 76cb3a2dccc2..08c1e81366e2 100644 --- a/packages/kit/types/internal.d.ts +++ b/packages/kit/types/internal.d.ts @@ -1,41 +1,39 @@ import { OutputAsset, OutputChunk } from 'rollup'; import { - SSRManifest, - ValidatedConfig, - RequestHandler, - Load, + App, + Config, + Either, ExternalFetch, + Fallthrough, GetSession, Handle, HandleError, + HttpMethod, + JSONObject, + JSONValue, + Load, + MaybePromise, RequestEvent, - App, + RequestHandler, RequestOptions, - PrerenderErrorHandler + ResponseHeaders, + RouteSegment, + SSRManifest, + TrailingSlash } from './index'; -export interface AdapterEntry { - /** - * A string that uniquely identifies an HTTP service (e.g. serverless function) and is used for deduplication. - * For example, `/foo/a-[b]` and `/foo/[c]` are different routes, but would both - * be represented in a Netlify _redirects file as `/foo/:param`, so they share an ID - */ - id: string; - - /** - * A function that compares the candidate route with the current route to determine - * if it should be treated as a fallback for the current route. For example, `/foo/[c]` - * is a fallback for `/foo/a-[b]`, and `/[...catchall]` is a fallback for all routes - */ - filter: (route: RouteDefinition) => boolean; - - /** - * A function that is invoked once the entry has been created. This is where you - * should write the function to the filesystem and generate redirect manifests. - */ - complete: (entry: { - generateManifest: (opts: { relativePath: string; format?: 'esm' | 'cjs' }) => string; - }) => void; +export interface SSRManifestInternal extends SSRManifest { + /** private fields */ + _: { + mime: Record; + entry: { + file: string; + js: string[]; + css: string[]; + }; + nodes: SSRNodeLoader[]; + routes: SSRRoute[]; + }; } export interface AppModule { @@ -58,8 +56,6 @@ export interface Asset { type: string | null; } -export type Body = JSONValue | Uint8Array | ReadableStream | import('stream').Readable; - export interface BuildData { app_dir: string; manifest_data: ManifestData; @@ -89,8 +85,6 @@ export type CSRComponentLoader = () => Promise; export type CSRRoute = [RegExp, CSRComponentLoader[], CSRComponentLoader[], GetParams?, HasShadow?]; -export type Either = Only | Only; - export interface EndpointData { type: 'endpoint'; key: string; @@ -100,10 +94,6 @@ export interface EndpointData { file: string; } -export interface Fallthrough { - fallthrough: true; -} - export type GetParams = (match: RegExpExecArray) => Record; type HasShadow = 1; @@ -115,8 +105,6 @@ export interface Hooks { handleError: HandleError; } -export type HttpMethod = 'get' | 'head' | 'post' | 'put' | 'delete' | 'patch'; - export class InternalApp extends App { render( request: Request, @@ -126,19 +114,6 @@ export class InternalApp extends App { ): Promise; } -export type JSONObject = { [key: string]: JSONValue }; - -export type JSONValue = string | number | boolean | null | ToJSON | JSONValue[] | JSONObject; - -export interface Logger { - (msg: string): void; - success(msg: string): void; - error(msg: string): void; - warn(msg: string): void; - minor(msg: string): void; - info(msg: string): void; -} - export interface ManifestData { assets: Asset[]; layout: string; @@ -147,8 +122,6 @@ export interface ManifestData { routes: RouteData[]; } -export type MaybePromise = T | Promise; - export interface MethodOverride { parameter: string; allowed: string[]; @@ -166,8 +139,6 @@ export type NormalizedLoadOutput = Either< Fallthrough >; -type Only = { [P in keyof T]: T[P] } & { [P in Exclude]?: never }; - export interface PageData { type: 'page'; key: string; @@ -185,8 +156,6 @@ export interface PrerenderDependency { body: null | string | Uint8Array; } -export type PrerenderOnErrorValue = 'fail' | 'continue' | PrerenderErrorHandler; - export interface PrerenderOptions { fallback?: string; all: boolean; @@ -204,33 +173,12 @@ export type RecursiveRequired = { : T[K]; // Use the exact type for everything else }; -export interface RequiredResolveOptions { - ssr: boolean; - transformPage: ({ html }: { html: string }) => string; -} - export interface Respond { (request: Request, options: SSROptions, state?: SSRState): Promise; } -/** `string[]` is only for set-cookie, everything else must be type of `string` */ -export type ResponseHeaders = Record; - export type RouteData = PageData | EndpointData; -export interface RouteDefinition { - type: 'page' | 'endpoint'; - pattern: RegExp; - segments: RouteSegment[]; - methods: HttpMethod[]; -} - -export interface RouteSegment { - content: string; - dynamic: boolean; - rest: boolean; -} - export interface ShadowEndpointOutput { status?: number; headers?: Partial; @@ -365,8 +313,6 @@ export interface SSRState { export type StrictBody = string | Uint8Array; -type ToJSON = { toJSON(...args: any[]): Exclude }; - -export type TrailingSlash = 'never' | 'always' | 'ignore'; +export type ValidatedConfig = RecursiveRequired; export * from './index';