@@ -16,6 +16,8 @@ import {
1616 HANDLER_FUNCTION_TITLE ,
1717 ODB_FUNCTION_TITLE ,
1818 IMAGE_FUNCTION_TITLE ,
19+ API_FUNCTION_TITLE ,
20+ API_FUNCTION_NAME ,
1921 LAMBDA_WARNING_SIZE ,
2022} from '../constants'
2123import { getApiHandler } from '../templates/getApiHandler'
@@ -32,6 +34,7 @@ import { getFunctionNameForPage } from './utils'
3234
3335export interface ApiRouteConfig {
3436 functionName : string
37+ functionTitle ?: string
3538 route : string
3639 config : ApiConfig
3740 compiled : string
@@ -40,6 +43,7 @@ export interface ApiRouteConfig {
4043
4144export interface APILambda {
4245 functionName : string
46+ functionTitle : string
4347 routes : ApiRouteConfig [ ]
4448 includedFiles : string [ ]
4549 type ?: ApiRouteType
@@ -61,7 +65,7 @@ export const generateFunctions = async (
6165 : undefined
6266
6367 for ( const apiLambda of apiLambdas ) {
64- const { functionName, routes, type, includedFiles } = apiLambda
68+ const { functionName, functionTitle , routes, type, includedFiles } = apiLambda
6569
6670 const apiHandlerSource = getApiHandler ( {
6771 // most api lambdas serve multiple routes, but scheduled functions need to be in separate lambdas.
@@ -103,6 +107,8 @@ export const generateFunctions = async (
103107 } )
104108 await writeFile ( join ( functionsDir , functionName , 'pages.js' ) , resolverSource )
105109
110+ await writeFunctionConfiguration ( { functionName, functionTitle, functionsDir } )
111+
106112 const nfInternalFiles = await glob ( join ( functionsDir , functionName , '**' ) )
107113 includedFiles . push ( ...nfInternalFiles )
108114 }
@@ -129,7 +135,7 @@ export const generateFunctions = async (
129135 join ( __dirname , '..' , '..' , 'lib' , 'templates' , 'handlerUtils.js' ) ,
130136 join ( functionsDir , functionName , 'handlerUtils.js' ) ,
131137 )
132- writeFunctionConfiguration ( { functionName, functionTitle, functionsDir } )
138+ await writeFunctionConfiguration ( { functionName, functionTitle, functionsDir } )
133139 }
134140
135141 await writeHandler ( HANDLER_FUNCTION_NAME , HANDLER_FUNCTION_TITLE , false )
@@ -333,12 +339,41 @@ export const getAPILambdas = async (
333339
334340 const bins = pack ( weighedRoutes , threshold )
335341
336- return bins . map ( ( bin , index ) => ( {
337- functionName : bin . length === 1 ? bin [ 0 ] . functionName : `api-${ index } ` ,
338- routes : bin ,
339- includedFiles : [ ...commonDependencies , ...routes . flatMap ( ( route ) => route . includedFiles ) ] ,
340- type,
341- } ) )
342+ return bins . map ( ( bin ) => {
343+ if ( bin . length === 1 ) {
344+ const [ func ] = bin
345+ const { functionName, functionTitle, config, includedFiles } = func
346+ return {
347+ functionName,
348+ functionTitle,
349+ routes : [ func ] ,
350+ includedFiles : [ ...commonDependencies , ...includedFiles ] ,
351+ type : config . type ,
352+ }
353+ }
354+
355+ const includedFiles = [ ...commonDependencies , ...bin . flatMap ( ( route ) => route . includedFiles ) ]
356+ const nonSingletonBins = bins . filter ( ( b ) => b . length > 1 )
357+ if ( nonSingletonBins . length === 1 ) {
358+ return {
359+ functionName : API_FUNCTION_NAME ,
360+ functionTitle : API_FUNCTION_TITLE ,
361+ includedFiles,
362+ routes : bin ,
363+ type,
364+ }
365+ }
366+
367+ const indexInNonSingletonBins = nonSingletonBins . indexOf ( bin )
368+
369+ return {
370+ functionName : `${ API_FUNCTION_NAME } -${ indexInNonSingletonBins + 1 } ` ,
371+ functionTitle : `${ API_FUNCTION_TITLE } ${ indexInNonSingletonBins + 1 } /${ nonSingletonBins . length } ` ,
372+ includedFiles,
373+ routes : bin ,
374+ type,
375+ }
376+ } )
342377 }
343378
344379 const standardFunctions = apiRoutes . filter (
@@ -365,7 +400,7 @@ export const getAPILambdas = async (
365400export const getApiRouteConfigs = async (
366401 publish : string ,
367402 appDir : string ,
368- pageExtensions : string [ ] ,
403+ pageExtensions ? : string [ ] ,
369404) : Promise < Array < ApiRouteConfig > > => {
370405 const pages = await readJSON ( join ( publish , 'server' , 'pages-manifest.json' ) )
371406 const apiRoutes = Object . keys ( pages ) . filter ( ( page ) => page . startsWith ( '/api/' ) )
@@ -380,6 +415,7 @@ export const getApiRouteConfigs = async (
380415 const config = await extractConfigFromFile ( filePath , appDir )
381416
382417 const functionName = getFunctionNameForPage ( apiRoute , config . type === ApiRouteType . BACKGROUND )
418+ const functionTitle = `${ API_FUNCTION_TITLE } ${ apiRoute } `
383419
384420 const compiled = pages [ apiRoute ]
385421 const compiledPath = join ( publish , 'server' , compiled )
@@ -389,6 +425,7 @@ export const getApiRouteConfigs = async (
389425
390426 return {
391427 functionName,
428+ functionTitle,
392429 route : apiRoute ,
393430 config,
394431 compiled,
@@ -404,7 +441,7 @@ export const getApiRouteConfigs = async (
404441export const getExtendedApiRouteConfigs = async (
405442 publish : string ,
406443 appDir : string ,
407- pageExtensions : string [ ] ,
444+ pageExtensions ? : string [ ] ,
408445) : Promise < Array < ApiRouteConfig > > => {
409446 const settledApiRoutes = await getApiRouteConfigs ( publish , appDir , pageExtensions )
410447
@@ -414,6 +451,7 @@ export const getExtendedApiRouteConfigs = async (
414451
415452export const packSingleFunction = ( func : ApiRouteConfig ) : APILambda => ( {
416453 functionName : func . functionName ,
454+ functionTitle : func . functionTitle ,
417455 includedFiles : func . includedFiles ,
418456 routes : [ func ] ,
419457 type : func . config . type ,
0 commit comments