Skip to content

Commit

Permalink
[chore] separate RequestHeaders and ResponseHeaders types
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Aug 19, 2021
1 parent 23ff8b5 commit ead8eee
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/empty-donuts-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[chore] separate RequestHeaders and ResponseHeaders types
2 changes: 1 addition & 1 deletion packages/kit/src/core/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ async function create_handler(vite, config, dir, cwd, get_manifest) {

const rendered = await respond(
{
headers: /** @type {import('types/helper').Headers} */ (req.headers),
headers: /** @type {import('types/helper').RequestHeaders} */ (req.headers),
method: req.method,
host,
path: parsed.pathname.replace(config.kit.paths.base, ''),
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/core/preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export async function preview({
host: /** @type {string} */ (config.kit.host ||
req.headers[config.kit.hostHeader || 'host']),
method: req.method,
headers: /** @type {import('types/helper').Headers} */ (req.headers),
headers: /** @type {import('types/helper').RequestHeaders} */ (req.headers),
path: parsed.pathname.replace(config.kit.paths.base, ''),
query: parsed.searchParams,
rawBody: body
Expand Down
3 changes: 2 additions & 1 deletion packages/kit/src/runtime/server/endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ export async function render_endpoint(request, route, match) {
!(body instanceof Uint8Array) &&
(!type || type.startsWith('application/json'))
) {
headers = { ...headers, 'content-type': 'application/json; charset=utf-8' };
headers = { ...headers };
headers['content-type'] = 'application/json; charset=utf-8';
normalized_body = JSON.stringify(typeof body === 'undefined' ? {} : body);
} else {
normalized_body = /** @type {import('types/hooks').StrictBody} */ (body);
Expand Down
6 changes: 4 additions & 2 deletions packages/kit/src/runtime/server/page/load_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ export async function load_node({
} else if (resolved.startsWith('/') && !resolved.startsWith('//')) {
const relative = resolved;

const headers = /** @type {import('types/helper').Headers} */ ({ ...opts.headers });
const headers = /** @type {import('types/helper').RequestHeaders} */ ({
...opts.headers
});

// TODO: fix type https://github.com/node-fetch/node-fetch/issues/1113
if (opts.credentials !== 'omit') {
Expand Down Expand Up @@ -211,7 +213,7 @@ export async function load_node({
async function text() {
const body = await response.text();

/** @type {import('types/helper').Headers} */
/** @type {import('types/helper').ResponseHeaders} */
const headers = {};
for (const [key, value] of response.headers) {
if (key !== 'etag' && key !== 'set-cookie') headers[key] = value;
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/server/page/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export async function render_response({
.join('\n\n\t\t\t')}
`.replace(/^\t{2}/gm, '');

/** @type {import('types/helper').Headers} */
/** @type {import('types/helper').ResponseHeaders} */
const headers = {
'content-type': 'text/html'
};
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/server/parse_body/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { read_only_form_data } from './read_only_form_data.js';

/**
* @param {import('types/helper').RawBody} raw
* @param {import('types/helper').Headers} headers
* @param {import('types/helper').RequestHeaders} headers
*/
export function parse_body(raw, headers) {
if (!raw) return raw;
Expand Down
4 changes: 2 additions & 2 deletions packages/kit/types/endpoint.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ServerRequest } from './hooks';
import { Headers, MaybePromise } from './helper';
import { MaybePromise, ResponseHeaders } from './helper';

type ToJSON = { toJSON(...args: any[]): JSONValue };
type JSONValue = Exclude<JSONResponse, ToJSON>;
Expand All @@ -16,7 +16,7 @@ type DefaultBody = JSONResponse | Uint8Array;

export interface EndpointOutput<Body extends DefaultBody = DefaultBody> {
status?: number;
headers?: Headers;
headers?: ResponseHeaders;
body?: Body;
}

Expand Down
10 changes: 5 additions & 5 deletions packages/kit/types/helper.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ export type ParameterizedBody<Body = unknown> = Body extends FormData
? ReadOnlyFormData
: (string | RawBody | ReadOnlyFormData) & Body;

// TODO we want to differentiate between request headers, which
// always follow this type, and response headers, in which
// '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 RequestHeaders = Record<string, string>;

export type ResponseHeaders = Record<string, string> & {
'Set-Cookie'?: string | string[];
};

export type Location<Params extends Record<string, string> = Record<string, string>> = {
host: string;
Expand Down
13 changes: 10 additions & 3 deletions packages/kit/types/hooks.d.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import { Headers, Location, MaybePromise, ParameterizedBody, RawBody } from './helper';
import {
Location,
MaybePromise,
ParameterizedBody,
RawBody,
RequestHeaders,
ResponseHeaders
} from './helper';

export type StrictBody = string | Uint8Array;

export interface ServerRequest<Locals = Record<string, any>, Body = unknown> extends Location {
method: string;
headers: Headers;
headers: RequestHeaders;
rawBody: RawBody;
body: ParameterizedBody<Body>;
locals: Locals;
}

export interface ServerResponse {
status: number;
headers: Headers;
headers: ResponseHeaders;
body?: StrictBody;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/kit/types/internal.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RequestHandler } from './endpoint';
import { Headers, Location, ParameterizedBody, RawBody } from './helper';
import { Location, ParameterizedBody, RawBody, RequestHeaders } from './helper';
import {
ExternalFetch,
GetSession,
Expand All @@ -14,7 +14,7 @@ type PageId = string;

export interface Incoming extends Omit<Location, 'params'> {
method: string;
headers: Headers;
headers: RequestHeaders;
rawBody: RawBody;
body?: ParameterizedBody;
}
Expand Down

0 comments on commit ead8eee

Please sign in to comment.