|
1 | 1 | import { write_if_changed } from './utils.js'; |
2 | 2 |
|
| 3 | +/** @param {string} imports */ |
| 4 | +const header = (imports) => ` |
| 5 | +// this file is auto-generated |
| 6 | +import type { ${imports} } from '@sveltejs/kit';`; |
| 7 | + |
| 8 | +/** @param {string} arg */ |
| 9 | +const endpoint = (arg) => ` |
| 10 | +export type RequestHandler<Output extends ResponseBody = ResponseBody> = GenericRequestHandler<${arg}, Output>;`; |
| 11 | + |
| 12 | +/** @param {string} arg */ |
| 13 | +const page = (arg) => ` |
| 14 | +export type Load< |
| 15 | + InputProps extends Record<string, any> = Record<string, any>, |
| 16 | + OutputProps extends Record<string, any> = InputProps |
| 17 | +> = GenericLoad<${arg}, InputProps, OutputProps>;`; |
| 18 | + |
3 | 19 | /** |
4 | 20 | * @param {import('types').ValidatedConfig} config |
5 | 21 | * @param {import('types').ManifestData} manifest_data |
@@ -53,24 +69,24 @@ export function write_types(config, manifest_data) { |
53 | 69 | const arg = |
54 | 70 | params.length > 0 ? `{ ${params.map((param) => `${param}: string`).join('; ')} }` : '{}'; |
55 | 71 |
|
56 | | - const imports = [ |
57 | | - type !== 'page' && 'RequestHandler as GenericRequestHandler', |
58 | | - type !== 'endpoint' && 'Load as GenericLoad' |
59 | | - ] |
60 | | - .filter(Boolean) |
61 | | - .join(', '); |
62 | | - |
63 | | - const file = `${config.kit.outDir}/types/${key || 'index'}.d.ts`; |
64 | | - const content = [ |
65 | | - '// this file is auto-generated', |
66 | | - `import type { ${imports} } from '@sveltejs/kit';`, |
67 | | - type !== 'page' && `export type RequestHandler = GenericRequestHandler<${arg}>;`, |
68 | | - type !== 'endpoint' && |
69 | | - `export type Load<Props = Record<string, any>> = GenericLoad<${arg}, Props>;` |
70 | | - ] |
71 | | - .filter(Boolean) |
72 | | - .join('\n'); |
73 | | - |
74 | | - write_if_changed(file, content); |
| 72 | + const imports = []; |
| 73 | + const content = []; |
| 74 | + |
| 75 | + if (type !== 'page') { |
| 76 | + imports.push('RequestHandler as GenericRequestHandler, ResponseBody'); |
| 77 | + content.push(endpoint(arg)); |
| 78 | + } |
| 79 | + |
| 80 | + if (type !== 'endpoint') { |
| 81 | + imports.push('Load as GenericLoad'); |
| 82 | + content.push(page(arg)); |
| 83 | + } |
| 84 | + |
| 85 | + content.unshift(header(imports.join(', '))); |
| 86 | + |
| 87 | + write_if_changed( |
| 88 | + `${config.kit.outDir}/types/${key || 'index'}.d.ts`, |
| 89 | + content.join('\n').trim() |
| 90 | + ); |
75 | 91 | }); |
76 | 92 | } |
0 commit comments