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 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/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,5 +1,5 @@
import { ServerRequest } from './hooks';
import { Headers } from './helper';
import { Headers, MaybePromise } from './helper';

type JSONValue =
| string
Expand All @@ -18,4 +18,4 @@ export type EndpointOutput = {

export type RequestHandler<Locals = Record<string, any>, Body = unknown> = (
request: ServerRequest<Locals, Body>
) => void | EndpointOutput | Promise<EndpointOutput>;
) => void | MaybePromise<EndpointOutput>;
9 changes: 7 additions & 2 deletions packages/kit/types/helper.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ 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 = {
export type Location<Params extends Record<string, string> = Record<string, string>> = {
host: string;
path: string;
params: Record<string, string>;
params: Params;
query: URLSearchParams;
};

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 { Headers, Location, ParameterizedBody } from './helper';
import { Headers, Location, MaybePromise, ParameterizedBody } from './helper';

export type StrictBody = string | Uint8Array;

Expand All @@ -24,10 +24,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>;
resolve: (request: ServerRequest<Locals>) => ServerResponse | Promise<ServerResponse>;
}) => ServerResponse | Promise<ServerResponse>;
resolve: (request: ServerRequest<Locals>) => MaybePromise<ServerResponse>;
}) => MaybePromise<ServerResponse>;
2 changes: 1 addition & 1 deletion packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import './ambient-modules';

export { Adapter, AdapterUtils, Config, ValidatedConfig } from './config';
export { EndpointOutput, RequestHandler } from './endpoint';
export { ErrorLoad, Load, Page } from './page';
export { ErrorLoad, Load, Page, LoadInput, LoadOutput, ErrorLoadInput } from './page';
export {
Incoming,
GetSession,
Expand Down
59 changes: 48 additions & 11 deletions packages/kit/types/page.d.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,64 @@
import { Location as Page } from './helper';
import { Location as Page, MaybePromise, InferValue } from './helper';

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

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

export type LoadOutput = {
export type 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?: Record<string, any> | Promise<Record<string, any>>;
context?: Record<string, any>;
props?: Props;
context?: Context;
maxage?: number;
};

/* Publicized Types */
export type Load = (input: LoadInput) => LoadOutput | Promise<LoadOutput>;
export type ErrorLoad = (input: ErrorLoadInput) => LoadOutput | Promise<LoadOutput>;
// 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<
InferValue<Input, 'pageParams', Record<string, string>>,
InferValue<Input, 'context', Record<string, any>>
>
) => MaybePromise<
LoadOutput<
InferValue<Output, 'props', Record<string, any>>,
InferValue<Output, 'context', Record<string, any>>
>
>;

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<
InferValue<Input, 'pageParams', Record<string, string>>,
InferValue<Input, 'context', Record<string, any>>
>
) => MaybePromise<
LoadOutput<
InferValue<Output, 'props', Record<string, any>>,
InferValue<Output, 'context', Record<string, any>>
>
>;

export { Page };