@@ -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
} from '../constants'
20
22
import { getApiHandler } from '../templates/getApiHandler'
21
23
import { getHandler } from '../templates/getHandler'
@@ -31,6 +33,7 @@ import { getFunctionNameForPage } from './utils'
31
33
32
34
export interface ApiRouteConfig {
33
35
functionName : string
36
+ functionTitle ?: string
34
37
route : string
35
38
config : ApiConfig
36
39
compiled : string
@@ -39,6 +42,7 @@ export interface ApiRouteConfig {
39
42
40
43
export interface APILambda {
41
44
functionName : string
45
+ functionTitle : string
42
46
routes : ApiRouteConfig [ ]
43
47
includedFiles : string [ ]
44
48
type ?: ApiRouteType
@@ -60,7 +64,7 @@ export const generateFunctions = async (
60
64
: undefined
61
65
62
66
for ( const apiLambda of apiLambdas ) {
63
- const { functionName, routes, type, includedFiles } = apiLambda
67
+ const { functionName, functionTitle , routes, type, includedFiles } = apiLambda
64
68
65
69
const apiHandlerSource = getApiHandler ( {
66
70
// most api lambdas serve multiple routes, but scheduled functions need to be in separate lambdas.
@@ -102,6 +106,8 @@ export const generateFunctions = async (
102
106
} )
103
107
await writeFile ( join ( functionsDir , functionName , 'pages.js' ) , resolverSource )
104
108
109
+ await writeFunctionConfiguration ( { functionName, functionTitle, functionsDir } )
110
+
105
111
const nfInternalFiles = await glob ( join ( functionsDir , functionName , '**' ) )
106
112
includedFiles . push ( ...nfInternalFiles )
107
113
}
@@ -128,7 +134,7 @@ export const generateFunctions = async (
128
134
join ( __dirname , '..' , '..' , 'lib' , 'templates' , 'handlerUtils.js' ) ,
129
135
join ( functionsDir , functionName , 'handlerUtils.js' ) ,
130
136
)
131
- writeFunctionConfiguration ( { functionName, functionTitle, functionsDir } )
137
+ await writeFunctionConfiguration ( { functionName, functionTitle, functionsDir } )
132
138
}
133
139
134
140
await writeHandler ( HANDLER_FUNCTION_NAME , HANDLER_FUNCTION_TITLE , false )
@@ -334,12 +340,41 @@ export const getAPILambdas = async (
334
340
335
341
const bins = pack ( weighedRoutes , threshold )
336
342
337
- return bins . map ( ( bin , index ) => ( {
338
- functionName : bin . length === 1 ? bin [ 0 ] . functionName : `api-${ index } ` ,
339
- routes : bin ,
340
- includedFiles : [ ...commonDependencies , ...routes . flatMap ( ( route ) => route . includedFiles ) ] ,
341
- type,
342
- } ) )
343
+ return bins . map ( ( bin ) => {
344
+ if ( bin . length === 1 ) {
345
+ const [ func ] = bin
346
+ const { functionName, functionTitle, config, includedFiles } = func
347
+ return {
348
+ functionName,
349
+ functionTitle,
350
+ routes : [ func ] ,
351
+ includedFiles : [ ...commonDependencies , ...includedFiles ] ,
352
+ type : config . type ,
353
+ }
354
+ }
355
+
356
+ const includedFiles = [ ...commonDependencies , ...bin . flatMap ( ( route ) => route . includedFiles ) ]
357
+ const nonSingletonBins = bins . filter ( ( b ) => b . length > 1 )
358
+ if ( nonSingletonBins . length === 1 ) {
359
+ return {
360
+ functionName : API_FUNCTION_NAME ,
361
+ functionTitle : API_FUNCTION_TITLE ,
362
+ includedFiles,
363
+ routes : bin ,
364
+ type,
365
+ }
366
+ }
367
+
368
+ const indexInNonSingletonBins = nonSingletonBins . indexOf ( bin )
369
+
370
+ return {
371
+ functionName : `${ API_FUNCTION_NAME } -${ indexInNonSingletonBins + 1 } ` ,
372
+ functionTitle : `${ API_FUNCTION_TITLE } ${ indexInNonSingletonBins + 1 } /${ nonSingletonBins . length } ` ,
373
+ includedFiles,
374
+ routes : bin ,
375
+ type,
376
+ }
377
+ } )
343
378
}
344
379
345
380
const standardFunctions = apiRoutes . filter (
@@ -381,6 +416,7 @@ export const getApiRouteConfigs = async (
381
416
const config = await extractConfigFromFile ( filePath , appDir )
382
417
383
418
const functionName = getFunctionNameForPage ( apiRoute , config . type === ApiRouteType . BACKGROUND )
419
+ const functionTitle = `${ API_FUNCTION_TITLE } ${ apiRoute } `
384
420
385
421
const compiled = pages [ apiRoute ]
386
422
const compiledPath = join ( publish , 'server' , compiled )
@@ -390,6 +426,7 @@ export const getApiRouteConfigs = async (
390
426
391
427
return {
392
428
functionName,
429
+ functionTitle,
393
430
route : apiRoute ,
394
431
config,
395
432
compiled,
@@ -415,6 +452,7 @@ export const getExtendedApiRouteConfigs = async (
415
452
416
453
export const packSingleFunction = ( func : ApiRouteConfig ) : APILambda => ( {
417
454
functionName : func . functionName ,
455
+ functionTitle : func . functionTitle ,
418
456
includedFiles : func . includedFiles ,
419
457
routes : [ func ] ,
420
458
type : func . config . type ,
0 commit comments