From eb96322f25b08af8e0f814a825a5db28c6794f50 Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Wed, 26 May 2021 22:49:49 +0700 Subject: [PATCH] types: group related and reduce potential inconsistencies (#1539) --- packages/kit/src/core/node/index.js | 2 +- packages/kit/src/runtime/hash.js | 2 +- packages/kit/src/runtime/server/endpoint.js | 6 ++--- packages/kit/src/runtime/server/page/index.js | 2 +- .../kit/src/runtime/server/page/load_node.js | 2 +- .../kit/src/runtime/server/page/respond.js | 2 +- .../runtime/server/page/respond_with_error.js | 2 +- packages/kit/types/ambient-modules.d.ts | 19 ++++++++++----- packages/kit/types/endpoint.d.ts | 17 +++---------- packages/kit/types/helper.d.ts | 9 ++++++- packages/kit/types/hooks.d.ts | 24 ++++++++++++------- packages/kit/types/index.d.ts | 10 ++++++-- packages/kit/types/page.d.ts | 9 +++---- 13 files changed, 59 insertions(+), 47 deletions(-) diff --git a/packages/kit/src/core/node/index.js b/packages/kit/src/core/node/index.js index 21e6de345f7a..1f01280eb936 100644 --- a/packages/kit/src/core/node/index.js +++ b/packages/kit/src/core/node/index.js @@ -1,6 +1,6 @@ /** * @param {import('http').IncomingMessage} req - * @returns {Promise} + * @returns {Promise} */ export function getRawBody(req) { return new Promise((fulfil, reject) => { diff --git a/packages/kit/src/runtime/hash.js b/packages/kit/src/runtime/hash.js index d88fa0a56f50..0b50be42c5f8 100644 --- a/packages/kit/src/runtime/hash.js +++ b/packages/kit/src/runtime/hash.js @@ -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; diff --git a/packages/kit/src/runtime/server/endpoint.js b/packages/kit/src/runtime/server/endpoint.js index 4bd9daeac7e1..155e81c182f0 100644 --- a/packages/kit/src/runtime/server/endpoint.js +++ b/packages/kit/src/runtime/server/endpoint.js @@ -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} */ @@ -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 }; diff --git a/packages/kit/src/runtime/server/page/index.js b/packages/kit/src/runtime/server/page/index.js index df62567e1a6d..d14dd8b09a90 100644 --- a/packages/kit/src/runtime/server/page/index.js +++ b/packages/kit/src/runtime/server/page/index.js @@ -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 diff --git a/packages/kit/src/runtime/server/page/load_node.js b/packages/kit/src/runtime/server/page/load_node.js index 0c728e6df4e5..053905ee30ec 100644 --- a/packages/kit/src/runtime/server/page/load_node.js +++ b/packages/kit/src/runtime/server/page/load_node.js @@ -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; diff --git a/packages/kit/src/runtime/server/page/respond.js b/packages/kit/src/runtime/server/page/respond.js index d4b39b0e62bb..29810c25899f 100644 --- a/packages/kit/src/runtime/server/page/respond.js +++ b/packages/kit/src/runtime/server/page/respond.js @@ -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; diff --git a/packages/kit/src/runtime/server/page/respond_with_error.js b/packages/kit/src/runtime/server/page/respond_with_error.js index beb06728a908..2126e6a41b1c 100644 --- a/packages/kit/src/runtime/server/page/respond_with_error.js +++ b/packages/kit/src/runtime/server/page/respond_with_error.js @@ -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; diff --git a/packages/kit/types/ambient-modules.d.ts b/packages/kit/types/ambient-modules.d.ts index 9e52c1f250bf..1683677b0408 100644 --- a/packages/kit/types/ambient-modules.d.ts +++ b/packages/kit/types/ambient-modules.d.ts @@ -34,7 +34,10 @@ declare module '$app/navigation' { */ export function invalidate(href: string): Promise; /** - * 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 `` 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. @@ -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; @@ -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; page: Readable; session: Writable; }; @@ -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; /** * 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. diff --git a/packages/kit/types/endpoint.d.ts b/packages/kit/types/endpoint.d.ts index 3c6d5586ce43..91ab6296a0f5 100644 --- a/packages/kit/types/endpoint.d.ts +++ b/packages/kit/types/endpoint.d.ts @@ -1,16 +1,5 @@ -import { Headers, ParameterizedBody } from './helper'; - -export type ServerRequest, Body = unknown> = { - method: string; - host: string; - headers: Headers; - path: string; - params: Record; - query: URLSearchParams; - rawBody: string | Uint8Array; - body: ParameterizedBody; - locals: Locals; -}; +import { ServerRequest } from './hooks'; +import { Headers } from './helper'; type JSONValue = | string @@ -24,7 +13,7 @@ type JSONValue = export type EndpointOutput = { status?: number; headers?: Partial; - body?: string | Uint8Array | JSONValue; + body?: JSONValue | Uint8Array; }; export type RequestHandler, Body = unknown> = ( diff --git a/packages/kit/types/helper.d.ts b/packages/kit/types/helper.d.ts index fde3a82f4897..764dc7c9d6f5 100644 --- a/packages/kit/types/helper.d.ts +++ b/packages/kit/types/helper.d.ts @@ -7,7 +7,7 @@ interface ReadOnlyFormData extends Iterator<[string, string]> { values: () => Iterator; } -export type BaseBody = string | Buffer | ReadOnlyFormData; +type BaseBody = string | Buffer | ReadOnlyFormData; export type ParameterizedBody = Body extends FormData ? ReadOnlyFormData : BaseBody & Body; @@ -17,3 +17,10 @@ export type ParameterizedBody = 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; + +export type Location = { + host: string; + path: string; + params: Record; + query: URLSearchParams; +}; diff --git a/packages/kit/types/hooks.d.ts b/packages/kit/types/hooks.d.ts index 15a12c47bed9..e2b91e6b7dbe 100644 --- a/packages/kit/types/hooks.d.ts +++ b/packages/kit/types/hooks.d.ts @@ -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 & { + method: string; + headers: Headers; + rawBody: StrictBody; + body?: ParameterizedBody; +}; + +export type ServerRequest, Body = unknown> = Location & { method: string; - host: string; headers: Headers; - path: string; - query: URLSearchParams; - rawBody: string | Uint8Array; - body?: BaseBody; + rawBody: StrictBody; + body: ParameterizedBody; + locals: Locals; }; export type ServerResponse = { status: number; headers: Headers; - body?: string | Uint8Array; + body?: StrictBody; }; export type GetSession, Session = any> = { diff --git a/packages/kit/types/index.d.ts b/packages/kit/types/index.d.ts index 832cbdb40fed..7499bc8dd712 100644 --- a/packages/kit/types/index.d.ts +++ b/packages/kit/types/index.d.ts @@ -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'; diff --git a/packages/kit/types/page.d.ts b/packages/kit/types/page.d.ts index 967439ddac1b..5b80353cb7c0 100644 --- a/packages/kit/types/page.d.ts +++ b/packages/kit/types/page.d.ts @@ -1,3 +1,5 @@ +import { Location as Page } from './helper'; + export type LoadInput = { page: Page; fetch: (info: RequestInfo, init?: RequestInit) => Promise; @@ -22,9 +24,4 @@ export type LoadOutput = { /* Publicized Types */ export type Load = (input: LoadInput) => LoadOutput | Promise; export type ErrorLoad = (input: ErrorLoadInput) => LoadOutput | Promise; -export type Page = { - host: string; - path: string; - params: Record; - query: URLSearchParams; -}; +export { Page };