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] export App types #2259

Merged
merged 2 commits into from
Aug 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nice-ways-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[chore] export App types
2 changes: 1 addition & 1 deletion packages/kit/src/core/adapt/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a

const server_root = resolve_path(dir);

/** @type {import('types/internal').App} */
/** @type {import('types/app').App} */
const app = await import(pathToFileURL(`${server_root}/server/app.js`).href);

app.init({
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/core/preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export async function preview({

const app_file = resolve(cwd, `${SVELTE_KIT}/output/server/app.js`);

/** @type {import('types/internal').App} */
/** @type {import('types/app').App} */
const app = await import(pathToFileURL(app_file).href);

/** @type {import('sirv').RequestHandler} */
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { coalesce_to_error } from '../utils.js';
import { hash } from '../hash.js';

/**
* @param {import('types/internal').Incoming} incoming
* @param {import('types/app').IncomingRequest} incoming
* @param {import('types/internal').SSRRenderOptions} options
* @param {import('types/internal').SSRRenderState} [state]
*/
Expand Down
36 changes: 36 additions & 0 deletions packages/kit/types/app.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Headers, RawBody } from './helper';
import { ServerResponse } from './hooks';

export interface IncomingRequest {
method: string;
host: string;
path: string;
query: URLSearchParams;
headers: Headers;
rawBody: RawBody;
}

export interface App {
init({
paths,
prerendering,
read
}?: {
paths: {
base: string;
assets: string;
};
prerendering: boolean;
read(file: string): Buffer;
}): void;
render(
incoming: IncomingRequest,
options?: {
prerender: {
fallback?: string;
all: boolean;
dependencies?: Map<string, ServerResponse>;
};
}
): Promise<ServerResponse>;
}
7 changes: 0 additions & 7 deletions packages/kit/types/helper.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ export type ParameterizedBody<Body = unknown> = Body extends FormData
// but this can't happen until TypeScript 4.3
export type Headers = Record<string, string>;

export type Location<Params extends Record<string, string> = Record<string, string>> = {
host: string;
path: string;
params: Params;
query: URLSearchParams;
};

export type InferValue<T, Key extends keyof T, Default> = T extends Record<Key, infer Val>
? Val
: Default;
Expand Down
10 changes: 5 additions & 5 deletions packages/kit/types/hooks.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Headers, Location, MaybePromise, ParameterizedBody, RawBody } from './helper';
import { IncomingRequest } from './app';
import { Headers, MaybePromise, ParameterizedBody } from './helper';

export type StrictBody = string | Uint8Array;

export interface ServerRequest<Locals = Record<string, any>, Body = unknown> extends Location {
method: string;
headers: Headers;
rawBody: RawBody;
export interface ServerRequest<Locals = Record<string, any>, Body = unknown>
extends IncomingRequest {
params: Record<string, string>;
body: ParameterizedBody<Body>;
locals: Locals;
}
Expand Down
1 change: 1 addition & 0 deletions packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import './ambient-modules';

export { App, IncomingRequest } from './app';
export { Adapter, AdapterUtils, Config, PrerenderErrorHandler, ValidatedConfig } from './config';
export { EndpointOutput, RequestHandler } from './endpoint';
export { ErrorLoad, ErrorLoadInput, Load, LoadInput, LoadOutput, Page } from './page';
Expand Down
33 changes: 0 additions & 33 deletions packages/kit/types/internal.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { RequestHandler } from './endpoint';
import { Headers, Location, ParameterizedBody, RawBody } from './helper';
import {
ExternalFetch,
GetSession,
Expand All @@ -12,13 +11,6 @@ import { Load } from './page';

type PageId = string;

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

export interface Logger {
(msg: string): void;
success(msg: string): void;
Expand All @@ -28,31 +20,6 @@ export interface Logger {
info(msg: string): void;
}

export interface App {
init({
paths,
prerendering,
read
}: {
paths: {
base: string;
assets: string;
};
prerendering: boolean;
read(file: string): Buffer;
}): void;
render(
incoming: Incoming,
options?: {
prerender: {
fallback?: string;
all: boolean;
dependencies?: Map<string, ServerResponse>;
};
}
): Promise<ServerResponse>;
}

export interface SSRComponent {
ssr?: boolean;
router?: boolean;
Expand Down
11 changes: 8 additions & 3 deletions packages/kit/types/page.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { InferValue, Location as Page, MaybePromise, Rec } from './helper';
import { InferValue, MaybePromise, Rec } from './helper';

export interface Page<Params extends Record<string, string> = Record<string, string>> {
host: string;
path: string;
params: Params;
query: URLSearchParams;
}

export interface LoadInput<
PageParams extends Rec<string> = Rec<string>,
Expand Down Expand Up @@ -68,5 +75,3 @@ export interface ErrorLoad<
>
): MaybePromise<LoadOutput<InferValue<Output, 'props', Rec>, InferValue<Output, 'context', Rec>>>;
}

export { Page };