Skip to content

Commit eb96322

Browse files
authored
types: group related and reduce potential inconsistencies (#1539)
1 parent 5f7c6a8 commit eb96322

File tree

13 files changed

+59
-47
lines changed

13 files changed

+59
-47
lines changed

packages/kit/src/core/node/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @param {import('http').IncomingMessage} req
3-
* @returns {Promise<string | Uint8Array>}
3+
* @returns {Promise<import('types/hooks').StrictBody>}
44
*/
55
export function getRawBody(req) {
66
return new Promise((fulfil, reject) => {

packages/kit/src/runtime/hash.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/** @param {string | Uint8Array} value */
1+
/** @param {import('types/hooks').StrictBody} value */
22
export function hash(value) {
33
let hash = 5381;
44
let i = value.length;

packages/kit/src/runtime/server/endpoint.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function error(body) {
1010
}
1111

1212
/**
13-
* @param {import('types/endpoint').ServerRequest} request
13+
* @param {import('types/hooks').ServerRequest} request
1414
* @param {import('types/internal').SSREndpoint} route
1515
* @returns {Promise<import('types/hooks').ServerResponse>}
1616
*/
@@ -51,14 +51,14 @@ export default async function render_route(request, route) {
5151
);
5252
}
5353

54-
/** @type {string | Uint8Array} */
54+
/** @type {import('types/hooks').StrictBody} */
5555
let normalized_body;
5656

5757
if (typeof body === 'object' && (!type || type === 'application/json')) {
5858
headers = { ...headers, 'content-type': 'application/json' };
5959
normalized_body = JSON.stringify(body);
6060
} else {
61-
normalized_body = /** @type {string | Uint8Array} */ (body);
61+
normalized_body = /** @type {import('types/hooks').StrictBody} */ (body);
6262
}
6363

6464
return { status, body: normalized_body, headers };

packages/kit/src/runtime/server/page/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { respond } from './respond.js';
22
import { respond_with_error } from './respond_with_error.js';
33

44
/**
5-
* @param {import('types/endpoint').ServerRequest} request
5+
* @param {import('types/hooks').ServerRequest} request
66
* @param {import('types/internal').SSRPage} route
77
* @param {import('types/internal').SSRRenderOptions} options
88
* @param {import('types/internal').SSRRenderState} state

packages/kit/src/runtime/server/page/load_node.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const s = JSON.stringify;
77
/**
88
*
99
* @param {{
10-
* request: import('types/endpoint').ServerRequest;
10+
* request: import('types/hooks').ServerRequest;
1111
* options: import('types/internal').SSRRenderOptions;
1212
* state: import('types/internal').SSRRenderState;
1313
* route: import('types/internal').SSRPage;

packages/kit/src/runtime/server/page/respond.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { respond_with_error } from './respond_with_error.js';
66

77
/**
88
* @param {{
9-
* request: import('types/endpoint').ServerRequest;
9+
* request: import('types/hooks').ServerRequest;
1010
* options: import('types/internal').SSRRenderOptions;
1111
* state: import('types/internal').SSRRenderState;
1212
* $session: any;

packages/kit/src/runtime/server/page/respond_with_error.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { load_node } from './load_node.js';
33

44
/**
55
* @param {{
6-
* request: import('types/endpoint').ServerRequest;
6+
* request: import('types/hooks').ServerRequest;
77
* options: import('types/internal').SSRRenderOptions;
88
* state: import('types/internal').SSRRenderState;
99
* $session: any;

packages/kit/types/ambient-modules.d.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ declare module '$app/navigation' {
3434
*/
3535
export function invalidate(href: string): Promise<any>;
3636
/**
37-
* Programmatically prefetches the given page, which means a) ensuring that the code for the page is loaded, and b) calling the page's load function with the appropriate options.
37+
* Programmatically prefetches the given page, which means
38+
* 1. ensuring that the code for the page is loaded, and
39+
* 2. calling the page's load function with the appropriate options.
40+
*
3841
* This is the same behaviour that SvelteKit triggers when the user taps or mouses over an `<a>` element with `sveltekit:prefetch`.
3942
* If the next navigation is to `href`, the values returned from load will be used, making navigation instantaneous.
4043
* Returns a Promise that resolves when the prefetch is complete.
@@ -45,8 +48,11 @@ declare module '$app/navigation' {
4548
/**
4649
* Programmatically prefetches the code for routes that haven't yet been fetched.
4750
* Typically, you might call this to speed up subsequent navigation.
48-
* If no argument is given, all routes will be fetched, otherwise you can specify routes by any matching pathname such as `/about` (to match `src/routes/about.svelte`)
49-
* or `/blog/*` (to match `src/routes/blog/[slug].svelte`). Unlike prefetch, this won't call preload for individual pages.
51+
*
52+
* If no argument is given, all routes will be fetched, otherwise you can specify routes by any matching pathname
53+
* such as `/about` (to match `src/routes/about.svelte`) or `/blog/*` (to match `src/routes/blog/[slug].svelte`).
54+
*
55+
* Unlike prefetch, this won't call preload for individual pages.
5056
* Returns a Promise that resolves when the routes have been prefetched.
5157
*/
5258
export function prefetchRoutes(routes?: string[]): Promise<any>;
@@ -65,14 +71,15 @@ declare module '$app/paths' {
6571

6672
declare module '$app/stores' {
6773
import { Readable, Writable } from 'svelte/store';
68-
import { Page } from '@sveltejs/kit';
74+
type Page = import('@sveltejs/kit').Page;
75+
type Navigating = { from: Page; to: Page };
6976

7077
/**
7178
* A convenience function around `getContext` that returns `{ navigating, page, session }`.
7279
* Most of the time, you won't need to use it.
7380
*/
7481
export function getStores(): {
75-
navigating: Readable<{ from: Page; to: Page } | null>;
82+
navigating: Readable<Navigating | null>;
7683
page: Readable<Page>;
7784
session: Writable<any>;
7885
};
@@ -85,7 +92,7 @@ declare module '$app/stores' {
8592
* When navigating starts, its value is `{ from, to }`, where from and to both mirror the page store value.
8693
* When navigating finishes, its value reverts to `null`.
8794
*/
88-
export const navigating: Readable<{ from: Page; to: Page } | null>;
95+
export const navigating: Readable<Navigating | null>;
8996
/**
9097
* A writable store whose initial value is whatever was returned from `getSession`.
9198
* It can be written to, but this will not cause changes to persist on the server — this is something you must implement yourself.

packages/kit/types/endpoint.d.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
1-
import { Headers, ParameterizedBody } from './helper';
2-
3-
export type ServerRequest<Locals = Record<string, any>, Body = unknown> = {
4-
method: string;
5-
host: string;
6-
headers: Headers;
7-
path: string;
8-
params: Record<string, string>;
9-
query: URLSearchParams;
10-
rawBody: string | Uint8Array;
11-
body: ParameterizedBody<Body>;
12-
locals: Locals;
13-
};
1+
import { ServerRequest } from './hooks';
2+
import { Headers } from './helper';
143

154
type JSONValue =
165
| string
@@ -24,7 +13,7 @@ type JSONValue =
2413
export type EndpointOutput = {
2514
status?: number;
2615
headers?: Partial<Headers>;
27-
body?: string | Uint8Array | JSONValue;
16+
body?: JSONValue | Uint8Array;
2817
};
2918

3019
export type RequestHandler<Locals = Record<string, any>, Body = unknown> = (

packages/kit/types/helper.d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ interface ReadOnlyFormData extends Iterator<[string, string]> {
77
values: () => Iterator<string>;
88
}
99

10-
export type BaseBody = string | Buffer | ReadOnlyFormData;
10+
type BaseBody = string | Buffer | ReadOnlyFormData;
1111
export type ParameterizedBody<Body = unknown> = Body extends FormData
1212
? ReadOnlyFormData
1313
: BaseBody & Body;
@@ -17,3 +17,10 @@ export type ParameterizedBody<Body = unknown> = Body extends FormData
1717
// 'set-cookie' is a `string[]` (or at least `string | string[]`)
1818
// but this can't happen until TypeScript 4.3
1919
export type Headers = Record<string, string>;
20+
21+
export type Location = {
22+
host: string;
23+
path: string;
24+
params: Record<string, string>;
25+
query: URLSearchParams;
26+
};

packages/kit/types/hooks.d.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
1-
import { BaseBody, Headers } from './helper';
2-
import { ServerRequest } from './endpoint';
1+
import { Headers, Location, ParameterizedBody } from './helper';
32

4-
export type Incoming = {
3+
export type StrictBody = string | Uint8Array;
4+
5+
export type Incoming = Omit<Location, 'params'> & {
6+
method: string;
7+
headers: Headers;
8+
rawBody: StrictBody;
9+
body?: ParameterizedBody;
10+
};
11+
12+
export type ServerRequest<Locals = Record<string, any>, Body = unknown> = Location & {
513
method: string;
6-
host: string;
714
headers: Headers;
8-
path: string;
9-
query: URLSearchParams;
10-
rawBody: string | Uint8Array;
11-
body?: BaseBody;
15+
rawBody: StrictBody;
16+
body: ParameterizedBody<Body>;
17+
locals: Locals;
1218
};
1319

1420
export type ServerResponse = {
1521
status: number;
1622
headers: Headers;
17-
body?: string | Uint8Array;
23+
body?: StrictBody;
1824
};
1925

2026
export type GetSession<Locals = Record<string, any>, Session = any> = {

packages/kit/types/index.d.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
import './ambient-modules';
55

66
export { Adapter, AdapterUtils, Config } from './config';
7+
export { EndpointOutput, RequestHandler } from './endpoint';
78
export { ErrorLoad, Load, Page } from './page';
8-
export { Incoming, GetSession, Handle, ServerResponse as Response } from './hooks';
9-
export { ServerRequest as Request, EndpointOutput, RequestHandler } from './endpoint';
9+
export {
10+
Incoming,
11+
GetSession,
12+
Handle,
13+
ServerRequest as Request,
14+
ServerResponse as Response
15+
} from './hooks';

packages/kit/types/page.d.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Location as Page } from './helper';
2+
13
export type LoadInput = {
24
page: Page;
35
fetch: (info: RequestInfo, init?: RequestInit) => Promise<Response>;
@@ -22,9 +24,4 @@ export type LoadOutput = {
2224
/* Publicized Types */
2325
export type Load = (input: LoadInput) => LoadOutput | Promise<LoadOutput>;
2426
export type ErrorLoad = (input: ErrorLoadInput) => LoadOutput | Promise<LoadOutput>;
25-
export type Page = {
26-
host: string;
27-
path: string;
28-
params: Record<string, string>;
29-
query: URLSearchParams;
30-
};
27+
export { Page };

0 commit comments

Comments
 (0)