-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make it possible to type context and props for LoadInput and LoadOutp…
…ut (#1447) - added generics for Context, Page Params and Props - added a helper type "MaybePromise" to not write T | Promise<T> all the time - Make LoadInput/Output public Fixes #1442
- Loading branch information
1 parent
6b186ae
commit 1bf1a02
Showing
6 changed files
with
67 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |