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

Make it possible to type context and props for LoadInput and LoadOutput #1447

Merged
merged 6 commits into from
May 31, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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/tricky-rockets-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Make it possible to type context, page params and props for LoadInput and LoadOutput
4 changes: 2 additions & 2 deletions packages/kit/types/endpoint.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Headers, ParameterizedBody } from './helper';
import { Headers, MaybePromise, ParameterizedBody } from './helper';

export type ServerRequest<Locals = Record<string, any>, Body = unknown> = {
method: string;
Expand Down Expand Up @@ -29,4 +29,4 @@ export type EndpointOutput = {

export type RequestHandler<Locals = Record<string, any>, Body = unknown> = (
request: ServerRequest<Locals, Body>
) => void | EndpointOutput | Promise<EndpointOutput>;
) => void | MaybePromise<EndpointOutput>;
5 changes: 5 additions & 0 deletions packages/kit/types/helper.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ export type ParameterizedBody<Body = unknown> = Body extends FormData
// 'set-cookie' is a `string[]` (or at least `string | string[]`)
// but this can't happen until TypeScript 4.3
export type Headers = Record<string, string>;

export type MaybePromise<T> = T | Promise<T>;
export type InferValue<T, Key extends keyof T, Default> = T extends Record<Key, infer Val>
? Val
: Default;
8 changes: 4 additions & 4 deletions packages/kit/types/hooks.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BaseBody, Headers } from './helper';
import { BaseBody, Headers, MaybePromise } from './helper';
import { ServerRequest } from './endpoint';

export type Incoming = {
Expand All @@ -18,10 +18,10 @@ export type ServerResponse = {
};

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

export type Handle<Locals = Record<string, any>> = (input: {
request: ServerRequest<Locals>;
render: (request: ServerRequest<Locals>) => ServerResponse | Promise<ServerResponse>;
}) => ServerResponse | Promise<ServerResponse>;
render: (request: ServerRequest<Locals>) => MaybePromise<ServerResponse>;
}) => MaybePromise<ServerResponse>;
40 changes: 28 additions & 12 deletions packages/kit/types/page.d.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,46 @@
export type LoadInput = {
page: Page;
import { MaybePromise, InferValue } from './helper';

export type LoadInput<
T extends { context?: Record<string, any>; pageParams?: Record<string, string> } = {}
> = {
page: Page<InferValue<T, 'pageParams', Record<string, string>>>;
fetch: (info: RequestInfo, init?: RequestInit) => Promise<Response>;
session: any;
context: Record<string, any>;
context: InferValue<T, 'context', Record<string, any>>;
};

export type ErrorLoadInput = LoadInput & {
export type ErrorLoadInput<
T extends { context?: Record<string, any>; pageParams?: Record<string, string> } = {}
> = LoadInput<T> & {
status: number;
error: Error;
};

export type LoadOutput = {
export type LoadOutput<
T extends { context?: Record<string, any>; props?: Record<string, any> } = {}
> = {
status?: number;
error?: string | Error;
redirect?: string;
props?: Record<string, any> | Promise<Record<string, any>>;
context?: Record<string, any>;
props?: InferValue<T, 'props', Record<string, any>>;
context?: InferValue<T, 'context', Record<string, any>>;
maxage?: number;
};

/* Publicized Types */
export type Load = (input: LoadInput) => LoadOutput | Promise<LoadOutput>;
export type ErrorLoad = (input: ErrorLoadInput) => LoadOutput | Promise<LoadOutput>;
export type Page = {
// Publicized Types
export type Load<
Input extends { context?: Record<string, any>; pageParams?: Record<string, string> } = {},
Output extends { context?: Record<string, any>; props?: Record<string, any> } = {}
> = (input: LoadInput<Input>) => MaybePromise<LoadOutput<Output>>;

export type ErrorLoad<
Input extends { context?: Record<string, any>; pageParams?: Record<string, string> } = {},
Output extends { context?: Record<string, any>; props?: Record<string, any> } = {}
> = (input: ErrorLoadInput<Input>) => MaybePromise<LoadOutput<Output>>;

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