Skip to content

Commit 3b79bcc

Browse files
committed
Path generation for pages; path checking of redirect, goto, fetch; fetch type safety when fetching from endpoints
1 parent 27c93d4 commit 3b79bcc

File tree

28 files changed

+482
-173
lines changed

28 files changed

+482
-173
lines changed

.changeset/afraid-cougars-return.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': minor
3+
---
4+
5+
feat: typed responses when fetching from endpoints

packages/kit/src/core/sync/sync.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { write_tsconfig } from './write_tsconfig.js';
66
import { write_types, write_all_types } from './write_types/index.js';
77
import { write_ambient } from './write_ambient.js';
88
import { write_server } from './write_server.js';
9-
import { write_api } from './write_api.js';
109

1110
/**
1211
* Initialize SvelteKit's generated files.
@@ -31,7 +30,6 @@ export async function create(config) {
3130
write_server(config, output);
3231
write_root(manifest_data, output);
3332
await write_all_types(config, manifest_data);
34-
write_api(config, manifest_data);
3533

3634
return { manifest_data };
3735
}
@@ -46,7 +44,6 @@ export async function create(config) {
4644
*/
4745
export async function update(config, manifest_data, file) {
4846
await write_types(config, manifest_data, file);
49-
write_api(config, manifest_data);
5047

5148
return { manifest_data };
5249
}
@@ -70,7 +67,6 @@ export async function all_types(config, mode) {
7067
init(config, mode);
7168
const manifest_data = create_manifest_data({ config });
7269
await write_all_types(config, manifest_data);
73-
write_api(config, manifest_data);
7470
}
7571

7672
/**

packages/kit/src/core/sync/write_api.js

Lines changed: 0 additions & 152 deletions
This file was deleted.

packages/kit/src/core/sync/write_tsconfig.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export function get_tsconfig(kit, include_base_url) {
8888
const include = new Set([
8989
'ambient.d.ts',
9090
'./types/**/$types.d.ts',
91+
'./types/$api.d.ts',
9192
config_relative('vite.config.js'),
9293
config_relative('vite.config.ts')
9394
]);

packages/kit/src/core/sync/write_tsconfig.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ test('Creates tsconfig include from kit.files', () => {
9999
expect(include).toEqual([
100100
'ambient.d.ts',
101101
'./types/**/$types.d.ts',
102+
'./types/$api.d.ts',
102103
'../vite.config.js',
103104
'../vite.config.ts',
104105
'../app/**/*.js',

packages/kit/src/core/sync/write_types/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import MagicString from 'magic-string';
44
import { posixify, rimraf, walk } from '../../../utils/filesystem.js';
55
import { compact } from '../../../utils/array.js';
66
import { ts } from '../ts.js';
7+
import { write_api } from './write_api/index.js';
78

89
/**
910
* @typedef {{
@@ -123,6 +124,8 @@ export async function write_all_types(config, manifest_data) {
123124
}
124125
}
125126

127+
write_api(config, manifest_data);
128+
126129
fs.writeFileSync(meta_data_file, JSON.stringify(meta_data, null, '\t'));
127130
}
128131

@@ -148,6 +151,7 @@ export async function write_types(config, manifest_data, file) {
148151
if (!route.leaf && !route.layout && !route.endpoint) return; // nothing to do
149152

150153
update_types(config, create_routes_map(manifest_data), route);
154+
write_api(config, manifest_data);
151155
}
152156

153157
/**
@@ -238,7 +242,7 @@ function update_types(config, routes, route, to_delete = new Set()) {
238242
const api_types_path = posixify(
239243
path.relative(outdir, path.join(config.kit.outDir, 'types', '$api')) // TODO: potential failure point if api file is moved
240244
);
241-
declarations.push(`type FetchType = typeof import('${api_types_path}').TypedFetch;`);
245+
declarations.push(`type FetchType = typeof import('${api_types_path}').fetch;`);
242246

243247
if (route.leaf) {
244248
let route_info = routes.get(route.leaf);
@@ -340,7 +344,9 @@ function update_types(config, routes, route, to_delete = new Set()) {
340344
}
341345

342346
if (route.endpoint) {
343-
exports.push('export type RequestHandler = Kit.RequestHandler<RouteParams, RouteId>;');
347+
exports.push(
348+
'export type RequestHandler = Kit.RequestHandler<RouteParams, RouteId, FetchType>;'
349+
);
344350
}
345351

346352
if (route.leaf?.server || route.layout?.server || route.endpoint) {

packages/kit/src/core/sync/write_types/test/tsconfig.json renamed to packages/kit/src/core/sync/write_types/test/actions/tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
"allowSyntheticDefaultImports": true,
1111
"baseUrl": ".",
1212
"paths": {
13-
"@sveltejs/kit": ["../../../../exports/public"],
14-
"types": ["../../../../types/internal"]
13+
"@sveltejs/kit": ["../../../../../exports/public"],
14+
"types": ["../../../../../types/internal"]
1515
}
1616
},
1717
"include": ["./**/*.js"],
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compilerOptions": {
3+
"allowJs": true,
4+
"checkJs": true,
5+
"noEmit": true,
6+
"strict": true,
7+
"target": "es2020",
8+
"module": "es2022",
9+
"moduleResolution": "node",
10+
"allowSyntheticDefaultImports": true,
11+
"baseUrl": ".",
12+
"paths": {
13+
"@sveltejs/kit": ["../../../../../exports/public"],
14+
"types": ["../../../../../types/internal"]
15+
}
16+
},
17+
"include": ["./**/*.js"],
18+
"exclude": ["./**/.svelte-kit/**"]
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compilerOptions": {
3+
"allowJs": true,
4+
"checkJs": true,
5+
"noEmit": true,
6+
"strict": true,
7+
"target": "es2020",
8+
"module": "es2022",
9+
"moduleResolution": "node",
10+
"allowSyntheticDefaultImports": true,
11+
"baseUrl": ".",
12+
"paths": {
13+
"@sveltejs/kit": ["../../../../../exports/public"],
14+
"types": ["../../../../../types/internal"]
15+
}
16+
},
17+
"include": ["./**/*.js"],
18+
"exclude": ["./**/.svelte-kit/**"]
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compilerOptions": {
3+
"allowJs": true,
4+
"checkJs": true,
5+
"noEmit": true,
6+
"strict": true,
7+
"target": "es2020",
8+
"module": "es2022",
9+
"moduleResolution": "node",
10+
"allowSyntheticDefaultImports": true,
11+
"baseUrl": ".",
12+
"paths": {
13+
"@sveltejs/kit": ["../../../../../exports/public"],
14+
"types": ["../../../../../types/internal"]
15+
}
16+
},
17+
"include": ["./**/*.js"],
18+
"exclude": ["./**/.svelte-kit/**"]
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compilerOptions": {
3+
"allowJs": true,
4+
"checkJs": true,
5+
"noEmit": true,
6+
"strict": true,
7+
"target": "es2020",
8+
"module": "es2022",
9+
"moduleResolution": "node",
10+
"allowSyntheticDefaultImports": true,
11+
"baseUrl": ".",
12+
"paths": {
13+
"@sveltejs/kit": ["../../../../../exports/public"],
14+
"types": ["../../../../../types/internal"]
15+
}
16+
},
17+
"include": ["./**/*.js"],
18+
"exclude": ["./**/.svelte-kit/**"]
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compilerOptions": {
3+
"allowJs": true,
4+
"checkJs": true,
5+
"noEmit": true,
6+
"strict": true,
7+
"target": "es2020",
8+
"module": "es2022",
9+
"moduleResolution": "node",
10+
"allowSyntheticDefaultImports": true,
11+
"baseUrl": ".",
12+
"paths": {
13+
"@sveltejs/kit": ["../../../../../exports/public"],
14+
"types": ["../../../../../types/internal"]
15+
}
16+
},
17+
"include": ["./**/*.js"],
18+
"exclude": ["./**/.svelte-kit/**"]
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compilerOptions": {
3+
"allowJs": true,
4+
"checkJs": true,
5+
"noEmit": true,
6+
"strict": true,
7+
"target": "es2020",
8+
"module": "es2022",
9+
"moduleResolution": "node",
10+
"allowSyntheticDefaultImports": true,
11+
"baseUrl": ".",
12+
"paths": {
13+
"@sveltejs/kit": ["../../../../../exports/public"],
14+
"types": ["../../../../../types/internal"]
15+
}
16+
},
17+
"include": ["./**/*.js"],
18+
"exclude": ["./**/.svelte-kit/**"]
19+
}

0 commit comments

Comments
 (0)