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

roundly cleanup of types and docs #3455

Merged
merged 12 commits into from
Jan 20, 2022
25 changes: 7 additions & 18 deletions documentation/docs/01-routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,37 +51,26 @@ Endpoints are modules written in `.js` (or `.ts`) files that export functions co
// Declaration types for Endpoints
// * declarations that are not exported are for internal use

// type of string[] is only for set-cookie
// everything else must be a type of string
type ResponseHeaders = Record<string, string | string[]>;

export interface RequestEvent<Locals = Record<string, any>> {
request: Request;
url: URL;
params: Record<string, string>;
locals: Locals;
}

type Body = JSONResponse | Uint8Array | string | ReadableStream | stream.Readable;

type Body = JSONString | Uint8Array | ReadableStream | stream.Readable;
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
export interface EndpointOutput {
status?: number;
headers?: HeadersInit;
body?: Body;
}

export type MaybePromise<T> = T | Promise<T>;

export interface Fallthrough {
fallthrough?: true;
type MaybePromise<T> = T | Promise<T>;
interface Fallthrough {
fallthrough: true;
}

export interface RequestHandler<
Locals = Record<string, any>,
Input = unknown,
Output extends DefaultBody = DefaultBody
> {
(request: Request<Locals, Input>): MaybePromise<Fallthrough | EndpointOutput<Output>>;
export interface RequestHandler<Locals = Record<string, any>> {
(event: RequestEvent<Locals>): MaybePromise<Either<Response | EndpointOutput, Fallthrough>>;
}
```

Expand Down Expand Up @@ -126,7 +115,7 @@ If the returned `body` is an object, and no `content-type` header is returned, i
For endpoints that handle other HTTP methods, like POST, export the corresponding function:

```js
export function post(request) {...}
export function post(event) {...}
```

Since `delete` is a reserved word in JavaScript, DELETE requests are handled with a `del` function.
Expand Down
45 changes: 38 additions & 7 deletions documentation/docs/03-loading.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ A component that defines a page or a layout can export a `load` function that ru
// Declaration types for Loading
// * declarations that are not exported are for internal use

export interface Fallthrough {
fallthrough?: true;
}

export interface LoadInput<
PageParams extends Record<string, string> = Record<string, string>,
Stuff extends Record<string, any> = Record<string, any>,
Expand All @@ -24,7 +20,7 @@ export interface LoadInput<
stuff: Stuff;
}

export type LoadOutput<
export interface LoadOutput<
Props extends Record<string, any> = Record<string, any>,
Stuff extends Record<string, any> = Record<string, any>
> {
Expand All @@ -34,7 +30,42 @@ export type LoadOutput<
props?: Props;
stuff?: Stuff;
maxage?: number;
} | Fallthrough
}

interface LoadInputExtends {
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
stuff?: Record<string, any>;
pageParams?: Record<string, string>;
session?: any;
}
interface LoadOutputExtends {
stuff?: Record<string, any>;
props?: Record<string, any>;
}

type MaybePromise<T> = T | Promise<T>;
interface Fallthrough {
fallthrough: true;
}
export interface Load<
Input extends LoadInputExtends = Required<LoadInputExtends>,
Output extends LoadOutputExtends = Required<LoadOutputExtends>
> {
(
input: LoadInput<
InferValue<Input, 'pageParams', Record<string, string>>,
InferValue<Input, 'stuff', Record<string, any>>,
InferValue<Input, 'session', any>
>
): MaybePromise<
Either<
Fallthrough,
LoadOutput<
InferValue<Output, 'props', Record<string, any>>,
InferValue<Output, 'stuff', Record<string, any>>
>
>
>;
}
```

Our example blog page might contain a `load` function like the following:
Expand Down Expand Up @@ -143,7 +174,7 @@ If something goes wrong during `load`, return an `Error` object or a `string` de

If the page should redirect (because the page is deprecated, or the user needs to be logged in, or whatever else) return a `string` containing the location to which they should be redirected alongside a `3xx` status code.

The `redirect` string should be a [properly encoded](https://en.wikipedia.org/wiki/Percent-encoding) URI. Both absolute and relative URIs are acceptable.
The `redirect` string should be a [properly encoded](https://developer.mozilla.org/en-US/docs/Glossary/percent-encoding) URI. Both absolute and relative URIs are acceptable.

#### maxage

Expand Down
2 changes: 1 addition & 1 deletion documentation/docs/04-hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export function getSession(event) {
email: event.locals.user.email,
avatar: event.locals.user.avatar
}
}
}
: {};
}
```
Expand Down
4 changes: 2 additions & 2 deletions documentation/migrating/99-integrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ const minification_options = {
sortClassName: true
};

export async function handle({ request, resolve }) {
const response = await resolve(request);
export async function handle({ event, resolve }) {
const response = await resolve(event);

if (prerendering && response.headers['content-type'] === 'text/html') {
response.body = minify(response.body, minification_options);
Expand Down
3 changes: 1 addition & 2 deletions packages/adapter-cloudflare-workers/files/entry.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
declare module 'APP' {
import { App } from '@sveltejs/kit';
export { App };
export { App } from '@sveltejs/kit';
}

declare module 'MANIFEST' {
Expand Down
3 changes: 1 addition & 2 deletions packages/adapter-cloudflare/files/worker.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
declare module 'APP' {
import { App } from '@sveltejs/kit';
export { App };
export { App } from '@sveltejs/kit';
}

declare module 'MANIFEST' {
Expand Down
3 changes: 1 addition & 2 deletions packages/adapter-netlify/src/handler.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
declare module 'APP' {
import { App } from '@sveltejs/kit';
export { App };
export { App } from '@sveltejs/kit';
}
3 changes: 1 addition & 2 deletions packages/adapter-node/src/ambient.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
declare module 'APP' {
import { App } from '@sveltejs/kit';
export { App };
export { App } from '@sveltejs/kit';
}

declare module 'MANIFEST' {
Expand Down
3 changes: 1 addition & 2 deletions packages/adapter-vercel/files/ambient.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
declare module 'APP' {
import { App } from '@sveltejs/kit';
export { App };
export { App } from '@sveltejs/kit';
}

declare module 'MANIFEST' {
Expand Down
4 changes: 2 additions & 2 deletions packages/create-svelte/types/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export type Condition = 'eslint' | 'prettier' | 'typescript' | 'skeleton' | 'def
export type Common = {
files: Array<{
name: string;
include: Array<Condition>;
exclude: Array<Condition>;
include: Condition[];
exclude: Condition[];
contents: string;
}>;
};
2 changes: 2 additions & 0 deletions packages/kit/types/ambient-modules.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,7 @@ declare module '@sveltejs/kit/node' {
declare module '@sveltejs/kit/install-fetch' {
import fetch, { Headers, Request, Response } from 'node-fetch';

export function __fetch_polyfill(): void;

export { fetch, Headers, Request, Response };
}
19 changes: 9 additions & 10 deletions packages/kit/types/endpoint.d.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import { RequestEvent } from './hooks';
import { JSONString, MaybePromise, ResponseHeaders, Either, Fallthrough } from './helper';
import { Either, JSONString, MaybePromise, ResponseHeaders } from './helper';

type DefaultBody = JSONString | Uint8Array;
type Body = JSONString | Uint8Array | ReadableStream | import('stream').Readable;

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

export interface RequestHandler<
Locals = Record<string, any>,
Output extends DefaultBody = DefaultBody
> {
(request: RequestEvent<Locals>): MaybePromise<
Either<Response | EndpointOutput<Output>, Fallthrough>
>;
export interface Fallthrough {
fallthrough: true;
}

export interface RequestHandler<Locals = Record<string, any>> {
(event: RequestEvent<Locals>): MaybePromise<Either<Response | EndpointOutput, Fallthrough>>;
}
28 changes: 4 additions & 24 deletions packages/kit/types/helper.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
interface ReadOnlyFormData {
get(key: string): string | null;
getAll(key: string): string[];
has(key: string): boolean;
entries(): Generator<[string, string], void>;
keys(): Generator<string, void>;
values(): Generator<string, void>;
[Symbol.iterator](): Generator<[string, string], void>;
}

type ToJSON = { toJSON(...args: any[]): JSONValue };
type JSONValue = Exclude<JSONString, ToJSON>;
export type JSONString =
Expand All @@ -22,12 +12,14 @@ export type JSONString =
/** `string[]` is only for set-cookie, everything else must be type of `string` */
export type ResponseHeaders = Record<string, string | string[]>;

// Utility Types
// <-- Utility Types -->
type Only<T, U> = { [P in keyof T]: T[P] } & { [P in keyof U]?: never };

export type Either<T, U> = Only<T, U> | Only<U, T>;
export type InferValue<T, Key extends keyof T, Default> = T extends Record<Key, infer Val>
? Val
: Default;
export type MaybePromise<T> = T | Promise<T>;
export type Rec<T = any> = Record<string, T>;
export type RecursiveRequired<T> = {
// Recursive implementation of TypeScript's Required utility type.
// Will recursively continue until it reaches primitive or union
Expand All @@ -38,15 +30,3 @@ export type RecursiveRequired<T> = {
? Extract<T[K], Function> // only take the Function type.
: T[K]; // Use the exact type for everything else
};

type Only<T, U> = {
[P in keyof T]: T[P];
} & {
[P in keyof U]?: never;
};

export type Either<T, U> = Only<T, U> | Only<U, T>;

export interface Fallthrough {
fallthrough: true;
}
6 changes: 2 additions & 4 deletions packages/kit/types/internal.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { OutputAsset, OutputChunk } from 'rollup';
import { RequestHandler } from './endpoint';
import { InternalApp, SSRManifest } from './app';
import { Fallthrough, RequestHandler } from './endpoint';
import { Either } from './helper';
import { ExternalFetch, GetSession, HandleError, InternalHandle, RequestEvent } from './hooks';
import { Load } from './page';
import { Either, Fallthrough } from './helper';

type PageId = string;

export interface PrerenderOptions {
fallback?: string;
Expand Down
46 changes: 29 additions & 17 deletions packages/kit/types/page.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { InferValue, MaybePromise, Rec, Either, Fallthrough } from './helper';
import { Fallthrough } from './endpoint';
import { Either, InferValue, MaybePromise } from './helper';

export interface LoadInput<
PageParams extends Rec<string> = Rec<string>,
Stuff extends Rec = Rec,
PageParams extends Record<string, string> = Record<string, string>,
Stuff extends Record<string, any> = Record<string, any>,
Session = any
> {
url: URL;
Expand All @@ -13,15 +14,18 @@ export interface LoadInput<
}

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

export interface LoadOutput<Props extends Rec = Rec, Stuff extends Rec = Rec> {
export interface LoadOutput<
Props extends Record<string, any> = Record<string, any>,
Stuff extends Record<string, any> = Record<string, any>
> {
status?: number;
error?: string | Error;
redirect?: string;
Expand All @@ -31,14 +35,14 @@ export interface LoadOutput<Props extends Rec = Rec, Stuff extends Rec = Rec> {
}

interface LoadInputExtends {
stuff?: Rec;
pageParams?: Rec<string>;
stuff?: Record<string, any>;
pageParams?: Record<string, string>;
session?: any;
}

interface LoadOutputExtends {
stuff?: Rec;
props?: Rec;
stuff?: Record<string, any>;
props?: Record<string, any>;
}

export interface Load<
Expand All @@ -47,14 +51,17 @@ export interface Load<
> {
(
input: LoadInput<
InferValue<Input, 'pageParams', Rec<string>>,
InferValue<Input, 'stuff', Rec>,
InferValue<Input, 'pageParams', Record<string, string>>,
InferValue<Input, 'stuff', Record<string, any>>,
InferValue<Input, 'session', any>
>
): MaybePromise<
Either<
LoadOutput<InferValue<Output, 'props', Rec>, InferValue<Output, 'stuff', Rec>>,
Fallthrough
Fallthrough,
LoadOutput<
InferValue<Output, 'props', Record<string, any>>,
InferValue<Output, 'stuff', Record<string, any>>
>
>
>;
}
Expand All @@ -65,9 +72,14 @@ export interface ErrorLoad<
> {
(
input: ErrorLoadInput<
InferValue<Input, 'pageParams', Rec<string>>,
InferValue<Input, 'stuff', Rec>,
InferValue<Input, 'pageParams', Record<string, string>>,
InferValue<Input, 'stuff', Record<string, any>>,
InferValue<Input, 'session', any>
>
): MaybePromise<LoadOutput<InferValue<Output, 'props', Rec>, InferValue<Output, 'stuff', Rec>>>;
): MaybePromise<
LoadOutput<
InferValue<Output, 'props', Record<string, any>>,
InferValue<Output, 'stuff', Record<string, any>>
>
>;
}