@@ -167,55 +167,21 @@ function upserKitRouteFile(
167167 insert ( pos , inserted ) ;
168168 }
169169
170- // add type to prerender variable if not explicitly typed
171- const prerender = exports . get ( 'prerender' ) ;
172- if ( prerender ?. type === 'var' && ! prerender . hasTypeDefinition && prerender . node . initializer ) {
173- const pos = prerender . node . name . getEnd ( ) ;
174- const inserted = surround ( ` : boolean | 'auto'` ) ;
175- insert ( pos , inserted ) ;
176- }
177-
178- // add type to trailingSlash variable if not explicitly typed
179- const trailingSlash = exports . get ( 'trailingSlash' ) ;
180- if (
181- trailingSlash ?. type === 'var' &&
182- ! trailingSlash . hasTypeDefinition &&
183- trailingSlash . node . initializer
184- ) {
185- const pos = trailingSlash . node . name . getEnd ( ) ;
186- const inserted = surround ( ` : 'never' | 'always' | 'ignore'` ) ;
187- insert ( pos , inserted ) ;
188- }
189-
190- // add type to ssr variable if not explicitly typed
191- const ssr = exports . get ( 'ssr' ) ;
192- if ( ssr ?. type === 'var' && ! ssr . hasTypeDefinition && ssr . node . initializer ) {
193- const pos = ssr . node . name . getEnd ( ) ;
194- const inserted = surround ( ` : boolean` ) ;
195- insert ( pos , inserted ) ;
196- }
197-
198- // add type to csr variable if not explicitly typed
199- const csr = exports . get ( 'csr' ) ;
200- if ( csr ?. type === 'var' && ! csr . hasTypeDefinition && csr . node . initializer ) {
201- const pos = csr . node . name . getEnd ( ) ;
202- const inserted = surround ( ` : boolean` ) ;
203- insert ( pos , inserted ) ;
204- }
170+ addTypeToVariable ( exports , surround , insert , 'prerender' , `boolean | 'auto'` ) ;
171+ addTypeToVariable ( exports , surround , insert , 'trailingSlash' , `'never' | 'always' | 'ignore'` ) ;
172+ addTypeToVariable ( exports , surround , insert , 'ssr' , `boolean` ) ;
173+ addTypeToVariable ( exports , surround , insert , 'csr' , `boolean` ) ;
205174
206175 // add types to GET/PUT/POST/PATCH/DELETE/OPTIONS if not explicitly typed
207176 const insertApiMethod = ( name : string ) => {
208- const api = exports . get ( name ) ;
209- if (
210- api ?. type === 'function' &&
211- api . node . parameters . length === 1 &&
212- ! api . hasTypeDefinition
213- ) {
214- const pos = api . node . parameters [ 0 ] . getEnd ( ) ;
215- const inserted = surround ( `: import('./$types').RequestEvent` ) ;
216-
217- insert ( pos , inserted ) ;
218- }
177+ addTypeToFunction (
178+ exports ,
179+ surround ,
180+ insert ,
181+ name ,
182+ `import('./$types').RequestEvent` ,
183+ `Response | Promise<Response>`
184+ ) ;
219185 } ;
220186 insertApiMethod ( 'GET' ) ;
221187 insertApiMethod ( 'PUT' ) ;
@@ -249,22 +215,7 @@ function upserKitParamsFile(
249215 const isTsFile = basename . endsWith ( '.ts' ) ;
250216 const exports = findExports ( source , isTsFile ) ;
251217
252- // add type to match function if not explicitly typed
253- const match = exports . get ( 'match' ) ;
254- if (
255- match ?. type === 'function' &&
256- match . node . parameters . length === 1 &&
257- ! match . hasTypeDefinition
258- ) {
259- const pos = match . node . parameters [ 0 ] . getEnd ( ) ;
260- const inserted = surround ( `: string` ) ;
261- insert ( pos , inserted ) ;
262- if ( ! match . node . type && match . node . body ) {
263- const returnPos = match . node . body . getStart ( ) ;
264- const returnInsertion = surround ( `: boolean` ) ;
265- insert ( returnPos , returnInsertion ) ;
266- }
267- }
218+ addTypeToFunction ( exports , surround , insert , 'match' , 'string' , 'boolean' ) ;
268219
269220 return { addedCode, originalText : source . getFullText ( ) } ;
270221}
@@ -291,26 +242,13 @@ function upserKitClientHooksFile(
291242 const isTsFile = basename . endsWith ( '.ts' ) ;
292243 const exports = findExports ( source , isTsFile ) ;
293244
294- // add type to handleError function if not explicitly typed
295- const handleError = exports . get ( 'handleError' ) ;
296- if (
297- handleError ?. type === 'function' &&
298- handleError . node . parameters . length === 1 &&
299- ! handleError . hasTypeDefinition
300- ) {
301- const paramPos = handleError . node . parameters [ 0 ] . getEnd ( ) ;
302- const paramInsertion = surround (
303- `: Parameters<import('@sveltejs/kit').HandleClientError>[0]`
304- ) ;
305- insert ( paramPos , paramInsertion ) ;
306- if ( ! handleError . node . type && handleError . node . body ) {
307- const returnPos = handleError . node . body . getStart ( ) ;
308- const returnInsertion = surround (
309- `: ReturnType<import('@sveltejs/kit').HandleClientError>`
310- ) ;
311- insert ( returnPos , returnInsertion ) ;
312- }
313- }
245+ addTypeToFunction (
246+ exports ,
247+ surround ,
248+ insert ,
249+ 'handleError' ,
250+ `import('@sveltejs/kit').HandleClientError`
251+ ) ;
314252
315253 return { addedCode, originalText : source . getFullText ( ) } ;
316254}
@@ -337,27 +275,71 @@ function upserKitServerHooksFile(
337275 const isTsFile = basename . endsWith ( '.ts' ) ;
338276 const exports = findExports ( source , isTsFile ) ;
339277
340- const addTypeToFunction = ( name : string , type : string ) => {
341- const fn = exports . get ( name ) ;
342- if ( fn ?. type === 'function' && fn . node . parameters . length === 1 && ! fn . hasTypeDefinition ) {
343- const paramPos = fn . node . parameters [ 0 ] . getEnd ( ) ;
344- const paramInsertion = surround ( `: Parameters<${ type } >[0]` ) ;
345- insert ( paramPos , paramInsertion ) ;
346- if ( ! fn . node . type && fn . node . body ) {
347- const returnPos = fn . node . body . getStart ( ) ;
348- const returnInsertion = surround ( `: ReturnType<${ type } >` ) ;
349- insert ( returnPos , returnInsertion ) ;
350- }
351- }
278+ const addType = ( name : string , type : string ) => {
279+ addTypeToFunction ( exports , surround , insert , name , type ) ;
352280 } ;
353281
354- addTypeToFunction ( 'handleError' , `import('@sveltejs/kit').HandleServerError` ) ;
355- addTypeToFunction ( 'handle' , `import('@sveltejs/kit').Handle` ) ;
356- addTypeToFunction ( 'handleFetch' , `import('@sveltejs/kit').HandleFetch` ) ;
282+ addType ( 'handleError' , `import('@sveltejs/kit').HandleServerError` ) ;
283+ addType ( 'handle' , `import('@sveltejs/kit').Handle` ) ;
284+ addType ( 'handleFetch' , `import('@sveltejs/kit').HandleFetch` ) ;
357285
358286 return { addedCode, originalText : source . getFullText ( ) } ;
359287}
360288
289+ function addTypeToVariable (
290+ exports : Map <
291+ string ,
292+ | {
293+ type : 'function' ;
294+ node : ts . FunctionDeclaration | ts . ArrowFunction | ts . FunctionExpression ;
295+ hasTypeDefinition : boolean ;
296+ }
297+ | { type : 'var' ; node : ts . VariableDeclaration ; hasTypeDefinition : boolean }
298+ > ,
299+ surround : ( text : string ) => string ,
300+ insert : ( pos : number , inserted : string ) => void ,
301+ name : string ,
302+ type : string
303+ ) {
304+ const variable = exports . get ( name ) ;
305+ if ( variable ?. type === 'var' && ! variable . hasTypeDefinition && variable . node . initializer ) {
306+ const pos = variable . node . name . getEnd ( ) ;
307+ const inserted = surround ( ` : ${ type } ` ) ;
308+ insert ( pos , inserted ) ;
309+ }
310+ }
311+
312+ function addTypeToFunction (
313+ exports : Map <
314+ string ,
315+ | {
316+ type : 'function' ;
317+ node : ts . FunctionDeclaration | ts . ArrowFunction | ts . FunctionExpression ;
318+ hasTypeDefinition : boolean ;
319+ }
320+ | { type : 'var' ; node : ts . VariableDeclaration ; hasTypeDefinition : boolean }
321+ > ,
322+ surround : ( text : string ) => string ,
323+ insert : ( pos : number , inserted : string ) => void ,
324+ name : string ,
325+ type : string ,
326+ returnType ?: string
327+ ) {
328+ const fn = exports . get ( name ) ;
329+ if ( fn ?. type === 'function' && fn . node . parameters . length === 1 && ! fn . hasTypeDefinition ) {
330+ const paramPos = fn . node . parameters [ 0 ] . getEnd ( ) ;
331+ const paramInsertion = surround ( ! returnType ? `: Parameters<${ type } >[0]` : `: ${ type } ` ) ;
332+ insert ( paramPos , paramInsertion ) ;
333+ if ( ! fn . node . type && fn . node . body ) {
334+ const returnPos = fn . node . body . getStart ( ) ;
335+ const returnInsertion = surround (
336+ ! returnType ? `: ReturnType<${ type } >` : `: ${ returnType } `
337+ ) ;
338+ insert ( returnPos , returnInsertion ) ;
339+ }
340+ }
341+ }
342+
361343function insertCode ( addedCode : AddedCode [ ] , pos : number , inserted : string ) {
362344 const insertionIdx = addedCode . findIndex ( ( c ) => c . originalPos > pos ) ;
363345 if ( insertionIdx >= 0 ) {
0 commit comments