Skip to content

Commit

Permalink
[chore] prefer interfaces to types (#2010)
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann authored Jul 26, 2021
1 parent b14a365 commit d656316
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 65 deletions.
5 changes: 5 additions & 0 deletions .changeset/curvy-dots-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[chore] prefer interfaces to types
8 changes: 4 additions & 4 deletions packages/kit/test/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { RequestInfo, RequestInit, Response as NodeFetchResponse } from 'node-fe
// seems like that's no longer an issue? in which case we don't need
// to wrap all these methods

export type TestContext = {
export interface TestContext {
base: string;
page: Page;
pages: {
Expand All @@ -34,14 +34,14 @@ export type TestContext = {
server: import('net').Server;
reset: () => Promise<void>;
unpatch: () => void;
};
}

type TestOptions = {
interface TestOptions {
js?: boolean;
nojs?: boolean;
dev?: boolean;
build?: boolean;
};
}

export interface TestFunctionBase {
(
Expand Down
16 changes: 8 additions & 8 deletions packages/kit/types/config.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Logger, TrailingSlash } from './internal';
import { UserConfig as ViteConfig } from 'vite';

export type AdapterUtils = {
export interface AdapterUtils {
log: Logger;
rimraf: (dir: string) => void;
mkdirp: (dir: string) => void;
Expand All @@ -18,14 +18,14 @@ export type AdapterUtils = {
dest: string;
fallback?: string;
}) => Promise<void>;
};
}

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

export type Config = {
export interface Config {
compilerOptions?: any;
extensions?: string[];
kit?: {
Expand Down Expand Up @@ -76,9 +76,9 @@ export type Config = {
vite?: ViteConfig | (() => ViteConfig);
};
preprocess?: any;
};
}

export type ValidatedConfig = {
export interface ValidatedConfig {
compilerOptions: any;
extensions: string[];
kit: {
Expand Down Expand Up @@ -130,4 +130,4 @@ export type ValidatedConfig = {
vite: () => ViteConfig;
};
preprocess: any;
};
}
4 changes: 2 additions & 2 deletions packages/kit/types/endpoint.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ type JSONValue =

type DefaultBody = JSONValue | Uint8Array;

export type EndpointOutput<Body extends DefaultBody = DefaultBody> = {
export interface EndpointOutput<Body extends DefaultBody = DefaultBody> {
status?: number;
headers?: Headers;
body?: Body;
};
}

export type RequestHandler<
Locals = Record<string, any>,
Expand Down
12 changes: 6 additions & 6 deletions packages/kit/types/hooks.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ import { Headers, Location, MaybePromise, ParameterizedBody } from './helper';

export type StrictBody = string | Uint8Array;

export type ServerRequest<Locals = Record<string, any>, Body = unknown> = Location & {
export interface ServerRequest<Locals = Record<string, any>, Body = unknown> extends Location {
method: string;
headers: Headers;
rawBody: StrictBody;
body: ParameterizedBody<Body>;
locals: Locals;
};
}

export type ServerResponse = {
export interface ServerResponse {
status: number;
headers: Headers;
body?: StrictBody;
};
}

export type GetSession<Locals = Record<string, any>, Session = any> = {
export interface GetSession<Locals = Record<string, any>, Session = any> {
(request: ServerRequest<Locals>): MaybePromise<Session>;
};
}

export type Handle<Locals = Record<string, any>> = (input: {
request: ServerRequest<Locals>;
Expand Down
72 changes: 36 additions & 36 deletions packages/kit/types/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ import { Load } from './page';

type PageId = string;

export type Incoming = Omit<Location, 'params'> & {
export interface Incoming extends Omit<Location, 'params'> {
method: string;
headers: Headers;
rawBody: StrictBody;
body?: ParameterizedBody;
};
}

export type Logger = {
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 App = {
export interface App {
init: ({
paths,
prerendering,
Expand All @@ -44,9 +44,9 @@ export type App = {
};
}
) => Promise<ServerResponse>;
};
}

export type SSRComponent = {
export interface SSRComponent {
ssr?: boolean;
router?: boolean;
hydrate?: boolean;
Expand All @@ -65,22 +65,22 @@ export type SSRComponent = {
};
};
};
};
}

export type SSRComponentLoader = () => Promise<SSRComponent>;

export type CSRComponent = any; // TODO

export type CSRComponentLoader = () => Promise<CSRComponent>;

export type SSRPagePart = {
export interface SSRPagePart {
id: string;
load: SSRComponentLoader;
};
}

export type GetParams = (match: RegExpExecArray) => Record<string, string>;

export type SSRPage = {
export interface SSRPage {
type: 'page';
pattern: RegExp;
params: GetParams;
Expand All @@ -90,16 +90,16 @@ export type SSRPage = {
// plan b — and render that instead
a: PageId[];
b: PageId[];
};
}

export type SSREndpoint = {
export interface SSREndpoint {
type: 'endpoint';
pattern: RegExp;
params: GetParams;
load: () => Promise<{
[method: string]: RequestHandler;
}>;
};
}

export type SSRRoute = SSREndpoint | SSRPage;

Expand All @@ -109,28 +109,28 @@ export type CSREndpoint = [RegExp];

export type CSRRoute = CSREndpoint | CSRPage;

export type SSRManifest = {
export interface SSRManifest {
assets: Asset[];
layout: string;
error: string;
routes: SSRRoute[];
};
}

export type Hooks = {
export interface Hooks {
getSession: GetSession;
handle: Handle;
serverFetch: ServerFetch;
};
}

export type SSRNode = {
export interface SSRNode {
module: SSRComponent;
entry: string; // client-side module corresponding to this component
css: string[];
js: string[];
styles: string[];
};
}

export type SSRRenderOptions = {
export interface SSRRenderOptions {
amp: boolean;
dev: boolean;
entry: {
Expand All @@ -157,9 +157,9 @@ export type SSRRenderOptions = {
target: string;
template: ({ head, body }: { head: string; body: string }) => string;
trailing_slash: TrailingSlash;
};
}

export type SSRRenderState = {
export interface SSRRenderState {
fetched?: string;
initiator?: SSRPage | null;
prerender?: {
Expand All @@ -169,54 +169,54 @@ export type SSRRenderState = {
error: Error;
};
fallback?: string;
};
}

export type Asset = {
export interface Asset {
file: string;
size: number;
type: string | null;
};
}

export type PageData = {
export interface PageData {
type: 'page';
pattern: RegExp;
params: string[];
path: string;
a: string[];
b: string[];
};
}

export type EndpointData = {
export interface EndpointData {
type: 'endpoint';
pattern: RegExp;
params: string[];
file: string;
};
}

export type RouteData = PageData | EndpointData;

export type ManifestData = {
export interface ManifestData {
assets: Asset[];
layout: string;
error: string;
components: string[];
routes: RouteData[];
};
}

export type BuildData = {
export interface BuildData {
client: string[];
server: string[];
static: string[];
entries: string[];
};
}

export type NormalizedLoadOutput = {
export interface NormalizedLoadOutput {
status: number;
error?: Error;
redirect?: string;
props?: Record<string, any> | Promise<Record<string, any>>;
context?: Record<string, any>;
maxage?: number;
};
}

export type TrailingSlash = 'never' | 'always' | 'ignore';
18 changes: 9 additions & 9 deletions packages/kit/types/page.d.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
import { Location as Page, MaybePromise, InferValue } from './helper';

export type LoadInput<
export interface LoadInput<
PageParams extends Record<string, string> = Record<string, string>,
Context extends Record<string, any> = Record<string, any>,
Session = any
> = {
> {
page: Page<PageParams>;
fetch: (info: RequestInfo, init?: RequestInit) => Promise<Response>;
session: Session;
context: Context;
};
}

export type ErrorLoadInput<
export interface ErrorLoadInput<
PageParams extends Record<string, string> = Record<string, string>,
Context extends Record<string, any> = Record<string, any>,
Session = any
> = LoadInput<PageParams, Context, Session> & {
> extends LoadInput<PageParams, Context, Session> {
status?: number;
error?: Error;
};
}

export type LoadOutput<
export interface LoadOutput<
Props extends Record<string, any> = Record<string, any>,
Context extends Record<string, any> = Record<string, any>
> = {
> {
status?: number;
error?: string | Error;
redirect?: string;
props?: Props;
context?: Context;
maxage?: number;
};
}

// Publicized Types
export type Load<
Expand Down

0 comments on commit d656316

Please sign in to comment.