Skip to content

Commit

Permalink
[feat] Add generic type for session (#1791)
Browse files Browse the repository at this point in the history
  • Loading branch information
seanlail authored Jul 1, 2021
1 parent 325c223 commit 0d69e55
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
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
> = {
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

0 comments on commit 0d69e55

Please sign in to comment.