Skip to content
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/calm-ravens-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[breaking] allow `InputProps` and `OutputProps` to be typed separately in generated `Load`
5 changes: 5 additions & 0 deletions .changeset/little-toys-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

allow `Output` to be typed in generated `RequestHandler`
8 changes: 4 additions & 4 deletions documentation/docs/14-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ export type RequestHandler<Body = any> = GenericRequestHandler<
Body
>;

export type Load<Props = Record<string, any>> = GenericLoad<
{ foo: string; bar: string; baz: string },
Props
>;
export type Load<
InputProps extends Record<string, any> = Record<string, any>,
OutputProps extends Record<string, any> = InputProps
> = GenericLoad<{ foo: string; bar: string; baz: string }, InputProps, OutputProps>
```

These files can be imported into your endpoints and pages as siblings, thanks to the [`rootDirs`](https://www.typescriptlang.org/tsconfig#rootDirs) option in your TypeScript configuration:
Expand Down
54 changes: 35 additions & 19 deletions packages/kit/src/core/sync/write_types.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import { write_if_changed } from './utils.js';

/** @param {string} imports */
const header = (imports) => `
// this file is auto-generated
import type { ${imports} } from '@sveltejs/kit';`;

/** @param {string} arg */
const endpoint = (arg) => `
export type RequestHandler<Output extends ResponseBody = ResponseBody> = GenericRequestHandler<${arg}, Output>;`;

/** @param {string} arg */
const page = (arg) => `
export type Load<
InputProps extends Record<string, any> = Record<string, any>,
OutputProps extends Record<string, any> = InputProps
> = GenericLoad<${arg}, InputProps, OutputProps>;`;

/**
* @param {import('types').ValidatedConfig} config
* @param {import('types').ManifestData} manifest_data
Expand Down Expand Up @@ -53,24 +69,24 @@ export function write_types(config, manifest_data) {
const arg =
params.length > 0 ? `{ ${params.map((param) => `${param}: string`).join('; ')} }` : '{}';

const imports = [
type !== 'page' && 'RequestHandler as GenericRequestHandler',
type !== 'endpoint' && 'Load as GenericLoad'
]
.filter(Boolean)
.join(', ');

const file = `${config.kit.outDir}/types/${key || 'index'}.d.ts`;
const content = [
'// this file is auto-generated',
`import type { ${imports} } from '@sveltejs/kit';`,
type !== 'page' && `export type RequestHandler = GenericRequestHandler<${arg}>;`,
type !== 'endpoint' &&
`export type Load<Props = Record<string, any>> = GenericLoad<${arg}, Props>;`
]
.filter(Boolean)
.join('\n');

write_if_changed(file, content);
const imports = [];
const content = [];

if (type !== 'page') {
imports.push('RequestHandler as GenericRequestHandler, ResponseBody');
content.push(endpoint(arg));
}

if (type !== 'endpoint') {
imports.push('Load as GenericLoad');
content.push(page(arg));
}

content.unshift(header(imports.join(', ')));

write_if_changed(
`${config.kit.outDir}/types/${key || 'index'}.d.ts`,
content.join('\n').trim()
);
});
}
16 changes: 12 additions & 4 deletions packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import './ambient';
import { CompileOptions } from 'svelte/types/compiler/interfaces';
import {
AdapterEntry,
Body,
CspDirectives,
Either,
ErrorLoadInput,
Fallthrough,
JSONValue,
LoadInput,
LoadOutput,
Logger,
Expand Down Expand Up @@ -158,7 +158,10 @@ export interface Config {
preprocess?: any;
}

export interface ErrorLoad<Params = Record<string, string>, Props = Record<string, any>> {
export interface ErrorLoad<
Params extends Record<string, string> = Record<string, string>,
Props extends Record<string, any> = Record<string, any>
> {
(input: ErrorLoadInput<Params>): MaybePromise<LoadOutput<Props>>;
}

Expand Down Expand Up @@ -218,11 +221,14 @@ export interface Page<Params extends Record<string, string> = Record<string, str
* Note that you can use [generated types](/docs/types#generated-types)
* instead of manually specifying the `Params` generic argument.
*/
export interface RequestHandler<Params = Record<string, string>, Output extends Body = Body> {
export interface RequestHandler<
Params extends Record<string, string> = Record<string, string>,
Output extends ResponseBody = ResponseBody
> {
(event: RequestEvent<Params>): RequestHandlerOutput<Output>;
}

export type RequestHandlerOutput<Output extends Body = Body> = MaybePromise<
export type RequestHandlerOutput<Output extends ResponseBody = ResponseBody> = MaybePromise<
Either<
{
status?: number;
Expand All @@ -233,6 +239,8 @@ export type RequestHandlerOutput<Output extends Body = Body> = MaybePromise<
>
>;

export type ResponseBody = JSONValue | Uint8Array | ReadableStream | import('stream').Readable;

export class Server {
constructor(manifest: SSRManifest);
respond(request: Request, options: RequestOptions): Promise<Response>;
Expand Down
14 changes: 8 additions & 6 deletions packages/kit/types/private.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ export interface AdapterEntry {
}) => void;
}

export type Body = JSONValue | Uint8Array | ReadableStream | import('stream').Readable;

// Based on https://github.com/josh-hemphill/csp-typed-directives/blob/latest/src/csp.types.ts
//
// MIT License
Expand Down Expand Up @@ -140,7 +138,8 @@ export interface CspDirectives {

export type Either<T, U> = Only<T, U> | Only<U, T>;

export interface ErrorLoadInput<Params = Record<string, string>> extends LoadInput<Params> {
export interface ErrorLoadInput<Params extends Record<string, string> = Record<string, string>>
extends LoadInput<Params> {
status?: number;
error?: Error;
}
Expand All @@ -165,7 +164,10 @@ export type JSONValue =
| JSONValue[]
| JSONObject;

export interface LoadInput<Params = Record<string, string>, Props = Record<string, any>> {
export interface LoadInput<
Params extends Record<string, string> = Record<string, string>,
Props extends Record<string, any> = Record<string, any>
> {
url: URL;
params: Params;
props: Props;
Expand All @@ -174,7 +176,7 @@ export interface LoadInput<Params = Record<string, string>, Props = Record<strin
stuff: Partial<App.Stuff>;
}

export interface LoadOutput<Props = Record<string, any>> {
export interface LoadOutput<Props extends Record<string, any> = Record<string, any>> {
status?: number;
error?: string | Error;
redirect?: string;
Expand Down Expand Up @@ -233,7 +235,7 @@ export interface PrerenderErrorHandler {

export type PrerenderOnErrorValue = 'fail' | 'continue' | PrerenderErrorHandler;

export interface RequestEvent<Params = Record<string, string>> {
export interface RequestEvent<Params extends Record<string, string> = Record<string, string>> {
clientAddress: string;
locals: App.Locals;
params: Params;
Expand Down