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

types: group related and reduce potential inconsistencies #1539

Merged
merged 5 commits into from
May 26, 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
2 changes: 1 addition & 1 deletion packages/kit/src/core/node/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @param {import('http').IncomingMessage} req
* @returns {Promise<string | Uint8Array>}
* @returns {Promise<import('types/hooks').StrictBody>}
*/
export function getRawBody(req) {
return new Promise((fulfil, reject) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/hash.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @param {string | Uint8Array} value */
/** @param {import('types/hooks').StrictBody} value */
export function hash(value) {
let hash = 5381;
let i = value.length;
Expand Down
6 changes: 3 additions & 3 deletions packages/kit/src/runtime/server/endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function error(body) {
}

/**
* @param {import('types/endpoint').ServerRequest} request
* @param {import('types/hooks').ServerRequest} request
* @param {import('types/internal').SSREndpoint} route
* @returns {Promise<import('types/hooks').ServerResponse>}
*/
Expand Down Expand Up @@ -51,14 +51,14 @@ export default async function render_route(request, route) {
);
}

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

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

return { status, body: normalized_body, headers };
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/server/page/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { respond } from './respond.js';
import { respond_with_error } from './respond_with_error.js';

/**
* @param {import('types/endpoint').ServerRequest} request
* @param {import('types/hooks').ServerRequest} request
* @param {import('types/internal').SSRPage} route
* @param {import('types/internal').SSRRenderOptions} options
* @param {import('types/internal').SSRRenderState} state
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/server/page/load_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const s = JSON.stringify;
/**
*
* @param {{
* request: import('types/endpoint').ServerRequest;
* request: import('types/hooks').ServerRequest;
* options: import('types/internal').SSRRenderOptions;
* state: import('types/internal').SSRRenderState;
* route: import('types/internal').SSRPage;
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/server/page/respond.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { respond_with_error } from './respond_with_error.js';

/**
* @param {{
* request: import('types/endpoint').ServerRequest;
* request: import('types/hooks').ServerRequest;
* options: import('types/internal').SSRRenderOptions;
* state: import('types/internal').SSRRenderState;
* $session: any;
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/server/page/respond_with_error.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { load_node } from './load_node.js';

/**
* @param {{
* request: import('types/endpoint').ServerRequest;
* request: import('types/hooks').ServerRequest;
* options: import('types/internal').SSRRenderOptions;
* state: import('types/internal').SSRRenderState;
* $session: any;
Expand Down
19 changes: 13 additions & 6 deletions packages/kit/types/ambient-modules.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ declare module '$app/navigation' {
*/
export function invalidate(href: string): Promise<any>;
/**
* 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.
* Programmatically prefetches the given page, which means
* 1. ensuring that the code for the page is loaded, and
* 2. calling the page's load function with the appropriate options.
*
* This is the same behaviour that SvelteKit triggers when the user taps or mouses over an `<a>` element with `sveltekit:prefetch`.
* If the next navigation is to `href`, the values returned from load will be used, making navigation instantaneous.
* Returns a Promise that resolves when the prefetch is complete.
Expand All @@ -45,8 +48,11 @@ declare module '$app/navigation' {
/**
* Programmatically prefetches the code for routes that haven't yet been fetched.
* Typically, you might call this to speed up subsequent navigation.
* 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`)
* or `/blog/*` (to match `src/routes/blog/[slug].svelte`). Unlike prefetch, this won't call preload for individual pages.
*
* 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`) or `/blog/*` (to match `src/routes/blog/[slug].svelte`).
*
* Unlike prefetch, this won't call preload for individual pages.
* Returns a Promise that resolves when the routes have been prefetched.
*/
export function prefetchRoutes(routes?: string[]): Promise<any>;
Expand All @@ -65,14 +71,15 @@ declare module '$app/paths' {

declare module '$app/stores' {
import { Readable, Writable } from 'svelte/store';
import { Page } from '@sveltejs/kit';
type Page = import('@sveltejs/kit').Page;
type Navigating = { from: Page; to: Page };

/**
* A convenience function around `getContext` that returns `{ navigating, page, session }`.
* Most of the time, you won't need to use it.
*/
export function getStores(): {
navigating: Readable<{ from: Page; to: Page } | null>;
navigating: Readable<Navigating | null>;
page: Readable<Page>;
session: Writable<any>;
};
Expand All @@ -85,7 +92,7 @@ declare module '$app/stores' {
* When navigating starts, its value is `{ from, to }`, where from and to both mirror the page store value.
* When navigating finishes, its value reverts to `null`.
*/
export const navigating: Readable<{ from: Page; to: Page } | null>;
export const navigating: Readable<Navigating | null>;
/**
* A writable store whose initial value is whatever was returned from `getSession`.
* It can be written to, but this will not cause changes to persist on the server — this is something you must implement yourself.
Expand Down
17 changes: 3 additions & 14 deletions packages/kit/types/endpoint.d.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
import { Headers, ParameterizedBody } from './helper';

export type ServerRequest<Locals = Record<string, any>, Body = unknown> = {
method: string;
host: string;
headers: Headers;
path: string;
params: Record<string, string>;
query: URLSearchParams;
rawBody: string | Uint8Array;
body: ParameterizedBody<Body>;
locals: Locals;
};
import { ServerRequest } from './hooks';
import { Headers } from './helper';

type JSONValue =
| string
Expand All @@ -24,7 +13,7 @@ type JSONValue =
export type EndpointOutput = {
status?: number;
headers?: Partial<Headers>;
body?: string | Uint8Array | JSONValue;
body?: JSONValue | Uint8Array;
};

export type RequestHandler<Locals = Record<string, any>, Body = unknown> = (
Expand Down
9 changes: 8 additions & 1 deletion packages/kit/types/helper.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface ReadOnlyFormData extends Iterator<[string, string]> {
values: () => Iterator<string>;
}

export type BaseBody = string | Buffer | ReadOnlyFormData;
type BaseBody = string | Buffer | ReadOnlyFormData;
export type ParameterizedBody<Body = unknown> = Body extends FormData
? ReadOnlyFormData
: BaseBody & Body;
Expand All @@ -17,3 +17,10 @@ export type ParameterizedBody<Body = unknown> = Body extends FormData
// 'set-cookie' is a `string[]` (or at least `string | string[]`)
// but this can't happen until TypeScript 4.3
export type Headers = Record<string, string>;

export type Location = {
host: string;
path: string;
params: Record<string, string>;
query: URLSearchParams;
};
24 changes: 15 additions & 9 deletions packages/kit/types/hooks.d.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import { BaseBody, Headers } from './helper';
import { ServerRequest } from './endpoint';
import { Headers, Location, ParameterizedBody } from './helper';

export type Incoming = {
export type StrictBody = string | Uint8Array;

export type Incoming = Omit<Location, 'params'> & {
method: string;
headers: Headers;
rawBody: StrictBody;
body?: ParameterizedBody;
};

export type ServerRequest<Locals = Record<string, any>, Body = unknown> = Location & {
method: string;
host: string;
headers: Headers;
path: string;
query: URLSearchParams;
rawBody: string | Uint8Array;
body?: BaseBody;
rawBody: StrictBody;
body: ParameterizedBody<Body>;
locals: Locals;
};

export type ServerResponse = {
status: number;
headers: Headers;
body?: string | Uint8Array;
body?: StrictBody;
};

export type GetSession<Locals = Record<string, any>, Session = any> = {
Expand Down
10 changes: 8 additions & 2 deletions packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
import './ambient-modules';

export { Adapter, AdapterUtils, Config } from './config';
export { EndpointOutput, RequestHandler } from './endpoint';
export { ErrorLoad, Load, Page } from './page';
export { Incoming, GetSession, Handle, ServerResponse as Response } from './hooks';
export { ServerRequest as Request, EndpointOutput, RequestHandler } from './endpoint';
export {
Incoming,
GetSession,
Handle,
ServerRequest as Request,
ServerResponse as Response
} from './hooks';
9 changes: 3 additions & 6 deletions packages/kit/types/page.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Location as Page } from './helper';

export type LoadInput = {
page: Page;
fetch: (info: RequestInfo, init?: RequestInit) => Promise<Response>;
Expand All @@ -22,9 +24,4 @@ export type LoadOutput = {
/* Publicized Types */
export type Load = (input: LoadInput) => LoadOutput | Promise<LoadOutput>;
export type ErrorLoad = (input: ErrorLoadInput) => LoadOutput | Promise<LoadOutput>;
export type Page = {
host: string;
path: string;
params: Record<string, string>;
query: URLSearchParams;
};
export { Page };