@@ -16,6 +16,8 @@ import {
16
16
HANDLER_FUNCTION_TITLE ,
17
17
ODB_FUNCTION_TITLE ,
18
18
IMAGE_FUNCTION_TITLE ,
19
+ API_FUNCTION_TITLE ,
20
+ API_FUNCTION_NAME ,
19
21
LAMBDA_WARNING_SIZE ,
20
22
} from '../constants'
21
23
import { getApiHandler } from '../templates/getApiHandler'
@@ -32,6 +34,7 @@ import { getFunctionNameForPage } from './utils'
32
34
33
35
export interface ApiRouteConfig {
34
36
functionName : string
37
+ functionTitle ?: string
35
38
route : string
36
39
config : ApiConfig
37
40
compiled : string
@@ -40,6 +43,7 @@ export interface ApiRouteConfig {
40
43
41
44
export interface APILambda {
42
45
functionName : string
46
+ functionTitle : string
43
47
routes : ApiRouteConfig [ ]
44
48
includedFiles : string [ ]
45
49
type ?: ApiRouteType
@@ -61,7 +65,7 @@ export const generateFunctions = async (
61
65
: undefined
62
66
63
67
for ( const apiLambda of apiLambdas ) {
64
- const { functionName, routes, type, includedFiles } = apiLambda
68
+ const { functionName, functionTitle , routes, type, includedFiles } = apiLambda
65
69
66
70
const apiHandlerSource = getApiHandler ( {
67
71
// most api lambdas serve multiple routes, but scheduled functions need to be in separate lambdas.
@@ -103,6 +107,8 @@ export const generateFunctions = async (
103
107
} )
104
108
await writeFile ( join ( functionsDir , functionName , 'pages.js' ) , resolverSource )
105
109
110
+ await writeFunctionConfiguration ( { functionName, functionTitle, functionsDir } )
111
+
106
112
const nfInternalFiles = await glob ( join ( functionsDir , functionName , '**' ) )
107
113
includedFiles . push ( ...nfInternalFiles )
108
114
}
@@ -129,7 +135,7 @@ export const generateFunctions = async (
129
135
join ( __dirname , '..' , '..' , 'lib' , 'templates' , 'handlerUtils.js' ) ,
130
136
join ( functionsDir , functionName , 'handlerUtils.js' ) ,
131
137
)
132
- writeFunctionConfiguration ( { functionName, functionTitle, functionsDir } )
138
+ await writeFunctionConfiguration ( { functionName, functionTitle, functionsDir } )
133
139
}
134
140
135
141
await writeHandler ( HANDLER_FUNCTION_NAME , HANDLER_FUNCTION_TITLE , false )
@@ -333,12 +339,41 @@ export const getAPILambdas = async (
333
339
334
340
const bins = pack ( weighedRoutes , threshold )
335
341
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
+ } )
342
377
}
343
378
344
379
const standardFunctions = apiRoutes . filter (
@@ -365,7 +400,7 @@ export const getAPILambdas = async (
365
400
export const getApiRouteConfigs = async (
366
401
publish : string ,
367
402
appDir : string ,
368
- pageExtensions : string [ ] ,
403
+ pageExtensions ? : string [ ] ,
369
404
) : Promise < Array < ApiRouteConfig > > => {
370
405
const pages = await readJSON ( join ( publish , 'server' , 'pages-manifest.json' ) )
371
406
const apiRoutes = Object . keys ( pages ) . filter ( ( page ) => page . startsWith ( '/api/' ) )
@@ -380,6 +415,7 @@ export const getApiRouteConfigs = async (
380
415
const config = await extractConfigFromFile ( filePath , appDir )
381
416
382
417
const functionName = getFunctionNameForPage ( apiRoute , config . type === ApiRouteType . BACKGROUND )
418
+ const functionTitle = `${ API_FUNCTION_TITLE } ${ apiRoute } `
383
419
384
420
const compiled = pages [ apiRoute ]
385
421
const compiledPath = join ( publish , 'server' , compiled )
@@ -389,6 +425,7 @@ export const getApiRouteConfigs = async (
389
425
390
426
return {
391
427
functionName,
428
+ functionTitle,
392
429
route : apiRoute ,
393
430
config,
394
431
compiled,
@@ -404,7 +441,7 @@ export const getApiRouteConfigs = async (
404
441
export const getExtendedApiRouteConfigs = async (
405
442
publish : string ,
406
443
appDir : string ,
407
- pageExtensions : string [ ] ,
444
+ pageExtensions ? : string [ ] ,
408
445
) : Promise < Array < ApiRouteConfig > > => {
409
446
const settledApiRoutes = await getApiRouteConfigs ( publish , appDir , pageExtensions )
410
447
@@ -414,6 +451,7 @@ export const getExtendedApiRouteConfigs = async (
414
451
415
452
export const packSingleFunction = ( func : ApiRouteConfig ) : APILambda => ( {
416
453
functionName : func . functionName ,
454
+ functionTitle : func . functionTitle ,
417
455
includedFiles : func . includedFiles ,
418
456
routes : [ func ] ,
419
457
type : func . config . type ,
0 commit comments