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

Add generic type for session #1791

Merged
merged 2 commits into from
Jul 1, 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/sharp-geckos-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Add generic type for session
4 changes: 2 additions & 2 deletions packages/kit/types/ambient-modules.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ declare module '$app/stores' {
* A convenience function around `getContext` that returns `{ navigating, page, session }`.
* Most of the time, you won't need to use it.
*/
export function getStores(): {
export function getStores<Session = any>(): {
navigating: Readable<Navigating | null>;
page: Readable<Page>;
session: Writable<any>;
session: Writable<Session>;
};
/**
* A readable store whose value reflects the object passed to load functions.
Expand Down
28 changes: 20 additions & 8 deletions packages/kit/types/page.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ import { Location as Page, MaybePromise, InferValue } from './helper';

export type LoadInput<
PageParams extends Record<string, string> = Record<string, string>,
Context extends Record<string, any> = Record<string, any>
Context extends Record<string, any> = Record<string, any>,
Session = any
dummdidumm marked this conversation as resolved.
Show resolved Hide resolved
> = {
page: Page<PageParams>;
fetch: (info: RequestInfo, init?: RequestInit) => Promise<Response>;
session: any;
session: Session;
context: Context;
};

export type ErrorLoadInput<
PageParams extends Record<string, string> = Record<string, string>,
Context extends Record<string, any> = Record<string, any>
> = LoadInput<PageParams, Context> & {
Context extends Record<string, any> = Record<string, any>,
Session = any
> = LoadInput<PageParams, Context, Session> & {
status: number;
error: Error;
};
Expand All @@ -32,25 +34,35 @@ export type LoadOutput<

// Publicized Types
export type Load<
Input extends { context?: Record<string, any>; pageParams?: Record<string, string> } = {},
Input extends {
context?: Record<string, any>;
pageParams?: Record<string, string>;
session?: any;
} = {},
Output extends { context?: Record<string, any>; props?: Record<string, any> } = {}
> = (
input: LoadInput<
InferValue<Input, 'pageParams', Record<string, string>>,
InferValue<Input, 'context', Record<string, any>>
InferValue<Input, 'context', Record<string, any>>,
InferValue<Input, 'session', any>
>
) => MaybePromise<void | 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> } = {},
Input extends {
context?: Record<string, any>;
pageParams?: Record<string, string>;
session?: any;
} = {},
Output extends { context?: Record<string, any>; props?: Record<string, any> } = {}
> = (
input: ErrorLoadInput<
InferValue<Input, 'pageParams', Record<string, string>>,
InferValue<Input, 'context', Record<string, any>>
InferValue<Input, 'context', Record<string, any>>,
InferValue<Input, 'session', any>
>
) => MaybePromise<
LoadOutput<
Expand Down